From d3eec312308b58a2c3b950e2bf3eb4a273fe4044 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Mon, 29 May 2006 03:53:21 +0000 Subject: [PATCH] Implemented many compatibility functions --- .indent.pro | 2 +- include/SDL_compat.h | 125 +++++-------- src/SDL_compat.c | 314 ++++++++++++++++++++++++++++++++ src/video/SDL_video.c | 41 ++++- src/video/dummy/SDL_nullvideo.c | 5 + 5 files changed, 401 insertions(+), 86 deletions(-) create mode 100644 src/SDL_compat.c diff --git a/.indent.pro b/.indent.pro index fbc40de84..d95d15d95 100644 --- a/.indent.pro +++ b/.indent.pro @@ -1 +1 @@ --i4 -nut -nsc -br -ce +-i4 -nut -nsc -br -ce -cdw -npcs diff --git a/include/SDL_compat.h b/include/SDL_compat.h index ef2463ae3..dddc02cef 100644 --- a/include/SDL_compat.h +++ b/include/SDL_compat.h @@ -30,54 +30,52 @@ #include "begin_code.h" /* Set up for C function definitions, even when using C++ */ #ifdef __cplusplus -extern "C" -{ +/* *INDENT-OFF* */ +extern "C" { +/* *INDENT-ON* */ #endif - extern DECLSPEC char *SDLCALL SDL_AudioDriverName (char *namebuf, - int maxlen); - extern DECLSPEC char *SDLCALL SDL_VideoDriverName (char *namebuf, - int maxlen); - extern DECLSPEC int SDLCALL SDL_VideoModeOK (int width, int height, - int bpp, Uint32 flags); - extern DECLSPEC SDL_Rect **SDLCALL SDL_ListModes (SDL_PixelFormat * - format, Uint32 flags); - extern DECLSPEC SDL_Surface *SDLCALL SDL_SetVideoMode (int width, - int height, - int bpp, - Uint32 flags); +#define SDL_SWSURFACE 0x00000000 +#define SDL_HWSURFACE 0x00000001 +#define SDL_ASYNCBLIT 0x00000004 +#define SDL_ANYFORMAT 0x10000000 +#define SDL_HWPALETTE 0x20000000 +#define SDL_DOUBLEBUF 0x40000000 +#define SDL_FULLSCREEN 0x80000000 +#define SDL_OPENGL 0x00000002 +#define SDL_INTERNALOPENGL 0x00000008 +#define SDL_RESIZABLE 0x00000010 +#define SDL_NOFRAME 0x00000020 +#define SDL_HWACCEL 0x00000100 +#define SDL_SRCCOLORKEY 0x00001000 +#define SDL_RLEACCELOK 0x00002000 +#define SDL_RLEACCEL 0x00004000 +#define SDL_SRCALPHA 0x00010000 + +#define SDL_APPMOUSEFOCUS 0x01 +#define SDL_APPINPUTFOCUS 0x02 +#define SDL_APPACTIVE 0x04 -/* Ends C function definitions when using C++ */ -#ifdef __cplusplus -} -#endif -#include "close_code.h" +#define SDL_LOGPAL 0x01 +#define SDL_PHYSPAL 0x02 -#endif /* _SDL_compat_h */ +typedef enum +{ + SDL_GRAB_QUERY = -1, + SDL_GRAB_OFF = 0, + SDL_GRAB_ON = 1 +} SDL_GrabMode; +extern DECLSPEC const SDL_version *SDLCALL SDL_Linked_Version (void); +extern DECLSPEC char *SDLCALL SDL_AudioDriverName (char *namebuf, int maxlen); +extern DECLSPEC char *SDLCALL SDL_VideoDriverName (char *namebuf, int maxlen); +extern DECLSPEC int SDLCALL SDL_VideoModeOK (int width, int height, int bpp, + Uint32 flags); +extern DECLSPEC SDL_Rect **SDLCALL SDL_ListModes (SDL_PixelFormat * format, + Uint32 flags); +extern DECLSPEC SDL_Surface *SDLCALL SDL_SetVideoMode (int width, int height, + int bpp, Uint32 flags); extern DECLSPEC SDL_Surface *SDLCALL SDL_GetVideoSurface (void); - -/* These are the currently supported flags for the SDL_surface */ -/* Available for SDL_CreateRGBSurface() or SDL_SetVideoMode() */ -#define SDL_SWSURFACE 0x00000000 /* Surface is in system memory */ -#define SDL_HWSURFACE 0x00000001 /* Surface is in video memory */ -#define SDL_ASYNCBLIT 0x00000004 /* Use asynchronous blits if possible */ -/* Available for SDL_SetVideoMode() */ -#define SDL_ANYFORMAT 0x10000000 /* Allow any video depth/pixel-format */ -#define SDL_HWPALETTE 0x20000000 /* Surface has exclusive palette */ -#define SDL_DOUBLEBUF 0x40000000 /* Set up double-buffered video mode */ -#define SDL_FULLSCREEN 0x80000000 /* Surface is a full screen display */ -#define SDL_OPENGL 0x00000002 /* Create an OpenGL rendering context */ -#define SDL_INTERNALOPENGL 0x00000008 /* SDL uses OpenGL internally for this window */ -#define SDL_RESIZABLE 0x00000010 /* This video mode may be resized */ -#define SDL_NOFRAME 0x00000020 /* No window caption or edge frame */ -/* Used internally (read-only) */ -#define SDL_HWACCEL 0x00000100 /* Blit uses hardware acceleration */ -#define SDL_SRCCOLORKEY 0x00001000 /* Blit uses a source color key */ -#define SDL_RLEACCELOK 0x00002000 /* Private flag */ -#define SDL_RLEACCEL 0x00004000 /* Surface is RLE encoded */ -#define SDL_SRCALPHA 0x00010000 /* Blit uses source alpha blending */ - extern DECLSPEC void SDLCALL SDL_WM_SetCaption (const char *title, const char *icon); extern DECLSPEC void SDLCALL SDL_WM_GetCaption (char **title, char **icon); @@ -85,42 +83,19 @@ extern DECLSPEC void SDLCALL SDL_WM_SetIcon (SDL_Surface * icon, Uint8 * mask); extern DECLSPEC int SDLCALL SDL_WM_IconifyWindow (void); extern DECLSPEC int SDLCALL SDL_WM_ToggleFullScreen (SDL_Surface * surface); -typedef enum -{ - SDL_GRAB_QUERY = -1, - SDL_GRAB_OFF = 0, - SDL_GRAB_ON = 1 -} SDL_GrabMode; extern DECLSPEC SDL_GrabMode SDLCALL SDL_WM_GrabInput (SDL_GrabMode mode); - -/* The available application states */ -#define SDL_APPMOUSEFOCUS 0x01 /* The app has mouse coverage */ -#define SDL_APPINPUTFOCUS 0x02 /* The app has input focus */ -#define SDL_APPACTIVE 0x04 /* The application is active */ - extern DECLSPEC Uint8 SDLCALL SDL_GetAppState (void); -extern DECLSPEC const SDL_version *SDLCALL SDL_Linked_Version (void); -/* flags for SDL_SetPalette() */ -#define SDL_LOGPAL 0x01 -#define SDL_PHYSPAL 0x02 - -/* - * Sets a portion of the colormap for a given 8-bit surface. - * 'flags' is one or both of: - * SDL_LOGPAL -- set logical palette, which controls how blits are mapped - * to/from the surface, - * SDL_PHYSPAL -- set physical palette, which controls how pixels look on - * the screen - * Only screens have physical palettes. Separate change of physical/logical - * palettes is only possible if the screen has SDL_HWPALETTE set. - * - * The return value is 1 if all colours could be set as requested, and 0 - * otherwise. - * - * SDL_SetColors() is equivalent to calling this function with - * flags = (SDL_LOGPAL|SDL_PHYSPAL). - */ extern DECLSPEC int SDLCALL SDL_SetPalette (SDL_Surface * surface, int flags, SDL_Color * colors, int firstcolor, int ncolors); extern DECLSPEC int SDLCALL SDL_GetWMInfo (SDL_SysWMinfo * info); + +/* Ends C function definitions when using C++ */ +#ifdef __cplusplus +/* *INDENT-OFF* */ +} +/* *INDENT-ON* */ +#endif +#include "close_code.h" + +#endif /* _SDL_compat_h */ diff --git a/src/SDL_compat.c b/src/SDL_compat.c new file mode 100644 index 000000000..0b6a03102 --- /dev/null +++ b/src/SDL_compat.c @@ -0,0 +1,314 @@ +/* + SDL - Simple DirectMedia Layer + Copyright (C) 1997-2006 Sam Lantinga + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Sam Lantinga + slouken@libsdl.org +*/ +#include "SDL_config.h" + +/* This file contains functions for backwards compatibility with SDL 1.2 */ + +#include "SDL.h" + +#include "video/SDL_sysvideo.h" + + +static SDL_WindowID window; +static char *wm_title; + +char * +SDL_AudioDriverName (char *namebuf, int maxlen) +{ + const char *name = SDL_GetCurrentAudioDriver (); + if (name) { + SDL_strlcpy (namebuf, name, maxlen); + return namebuf; + } + return NULL; +} + +char * +SDL_VideoDriverName (char *namebuf, int maxlen) +{ + const char *name = SDL_GetCurrentVideoDriver (); + if (name) { + SDL_strlcpy (namebuf, name, maxlen); + return namebuf; + } + return NULL; +} + +int +SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags) +{ + int i, actual_bpp = 0; + + if (!SDL_GetVideoDevice ()) { + return 0; + } + + if (!(flags & SDL_FULLSCREEN)) { + return SDL_BITSPERPIXEL (SDL_GetDesktopDisplayMode ()->format); + } + + for (i = 0; i < SDL_GetNumDisplayModes (); ++i) { + const SDL_DisplayMode *mode = SDL_GetDisplayMode (i); + if (!mode->w || !mode->h || (width == mode->w && height == mode->h)) { + if (!mode->format) { + return bpp; + } + if (SDL_BITSPERPIXEL (mode->format) >= bpp) { + actual_bpp = SDL_BITSPERPIXEL (mode->format); + } + } + } + return actual_bpp; +} + +SDL_Rect ** +SDL_ListModes (SDL_PixelFormat * format, Uint32 flags) +{ + int i, nmodes; + SDL_Rect **modes; + + if (!SDL_GetVideoDevice ()) { + return NULL; + } + + if (!(flags & SDL_FULLSCREEN)) { + return (SDL_Rect **) (-1); + } + + /* Memory leak, but this is a compatibility function, who cares? */ + nmodes = 0; + for (i = 0; i < SDL_GetNumDisplayModes (); ++i) { + const SDL_DisplayMode *mode = SDL_GetDisplayMode (i); + if (!mode->w || !mode->h) { + return (SDL_Rect **) (-1); + } + if (SDL_BITSPERPIXEL (mode->format) != format->BitsPerPixel) { + continue; + } + if (nmodes > 0 && modes[nmodes - 1]->w == mode->w + && modes[nmodes - 1]->h == mode->h) { + continue; + } + + modes = SDL_realloc (modes, (nmodes + 2) * sizeof (*modes)); + if (!modes) { + return NULL; + } + modes[nmodes] = (SDL_Rect *) SDL_malloc (sizeof (SDL_Rect)); + if (!modes[nmodes]) { + return NULL; + } + modes[nmodes]->x = 0; + modes[nmodes]->y = 0; + modes[nmodes]->w = mode->w; + modes[nmodes]->h = mode->h; + ++nmodes; + } + if (modes) { + modes[nmodes] = NULL; + } + return modes; +} + +SDL_Surface * +SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags) +{ + SDL_DisplayMode mode; + int i; + Uint32 window_flags; + Uint32 desktop_format; + Uint32 desired_format; + + if (!SDL_GetVideoDevice ()) { + if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) { + return NULL; + } + } + + /* Destroy existing window */ + SDL_DestroyWindow (window); + + /* Create a new window */ + window_flags = SDL_WINDOW_SHOWN; + if (flags & SDL_FULLSCREEN) { + window_flags |= SDL_WINDOW_FULLSCREEN; + } + if (flags & SDL_OPENGL) { + window_flags |= SDL_WINDOW_OPENGL; + } + if (flags & SDL_RESIZABLE) { + window_flags |= SDL_WINDOW_RESIZABLE; + } + if (flags & SDL_NOFRAME) { + window_flags |= SDL_WINDOW_BORDERLESS; + } + window = SDL_CreateWindow (wm_title, 0, 0, width, height, window_flags); + if (!window) { + return NULL; + } + + /* Set up the desired display mode */ + desktop_format = SDL_GetDesktopDisplayMode ()->format; + if ((bpp == SDL_BITSPERPIXEL(desktop_format)) || + (desktop_format && (flags & SDL_ANYFORMAT))) { + desired_format = desktop_format; + } else { + switch (bpp) { + case 8: + desired_format = SDL_PixelFormat_Index8; + break; + case 15: + desired_format = SDL_PixelFormat_RGB555; + break; + case 16: + desired_format = SDL_PixelFormat_RGB565; + break; + case 24: + desired_format = SDL_PixelFormat_RGB24; + break; + case 32: + desired_format = SDL_PixelFormat_RGB888; + break; + default: + SDL_SetError ("Unsupported bpp in SDL_SetVideoMode()"); + return NULL; + } + } + mode.format = desired_format; + mode.w = width; + mode.h = height; + mode.refresh_rate = 0; + + /* Set the desired display mode */ + if (flags & SDL_FULLSCREEN) { + if (!SDL_GetClosestDisplayMode (&mode, &mode)) { + return NULL; + } + } else { + mode = *SDL_GetDesktopDisplayMode (); + } + if (SDL_SetDisplayMode (&mode) < 0) { + return NULL; + } + + /* Create the display surface */ + return SDL_CreateWindowSurface (window, desired_format, flags); +} + +SDL_Surface * +SDL_GetVideoSurface (void) +{ + SDL_VideoDevice *_this = SDL_GetVideoDevice (); + + return SDL_VideoSurface; +} + +void +SDL_WM_SetCaption (const char *title, const char *icon) +{ + if (wm_title) { + SDL_free (wm_title); + } else { + wm_title = SDL_strdup (title); + } + SDL_SetWindowTitle (window, wm_title); +} + +void +SDL_WM_GetCaption (char **title, char **icon) +{ + if (title) { + *title = wm_title; + } + if (icon) { + *icon = ""; + } +} + +void +SDL_WM_SetIcon (SDL_Surface * icon, Uint8 * mask) +{ + /* FIXME */ +} + +int +SDL_WM_IconifyWindow (void) +{ + SDL_MinimizeWindow (window); +} + +int +SDL_WM_ToggleFullScreen (SDL_Surface * surface) +{ + return 0; +} + +SDL_GrabMode +SDL_WM_GrabInput (SDL_GrabMode mode) +{ + if (mode != SDL_GRAB_QUERY) { + SDL_SetWindowGrab (window, mode); + } + return (SDL_GrabMode) SDL_GetWindowGrab (window); +} + +Uint8 +SDL_GetAppState (void) +{ + Uint8 state = 0; + Uint32 flags = 0; + + flags = SDL_GetWindowFlags (window); + if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) { + state |= SDL_APPACTIVE; + } + if (flags & SDL_WINDOW_KEYBOARD_FOCUS) { + state |= SDL_APPINPUTFOCUS; + } + if (flags & SDL_WINDOW_MOUSE_FOCUS) { + state |= SDL_APPMOUSEFOCUS; + } + return state; +} + +const SDL_version * +SDL_Linked_Version (void) +{ + static SDL_version version; + SDL_VERSION (&version); + return &version; +} + +int +SDL_SetPalette (SDL_Surface * surface, int flags, SDL_Color * colors, + int firstcolor, int ncolors) +{ + SDL_SetColors (surface, colors, firstcolor, ncolors); +} + +int +SDL_GetWMInfo (SDL_SysWMinfo * info) +{ + return SDL_GetWindowWMInfo (window, info); +} + +/* vi: set ts=4 sw=4 expandtab: */ diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c index 30682796e..5fb8ff3e2 100644 --- a/src/video/SDL_video.c +++ b/src/video/SDL_video.c @@ -512,7 +512,9 @@ SDL_GetClosestDisplayMode (const SDL_DisplayMode * mode, int SDL_SetDisplayMode (const SDL_DisplayMode * mode) { + SDL_VideoDisplay *display; SDL_DisplayMode display_mode; + int i; if (!_this) { SDL_SetError ("Video subsystem has not been initialized"); @@ -524,21 +526,21 @@ SDL_SetDisplayMode (const SDL_DisplayMode * mode) SDL_SetError ("No mode passed to SDL_SetDisplayMode"); return -1; } + display = &SDL_CurrentDisplay; display_mode = *mode; /* Default to the current mode */ if (!display_mode.format) { - display_mode.format = SDL_CurrentDisplay.current_mode.format; + display_mode.format = display->current_mode.format; } if (!display_mode.w) { - display_mode.w = SDL_CurrentDisplay.current_mode.w; + display_mode.w = display->current_mode.w; } if (!display_mode.h) { - display_mode.h = SDL_CurrentDisplay.current_mode.h; + display_mode.h = display->current_mode.h; } if (!display_mode.refresh_rate) { - display_mode.refresh_rate = - SDL_CurrentDisplay.current_mode.refresh_rate; + display_mode.refresh_rate = display->current_mode.refresh_rate; } /* Get a good video mode, the closest one possible */ @@ -555,6 +557,19 @@ SDL_SetDisplayMode (const SDL_DisplayMode * mode) return 0; } + /* Free any previous window surfaces */ + for (i = 0; i < display->num_windows; ++i) { + SDL_Window *window = &display->windows[i]; + if (window->shadow) { + SDL_FreeSurface (window->shadow); + window->shadow = NULL; + } + if (window->surface) { + SDL_FreeSurface (window->surface); + window->surface = NULL; + } + } + return _this->SetDisplayMode (_this, &display_mode); } @@ -580,7 +595,7 @@ SDL_CreateWindow (const char *title, int x, int y, int w, int h, Uint32 flags) SDL_zero (window); window.id = _this->next_window_id++; - window.title = SDL_strdup (title); + window.title = title ? SDL_strdup (title) : NULL; window.x = x; window.y = y; window.w = w; @@ -588,7 +603,9 @@ SDL_CreateWindow (const char *title, int x, int y, int w, int h, Uint32 flags) window.flags = (flags & allowed_flags); if (_this->CreateWindow && _this->CreateWindow (_this, &window) < 0) { - SDL_free (window.title); + if (window.title) { + SDL_free (window.title); + } return 0; } @@ -600,7 +617,9 @@ SDL_CreateWindow (const char *title, int x, int y, int w, int h, Uint32 flags) if (_this->DestroyWindow) { _this->DestroyWindow (_this, &window); } - SDL_free (window.title); + if (window.title) { + SDL_free (window.title); + } return 0; } windows[num_windows] = window; @@ -638,7 +657,9 @@ SDL_CreateWindowFrom (void *data) if (_this->DestroyWindow) { _this->DestroyWindow (_this, &window); } - SDL_free (window.title); + if (window.title) { + SDL_free (window.title); + } return 0; } windows[num_windows] = window; @@ -674,7 +695,7 @@ SDL_GetWindowFromSurface (SDL_Surface * surface) { int i, j; - if (!_this) { + if (!_this || !surface) { return NULL; } diff --git a/src/video/dummy/SDL_nullvideo.c b/src/video/dummy/SDL_nullvideo.c index 7365e2861..20e0a44cb 100644 --- a/src/video/dummy/SDL_nullvideo.c +++ b/src/video/dummy/SDL_nullvideo.c @@ -118,8 +118,13 @@ VideoBootStrap DUMMY_bootstrap = { int DUMMY_VideoInit (_THIS) { + SDL_DisplayMode mode; + SDL_AddBasicVideoDisplay (NULL); + SDL_zero(mode); + SDL_AddDisplayMode(0, &mode); + /* We're done! */ return 0; }