src/SDL_compat.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 29 May 2006 03:53:21 +0000
branchSDL-1.3
changeset 1667 1fddae038bc8
child 1668 4da1ee79c9af
permissions -rw-r--r--
Implemented many compatibility functions
     1 /*
     2     SDL - Simple DirectMedia Layer
     3     Copyright (C) 1997-2006 Sam Lantinga
     4 
     5     This library is free software; you can redistribute it and/or
     6     modify it under the terms of the GNU Lesser General Public
     7     License as published by the Free Software Foundation; either
     8     version 2.1 of the License, or (at your option) any later version.
     9 
    10     This library is distributed in the hope that it will be useful,
    11     but WITHOUT ANY WARRANTY; without even the implied warranty of
    12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13     Lesser General Public License for more details.
    14 
    15     You should have received a copy of the GNU Lesser General Public
    16     License along with this library; if not, write to the Free Software
    17     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    18 
    19     Sam Lantinga
    20     slouken@libsdl.org
    21 */
    22 #include "SDL_config.h"
    23 
    24 /* This file contains functions for backwards compatibility with SDL 1.2 */
    25 
    26 #include "SDL.h"
    27 
    28 #include "video/SDL_sysvideo.h"
    29 
    30 
    31 static SDL_WindowID window;
    32 static char *wm_title;
    33 
    34 char *
    35 SDL_AudioDriverName (char *namebuf, int maxlen)
    36 {
    37     const char *name = SDL_GetCurrentAudioDriver ();
    38     if (name) {
    39         SDL_strlcpy (namebuf, name, maxlen);
    40         return namebuf;
    41     }
    42     return NULL;
    43 }
    44 
    45 char *
    46 SDL_VideoDriverName (char *namebuf, int maxlen)
    47 {
    48     const char *name = SDL_GetCurrentVideoDriver ();
    49     if (name) {
    50         SDL_strlcpy (namebuf, name, maxlen);
    51         return namebuf;
    52     }
    53     return NULL;
    54 }
    55 
    56 int
    57 SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags)
    58 {
    59     int i, actual_bpp = 0;
    60 
    61     if (!SDL_GetVideoDevice ()) {
    62         return 0;
    63     }
    64 
    65     if (!(flags & SDL_FULLSCREEN)) {
    66         return SDL_BITSPERPIXEL (SDL_GetDesktopDisplayMode ()->format);
    67     }
    68 
    69     for (i = 0; i < SDL_GetNumDisplayModes (); ++i) {
    70         const SDL_DisplayMode *mode = SDL_GetDisplayMode (i);
    71         if (!mode->w || !mode->h || (width == mode->w && height == mode->h)) {
    72             if (!mode->format) {
    73                 return bpp;
    74             }
    75             if (SDL_BITSPERPIXEL (mode->format) >= bpp) {
    76                 actual_bpp = SDL_BITSPERPIXEL (mode->format);
    77             }
    78         }
    79     }
    80     return actual_bpp;
    81 }
    82 
    83 SDL_Rect **
    84 SDL_ListModes (SDL_PixelFormat * format, Uint32 flags)
    85 {
    86     int i, nmodes;
    87     SDL_Rect **modes;
    88 
    89     if (!SDL_GetVideoDevice ()) {
    90         return NULL;
    91     }
    92 
    93     if (!(flags & SDL_FULLSCREEN)) {
    94         return (SDL_Rect **) (-1);
    95     }
    96 
    97     /* Memory leak, but this is a compatibility function, who cares? */
    98     nmodes = 0;
    99     for (i = 0; i < SDL_GetNumDisplayModes (); ++i) {
   100         const SDL_DisplayMode *mode = SDL_GetDisplayMode (i);
   101         if (!mode->w || !mode->h) {
   102             return (SDL_Rect **) (-1);
   103         }
   104         if (SDL_BITSPERPIXEL (mode->format) != format->BitsPerPixel) {
   105             continue;
   106         }
   107         if (nmodes > 0 && modes[nmodes - 1]->w == mode->w
   108             && modes[nmodes - 1]->h == mode->h) {
   109             continue;
   110         }
   111 
   112         modes = SDL_realloc (modes, (nmodes + 2) * sizeof (*modes));
   113         if (!modes) {
   114             return NULL;
   115         }
   116         modes[nmodes] = (SDL_Rect *) SDL_malloc (sizeof (SDL_Rect));
   117         if (!modes[nmodes]) {
   118             return NULL;
   119         }
   120         modes[nmodes]->x = 0;
   121         modes[nmodes]->y = 0;
   122         modes[nmodes]->w = mode->w;
   123         modes[nmodes]->h = mode->h;
   124         ++nmodes;
   125     }
   126     if (modes) {
   127         modes[nmodes] = NULL;
   128     }
   129     return modes;
   130 }
   131 
   132 SDL_Surface *
   133 SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags)
   134 {
   135     SDL_DisplayMode mode;
   136     int i;
   137     Uint32 window_flags;
   138     Uint32 desktop_format;
   139     Uint32 desired_format;
   140 
   141     if (!SDL_GetVideoDevice ()) {
   142         if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
   143             return NULL;
   144         }
   145     }
   146 
   147     /* Destroy existing window */
   148     SDL_DestroyWindow (window);
   149 
   150     /* Create a new window */
   151     window_flags = SDL_WINDOW_SHOWN;
   152     if (flags & SDL_FULLSCREEN) {
   153         window_flags |= SDL_WINDOW_FULLSCREEN;
   154     }
   155     if (flags & SDL_OPENGL) {
   156         window_flags |= SDL_WINDOW_OPENGL;
   157     }
   158     if (flags & SDL_RESIZABLE) {
   159         window_flags |= SDL_WINDOW_RESIZABLE;
   160     }
   161     if (flags & SDL_NOFRAME) {
   162         window_flags |= SDL_WINDOW_BORDERLESS;
   163     }
   164     window = SDL_CreateWindow (wm_title, 0, 0, width, height, window_flags);
   165     if (!window) {
   166         return NULL;
   167     }
   168 
   169     /* Set up the desired display mode */
   170     desktop_format = SDL_GetDesktopDisplayMode ()->format;
   171     if ((bpp == SDL_BITSPERPIXEL(desktop_format)) ||
   172         (desktop_format && (flags & SDL_ANYFORMAT))) {
   173         desired_format = desktop_format;
   174     } else {
   175         switch (bpp) {
   176         case 8:
   177             desired_format = SDL_PixelFormat_Index8;
   178             break;
   179         case 15:
   180             desired_format = SDL_PixelFormat_RGB555;
   181             break;
   182         case 16:
   183             desired_format = SDL_PixelFormat_RGB565;
   184             break;
   185         case 24:
   186             desired_format = SDL_PixelFormat_RGB24;
   187             break;
   188         case 32:
   189             desired_format = SDL_PixelFormat_RGB888;
   190             break;
   191         default:
   192             SDL_SetError ("Unsupported bpp in SDL_SetVideoMode()");
   193             return NULL;
   194         }
   195     }
   196     mode.format = desired_format;
   197     mode.w = width;
   198     mode.h = height;
   199     mode.refresh_rate = 0;
   200 
   201     /* Set the desired display mode */
   202     if (flags & SDL_FULLSCREEN) {
   203         if (!SDL_GetClosestDisplayMode (&mode, &mode)) {
   204             return NULL;
   205         }
   206     } else {
   207         mode = *SDL_GetDesktopDisplayMode ();
   208     }
   209     if (SDL_SetDisplayMode (&mode) < 0) {
   210         return NULL;
   211     }
   212 
   213     /* Create the display surface */
   214     return SDL_CreateWindowSurface (window, desired_format, flags);
   215 }
   216 
   217 SDL_Surface *
   218 SDL_GetVideoSurface (void)
   219 {
   220     SDL_VideoDevice *_this = SDL_GetVideoDevice ();
   221 
   222     return SDL_VideoSurface;
   223 }
   224 
   225 void
   226 SDL_WM_SetCaption (const char *title, const char *icon)
   227 {
   228     if (wm_title) {
   229         SDL_free (wm_title);
   230     } else {
   231         wm_title = SDL_strdup (title);
   232     }
   233     SDL_SetWindowTitle (window, wm_title);
   234 }
   235 
   236 void
   237 SDL_WM_GetCaption (char **title, char **icon)
   238 {
   239     if (title) {
   240         *title = wm_title;
   241     }
   242     if (icon) {
   243         *icon = "";
   244     }
   245 }
   246 
   247 void
   248 SDL_WM_SetIcon (SDL_Surface * icon, Uint8 * mask)
   249 {
   250     /* FIXME */
   251 }
   252 
   253 int
   254 SDL_WM_IconifyWindow (void)
   255 {
   256     SDL_MinimizeWindow (window);
   257 }
   258 
   259 int
   260 SDL_WM_ToggleFullScreen (SDL_Surface * surface)
   261 {
   262     return 0;
   263 }
   264 
   265 SDL_GrabMode
   266 SDL_WM_GrabInput (SDL_GrabMode mode)
   267 {
   268     if (mode != SDL_GRAB_QUERY) {
   269         SDL_SetWindowGrab (window, mode);
   270     }
   271     return (SDL_GrabMode) SDL_GetWindowGrab (window);
   272 }
   273 
   274 Uint8
   275 SDL_GetAppState (void)
   276 {
   277     Uint8 state = 0;
   278     Uint32 flags = 0;
   279 
   280     flags = SDL_GetWindowFlags (window);
   281     if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
   282         state |= SDL_APPACTIVE;
   283     }
   284     if (flags & SDL_WINDOW_KEYBOARD_FOCUS) {
   285         state |= SDL_APPINPUTFOCUS;
   286     }
   287     if (flags & SDL_WINDOW_MOUSE_FOCUS) {
   288         state |= SDL_APPMOUSEFOCUS;
   289     }
   290     return state;
   291 }
   292 
   293 const SDL_version *
   294 SDL_Linked_Version (void)
   295 {
   296     static SDL_version version;
   297     SDL_VERSION (&version);
   298     return &version;
   299 }
   300 
   301 int
   302 SDL_SetPalette (SDL_Surface * surface, int flags, SDL_Color * colors,
   303                 int firstcolor, int ncolors)
   304 {
   305     SDL_SetColors (surface, colors, firstcolor, ncolors);
   306 }
   307 
   308 int
   309 SDL_GetWMInfo (SDL_SysWMinfo * info)
   310 {
   311     return SDL_GetWindowWMInfo (window, info);
   312 }
   313 
   314 /* vi: set ts=4 sw=4 expandtab: */