src/video/photon/SDL_ph_modes.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 23 Aug 2003 23:20:21 +0000
changeset 692 04dd6c6d7c30
parent 663 8bedd6d61642
child 769 b8d311d90021
permissions -rw-r--r--
Date: Fri, 15 Aug 2003 09:13:59 +0300
From: "Mike Gorchak"
Subject: Patches for tests and QNX6

Here more fixes for the QNX6 in sdlqnx.diff file:

- Spellchecked README.QNX (thanks to Julian Kinraid)
- Fixed bugs in fullscreen mode: window region wasn't on top by default, so \
it caused some artifacts to be appeared on the screen, prevent window conten\
ts default filler in Photon while in fullscreen mode, it damages the screen.
- Added support for the SDL_VIDEO_WINDOW_POS, SDL_VIDEO_CENTERED env variabl\
es.
- Some minor code restructurization.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@297
     3
    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002  Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@663
    28
#include "SDL_error.h"
slouken@0
    29
#include "SDL_ph_modes_c.h"
slouken@0
    30
slouken@0
    31
static unsigned long key1, key2;
slouken@0
    32
static PgVideoModeInfo_t mode_info;
slouken@0
    33
static PgVideoModes_t mode_list;
slouken@266
    34
slouken@571
    35
/* The current list of available video modes */
slouken@266
    36
SDL_Rect  SDL_modelist[PH_MAX_VIDEOMODES];
slouken@266
    37
SDL_Rect* SDL_modearray[PH_MAX_VIDEOMODES];
slouken@0
    38
slouken@0
    39
static int compare_modes_by_res(const void* mode1, const void* mode2)
slouken@0
    40
{
slouken@571
    41
    if (PgGetVideoModeInfo(*(unsigned short*)mode1, &mode_info) < 0)
slouken@571
    42
    {
slouken@571
    43
        return 0;
slouken@571
    44
    }
slouken@0
    45
slouken@571
    46
    key1 = mode_info.width * mode_info.height;
slouken@0
    47
slouken@571
    48
    if (PgGetVideoModeInfo(*(unsigned short*)mode2, &mode_info) < 0)
slouken@571
    49
    {
slouken@571
    50
        return 0;
slouken@571
    51
    }
slouken@0
    52
slouken@571
    53
    key2 = mode_info.width * mode_info.height;
slouken@571
    54
slouken@571
    55
    if (key1 > key2)
slouken@663
    56
    {
slouken@571
    57
        return 1;
slouken@663
    58
    }
slouken@571
    59
    else
slouken@663
    60
    {
slouken@663
    61
        if (key1 == key2)
slouken@663
    62
        {
slouken@663
    63
           return 0;
slouken@663
    64
        }
slouken@663
    65
        else
slouken@663
    66
        {
slouken@663
    67
            return -1;
slouken@663
    68
        }
slouken@663
    69
    }
slouken@0
    70
}
slouken@0
    71
slouken@266
    72
