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