Skip to content

Commit

Permalink
Restore backwards compatibility with SDL 1.2.
Browse files Browse the repository at this point in the history
  • Loading branch information
adelva1984 committed Jul 4, 2013
1 parent a2d3d43 commit 11e3c40
Show file tree
Hide file tree
Showing 11 changed files with 230 additions and 28 deletions.
9 changes: 5 additions & 4 deletions arch/mingw/Makefile.in
Expand Up @@ -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
Expand All @@ -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
Expand Down
80 changes: 79 additions & 1 deletion src/compat.h
Expand Up @@ -203,10 +203,19 @@ static inline void *crealloc(void *ptr, size_t size)
#if defined(CONFIG_SDL) && !defined(SKIP_SDL)

#include <SDL.h>
#include <SDL_syswm.h>

#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
Expand All @@ -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 */

Expand Down
13 changes: 10 additions & 3 deletions src/editor/robo_ed.c
Expand Up @@ -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);
Expand All @@ -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;
Expand Down
54 changes: 51 additions & 3 deletions src/event_sdl.c
Expand Up @@ -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);
Expand All @@ -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:
{
Expand Down Expand Up @@ -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) &&
Expand Down Expand Up @@ -317,15 +354,26 @@ 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;
}

case SDL_KEYUP:
{
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)
{
Expand Down
3 changes: 2 additions & 1 deletion src/graphics.c
Expand Up @@ -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__
Expand Down
3 changes: 3 additions & 0 deletions src/platform_sdl.c
Expand Up @@ -78,6 +78,9 @@ bool platform_init(void)
}
}

#if !SDL_VERSION_ATLEAST(2,0,0)
SDL_EnableUNICODE(1);
#endif
return true;
}

Expand Down
12 changes: 5 additions & 7 deletions src/render_gp2x.c
Expand Up @@ -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)
{
Expand Down Expand Up @@ -357,15 +351,19 @@ 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)
{
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;
Expand Down

0 comments on commit 11e3c40

Please sign in to comment.