SDL_Rect **ph_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
slouken@0
    73
{
slouken@309
    74
    int i = 0;
slouken@309
    75
    int j = 0;
slouken@309
    76
    SDL_Rect Amodelist[PH_MAX_VIDEOMODES];
slouken@0
    77
slouken@309
    78
    for (i=0; i<PH_MAX_VIDEOMODES; i++)
slouken@309
    79
    {
slouken@309
    80
        SDL_modearray[i]=&SDL_modelist[i];
slouken@309
    81
    }
slouken@309
    82
slouken@309
    83
    if (PgGetVideoModeList( &mode_list ) < 0)
slouken@309
    84
    {
slouken@663
    85
       SDL_SetError("ph_ListModes(): PgGetVideoModeList() function failed !\n");
slouken@309
    86
       return NULL;
slouken@309
    87
    }
slouken@309
    88
slouken@309
    89
    mode_info.bits_per_pixel = 0;
slouken@309
    90
slouken@309
    91
    for (i=0; i < mode_list.num_modes; i++) 
slouken@309
    92
    {
slouken@309
    93
        if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
slouken@266
    94
        {
slouken@663
    95
            SDL_SetError("ph_ListModes(): PgGetVideoModeInfo() function failed on mode: 0x%X.\n", mode_list.modes[i]);
slouken@309
    96
            return NULL;
slouken@266
    97
        }
slouken@309
    98
        if(mode_info.bits_per_pixel == format->BitsPerPixel)
slouken@309
    99
        {
slouken@309
   100
            Amodelist[j].w = mode_info.width;
slouken@309
   101
            Amodelist[j].h = mode_info.height;
slouken@309
   102
            Amodelist[j].x = 0;
slouken@309
   103
            Amodelist[j].y = 0;
slouken@309
   104
            j++;	
slouken@309
   105
        }
slouken@309
   106
    }
slouken@309
   107
	
slouken@309
   108
    /* reorder biggest for smallest, assume width dominates */
slouken@0
   109
slouken@309
   110
    for(i=0; i<j; i++)
slouken@309
   111
    {
slouken@309
   112
        SDL_modelist[i].w = Amodelist[j - i -1].w;
slouken@309
   113
        SDL_modelist[i].h = Amodelist[j - i -1].h;
slouken@309
   114
        SDL_modelist[i].x = Amodelist[j - i -1].x;
slouken@309
   115
        SDL_modelist[i].y = Amodelist[j - i -1].y;
slouken@309
   116
    }
slouken@309
   117
    SDL_modearray[j]=NULL;
slouken@0
   118
	
slouken@309
   119
    return SDL_modearray;
slouken@0
   120
}
slouken@0
   121
slouken@0
   122
void ph_FreeVideoModes(_THIS)
slouken@0
   123
{
slouken@266
   124
   return;
slouken@0
   125
}
slouken@0
   126
slouken@309
   127
/* return the mode associated with width, height and bpp */
slouken@309
   128
/* if there is no mode then zero is returned             */
slouken@663
   129
int ph_GetVideoMode(int width, int height, int bpp)
slouken@0
   130
{
slouken@309
   131
    int i;
slouken@0
   132
slouken@309
   133
    if (PgGetVideoModeList(&mode_list) < 0)
slouken@309
   134
    {
slouken@309
   135
        return -1;
slouken@309
   136
    }
slouken@0
   137
slouken@309
   138
    /* search list for exact match */
slouken@309
   139
    for (i=0;i<mode_list.num_modes;i++)
slouken@309
   140
    {
slouken@309
   141
        if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
slouken@309
   142
        {
slouken@309
   143
            return 0;
slouken@309
   144
        }
slouken@0
   145
slouken@309
   146
        if ((mode_info.width == width) && 
slouken@309
   147
            (mode_info.height == height) && 
slouken@309
   148
            (mode_info.bits_per_pixel == bpp))
slouken@309
   149
        {
slouken@309
   150
            return mode_list.modes[i];
slouken@309
   151
        }
slouken@309
   152
    }
slouken@0
   153
slouken@309
   154
    return (i == mode_list.num_modes) ? 0 : mode_list.modes[i];
slouken@0
   155
}
slouken@0
   156
slouken@0
   157
/* return the mode associated with width, height and bpp */
slouken@0
   158
/* if requested bpp is not found the mode with closest bpp is returned */
slouken@663
   159
