branches/osd/src/osd/sdl/drawbgfx.c
| r0 | r32173 | |
| 1 | // license:BSD-3-Clause |
| 2 | // copyright-holders:Miodrag Milanovic |
| 3 | //============================================================ |
| 4 | // |
| 5 | // drawbgfx.c - BGFX drawer |
| 6 | // |
| 7 | //============================================================ |
| 8 | #define __STDC_LIMIT_MACROS |
| 9 | #define __STDC_FORMAT_MACROS |
| 10 | #define __STDC_CONSTANT_MACROS |
| 11 | |
| 12 | // standard C headers |
| 13 | #include <math.h> |
| 14 | #include <stdio.h> |
| 15 | |
| 16 | // MAME headers |
| 17 | #include "emu.h" |
| 18 | #include "options.h" |
| 19 | |
| 20 | // standard SDL headers |
| 21 | #include "sdlinc.h" |
| 22 | |
| 23 | // OSD headers |
| 24 | #include "osdsdl.h" |
| 25 | #include "window.h" |
| 26 | |
| 27 | #include <bgfxplatform.h> |
| 28 | #include <bgfx.h> |
| 29 | |
| 30 | |
| 31 | |
| 32 | //============================================================ |
| 33 | // PROTOTYPES |
| 34 | //============================================================ |
| 35 | |
| 36 | // core functions |
| 37 | |
| 38 | static void drawbgfx_exit(void); |
| 39 | static void drawbgfx_attach(sdl_draw_info *info, sdl_window_info *window); |
| 40 | static int drawbgfx_window_create(sdl_window_info *window, int width, int height); |
| 41 | static void drawbgfx_window_resize(sdl_window_info *window, int width, int height); |
| 42 | static void drawbgfx_window_destroy(sdl_window_info *window); |
| 43 | static int drawbgfx_window_draw(sdl_window_info *window, UINT32 dc, int update); |
| 44 | static render_primitive_list &drawbgfx_window_get_primitives(sdl_window_info *window); |
| 45 | static void drawbgfx_destroy_all_textures(sdl_window_info *window); |
| 46 | static void drawbgfx_window_clear(sdl_window_info *window); |
| 47 | static int drawbgfx_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt); |
| 48 | |
| 49 | |
| 50 | int drawbgfx_init(running_machine &machine, sdl_draw_info *callbacks) |
| 51 | { |
| 52 | const char *stemp; |
| 53 | |
| 54 | // fill in the callbacks |
| 55 | callbacks->exit = drawbgfx_exit; |
| 56 | callbacks->attach = drawbgfx_attach; |
| 57 | |
| 58 | osd_printf_verbose("Using SDL native texturing driver (SDL 2.0+)\n"); |
| 59 | |
| 60 | // Load the GL library now - else MT will fail |
| 61 | stemp = downcast<sdl_options &>(machine.options()).gl_lib(); |
| 62 | if (stemp != NULL && strcmp(stemp, SDLOPTVAL_AUTO) == 0) |
| 63 | stemp = NULL; |
| 64 | |
| 65 | // No fatalerror here since not all video drivers support GL ! |
| 66 | if (SDL_GL_LoadLibrary(stemp) != 0) // Load library (default for e==NULL |
| 67 | osd_printf_verbose("Warning: Unable to load opengl library: %s\n", stemp ? stemp : "<default>"); |
| 68 | else |
| 69 | osd_printf_verbose("Loaded opengl shared library: %s\n", stemp ? stemp : "<default>"); |
| 70 | |
| 71 | return 0; |
| 72 | |
| 73 | } |
| 74 | |
| 75 | static void drawbgfx_destroy_all_textures(sdl_window_info *window) |
| 76 | { |
| 77 | } |
| 78 | //============================================================ |
| 79 | // drawbgfx_exit |
| 80 | //============================================================ |
| 81 | |
| 82 | static void drawbgfx_exit(void) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | //============================================================ |
| 87 | // drawbgfx_attach |
| 88 | //============================================================ |
| 89 | |
| 90 | static void drawbgfx_attach(sdl_draw_info *info, sdl_window_info *window) |
| 91 | { |
| 92 | // fill in the callbacks |
| 93 | window->create = drawbgfx_window_create; |
| 94 | window->resize = drawbgfx_window_resize; |
| 95 | window->get_primitives = drawbgfx_window_get_primitives; |
| 96 | window->draw = drawbgfx_window_draw; |
| 97 | window->destroy = drawbgfx_window_destroy; |
| 98 | window->destroy_all_textures = drawbgfx_destroy_all_textures; |
| 99 | window->clear = drawbgfx_window_clear; |
| 100 | window->xy_to_render_target = drawbgfx_xy_to_render_target; |
| 101 | } |
| 102 | |
| 103 | //============================================================ |
| 104 | // drawbgfx_window_create |
| 105 | //============================================================ |
| 106 | |
| 107 | static int drawbgfx_window_create(sdl_window_info *window, int width, int height) |
| 108 | { |
| 109 | // allocate memory for our structures |
| 110 | osd_printf_verbose("Enter drawbgfx_window_create\n"); |
| 111 | int extra_flags = (window->fullscreen ? |
| 112 | SDL_WINDOW_BORDERLESS | SDL_WINDOW_INPUT_FOCUS | SDL_WINDOW_FULLSCREEN : SDL_WINDOW_RESIZABLE); |
| 113 | |
| 114 | // create the SDL window |
| 115 | window->sdl_window = SDL_CreateWindow(window->title, SDL_WINDOWPOS_UNDEFINED_DISPLAY(window->monitor->handle), SDL_WINDOWPOS_UNDEFINED, |
| 116 | width, height, extra_flags); |
| 117 | |
| 118 | bgfx::sdlSetWindow(window->sdl_window); |
| 119 | if (window->fullscreen && video_config.switchres) |
| 120 | { |
| 121 | SDL_DisplayMode mode; |
| 122 | SDL_GetCurrentDisplayMode(window->monitor->handle, &mode); |
| 123 | mode.w = width; |
| 124 | mode.h = height; |
| 125 | if (window->refresh) |
| 126 | mode.refresh_rate = window->refresh; |
| 127 | if (window->depth) |
| 128 | { |
| 129 | switch (window->depth) |
| 130 | { |
| 131 | case 15: |
| 132 | mode.format = SDL_PIXELFORMAT_RGB555; |
| 133 | break; |
| 134 | case 16: |
| 135 | mode.format = SDL_PIXELFORMAT_RGB565; |
| 136 | break; |
| 137 | case 24: |
| 138 | mode.format = SDL_PIXELFORMAT_RGB24; |
| 139 | break; |
| 140 | case 32: |
| 141 | mode.format = SDL_PIXELFORMAT_RGB888; |
| 142 | break; |
| 143 | default: |
| 144 | osd_printf_warning("Ignoring depth %d\n", window->depth); |
| 145 | } |
| 146 | } |
| 147 | SDL_SetWindowDisplayMode(window->sdl_window, &mode); // Try to set mode |
| 148 | } |
| 149 | else |
| 150 | SDL_SetWindowDisplayMode(window->sdl_window, NULL); // Use desktop |
| 151 | |
| 152 | // create renderer |
| 153 | |
| 154 | //SDL_SelectRenderer(window->sdl_window); |
| 155 | |
| 156 | SDL_ShowWindow(window->sdl_window); |
| 157 | //SDL_SetWindowFullscreen(window->window_id, window->fullscreen); |
| 158 | SDL_RaiseWindow(window->sdl_window); |
| 159 | SDL_GetWindowSize(window->sdl_window, &window->width, &window->height); |
| 160 | |
| 161 | |
| 162 | bgfx::init(); |
| 163 | bgfx::reset(window->width, window->height, BGFX_RESET_VSYNC); |
| 164 | |
| 165 | // Enable debug text. |
| 166 | bgfx::setDebug(BGFX_DEBUG_STATS);// BGFX_DEBUG_TEXT); |
| 167 | |
| 168 | |
| 169 | osd_printf_verbose("Leave drawbgfx_window_create\n"); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | //============================================================ |
| 174 | // drawbgfx_window_resize |
| 175 | //============================================================ |
| 176 | |
| 177 | static void drawbgfx_window_resize(sdl_window_info *window, int width, int height) |
| 178 | { |
| 179 | window->width = width; |
| 180 | window->height = height; |
| 181 | } |
| 182 | |
| 183 | //============================================================ |
| 184 | // drawsdl_xy_to_render_target |
| 185 | //============================================================ |
| 186 | |
| 187 | static int drawbgfx_xy_to_render_target(sdl_window_info *window, int x, int y, int *xt, int *yt) |
| 188 | { |
| 189 | return 1; |
| 190 | } |
| 191 | |
| 192 | //============================================================ |
| 193 | // drawbgfx_window_get_primitives |
| 194 | //============================================================ |
| 195 | |
| 196 | static render_primitive_list &drawbgfx_window_get_primitives(sdl_window_info *window) |
| 197 | { |
| 198 | if ((!window->fullscreen) || (video_config.switchres)) |
| 199 | { |
| 200 | sdlwindow_blit_surface_size(window, window->width, window->height); |
| 201 | } |
| 202 | else |
| 203 | { |
| 204 | sdlwindow_blit_surface_size(window, window->monitor->center_width, window->monitor->center_height); |
| 205 | } |
| 206 | window->target->set_bounds(window->blitwidth, window->blitheight, sdlvideo_monitor_get_aspect(window->monitor)); |
| 207 | return window->target->get_primitives(); |
| 208 | } |
| 209 | |
| 210 | //============================================================ |
| 211 | // drawbgfx_window_draw |
| 212 | //============================================================ |
| 213 | |
| 214 | static int drawbgfx_window_draw(sdl_window_info *window, UINT32 dc, int update) |
| 215 | { |
| 216 | if (video_config.novideo) |
| 217 | { |
| 218 | return 0; |
| 219 | } |
| 220 | |
| 221 | window->primlist->acquire_lock(); |
| 222 | |
| 223 | bgfx::setViewClear(0 |
| 224 | , BGFX_CLEAR_COLOR_BIT|BGFX_CLEAR_DEPTH_BIT |
| 225 | , 0x00000000 |
| 226 | , 1.0f |
| 227 | , 0 |
| 228 | ); |
| 229 | // Set view 0 default viewport. |
| 230 | bgfx::setViewRect(0, 0, 0, window->width, window->height); |
| 231 | |
| 232 | // This dummy draw call is here to make sure that view 0 is cleared |
| 233 | // if no other draw calls are submitted to view 0. |
| 234 | bgfx::submit(0); |
| 235 | |
| 236 | // now draw |
| 237 | for (render_primitive *prim = window->primlist->first(); prim != NULL; prim = prim->next()) |
| 238 | { |
| 239 | switch (prim->type) |
| 240 | { |
| 241 | case render_primitive::LINE: |
| 242 | break; |
| 243 | case render_primitive::QUAD: |
| 244 | break; |
| 245 | default: |
| 246 | throw emu_fatalerror("Unexpected render_primitive type\n"); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | window->primlist->release_lock(); |
| 251 | // Advance to next frame. Rendering thread will be kicked to |
| 252 | // process submitted rendering primitives. |
| 253 | bgfx::frame(); |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
| 258 | |
| 259 | //============================================================ |
| 260 | // drawbgfx_window_clear |
| 261 | //============================================================ |
| 262 | |
| 263 | static void drawbgfx_window_clear(sdl_window_info *window) |
| 264 | { |
| 265 | } |
| 266 | |
| 267 | |
| 268 | //============================================================ |
| 269 | // drawbgfx_window_destroy |
| 270 | //============================================================ |
| 271 | |
| 272 | static void drawbgfx_window_destroy(sdl_window_info *window) |
| 273 | { |
| 274 | // free the memory in the window |
| 275 | SDL_DestroyWindow(window->sdl_window); |
| 276 | // Shutdown bgfx. |
| 277 | bgfx::shutdown(); |
| 278 | |
| 279 | window->dxdata = NULL; |
| 280 | } |
| 281 | |