trunk/src/osd/sdl/drawbgfx.c
| r243309 | r243310 | |
| 64 | 64 | // core functions |
| 65 | 65 | |
| 66 | 66 | static void drawbgfx_exit(void); |
| 67 | | static void drawbgfx_attach(sdl_draw_info *info, sdl_window_info *window); |
| 68 | | static int drawbgfx_window_create(sdl_window_info *window, int width, int height); |
| 69 | | static void drawbgfx_window_resize(sdl_window_info *window, int width, int height); |
| 70 | | static void drawbgfx_window_destroy(sdl_window_info *window); |
| 71 | | static int drawbgfx_window_draw(sdl_window_info *window, UINT32 dc, int update); |
| 72 | | static void drawbgfx_set_target_bounds(sdl_window_info *window); |
| 73 | | static void drawbgfx_destroy_all_textures(sdl_window_info *window); |
| 74 | | static void drawbgfx_window_clear(sdl_window_info *window); |
| 75 | | static int drawbgfx_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt); |
| 76 | 67 | |
| 77 | 68 | //============================================================ |
| 78 | 69 | // Textures |
| 79 | 70 | //============================================================ |
| 80 | 71 | |
| 81 | 72 | /* sdl_info is the information about SDL for the current screen */ |
| 82 | | struct sdl_info13 |
| 73 | class sdl_info_bgfx : public osd_renderer |
| 83 | 74 | { |
| 84 | | sdl_info13() |
| 85 | | : m_blittimer(0), m_renderer(NULL), |
| 86 | | m_hofs(0), m_vofs(0), |
| 75 | public: |
| 76 | sdl_info_bgfx(sdl_window_info *w) |
| 77 | : osd_renderer(w), m_blittimer(0), m_renderer(NULL), |
| 78 | m_last_hofs(0), m_last_vofs(0), |
| 87 | 79 | m_resize_pending(0), m_resize_width(0), m_resize_height(0), |
| 88 | 80 | m_last_blit_time(0), m_last_blit_pixels(0) |
| 89 | 81 | {} |
| 90 | 82 | |
| 83 | /* virtual */ int create(int width, int height); |
| 84 | /* virtual */ void resize(int width, int height); |
| 85 | /* virtual */ int draw(UINT32 dc, int update); |
| 86 | /* virtual */ void set_target_bounds(); |
| 87 | /* virtual */ int xy_to_render_target(int x, int y, int *xt, int *yt); |
| 88 | /* virtual */ void destroy_all_textures(); |
| 89 | /* virtual */ void destroy(); |
| 90 | /* virtual */ void clear(); |
| 91 | |
| 91 | 92 | // void render_quad(texture_info *texture, const render_primitive *prim, const int x, const int y); |
| 92 | 93 | |
| 93 | 94 | //texture_info *texture_find(const render_primitive &prim, const quad_setup_data &setup); |
| r243309 | r243310 | |
| 98 | 99 | SDL_Renderer * m_renderer; |
| 99 | 100 | //simple_list<texture_info> m_texlist; // list of active textures |
| 100 | 101 | |
| 101 | | float m_hofs; |
| 102 | | float m_vofs; |
| 102 | float m_last_hofs; |
| 103 | float m_last_vofs; |
| 103 | 104 | |
| 104 | 105 | // resize information |
| 105 | 106 | |
| r243309 | r243310 | |
| 123 | 124 | // drawbgfx_init |
| 124 | 125 | //============================================================ |
| 125 | 126 | |
| 127 | static osd_renderer *drawbgfx_create(sdl_window_info *window) |
| 128 | { |
| 129 | return global_alloc(sdl_info_bgfx(window)); |
| 130 | } |
| 131 | |
| 132 | |
| 126 | 133 | int drawbgfx_init(running_machine &machine, sdl_draw_info *callbacks) |
| 127 | 134 | { |
| 128 | 135 | // fill in the callbacks |
| 129 | 136 | callbacks->exit = drawbgfx_exit; |
| 130 | | callbacks->attach = drawbgfx_attach; |
| 137 | callbacks->create = drawbgfx_create; |
| 131 | 138 | |
| 132 | 139 | return 0; |
| 133 | 140 | } |
| 134 | 141 | |
| 135 | | |
| 136 | 142 | //============================================================ |
| 137 | | // drawbgfx_attach |
| 143 | // sdl_info_bgfx::create |
| 138 | 144 | //============================================================ |
| 139 | 145 | |
| 140 | | static void drawbgfx_attach(sdl_draw_info *info, sdl_window_info *window) |
| 146 | int sdl_info_bgfx::create(int width, int height) |
| 141 | 147 | { |
| 142 | | // fill in the callbacks |
| 143 | | window->create = drawbgfx_window_create; |
| 144 | | window->resize = drawbgfx_window_resize; |
| 145 | | window->set_target_bounds = drawbgfx_set_target_bounds; |
| 146 | | window->draw = drawbgfx_window_draw; |
| 147 | | window->destroy = drawbgfx_window_destroy; |
| 148 | | window->destroy_all_textures = drawbgfx_destroy_all_textures; |
| 149 | | window->clear = drawbgfx_window_clear; |
| 150 | | window->xy_to_render_target = drawbgfx_xy_to_render_target; |
| 151 | | } |
| 152 | | |
| 153 | | //============================================================ |
| 154 | | // drawbgfx_window_create |
| 155 | | //============================================================ |
| 156 | | |
| 157 | | static int drawbgfx_window_create(sdl_window_info *window, int width, int height) |
| 158 | | { |
| 159 | | // allocate memory for our structures |
| 160 | | sdl_info13 *sdl = global_alloc(sdl_info13); |
| 161 | | |
| 162 | 148 | /* FIXME: On Ubuntu and potentially other Linux OS you should use |
| 163 | 149 | * to disable panning. This has to be done before every invocation of mame. |
| 164 | 150 | * |
| r243309 | r243310 | |
| 168 | 154 | |
| 169 | 155 | osd_printf_verbose("Enter drawsdl2_window_create\n"); |
| 170 | 156 | |
| 171 | | window->dxdata = sdl; |
| 172 | | |
| 173 | | UINT32 extra_flags = (window->fullscreen() ? |
| 157 | UINT32 extra_flags = (window().fullscreen() ? |
| 174 | 158 | SDL_WINDOW_BORDERLESS | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE); |
| 175 | 159 | |
| 176 | 160 | #if defined(SDLMAME_WIN32) |
| 177 | 161 | SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0"); |
| 178 | 162 | #endif |
| 179 | 163 | // create the SDL window |
| 180 | | window->sdl_window = SDL_CreateWindow(window->title, |
| 181 | | window->monitor()->position_size().x, window->monitor()->position_size().y, |
| 164 | window().m_sdl_window = SDL_CreateWindow(window().m_title, |
| 165 | window().monitor()->position_size().x, window().monitor()->position_size().y, |
| 182 | 166 | width, height, extra_flags); |
| 183 | 167 | |
| 184 | | if (window->fullscreen() && video_config.switchres) |
| 168 | if (window().fullscreen() && video_config.switchres) |
| 185 | 169 | { |
| 186 | 170 | SDL_DisplayMode mode; |
| 187 | | //SDL_GetCurrentDisplayMode(window->monitor()->handle, &mode); |
| 188 | | SDL_GetWindowDisplayMode(window->sdl_window, &mode); |
| 189 | | sdl->m_original_mode = mode; |
| 171 | //SDL_GetCurrentDisplayMode(window().monitor()->handle, &mode); |
| 172 | SDL_GetWindowDisplayMode(window().m_sdl_window, &mode); |
| 173 | m_original_mode = mode; |
| 190 | 174 | mode.w = width; |
| 191 | 175 | mode.h = height; |
| 192 | | if (window->refresh) |
| 193 | | mode.refresh_rate = window->refresh; |
| 176 | if (window().m_refresh) |
| 177 | mode.refresh_rate = window().m_refresh; |
| 194 | 178 | |
| 195 | | SDL_SetWindowDisplayMode(window->sdl_window, &mode); // Try to set mode |
| 179 | SDL_SetWindowDisplayMode(window().m_sdl_window, &mode); // Try to set mode |
| 196 | 180 | #ifndef SDLMAME_WIN32 |
| 197 | 181 | /* FIXME: Warp the mouse to 0,0 in case a virtual desktop resolution |
| 198 | 182 | * is in place after the mode switch - which will most likely be the case |
| 199 | 183 | * This is a hack to work around a deficiency in SDL2 |
| 200 | 184 | */ |
| 201 | | SDL_WarpMouseInWindow(window->sdl_window, 1, 1); |
| 185 | SDL_WarpMouseInWindow(window().m_sdl_window, 1, 1); |
| 202 | 186 | #endif |
| 203 | 187 | } |
| 204 | 188 | else |
| 205 | 189 | { |
| 206 | | //SDL_SetWindowDisplayMode(window->sdl_window, NULL); // Use desktop |
| 190 | //SDL_SetWindowDisplayMode(window().m_sdl_window, NULL); // Use desktop |
| 207 | 191 | } |
| 208 | 192 | // create renderer |
| 209 | 193 | |
| 210 | 194 | if (video_config.waitvsync) |
| 211 | | sdl->m_renderer = SDL_CreateRenderer(window->sdl_window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED); |
| 195 | m_renderer = SDL_CreateRenderer(window().m_sdl_window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED); |
| 212 | 196 | else |
| 213 | | sdl->m_renderer = SDL_CreateRenderer(window->sdl_window, -1, SDL_RENDERER_ACCELERATED); |
| 197 | m_renderer = SDL_CreateRenderer(window().m_sdl_window, -1, SDL_RENDERER_ACCELERATED); |
| 214 | 198 | |
| 215 | | if (!sdl->m_renderer) |
| 199 | if (!m_renderer) |
| 216 | 200 | { |
| 217 | 201 | fatalerror("Error on creating renderer: %s\n", SDL_GetError()); |
| 218 | 202 | } |
| 219 | 203 | |
| 220 | | //SDL_SelectRenderer(window->sdl_window); |
| 221 | | SDL_ShowWindow(window->sdl_window); |
| 222 | | //SDL_SetWindowFullscreen(window->window_id, window->fullscreen); |
| 223 | | SDL_RaiseWindow(window->sdl_window); |
| 204 | //SDL_SelectRenderer(window().m_sdl_window); |
| 205 | SDL_ShowWindow(window().m_sdl_window); |
| 206 | //SDL_SetWindowFullscreen(window().window_id, window().fullscreen); |
| 207 | SDL_RaiseWindow(window().m_sdl_window); |
| 224 | 208 | |
| 225 | | SDL_GetWindowSize(window->sdl_window, &window->width, &window->height); |
| 209 | SDL_GetWindowSize(window().m_sdl_window, &window().m_width, &window().m_height); |
| 226 | 210 | |
| 227 | | sdl->m_blittimer = 3; |
| 211 | m_blittimer = 3; |
| 228 | 212 | |
| 229 | | SDL_RenderPresent(sdl->m_renderer); |
| 213 | SDL_RenderPresent(m_renderer); |
| 230 | 214 | |
| 231 | | bgfx::sdlSetWindow(window->sdl_window); |
| 215 | bgfx::sdlSetWindow(window().m_sdl_window); |
| 232 | 216 | bgfx::init(); |
| 233 | | bgfx::reset(window->width, window->height, BGFX_RESET_VSYNC); |
| 217 | bgfx::reset(window().m_width, window().m_height, BGFX_RESET_VSYNC); |
| 234 | 218 | |
| 235 | 219 | // Enable debug text. |
| 236 | 220 | bgfx::setDebug(BGFX_DEBUG_STATS);// BGFX_DEBUG_TEXT); |
| r243309 | r243310 | |
| 239 | 223 | } |
| 240 | 224 | |
| 241 | 225 | //============================================================ |
| 242 | | // drawbgfx_window_resize |
| 226 | // sdl_info_bgfx::resize |
| 243 | 227 | //============================================================ |
| 244 | 228 | |
| 245 | | static void drawbgfx_window_resize(sdl_window_info *window, int width, int height) |
| 229 | void sdl_info_bgfx::resize(int width, int height) |
| 246 | 230 | { |
| 247 | | sdl_info13 *sdl = (sdl_info13 *) window->dxdata; |
| 231 | m_resize_pending = 1; |
| 232 | m_resize_height = height; |
| 233 | m_resize_width = width; |
| 248 | 234 | |
| 249 | | sdl->m_resize_pending = 1; |
| 250 | | sdl->m_resize_height = height; |
| 251 | | sdl->m_resize_width = width; |
| 235 | window().m_width = width; |
| 236 | window().m_height = height; |
| 252 | 237 | |
| 253 | | window->width = width; |
| 254 | | window->height = height; |
| 255 | | |
| 256 | | sdl->m_blittimer = 3; |
| 238 | m_blittimer = 3; |
| 257 | 239 | } |
| 258 | 240 | |
| 259 | 241 | //============================================================ |
| 260 | 242 | // drawsdl_xy_to_render_target |
| 261 | 243 | //============================================================ |
| 262 | 244 | |
| 263 | | static int drawbgfx_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt) |
| 245 | int sdl_info_bgfx::xy_to_render_target(int x, int y, int *xt, int *yt) |
| 264 | 246 | { |
| 265 | | sdl_info13 *sdl = (sdl_info13 *) window->dxdata; |
| 266 | | |
| 267 | | *xt = x - sdl->m_hofs; |
| 268 | | *yt = y - sdl->m_vofs; |
| 269 | | if (*xt<0 || *xt >= window->blitwidth) |
| 247 | *xt = x - m_last_hofs; |
| 248 | *yt = y - m_last_vofs; |
| 249 | if (*xt<0 || *xt >= window().m_blitwidth) |
| 270 | 250 | return 0; |
| 271 | | if (*yt<0 || *yt >= window->blitheight) |
| 251 | if (*yt<0 || *yt >= window().m_blitheight) |
| 272 | 252 | return 0; |
| 273 | 253 | return 1; |
| 274 | 254 | } |
| 275 | 255 | |
| 276 | 256 | //============================================================ |
| 277 | | // drawbgfx_window_get_primitives |
| 257 | // sdl_info_bgfx::get_primitives |
| 278 | 258 | //============================================================ |
| 279 | 259 | |
| 280 | | static void drawbgfx_set_target_bounds(sdl_window_info *window) |
| 260 | void sdl_info_bgfx::set_target_bounds() |
| 281 | 261 | { |
| 282 | | window->target->set_bounds(window->blitwidth, window->blitheight, window->monitor()->aspect()); |
| 262 | window().m_target->set_bounds(window().m_blitwidth, window().m_blitheight, window().monitor()->aspect()); |
| 283 | 263 | } |
| 284 | 264 | |
| 285 | 265 | //============================================================ |
| 286 | | // drawbgfx_window_draw |
| 266 | // sdl_info_bgfx::draw |
| 287 | 267 | //============================================================ |
| 288 | 268 | |
| 289 | | static int drawbgfx_window_draw(sdl_window_info *window, UINT32 dc, int update) |
| 269 | int sdl_info_bgfx::draw(UINT32 dc, int update) |
| 290 | 270 | { |
| 291 | 271 | bgfx::setViewClear(0 |
| 292 | 272 | , BGFX_CLEAR_COLOR|BGFX_CLEAR_DEPTH |
| r243309 | r243310 | |
| 295 | 275 | , 0 |
| 296 | 276 | ); |
| 297 | 277 | // Set view 0 default viewport. |
| 298 | | bgfx::setViewRect(0, 0, 0, window->blitwidth, window->blitheight); |
| 278 | bgfx::setViewRect(0, 0, 0, window().m_blitwidth, window().m_blitheight); |
| 299 | 279 | |
| 300 | 280 | // This dummy draw call is here to make sure that view 0 is cleared |
| 301 | 281 | // if no other draw calls are submitted to view 0. |
| 302 | 282 | bgfx::submit(0); |
| 303 | 283 | |
| 304 | | window->primlist->acquire_lock(); |
| 305 | | window->primlist->release_lock(); |
| 284 | window().m_primlist->acquire_lock(); |
| 285 | window().m_primlist->release_lock(); |
| 306 | 286 | // Advance to next frame. Rendering thread will be kicked to |
| 307 | 287 | // process submitted rendering primitives. |
| 308 | 288 | bgfx::frame(); |
| r243309 | r243310 | |
| 319 | 299 | } |
| 320 | 300 | |
| 321 | 301 | //============================================================ |
| 322 | | // drawbgfx_window_destroy |
| 302 | // sdl_info_bgfx::destroy |
| 323 | 303 | //============================================================ |
| 324 | 304 | |
| 325 | | static void drawbgfx_window_destroy(sdl_window_info *window) |
| 305 | void sdl_info_bgfx::destroy() |
| 326 | 306 | { |
| 327 | | sdl_info13 *sdl = (sdl_info13 *) window->dxdata; |
| 328 | | |
| 329 | | // skip if nothing |
| 330 | | if (sdl == NULL) |
| 331 | | return; |
| 332 | | |
| 333 | 307 | // free the memory in the window |
| 334 | 308 | |
| 335 | | drawbgfx_destroy_all_textures(window); |
| 309 | destroy_all_textures(); |
| 336 | 310 | |
| 337 | | if (window->fullscreen() && video_config.switchres) |
| 311 | if (window().fullscreen() && video_config.switchres) |
| 338 | 312 | { |
| 339 | | SDL_SetWindowFullscreen(window->sdl_window, 0); // Try to set mode |
| 340 | | SDL_SetWindowDisplayMode(window->sdl_window, &sdl->m_original_mode); // Try to set mode |
| 341 | | SDL_SetWindowFullscreen(window->sdl_window, SDL_WINDOW_FULLSCREEN); // Try to set mode |
| 313 | SDL_SetWindowFullscreen(window().m_sdl_window, 0); // Try to set mode |
| 314 | SDL_SetWindowDisplayMode(window().m_sdl_window, &m_original_mode); // Try to set mode |
| 315 | SDL_SetWindowFullscreen(window().m_sdl_window, SDL_WINDOW_FULLSCREEN); // Try to set mode |
| 342 | 316 | } |
| 343 | 317 | |
| 344 | | SDL_DestroyWindow(window->sdl_window); |
| 345 | | |
| 346 | | global_free(sdl); |
| 347 | | window->dxdata = NULL; |
| 318 | SDL_DestroyWindow(window().m_sdl_window); |
| 348 | 319 | |
| 349 | 320 | // Shutdown bgfx. |
| 350 | 321 | bgfx::shutdown(); |
| 351 | 322 | } |
| 352 | 323 | |
| 353 | | static void drawbgfx_destroy_all_textures(sdl_window_info *window) |
| 324 | void sdl_info_bgfx::destroy_all_textures() |
| 354 | 325 | { |
| 355 | 326 | } |
| 356 | 327 | |
| r243309 | r243310 | |
| 358 | 329 | // TEXCOPY FUNCS |
| 359 | 330 | //============================================================ |
| 360 | 331 | |
| 361 | | static void drawbgfx_window_clear(sdl_window_info *window) |
| 332 | void sdl_info_bgfx::clear() |
| 362 | 333 | { |
| 363 | | sdl_info13 *sdl = (sdl_info13 *) window->dxdata; |
| 364 | | |
| 365 | | sdl->m_blittimer = 2; |
| 334 | m_blittimer = 2; |
| 366 | 335 | } |