int get_mode_any_format(int width, int height, int bpp)
slouken@0
   160
{
slouken@0
   161
    int i, closest, delta, min_delta;
slouken@0
   162
slouken@663
   163
    if (PgGetVideoModeList(&mode_list) < 0)
slouken@663
   164
    {
slouken@663
   165
        return -1;
slouken@663
   166
    }
slouken@0
   167
slouken@663
   168
    qsort(mode_list.modes, mode_list.num_modes, sizeof(unsigned short), compare_modes_by_res);
slouken@663
   169
slouken@663
   170
    for(i=0;i<mode_list.num_modes;i++)
slouken@663
   171
    {
slouken@663
   172
        if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
slouken@663
   173
        {
slouken@663
   174
            return 0;
slouken@663
   175
        }
slouken@663
   176
        if ((mode_info.width == width) && (mode_info.height == height))
slouken@663
   177
        {
slouken@0
   178
           break;
slouken@663
   179
        }
slouken@663
   180
    }
slouken@0
   181
slouken@663
   182
    if (i<mode_list.num_modes)
slouken@663
   183
    {
slouken@663
   184
        /* get closest bpp */
slouken@663
   185
        closest = i++;
slouken@663
   186
        if (mode_info.bits_per_pixel == bpp)
slouken@663
   187
        {
slouken@663
   188
            return mode_list.modes[closest];
slouken@663
   189
        }
slouken@0
   190
slouken@663
   191
        min_delta = abs(mode_info.bits_per_pixel - bpp);
slouken@663
   192
slouken@663
   193
        while(1)
slouken@663
   194
        {
slouken@663
   195
            if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
slouken@663
   196
            {
slouken@663
   197
                return 0;
slouken@663
   198
            }
slouken@663
   199
slouken@663
   200
            if ((mode_info.width != width) || (mode_info.height != height))
slouken@663
   201
            {
slouken@663
   202
                break;
slouken@663
   203
            }
slouken@663
   204
            else
slouken@663
   205
            {
slouken@663
   206
                if (mode_info.bits_per_pixel == bpp)
slouken@663
   207
                {
slouken@663
   208
                    closest = i;
slouken@663
   209
                    break;
slouken@663
   210
                }
slouken@663
   211
                else
slouken@663
   212
                {
slouken@663
   213
                    delta = abs(mode_info.bits_per_pixel - bpp);
slouken@663
   214
                    if (delta < min_delta)
slouken@663
   215
                    {
slouken@663
   216
                        closest = i;
slouken@663
   217
                        min_delta = delta;
slouken@663
   218
                    }
slouken@663
   219
                    i++;
slouken@663
   220
                }
slouken@663
   221
            }
slouken@663
   222
        }
slouken@663
   223
        return mode_list.modes[closest];
slouken@663
   224
    }
slouken@663
   225
slouken@309
   226
    return 0;
slouken@0
   227
}
slouken@0
   228
slouken@0
   229
int ph_ToggleFullScreen(_THIS, int on)
slouken@0
   230
{
slouken@663
   231
    return -1;
slouken@0
   232
}
slouken@0
   233
slouken@663
   234
int ph_EnterFullScreen(_THIS, SDL_Surface* screen)
slouken@0
   235
{
slouken@663
   236
    PgDisplaySettings_t settings;
slouken@663
   237
    int mode;
slouken@663
   238
slouken@309
   239
    if (!currently_fullscreen)
slouken@309
   240
    {
slouken@663
   241
        /* Get the video mode and set it */
slouken@663
   242
        if (screen->flags & SDL_ANYFORMAT)
slouken@663
   243
        {
slouken@663
   244
            if ((mode = get_mode_any_format(screen->w, screen->h, screen->format->BitsPerPixel)) == 0)
slouken@663
   245
            {
slouken@663
   246
                SDL_SetError("ph_EnterFullScreen(): can't find appropriate video mode !\n");
slouken@663
   247
                return 0;
slouken@663
   248
            }
slouken@663
   249
        }
slouken@663
   250
        else
slouken@663
   251
        {
slouken@663
   252
            if ((mode = ph_GetVideoMode(screen->w, screen->h, screen->format->BitsPerPixel)) == 0)
slouken@663
   253
            {
slouken@663
   254
                SDL_SetError("ph_EnterFullScreen(): can't find appropriate video mode !\n");
slouken@663
   255
                return 0;
slouken@663
   256
            }
slouken@663
   257
        }
slouken@663
   258
slouken@663
   259
        /* save old video mode caps */
slouken@663
   260
        PgGetVideoMode(&settings);
slouken@663
   261
        old_video_mode=settings.mode;
slouken@663
   262
        old_refresh_rate=settings.refresh;
slouken@663
   263
slouken@663
   264
        /* setup new video mode */
slouken@663
   265
        settings.mode = mode;
slouken@663
   266
        settings.refresh = 0;
slouken@663
   267
        settings.flags = 0;
slouken@663
   268
slouken@663
   269
        if (PgSetVideoMode(&settings) < 0)
slouken@663
   270
        {
slouken@663
   271
            SDL_SetError("ph_EnterFullScreen(): PgSetVideoMode() call failed !\n");
slouken@663
   272
            return 0;
slouken@663
   273
        }
slouken@663
   274
slouken@315
   275
        if (this->screen)
slouken@309
   276
        {
slouken@315
   277
            if ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL)
slouken@315
   278
            {
slouken@309
   279
#ifdef HAVE_OPENGL
slouken@309
   280
#endif /* HAVE_OPENGL */
slouken@315
   281
                return 0;
slouken@315
   282
            }
slouken@309
   283
        }
slouken@315
   284
slouken@571
   285
        if (OCImage.direct_context==NULL)
slouken@309
   286
        {
slouken@315
   287
            OCImage.direct_context=(PdDirectContext_t*)PdCreateDirectContext();
slouken@663
   288
            if (!OCImage.direct_context)
slouken@663
   289
            {
slouken@663
   290
                SDL_SetError("ph_EnterFullScreen(): Can't create direct context !\n");
slouken@663
   291
                ph_LeaveFullScreen(this);
slouken@663
   292
                return 0;
slouken@663
   293
            }
slouken@315
   294
        }
slouken@0
   295
slouken@571
   296
        OCImage.oldDC=PdDirectStart(OCImage.direct_context);
slouken@0
   297
slouken@315
   298
        currently_fullscreen = 1;
slouken@309
   299
    }
