From 1386e2ee901c249ed3c9cac32e8e7f9bb7d30893 Mon Sep 17 00:00:00 2001 From: Twinaphex Date: Sat, 26 May 2012 04:47:24 +0200 Subject: [PATCH] Restructuring of sdlwrap.c for portability reasons - could need a code review --- Makefile | 2 +- Makefile.win | 2 +- gfx/{sdlwrap.c => context/sdl_ctx.c} | 53 +++++++++++-------- gfx/ext_gfx.c | 1 - gfx/gfx_context.h | 62 ++++++++++++++++++++++ gfx/gl.c | 36 ++++++------- gfx/sdlwrap.h | 78 ---------------------------- gfx/shader_glsl.c | 3 +- input/dinput.c | 4 +- input/sdl_input.c | 4 +- 10 files changed, 118 insertions(+), 127 deletions(-) rename gfx/{sdlwrap.c => context/sdl_ctx.c} (89%) create mode 100644 gfx/gfx_context.h delete mode 100644 gfx/sdlwrap.h diff --git a/Makefile b/Makefile index 4ca552f44b..67697bfd83 100644 --- a/Makefile +++ b/Makefile @@ -100,7 +100,7 @@ ifeq ($(HAVE_COREAUDIO), 1) endif ifeq ($(HAVE_SDL), 1) - OBJ += gfx/sdl_gfx.o gfx/sdlwrap.o input/sdl_input.o audio/sdl_audio.o fifo_buffer.o + OBJ += gfx/sdl_gfx.o gfx/context/sdl_ctx.o input/sdl_input.o audio/sdl_audio.o fifo_buffer.o DEFINES += $(SDL_CFLAGS) $(BSD_LOCAL_INC) LIBS += $(SDL_LIBS) diff --git a/Makefile.win b/Makefile.win index 100fd79c7e..df7ca46281 100644 --- a/Makefile.win +++ b/Makefile.win @@ -39,7 +39,7 @@ ifeq ($(TDM_GCC),) endif ifeq ($(HAVE_SDL), 1) - OBJ += gfx/sdl_gfx.o gfx/gl.o gfx/sdlwrap.o input/sdl_input.o audio/sdl_audio.o fifo_buffer.o + OBJ += gfx/sdl_gfx.o gfx/gl.o gfx/context/sdl_ctx.o input/sdl_input.o audio/sdl_audio.o fifo_buffer.o LIBS += -lSDL DEFINES += -ISDL -DHAVE_SDL endif diff --git a/gfx/sdlwrap.c b/gfx/context/sdl_ctx.c similarity index 89% rename from gfx/sdlwrap.c rename to gfx/context/sdl_ctx.c index 2fd95f6f91..b659fa0590 100644 --- a/gfx/sdlwrap.c +++ b/gfx/context/sdl_ctx.c @@ -13,16 +13,23 @@ * If not, see . */ -#include "sdlwrap.h" -#include "SDL_syswm.h" -#include "gfx_common.h" -#include "../general.h" +// Compatibility wrapper between SDL 1.2/1.3 for OpenGL. +// Wraps functions which differ in 1.2 and 1.3. + +#include "SDL.h" +#include "SDL_version.h" + +#include "../gfx_context.h" +#include "../gfx_common.h" +#include "../../general.h" #ifdef __APPLE__ #include #include #endif +#include "sdl_ctx.h" + // SDL 1.2 is portable, sure, but you still need some platform specific workarounds ;) // Hopefully SDL 1.3 will solve this more cleanly :D // Welcome to #ifdef HELL. :D @@ -36,7 +43,7 @@ static SDL_GLContext g_ctx; static bool g_fullscreen; static unsigned g_interval; -void sdlwrap_set_swap_interval(unsigned interval, bool inited) +void gfx_ctx_set_swap_interval(unsigned interval, bool inited) { g_interval = interval; @@ -75,7 +82,7 @@ void sdlwrap_set_swap_interval(unsigned interval, bool inited) RARCH_WARN("Failed to set swap interval.\n"); } -static void sdlwrap_wm_set_caption(const char *str) +static void gfx_ctx_wm_set_caption(const char *str) { #if SDL_MODERN SDL_SetWindowTitle(g_window, str); @@ -84,17 +91,17 @@ static void sdlwrap_wm_set_caption(const char *str) #endif } -void sdlwrap_update_window_title(bool reset) +void gfx_ctx_update_window_title(bool reset) { if (reset) gfx_window_title_reset(); char buf[128]; if (gfx_window_title(buf, sizeof(buf))) - sdlwrap_wm_set_caption(buf); + gfx_ctx_wm_set_caption(buf); } -void sdlwrap_get_video_size(unsigned *width, unsigned *height) +void gfx_ctx_get_video_size(unsigned *width, unsigned *height) { const SDL_VideoInfo *video_info = SDL_GetVideoInfo(); rarch_assert(video_info); @@ -102,7 +109,7 @@ void sdlwrap_get_video_size(unsigned *width, unsigned *height) *height = video_info->current_h; } -bool sdlwrap_init(void) +bool gfx_ctx_init(void) { #if SDL_MODERN bool ret = SDL_VideoInit(NULL) == 0; @@ -118,7 +125,7 @@ bool sdlwrap_init(void) } #if SDL_MODERN -void sdlwrap_destroy(void) +void gfx_ctx_destroy(void) { if (g_ctx) SDL_GL_DeleteContext(g_ctx); @@ -130,13 +137,13 @@ void sdlwrap_destroy(void) SDL_VideoQuit(); } #else -void sdlwrap_destroy(void) +void gfx_ctx_destroy(void) { SDL_QuitSubSystem(SDL_INIT_VIDEO); } #endif -bool sdlwrap_set_video_mode( +bool gfx_ctx_set_video_mode( unsigned width, unsigned height, unsigned bits, bool fullscreen) { @@ -195,7 +202,7 @@ bool sdlwrap_set_video_mode( if (attr <= 0 && g_interval) { RARCH_WARN("SDL failed to setup VSync, attempting to recover using native calls.\n"); - sdlwrap_set_swap_interval(g_interval, true); + gfx_ctx_set_swap_interval(g_interval, true); } #endif @@ -214,7 +221,7 @@ bool sdlwrap_set_video_mode( // SDL 1.2 has an awkward model where you need to "confirm" window resizing. // SDL 1.3 luckily removes this quirk. -void sdlwrap_set_resize(unsigned width, unsigned height) +void gfx_ctx_set_resize(unsigned width, unsigned height) { #if SDL_MODERN (void)width; @@ -229,7 +236,7 @@ void sdlwrap_set_resize(unsigned width, unsigned height) #endif } -void sdlwrap_swap_buffers(void) +void gfx_ctx_swap_buffers(void) { #if SDL_MODERN SDL_GL_SwapWindow(g_window); @@ -238,7 +245,7 @@ void sdlwrap_swap_buffers(void) #endif } -bool sdlwrap_key_pressed(int key) +bool gfx_ctx_key_pressed(int key) { int num_keys; #if SDL_MODERN @@ -260,7 +267,7 @@ bool sdlwrap_key_pressed(int key) // 1.2 specific workaround for tiling WMs. In 1.3 we call GetSize directly, so we don't need to rely on // proper event handling (I hope). #if !defined(__APPLE__) && !defined(_WIN32) && !SDL_MODERN && !defined(XENON) -static void sdlwrap_get_window_size(unsigned *width, unsigned *height) +static void gfx_ctx_get_window_size(unsigned *width, unsigned *height) { SDL_SysWMinfo info; SDL_VERSION(&info.version); @@ -277,7 +284,7 @@ static void sdlwrap_get_window_size(unsigned *width, unsigned *height) } #endif -void sdlwrap_check_window(bool *quit, +void gfx_ctx_check_window(bool *quit, bool *resize, unsigned *width, unsigned *height, unsigned frame_count) { *quit = false; @@ -342,7 +349,7 @@ void sdlwrap_check_window(bool *quit, if (!*resize && !g_fullscreen) { unsigned new_width, new_height; - sdlwrap_get_window_size(&new_width, &new_height); + gfx_ctx_get_window_size(&new_width, &new_height); if ((new_width != *width || new_height != *height) || (frame_count == 10)) // Ugly hack :D { *resize = true; @@ -355,7 +362,7 @@ void sdlwrap_check_window(bool *quit, #endif } -bool sdlwrap_get_wm_info(SDL_SysWMinfo *info) +bool gfx_ctx_get_wm_info(SDL_SysWMinfo *info) { #ifdef XENON (void)info; @@ -370,7 +377,7 @@ bool sdlwrap_get_wm_info(SDL_SysWMinfo *info) #endif } -bool sdlwrap_window_has_focus(void) +bool gfx_ctx_window_has_focus(void) { #if SDL_MODERN Uint32 flags = SDL_GetWindowFlags(g_window); @@ -381,7 +388,7 @@ bool sdlwrap_window_has_focus(void) #endif } -void sdlwrap_input_driver(const input_driver_t **input, void **input_data) +void gfx_ctx_input_driver(const input_driver_t **input, void **input_data) { void *sdl_input = input_sdl.init(); if (sdl_input) diff --git a/gfx/ext_gfx.c b/gfx/ext_gfx.c index 92176db596..574e98c29a 100644 --- a/gfx/ext_gfx.c +++ b/gfx/ext_gfx.c @@ -22,7 +22,6 @@ #include #include "../dynamic.h" #include "../general.h" -#include "sdlwrap.h" #include "gfx_common.h" #ifdef HAVE_FREETYPE diff --git a/gfx/gfx_context.h b/gfx/gfx_context.h new file mode 100644 index 0000000000..3593b6ae11 --- /dev/null +++ b/gfx/gfx_context.h @@ -0,0 +1,62 @@ +/* RetroArch - A frontend for libretro. + * Copyright (C) 2010-2012 - Hans-Kristian Arntzen + * + + * + * RetroArch is free software: you can redistribute it and/or modify it under the terms + * of the GNU General Public License as published by the Free Software Found- + * ation, either version 3 of the License, or (at your option) any later version. + * + * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; + * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + * PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with RetroArch. + * If not, see . + */ + +#ifndef __GFX_CONTEXT_H +#define __GFX_CONTEXT_H + +#include "../boolean.h" +#include "../driver.h" + +#ifdef HAVE_CONFIG_H +#include "../config.h" +#endif + +#ifdef HAVE_SDL +#include "SDL_syswm.h" +#endif + +void gfx_ctx_set_swap_interval(unsigned interval, bool inited); + +bool gfx_ctx_set_video_mode( + unsigned width, unsigned height, + unsigned bits, bool fullscreen); + +bool gfx_ctx_init(void); +void gfx_ctx_destroy(void); + +void gfx_ctx_get_video_size(unsigned *width, unsigned *height); +void gfx_ctx_update_window_title(bool reset); + +void gfx_ctx_swap_buffers(void); + +bool gfx_ctx_key_pressed(int key); + +void gfx_ctx_check_window(bool *quit, + bool *resize, unsigned *width, unsigned *height, unsigned frame_count); + +void gfx_ctx_set_resize(unsigned width, unsigned height); + +#ifdef HAVE_SDL +bool gfx_ctx_get_wm_info(SDL_SysWMinfo *info); +#endif + +bool gfx_ctx_window_has_focus(void); + +void gfx_ctx_input_driver(const input_driver_t **input, void **input_data); + +#endif + diff --git a/gfx/gl.c b/gfx/gl.c index 004086af01..7c60d62f15 100644 --- a/gfx/gl.c +++ b/gfx/gl.c @@ -29,7 +29,7 @@ #include "gl_common.h" #include "gfx_common.h" -#include "sdlwrap.h" +#include "gfx_context.h" #include "../compat/strl.h" #ifdef HAVE_SDL @@ -865,7 +865,7 @@ static void check_window(gl_t *gl) { bool quit, resize; - sdlwrap_check_window(&quit, + gfx_ctx_check_window(&quit, &resize, &gl->win_width, &gl->win_height, gl->frame_count); @@ -1141,7 +1141,7 @@ static bool gl_frame(void *data, const void *frame, unsigned width, unsigned hei if (gl->should_resize) { gl->should_resize = false; - sdlwrap_set_resize(gl->win_width, gl->win_height); + gfx_ctx_set_resize(gl->win_width, gl->win_height); // On resize, we might have to recreate our FBOs due to "Viewport" scale, and set a new viewport. gl_update_resize(gl); @@ -1179,8 +1179,8 @@ static bool gl_frame(void *data, const void *frame, unsigned width, unsigned hei if (msg) gl_render_msg(gl, msg); - sdlwrap_update_window_title(false); - sdlwrap_swap_buffers(); + gfx_ctx_update_window_title(false); + gfx_ctx_swap_buffers(); return true; } @@ -1200,7 +1200,7 @@ static void gl_free(void *data) gl_deinit_fbo(gl); #endif - sdlwrap_destroy(); + gfx_ctx_destroy(); if (gl->empty_buf) free(gl->empty_buf); @@ -1214,7 +1214,7 @@ static void gl_set_nonblock_state(void *data, bool state) if (gl->vsync) { RARCH_LOG("GL VSync => %s\n", state ? "off" : "on"); - sdlwrap_set_swap_interval(state ? 0 : 1, true); + gfx_ctx_set_swap_interval(state ? 0 : 1, true); } } @@ -1224,14 +1224,14 @@ static void *gl_init(const video_info_t *video, const input_driver_t **input, vo gfx_set_dwm(); #endif - if (!sdlwrap_init()) + if (!gfx_ctx_init()) return NULL; unsigned full_x = 0, full_y = 0; - sdlwrap_get_video_size(&full_x, &full_y); + gfx_ctx_get_video_size(&full_x, &full_y); RARCH_LOG("Detecting desktop resolution %ux%u.\n", full_x, full_y); - sdlwrap_set_swap_interval(video->vsync ? 1 : 0, false); + gfx_ctx_set_swap_interval(video->vsync ? 1 : 0, false); unsigned win_width = video->width; unsigned win_height = video->height; @@ -1241,11 +1241,11 @@ static void *gl_init(const video_info_t *video, const input_driver_t **input, vo win_height = full_y; } - if (!sdlwrap_set_video_mode(win_width, win_height, + if (!gfx_ctx_set_video_mode(win_width, win_height, g_settings.video.force_16bit ? 15 : 0, video->fullscreen)) return NULL; - sdlwrap_update_window_title(true); + gfx_ctx_update_window_title(true); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -1254,7 +1254,7 @@ static void *gl_init(const video_info_t *video, const input_driver_t **input, vo // Need to load dynamically :( if (!load_gl_proc()) { - sdlwrap_destroy(); + gfx_ctx_destroy(); return NULL; } #endif @@ -1262,7 +1262,7 @@ static void *gl_init(const video_info_t *video, const input_driver_t **input, vo gl_t *gl = (gl_t*)calloc(1, sizeof(gl_t)); if (!gl) { - sdlwrap_destroy(); + gfx_ctx_destroy(); return NULL; } @@ -1279,7 +1279,7 @@ static void *gl_init(const video_info_t *video, const input_driver_t **input, vo if (!gl_shader_init()) { RARCH_ERR("Shader init failed.\n"); - sdlwrap_destroy(); + gfx_ctx_destroy(); free(gl); return NULL; } @@ -1375,12 +1375,12 @@ static void *gl_init(const video_info_t *video, const input_driver_t **input, vo memcpy(gl->prev_info[i].coord, tex_coords, sizeof(tex_coords)); } - sdlwrap_input_driver(input, input_data); + gfx_ctx_input_driver(input, input_data); gl_init_font(gl, g_settings.video.font_path, g_settings.video.font_size); if (!gl_check_error()) { - sdlwrap_destroy(); + gfx_ctx_destroy(); free(gl); return NULL; } @@ -1398,7 +1398,7 @@ static bool gl_alive(void *data) static bool gl_focus(void *data) { (void)data; - return sdlwrap_window_has_focus(); + return gfx_ctx_window_has_focus(); } #ifdef HAVE_XML diff --git a/gfx/sdlwrap.h b/gfx/sdlwrap.h deleted file mode 100644 index c0fe3ecfae..0000000000 --- a/gfx/sdlwrap.h +++ /dev/null @@ -1,78 +0,0 @@ -/* RetroArch - A frontend for libretro. - * Copyright (C) 2010-2012 - Hans-Kristian Arntzen - * - - * - * RetroArch is free software: you can redistribute it and/or modify it under the terms - * of the GNU General Public License as published by the Free Software Found- - * ation, either version 3 of the License, or (at your option) any later version. - * - * RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; - * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR - * PURPOSE. See the GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along with RetroArch. - * If not, see . - */ - -// Compatibility wrapper between SDL 1.2/1.3 for OpenGL. -// Wraps functions which differ in 1.2 and 1.3. - -#ifndef __SDLWRAP_H -#define __SDLWRAP_H - -#include "../boolean.h" -#include "../driver.h" - -#ifdef HAVE_CONFIG_H -#include "../config.h" -#endif - -#ifdef HAVE_SDL -#include "SDL.h" -#include "SDL_version.h" -#include "SDL_syswm.h" - -#if SDL_VERSION_ATLEAST(1, 3, 0) -#define SDL_MODERN 1 -#else -#define SDL_MODERN 0 -#endif - -// Not legal to cast (void*) to fn-pointer. Need workaround to be compliant. -#define SDL_SYM_WRAP(sym, symbol) { \ - rarch_assert(sizeof(void*) == sizeof(void (*)(void))); \ - void *sym__ = SDL_GL_GetProcAddress(symbol); \ - memcpy(&(sym), &sym__, sizeof(void*)); \ -} -#endif - -void sdlwrap_set_swap_interval(unsigned interval, bool inited); - -bool sdlwrap_set_video_mode( - unsigned width, unsigned height, - unsigned bits, bool fullscreen); - -bool sdlwrap_init(void); -void sdlwrap_destroy(void); - -void sdlwrap_get_video_size(unsigned *width, unsigned *height); -void sdlwrap_update_window_title(bool reset); - -void sdlwrap_swap_buffers(void); - -bool sdlwrap_key_pressed(int key); - -void sdlwrap_check_window(bool *quit, - bool *resize, unsigned *width, unsigned *height, unsigned frame_count); - -void sdlwrap_set_resize(unsigned width, unsigned height); - -bool sdlwrap_get_wm_info(SDL_SysWMinfo *info); - -bool sdlwrap_window_has_focus(void); - -void sdlwrap_input_driver(const input_driver_t **input, void **input_data); - -#endif - diff --git a/gfx/shader_glsl.c b/gfx/shader_glsl.c index f5eb798516..9956d5d05c 100644 --- a/gfx/shader_glsl.c +++ b/gfx/shader_glsl.c @@ -32,7 +32,8 @@ #define NO_SDL_GLEXT -#include "sdlwrap.h" +#include "gfx_context.h" +#include "context/sdl_ctx.h" #include "SDL_opengl.h" #include diff --git a/input/dinput.c b/input/dinput.c index 5e2ea43c06..2bdb09e7c1 100644 --- a/input/dinput.c +++ b/input/dinput.c @@ -20,7 +20,7 @@ #include #include #include -#include "../gfx/sdlwrap.h" +#include "../gfx/gfx_context.h" void sdl_dinput_free(sdl_dinput_t *di) { @@ -108,7 +108,7 @@ sdl_dinput_t* sdl_dinput_init(void) SDL_SysWMinfo info; SDL_VERSION(&info.version); - if (!sdlwrap_get_wm_info(&info)) + if (!gfx_ctx_get_wm_info(&info)) { RARCH_ERR("Failed to get SysWM info.\n"); goto error; diff --git a/input/sdl_input.c b/input/sdl_input.c index 3e9826cd65..a0bc134269 100644 --- a/input/sdl_input.c +++ b/input/sdl_input.c @@ -15,7 +15,7 @@ #include "../driver.h" -#include "../gfx/sdlwrap.h" +#include "../gfx/gfx_context.h" #include "../boolean.h" #include "../general.h" #include @@ -183,7 +183,7 @@ static void *sdl_input_init(void) static bool sdl_key_pressed(int key) { - return sdlwrap_key_pressed(keysym_lut[key]); + return gfx_ctx_key_pressed(keysym_lut[key]); } #ifndef HAVE_DINPUT