src/SDL_compat.c
branchSDL-1.3
changeset 1667 1fddae038bc8
child 1668 4da1ee79c9af
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/SDL_compat.c	Mon May 29 03:53:21 2006 +0000
     1.3 @@ -0,0 +1,314 @@
     1.4 +/*
     1.5 +    SDL - Simple DirectMedia Layer
     1.6 +    Copyright (C) 1997-2006 Sam Lantinga
     1.7 +
     1.8 +    This library is free software; you can redistribute it and/or
     1.9 +    modify it under the terms of the GNU Lesser General Public
    1.10 +    License as published by the Free Software Foundation; either
    1.11 +    version 2.1 of the License, or (at your option) any later version.
    1.12 +
    1.13 +    This library is distributed in the hope that it will be useful,
    1.14 +    but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    1.16 +    Lesser General Public License for more details.
    1.17 +
    1.18 +    You should have received a copy of the GNU Lesser General Public
    1.19 +    License along with this library; if not, write to the Free Software
    1.20 +    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
    1.21 +
    1.22 +    Sam Lantinga
    1.23 +    slouken@libsdl.org
    1.24 +*/
    1.25 +#include "SDL_config.h"
    1.26 +
    1.27 +/* This file contains functions for backwards compatibility with SDL 1.2 */
    1.28 +
    1.29 +#include "SDL.h"
    1.30 +
    1.31 +#include "video/SDL_sysvideo.h"
    1.32 +
    1.33 +
    1.34 +static SDL_WindowID window;
    1.35 +static char *wm_title;
    1.36 +
    1.37 +char *
    1.38 +SDL_AudioDriverName (char *namebuf, int maxlen)
    1.39 +{
    1.40 +    const char *name = SDL_GetCurrentAudioDriver ();
    1.41 +    if (name) {
    1.42 +        SDL_strlcpy (namebuf, name, maxlen);
    1.43 +        return namebuf;
    1.44 +    }
    1.45 +    return NULL;
    1.46 +}
    1.47 +
    1.48 +char *
    1.49 +SDL_VideoDriverName (char *namebuf, int maxlen)
    1.50 +{
    1.51 +    const char *name = SDL_GetCurrentVideoDriver ();
    1.52 +    if (name) {
    1.53 +        SDL_strlcpy (namebuf, name, maxlen);
    1.54 +        return namebuf;
    1.55 +    }
    1.56 +    return NULL;
    1.57 +}
    1.58 +
    1.59 +int
    1.60 +SDL_VideoModeOK (int width, int height, int bpp, Uint32 flags)
    1.61 +{
    1.62 +    int i, actual_bpp = 0;
    1.63 +
    1.64 +    if (!SDL_GetVideoDevice ()) {
    1.65 +        return 0;
    1.66 +    }
    1.67 +
    1.68 +    if (!(flags & SDL_FULLSCREEN)) {
    1.69 +        return SDL_BITSPERPIXEL (SDL_GetDesktopDisplayMode ()->format);
    1.70 +    }
    1.71 +
    1.72 +    for (i = 0; i < SDL_GetNumDisplayModes (); ++i) {
    1.73 +        const SDL_DisplayMode *mode = SDL_GetDisplayMode (i);
    1.74 +        if (!mode->w || !mode->h || (width == mode->w && height == mode->h)) {
    1.75 +            if (!mode->format) {
    1.76 +                return bpp;
    1.77 +            }
    1.78 +            if (SDL_BITSPERPIXEL (mode->format) >= bpp) {
    1.79 +                actual_bpp = SDL_BITSPERPIXEL (mode->format);
    1.80 +            }
    1.81 +        }
    1.82 +    }
    1.83 +    return actual_bpp;
    1.84 +}
    1.85 +
    1.86 +SDL_Rect **
    1.87 +SDL_ListModes (SDL_PixelFormat * format, Uint32 flags)
    1.88 +{
    1.89 +    int i, nmodes;
    1.90 +    SDL_Rect **modes;
    1.91 +
    1.92 +    if (!SDL_GetVideoDevice ()) {
    1.93 +        return NULL;
    1.94 +    }
    1.95 +
    1.96 +    if (!(flags & SDL_FULLSCREEN)) {
    1.97 +        return (SDL_Rect **) (-1);
    1.98 +    }
    1.99 +
   1.100 +    /* Memory leak, but this is a compatibility function, who cares? */
   1.101 +    nmodes = 0;
   1.102 +    for (i = 0; i < SDL_GetNumDisplayModes (); ++i) {
   1.103 +        const SDL_DisplayMode *mode = SDL_GetDisplayMode (i);
   1.104 +        if (!mode->w || !mode->h) {
   1.105 +            return (SDL_Rect **) (-1);
   1.106 +        }
   1.107 +        if (SDL_BITSPERPIXEL (mode->format) != format->BitsPerPixel) {
   1.108 +            continue;
   1.109 +        }
   1.110 +        if (nmodes > 0 && modes[nmodes - 1]->w == mode->w
   1.111 +            && modes[nmodes - 1]->h == mode->h) {
   1.112 +            continue;
   1.113 +        }
   1.114 +
   1.115 +        modes = SDL_realloc (modes, (nmodes + 2) * sizeof (*modes));
   1.116 +        if (!modes) {
   1.117 +            return NULL;
   1.118 +        }
   1.119 +        modes[nmodes] = (SDL_Rect *) SDL_malloc (sizeof (SDL_Rect));
   1.120 +        if (!modes[nmodes]) {
   1.121 +            return NULL;
   1.122 +        }
   1.123 +        modes[nmodes]->x = 0;
   1.124 +        modes[nmodes]->y = 0;
   1.125 +        modes[nmodes]->w = mode->w;
   1.126 +        modes[nmodes]->h = mode->h;
   1.127 +        ++nmodes;
   1.128 +    }
   1.129 +    if (modes) {
   1.130 +        modes[nmodes] = NULL;
   1.131 +    }
   1.132 +    return modes;
   1.133 +}
   1.134 +
   1.135 +SDL_Surface *
   1.136 +SDL_SetVideoMode (int width, int height, int bpp, Uint32 flags)
   1.137 +{
   1.138 +    SDL_DisplayMode mode;
   1.139 +    int i;
   1.140 +    Uint32 window_flags;
   1.141 +    Uint32 desktop_format;
   1.142 +    Uint32 desired_format;
   1.143 +
   1.144 +    if (!SDL_GetVideoDevice ()) {
   1.145 +        if (SDL_Init (SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE) < 0) {
   1.146 +            return NULL;
   1.147 +        }
   1.148 +    }
   1.149 +
   1.150 +    /* Destroy existing window */
   1.151 +    SDL_DestroyWindow (window);
   1.152 +
   1.153 +    /* Create a new window */
   1.154 +    window_flags = SDL_WINDOW_SHOWN;
   1.155 +    if (flags & SDL_FULLSCREEN) {
   1.156 +        window_flags |= SDL_WINDOW_FULLSCREEN;
   1.157 +    }
   1.158 +    if (flags & SDL_OPENGL) {
   1.159 +        window_flags |= SDL_WINDOW_OPENGL;
   1.160 +    }
   1.161 +    if (flags & SDL_RESIZABLE) {
   1.162 +        window_flags |= SDL_WINDOW_RESIZABLE;
   1.163 +    }
   1.164 +    if (flags & SDL_NOFRAME) {
   1.165 +        window_flags |= SDL_WINDOW_BORDERLESS;
   1.166 +    }
   1.167 +    window = SDL_CreateWindow (wm_title, 0, 0, width, height, window_flags);
   1.168 +    if (!window) {
   1.169 +        return NULL;
   1.170 +    }
   1.171 +
   1.172 +    /* Set up the desired display mode */
   1.173 +    desktop_format = SDL_GetDesktopDisplayMode ()->format;
   1.174 +    if ((bpp == SDL_BITSPERPIXEL(desktop_format)) ||
   1.175 +        (desktop_format && (flags & SDL_ANYFORMAT))) {
   1.176 +        desired_format = desktop_format;
   1.177 +    } else {
   1.178 +        switch (bpp) {
   1.179 +        case 8:
   1.180 +            desired_format = SDL_PixelFormat_Index8;
   1.181 +            break;
   1.182 +        case 15:
   1.183 +            desired_format = SDL_PixelFormat_RGB555;
   1.184 +            break;
   1.185 +        case 16:
   1.186 +            desired_format = SDL_PixelFormat_RGB565;
   1.187 +            break;
   1.188 +        case 24:
   1.189 +            desired_format = SDL_PixelFormat_RGB24;
   1.190 +            break;
   1.191 +        case 32:
   1.192 +            desired_format = SDL_PixelFormat_RGB888;
   1.193 +            break;
   1.194 +        default:
   1.195 +            SDL_SetError ("Unsupported bpp in SDL_SetVideoMode()");
   1.196 +            return NULL;
   1.197 +        }
   1.198 +    }
   1.199 +    mode.format = desired_format;
   1.200 +    mode.w = width;
   1.201 +    mode.h = height;
   1.202 +    mode.refresh_rate = 0;
   1.203 +
   1.204 +    /* Set the desired display mode */
   1.205 +    if (flags & SDL_FULLSCREEN) {
   1.206 +        if (!SDL_GetClosestDisplayMode (&mode, &mode)) {
   1.207 +            return NULL;
   1.208 +        }
   1.209 +    } else {
   1.210 +        mode = *SDL_GetDesktopDisplayMode ();
   1.211 +    }
   1.212 +    if (SDL_SetDisplayMode (&mode) < 0) {
   1.213 +        return NULL;
   1.214 +    }
   1.215 +
   1.216 +    /* Create the display surface */
   1.217 +    return SDL_CreateWindowSurface (window, desired_format, flags);
   1.218 +}
   1.219 +
   1.220 +SDL_Surface *
   1.221 +SDL_GetVideoSurface (void)
   1.222 +{
   1.223 +    SDL_VideoDevice *_this = SDL_GetVideoDevice ();
   1.224 +
   1.225 +    return SDL_VideoSurface;
   1.226 +}
   1.227 +
   1.228 +void
   1.229 +SDL_WM_SetCaption (const char *title, const char *icon)
   1.230 +{
   1.231 +    if (wm_title) {
   1.232 +        SDL_free (wm_title);
   1.233 +    } else {
   1.234 +        wm_title = SDL_strdup (title);
   1.235 +    }
   1.236 +    SDL_SetWindowTitle (window, wm_title);
   1.237 +}
   1.238 +
   1.239 +void
   1.240 +SDL_WM_GetCaption (char **title, char **icon)
   1.241 +{
   1.242 +    if (title) {
   1.243 +        *title = wm_title;
   1.244 +    }
   1.245 +    if (icon) {
   1.246 +        *icon = "";
   1.247 +    }
   1.248 +}
   1.249 +
   1.250 +void
   1.251 +SDL_WM_SetIcon (SDL_Surface * icon, Uint8 * mask)
   1.252 +{
   1.253 +    /* FIXME */
   1.254 +}
   1.255 +
   1.256 +int
   1.257 +SDL_WM_IconifyWindow (void)
   1.258 +{
   1.259 +    SDL_MinimizeWindow (window);
   1.260 +}
   1.261 +
   1.262 +int
   1.263 +SDL_WM_ToggleFullScreen (SDL_Surface * surface)
   1.264 +{
   1.265 +    return 0;
   1.266 +}
   1.267 +
   1.268 +SDL_GrabMode
   1.269 +SDL_WM_GrabInput (SDL_GrabMode mode)
   1.270 +{
   1.271 +    if (mode != SDL_GRAB_QUERY) {
   1.272 +        SDL_SetWindowGrab (window, mode);
   1.273 +    }
   1.274 +    return (SDL_GrabMode) SDL_GetWindowGrab (window);
   1.275 +}
   1.276 +
   1.277 +Uint8
   1.278 +SDL_GetAppState (void)
   1.279 +{
   1.280 +    Uint8 state = 0;
   1.281 +    Uint32 flags = 0;
   1.282 +
   1.283 +    flags = SDL_GetWindowFlags (window);
   1.284 +    if ((flags & SDL_WINDOW_SHOWN) && !(flags & SDL_WINDOW_MINIMIZED)) {
   1.285 +        state |= SDL_APPACTIVE;
   1.286 +    }
   1.287 +    if (flags & SDL_WINDOW_KEYBOARD_FOCUS) {
   1.288 +        state |= SDL_APPINPUTFOCUS;
   1.289 +    }
   1.290 +    if (flags & SDL_WINDOW_MOUSE_FOCUS) {
   1.291 +        state |= SDL_APPMOUSEFOCUS;
   1.292 +    }
   1.293 +    return state;
   1.294 +}
   1.295 +
   1.296 +const SDL_version *
   1.297 +SDL_Linked_Version (void)
   1.298 +{
   1.299 +    static SDL_version version;
   1.300 +    SDL_VERSION (&version);
   1.301 +    return &version;
   1.302 +}
   1.303 +
   1.304 +int
   1.305 +SDL_SetPalette (SDL_Surface * surface, int flags, SDL_Color * colors,
   1.306 +                int firstcolor, int ncolors)
   1.307 +{
   1.308 +    SDL_SetColors (surface, colors, firstcolor, ncolors);
   1.309 +}
   1.310 +
   1.311 +int
   1.312 +SDL_GetWMInfo (SDL_SysWMinfo * info)
   1.313 +{
   1.314 +    return SDL_GetWindowWMInfo (window, info);
   1.315 +}
   1.316 +
   1.317 +/* vi: set ts=4 sw=4 expandtab: */