slouken@692
   300
    PgFlush();
slouken@0
   301
slouken@309
   302
    return 1;
slouken@0
   303
}
slouken@0
   304
slouken@315
   305
int ph_LeaveFullScreen(_THIS)
slouken@0
   306
{
slouken@692
   307
    PgDisplaySettings_t oldmode_settings;
slouken@0
   308
       
slouken@309
   309
    if (currently_fullscreen)
slouken@309
   310
    {
slouken@663
   311
        if ((this->screen) && ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL))
slouken@309
   312
        {
slouken@309
   313
#ifdef HAVE_OPENGL
slouken@309
   314
#endif /* HAVE_OPENGL */
slouken@309
   315
           return 0;
slouken@309
   316
        }
slouken@309
   317
        else
slouken@309
   318
        {
slouken@663
   319
            if (OCImage.direct_context)
slouken@663
   320
            {
slouken@663
   321
                PdDirectStop(OCImage.direct_context);
slouken@663
   322
                PdReleaseDirectContext(OCImage.direct_context);
slouken@663
   323
                OCImage.direct_context=NULL;
slouken@663
   324
            }
slouken@663
   325
            if (OCImage.oldDC)
slouken@663
   326
            {
slouken@663
   327
                PhDCSetCurrent(OCImage.oldDC);
slouken@663
   328
                OCImage.oldDC=NULL;
slouken@663
   329
            }
slouken@571
   330
slouken@315
   331
            currently_fullscreen=0;
slouken@0
   332
slouken@309
   333
            /* Restore old video mode */
slouken@309
   334
            if (old_video_mode != -1)
slouken@309
   335
            {
slouken@692
   336
                oldmode_settings.mode = (unsigned short) old_video_mode;
slouken@692
   337
                oldmode_settings.refresh = (unsigned short) old_refresh_rate;
slouken@692
   338
                oldmode_settings.flags = 0;
slouken@315
   339
                
slouken@692
   340
                if (PgSetVideoMode(&oldmode_settings) < 0)
slouken@309
   341
                {
slouken@663
   342
                    SDL_SetError("Ph_LeaveFullScreen(): PgSetVideoMode() function failed !\n");
slouken@571
   343
                    return 0;
slouken@309
   344
                }
slouken@309
   345
            }
slouken@309
   346
slouken@309
   347
            old_video_mode=-1;
slouken@315
   348
            old_refresh_rate=-1;
slouken@309
   349
        }
slouken@309
   350
    }
slouken@309
   351
    return 1;
slouken@0
   352
}