From 11e3c405c1ae70f0dcf26ce21e5458b6ddfb605f Mon Sep 17 00:00:00 2001 From: Alistair John Strachan Date: Tue, 2 Jul 2013 22:00:52 -0700 Subject: [PATCH] Restore backwards compatibility with SDL 1.2. --- arch/mingw/Makefile.in | 9 ++--- src/compat.h | 80 +++++++++++++++++++++++++++++++++++++++++- src/editor/robo_ed.c | 13 +++++-- src/event_sdl.c | 54 ++++++++++++++++++++++++++-- src/graphics.c | 3 +- src/platform_sdl.c | 3 ++ src/render_gp2x.c | 12 +++---- src/render_sdl.c | 55 ++++++++++++++++++++++++++++- src/render_sdl.h | 11 +++++- src/render_soft.c | 17 +++++---- src/render_yuv1.c | 1 + 11 files changed, 230 insertions(+), 28 deletions(-) diff --git a/arch/mingw/Makefile.in b/arch/mingw/Makefile.in index 749e5f05..85f01f6d 100644 --- a/arch/mingw/Makefile.in +++ b/arch/mingw/Makefile.in @@ -33,11 +33,12 @@ LIBPNG_LDFLAGS = $(shell libpng-config --static --ldflags) ifeq (${BUILD_LIBSDL2},) SDL_PREFIX := ${shell sdl-config --prefix} +SDL_DLL := SDL.dll else SDL_PREFIX := ${shell sdl2-config --prefix} +SDL_DLL := SDL2.dll endif - ifdef DEBUG ARCH_LDFLAGS += -mconsole endif @@ -60,11 +61,11 @@ clean: ${RM} -f ${pefix} arch/mingw/pefix.o arch/mingw/pefix.d # -# Windows builds must copy SDL.dll and create a directx.bat +# Windows builds must copy $SDL_DLL and create a directx.bat # build: ${build} - ${CP} ${SDL_PREFIX} /bin/SDL.dll ${build} - ${PEFIX} ${build}/SDL.dll + ${CP} ${SDL_PREFIX}/bin/${SDL_DLL} ${build} + ${PEFIX} ${build}/${SDL_DLL} ${CP} arch/mingw/directx.bat ${build} ifeq (${BUILD_UTILS},1) ${CP} arch/mingw/checkres* ${build}/utils diff --git a/src/compat.h b/src/compat.h index 42503282..5bd8caee 100644 --- a/src/compat.h +++ b/src/compat.h @@ -203,10 +203,19 @@ static inline void *crealloc(void *ptr, size_t size) #if defined(CONFIG_SDL) && !defined(SKIP_SDL) #include +#include #if !SDL_VERSION_ATLEAST(2,0,0) +// This block remaps anything that is EXACTLY equivalent to its new SDL 2.0 +// counterpart. More complex changes are handled with #ifdefs "in situ". + +// Data types + typedef SDLKey SDL_Keycode; +typedef void SDL_Window; + +// Macros / enumerants #define SDLK_KP_0 SDLK_KP0 #define SDLK_KP_1 SDLK_KP1 @@ -224,7 +233,76 @@ typedef SDLKey SDL_Keycode; #define SDLK_RGUI SDLK_RSUPER #define SDLK_PAUSE SDLK_BREAK -#endif /* !SDL_VERSION_ATLEAST(2,0,0) */ +#define SDL_WINDOW_FULLSCREEN SDL_FULLSCREEN +#define SDL_WINDOW_RESIZABLE SDL_RESIZABLE +#define SDL_WINDOW_OPENGL SDL_OPENGL + +// API functions + +#define SDL_SetEventFilter(filter, userdata) SDL_SetEventFilter(filter) + +static inline SDL_bool SDL_GetWindowWMInfo(SDL_Window *window, + SDL_SysWMinfo *info) +{ + return SDL_GetWMInfo(info) == 1 ? SDL_TRUE : SDL_FALSE; +} + +static inline SDL_Window *SDL_GetWindowFromID(Uint32 id) +{ + return NULL; +} + +static inline void SDL_WarpMouseInWindow(SDL_Window *window, int x, int y) +{ + SDL_WarpMouse(x, y); +} + +static inline void SDL_SetWindowTitle(SDL_Window *window, const char *title) +{ + SDL_WM_SetCaption(title, ""); +} + +static inline void SDL_SetWindowIcon(SDL_Window *window, SDL_Surface *icon) +{ + SDL_WM_SetIcon(icon, NULL); +} + +static inline int SDL_GL_SetSwapInterval(int interval) +{ + return SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, interval); +} + +#ifdef CONFIG_X11 +static inline XEvent *SDL_SysWMmsg_GetXEvent(SDL_SysWMmsg *msg) +{ + return &msg->event.xevent; +} +#endif /* CONFIG_X11 */ + +#if defined(CONFIG_ICON) && defined(__WIN32__) +static inline HWND SDL_SysWMinfo_GetWND(SDL_SysWMinfo *info) +{ + return info->window; +} +#endif /* CONFIG_ICON && __WIN32__ */ + +#else /* SDL_VERSION_ATLEAST(2,0,0) */ + +#ifdef CONFIG_X11 +static inline XEvent *SDL_SysWMmsg_GetXEvent(SDL_SysWMmsg *msg) +{ + return &msg->msg.x11.event; +} +#endif /* CONFIG_X11 */ + +#if defined(CONFIG_ICON) && defined(__WIN32__) +static inline HWND SDL_SysWMinfo_GetWND(SDL_SysWMinfo *info) +{ + return info->info.win.window; +} +#endif /* CONFIG_ICON && __WIN32__ */ + +#endif /* SDL_VERSION_ATLEAST(2,0,0) */ #endif /* CONFIG_SDL && !SKIP_SDL */ diff --git a/src/editor/robo_ed.c b/src/editor/robo_ed.c index 6eb2e37d..0ca95eac 100644 --- a/src/editor/robo_ed.c +++ b/src/editor/robo_ed.c @@ -1311,20 +1311,27 @@ static bool copy_selection_to_buffer(struct robot_state *rstate) #elif defined(CONFIG_X11) && defined(CONFIG_SDL) +#if SDL_VERSION_ATLEAST(2,0,0) static int copy_buffer_to_X11_selection(void *userdata, SDL_Event *event) +#else +static int copy_buffer_to_X11_selection(const SDL_Event *event) +#endif { XSelectionRequestEvent *request; +#if SDL_VERSION_ATLEAST(2,0,0) SDL_Window *window = userdata; +#endif char *dest_data, *dest_ptr; + XEvent response, *xevent; SDL_SysWMinfo info; int i, line_length; Display *display; - XEvent response; if(event->type != SDL_SYSWMEVENT) return 1; - if(event->syswm.msg->msg.x11.event.type != SelectionRequest || !copy_buffer) + xevent = SDL_SysWMmsg_GetXEvent(event->syswm.msg); + if(xevent->type != SelectionRequest || !copy_buffer) return 0; SDL_VERSION(&info.version); @@ -1347,7 +1354,7 @@ static int copy_buffer_to_X11_selection(void *userdata, SDL_Event *event) memcpy(dest_ptr, copy_buffer[i], line_length); dest_ptr[line_length] = 0; - request = &(event->syswm.msg->msg.x11.event.xselectionrequest); + request = &(SDL_SysWMmsg_GetXEvent(event->syswm.msg)->xselectionrequest); response.xselection.type = SelectionNotify; response.xselection.display = request->display; response.xselection.selection = request->selection; diff --git a/src/event_sdl.c b/src/event_sdl.c index 58814827..acda3a21 100644 --- a/src/event_sdl.c +++ b/src/event_sdl.c @@ -183,18 +183,22 @@ static bool process_event(SDL_Event *event) break; } +#if SDL_VERSION_ATLEAST(2,0,0) case SDL_WINDOWEVENT: { switch(event->window.event) { case SDL_WINDOWEVENT_RESIZED: + { resize_screen(event->window.data1, event->window.data2); break; + } + case SDL_WINDOWEVENT_FOCUS_LOST: + { // Pause while minimized if(input.unfocus_pause) { - // Wait for SDL_APPACTIVE with gain of 1 while(1) { SDL_WaitEvent(event); @@ -205,10 +209,36 @@ static bool process_event(SDL_Event *event) } } break; + } } break; } +#else // !SDL_VERSION_ATLEAST(2,0,0) + case SDL_VIDEORESIZE: + { + resize_screen(event->resize.w, event->resize.h); + break; + } + + case SDL_ACTIVEEVENT: + { + if(input.unfocus_pause) + { + // Pause while minimized + if(event->active.state & (SDL_APPACTIVE | SDL_APPINPUTFOCUS)) + { + // Wait for SDL_APPACTIVE with gain of 1 + do + { + SDL_WaitEvent(event); + } while((event->type != SDL_ACTIVEEVENT) || + (event->active.state & ~(SDL_APPACTIVE | SDL_APPINPUTFOCUS))); + } + } + break; + } +#endif // !SDL_VERSION_ATLEAST(2,0,0) case SDL_MOUSEMOTION: { @@ -263,7 +293,14 @@ static bool process_event(SDL_Event *event) { ckey = convert_SDL_internal(event->key.keysym.sym); if(!ckey) - break; + { +#if !SDL_VERSION_ATLEAST(2,0,0) + if(event->key.keysym.unicode) + ckey = IKEY_UNICODE; + else +#endif + break; + } if((ckey == IKEY_RETURN) && get_alt_status(keycode_internal) && @@ -317,7 +354,11 @@ static bool process_event(SDL_Event *event) } } +#if SDL_VERSION_ATLEAST(2,0,0) key_press(status, ckey, event->key.keysym.sym); +#else + key_press(status, ckey, event->key.keysym.unicode); +#endif break; } @@ -325,7 +366,14 @@ static bool process_event(SDL_Event *event) { ckey = convert_SDL_internal(event->key.keysym.sym); if(!ckey) - break; + { +#if !SDL_VERSION_ATLEAST(2,0,0) + if(status->keymap[IKEY_UNICODE]) + ckey = IKEY_UNICODE; + else +#endif + break; + } if(ckey == IKEY_NUMLOCK) { diff --git a/src/graphics.c b/src/graphics.c index 1962d7dc..bceb9e65 100644 --- a/src/graphics.c +++ b/src/graphics.c @@ -929,7 +929,8 @@ bool init_video(struct config_info *conf, const char *caption) SDL_VERSION(&info.version); SDL_GetWindowWMInfo(window, &info); - SendMessage(info.info.win.window, WM_SETICON, ICON_BIG, (LPARAM)icon); + SendMessage(SDL_SysWMinfo_GetWND(&info), + WM_SETICON, ICON_BIG, (LPARAM)icon); } } #else // !__WIN32__ diff --git a/src/platform_sdl.c b/src/platform_sdl.c index 538e8408..859108b8 100644 --- a/src/platform_sdl.c +++ b/src/platform_sdl.c @@ -78,6 +78,9 @@ bool platform_init(void) } } +#if !SDL_VERSION_ATLEAST(2,0,0) + SDL_EnableUNICODE(1); +#endif return true; } diff --git a/src/render_gp2x.c b/src/render_gp2x.c index 9188a6b4..8fb74865 100644 --- a/src/render_gp2x.c +++ b/src/render_gp2x.c @@ -231,12 +231,6 @@ static void gp2x_free_video(struct graphics_data *graphics) graphics->render_data = NULL; } -static bool gp2x_check_video_mode(struct graphics_data *graphics, - int width, int height, int depth, bool fullscreen, bool resize) -{ - return true; -} - static bool gp2x_set_video_mode(struct graphics_data *graphics, int width, int height, int depth, bool fullscreen, bool resize) { @@ -357,7 +351,11 @@ static void gp2x_sync_screen(struct graphics_data *graphics) &render_data->sdl.screen->clip_rect); } +#if SDL_VERSION_ATLEAST(2,0,0) SDL_RenderPresent(render_data->sdl.renderer); +#else + SDL_Flip(render_data->sdl.screen); +#endif } void render_gp2x_register(struct renderer *renderer) @@ -365,7 +363,7 @@ void render_gp2x_register(struct renderer *renderer) memset(renderer, 0, sizeof(struct renderer)); renderer->init_video = gp2x_init_video; renderer->free_video = gp2x_free_video; - renderer->check_video_mode = gp2x_check_video_mode; + renderer->check_video_mode = sdl_check_video_mode; renderer->set_video_mode = gp2x_set_video_mode; renderer->update_colors = gp2x_update_colors; renderer->resize_screen = resize_screen_standard; diff --git a/src/render_sdl.c b/src/render_sdl.c index cca9f165..7261a142 100644 --- a/src/render_sdl.c +++ b/src/render_sdl.c @@ -21,7 +21,10 @@ #include "SDL.h" -static int sdl_flags(int depth, bool fullscreen, bool resize) +#ifndef CONFIG_RENDER_YUV +static +#endif +int sdl_flags(int depth, bool fullscreen, bool resize) { int flags = 0; @@ -44,6 +47,8 @@ bool sdl_set_video_mode(struct graphics_data *graphics, int width, int height, int depth, bool fullscreen, bool resize) { struct sdl_render_data *render_data = graphics->render_data; + +#if SDL_VERSION_ATLEAST(2,0,0) bool matched = false; Uint32 fmt; @@ -160,8 +165,22 @@ bool sdl_set_video_mode(struct graphics_data *graphics, int width, int height, } graphics->window_id = SDL_GetWindowID(render_data->window); + +#else // !SDL_VERSION_ATLEAST(2,0,0) + + render_data->screen = SDL_SetVideoMode(width, height, depth, + sdl_flags(depth, fullscreen, resize)); + + if(!render_data->screen) + return false; + + render_data->shadow = NULL; + +#endif // !SDL_VERSION_ATLEAST(2,0,0) + return true; +#if SDL_VERSION_ATLEAST(2,0,0) err_free_palette: SDL_FreePalette(render_data->palette); render_data->palette = NULL; @@ -173,6 +192,18 @@ bool sdl_set_video_mode(struct graphics_data *graphics, int width, int height, render_data->window = NULL; err_out: return false; +#endif // SDL_VERSION_ATLEAST(2,0,0) +} + +bool sdl_check_video_mode(struct graphics_data *graphics, int width, + int height, int depth, bool fullscreen, bool resize) +{ +#if SDL_VERSION_ATLEAST(2,0,0) + return true; +#else + return SDL_VideoModeOK(width, height, depth, + sdl_flags(depth, fullscreen, resize)); +#endif } #if defined(CONFIG_RENDER_GL_FIXED) || defined(CONFIG_RENDER_GL_PROGRAM) @@ -181,6 +212,8 @@ bool gl_set_video_mode(struct graphics_data *graphics, int width, int height, int depth, bool fullscreen, bool resize) { struct sdl_render_data *render_data = graphics->render_data; + +#if SDL_VERSION_ATLEAST(2,0,0) SDL_GLContext context; render_data->window = SDL_CreateWindow("MegaZeux", @@ -216,14 +249,30 @@ bool gl_set_video_mode(struct graphics_data *graphics, int width, int height, } graphics->window_id = SDL_GetWindowID(render_data->window); + +#else // !SDL_VERSION_ATLEAST(2,0,0) + + if(!SDL_SetVideoMode(width, height, depth, + GL_STRIP_FLAGS(sdl_flags(depth, fullscreen, resize)))) + return false; + +#endif // !SDL_VERSION_ATLEAST(2,0,0) + render_data->screen = NULL; + render_data->shadow = NULL; + return true; } bool gl_check_video_mode(struct graphics_data *graphics, int width, int height, int depth, bool fullscreen, bool resize) { +#if SDL_VERSION_ATLEAST(2,0,0) return true; +#else + return SDL_VideoModeOK(width, height, depth, + GL_STRIP_FLAGS(sdl_flags(depth, fullscreen, resize))); +#endif } void gl_set_attributes(struct graphics_data *graphics) @@ -238,8 +287,12 @@ void gl_set_attributes(struct graphics_data *graphics) bool gl_swap_buffers(struct graphics_data *graphics) { +#if SDL_VERSION_ATLEAST(2,0,0) struct sdl_render_data *render_data = graphics->render_data; SDL_GL_SwapWindow(render_data->window); +#else + SDL_GL_SwapBuffers(); +#endif return true; } diff --git a/src/render_sdl.h b/src/render_sdl.h index a6da1a8c..dcddd159 100644 --- a/src/render_sdl.h +++ b/src/render_sdl.h @@ -30,16 +30,25 @@ __M_BEGIN_DECLS struct sdl_render_data { +#if SDL_VERSION_ATLEAST(2,0,0) SDL_Renderer *renderer; SDL_Palette *palette; + SDL_Window *window; +#endif SDL_Surface *screen; SDL_Surface *shadow; - SDL_Window *window; }; +#ifdef CONFIG_RENDER_YUV +int sdl_flags(int depth, bool fullscreen, bool resize); +#endif + bool sdl_set_video_mode(struct graphics_data *graphics, int width, int height, int depth, bool fullscreen, bool resize); +bool sdl_check_video_mode(struct graphics_data *graphics, int width, + int height, int depth, bool fullscreen, bool resize); + #if defined(CONFIG_RENDER_GL_FIXED) || defined(CONFIG_RENDER_GL_PROGRAM) #include "render_gl.h" diff --git a/src/render_soft.c b/src/render_soft.c index d50afcd7..630ad568 100644 --- a/src/render_soft.c +++ b/src/render_soft.c @@ -62,12 +62,6 @@ static bool soft_init_video(struct graphics_data *graphics, return set_video_mode(); } -static bool soft_check_video_mode(struct graphics_data *graphics, - int width, int height, int depth, bool fullscreen, bool resize) -{ - return true; -} - static void soft_update_colors(struct graphics_data *graphics, struct rgb_color *palette, Uint32 count) { @@ -93,7 +87,12 @@ static void soft_update_colors(struct graphics_data *graphics, sdlpal[i].g = palette[i].g; sdlpal[i].b = palette[i].b; } + +#if SDL_VERSION_ATLEAST(2,0,0) SDL_SetPaletteColors(render_data->palette, sdlpal, 0, count); +#else + SDL_SetColors(render_data->screen, sdlpal, 0, count); +#endif } } @@ -189,14 +188,18 @@ static void soft_sync_screen(struct graphics_data *graphics) &render_data->screen->clip_rect); } +#if SDL_VERSION_ATLEAST(2,0,0) SDL_RenderPresent(render_data->renderer); +#else + SDL_Flip(render_data->screen); +#endif } void render_soft_register(struct renderer *renderer) { memset(renderer, 0, sizeof(struct renderer)); renderer->init_video = soft_init_video; - renderer->check_video_mode = soft_check_video_mode; + renderer->check_video_mode = sdl_check_video_mode; renderer->set_video_mode = sdl_set_video_mode; renderer->update_colors = soft_update_colors; renderer->resize_screen = resize_screen_standard; diff --git a/src/render_yuv1.c b/src/render_yuv1.c index 99494f00..bdee9b47 100644 --- a/src/render_yuv1.c +++ b/src/render_yuv1.c @@ -19,6 +19,7 @@ #include "graphics.h" #include "render.h" +#include "render_sdl.h" #include "render_yuv.h" #include "renderers.h"