src/video/photon/SDL_ph_modes.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 20 Jan 2003 01:38:37 +0000
changeset 571 8e3ce997621c
parent 315 3333b6e68289
child 663 8bedd6d61642
permissions -rw-r--r--
Date: Thu, 16 Jan 2003 13:48:31 +0200
From: "Mike Gorchak"
Subject: All QNX patches

whole patches concerning QNX. Almost all code has been rewritten by Julian
and me. Added initial support for hw overlays in QNX and many many others
fixes.

P.S. This patches has been reviewed by Dave Rempel from QSSL and included in
SDL 1.2.5 distribution, which coming on 3rd party CD for newest 6.2.1
version of QNX, which will be available soon.
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@0
    28
#include "SDL_ph_modes_c.h"
slouken@0
    29
slouken@0
    30
static unsigned long key1, key2;
slouken@0
    31
static PgVideoModeInfo_t mode_info;
slouken@0
    32
static PgVideoModes_t mode_list;
slouken@266
    33
slouken@571
    34
/* The current list of available video modes */
slouken@266
    35
SDL_Rect  SDL_modelist[PH_MAX_VIDEOMODES];
slouken@266
    36
SDL_Rect* SDL_modearray[PH_MAX_VIDEOMODES];
slouken@0
    37
slouken@0
    38
static int compare_modes_by_res(const void* mode1, const void* mode2)
slouken@0
    39
{
slouken@571
    40
    if (PgGetVideoModeInfo(*(unsigned short*)mode1, &mode_info) < 0)
slouken@571
    41
    {
slouken@571
    42
        fprintf(stderr,"error: In compare_modes_by_res PgGetVideoModeInfo failed on mode: 0x%x\n",
slouken@571
    43
            *(unsigned short*)mode1);
slouken@571
    44
        return 0;
slouken@571
    45
    }
slouken@0
    46
slouken@571
    47
    key1 = mode_info.width * mode_info.height;
slouken@0
    48
slouken@571
    49
    if (PgGetVideoModeInfo(*(unsigned short*)mode2, &mode_info) < 0)
slouken@571
    50
    {
slouken@571
    51
        fprintf(stderr,"error: In compare_modes_by_res PgGetVideoModeInfo failed on mode: 0x%x\n",
slouken@571
    52
            *(unsigned short*)mode2);
slouken@571
    53
        return 0;
slouken@571
    54
    }
slouken@0
    55
slouken@571
    56
    key2 = mode_info.width * mode_info.height;
slouken@571
    57
slouken@571
    58
    if (key1 > key2)
slouken@571
    59
        return 1;
slouken@571
    60
    else if (key1 == key2)
slouken@571
    61
        return 0;
slouken@571
    62
    else
slouken@571
    63
        return -1;
slouken@0
    64
}
slouken@0
    65
slouken@266
    66
SDL_Rect **ph_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
slouken@0
    67
{
slouken@309
    68
    int i = 0;
slouken@309
    69
    int j = 0;
slouken@309
    70
    SDL_Rect Amodelist[PH_MAX_VIDEOMODES];
slouken@0
    71
slouken@309
    72
    for (i=0; i<PH_MAX_VIDEOMODES; i++)
slouken@309
    73
    {
slouken@309
    74
        SDL_modearray[i]=&SDL_modelist[i];
slouken@309
    75
    }
slouken@309
    76
slouken@309
    77
    if (PgGetVideoModeList( &mode_list ) < 0)
slouken@309
    78
    {
slouken@309
    79
       fprintf(stderr,"error: PgGetVideoModeList failed\n");
slouken@309
    80
       return NULL;
slouken@309
    81
    }
slouken@309
    82
slouken@309
    83
    mode_info.bits_per_pixel = 0;
slouken@309
    84
slouken@309
    85
    for (i=0; i < mode_list.num_modes; i++) 
slouken@309
    86
    {
slouken@309
    87
        if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
slouken@266
    88
        {
slouken@309
    89
            fprintf(stderr,"error: PgGetVideoModeInfo failed on mode: 0x%x\n", mode_list.modes[i]);
slouken@309
    90
            return NULL;
slouken@266
    91
        }
slouken@309
    92
        if(mode_info.bits_per_pixel == format->BitsPerPixel)
slouken@309
    93
        {
slouken@309
    94
            Amodelist[j].w = mode_info.width;
slouken@309
    95
            Amodelist[j].h = mode_info.height;
slouken@309
    96
            Amodelist[j].x = 0;
slouken@309
    97
            Amodelist[j].y = 0;
slouken@309
    98
            j++;	
slouken@309
    99
        }
slouken@309
   100
    }
slouken@309
   101
	
slouken@309
   102
    /* reorder biggest for smallest, assume width dominates */
slouken@0
   103
slouken@309
   104
    for(i=0; i<j; i++)
slouken@309
   105
    {
slouken@309
   106
        SDL_modelist[i].w = Amodelist[j - i -1].w;
slouken@309
   107
        SDL_modelist[i].h = Amodelist[j - i -1].h;
slouken@309
   108
        SDL_modelist[i].x = Amodelist[j - i -1].x;
slouken@309
   109
        SDL_modelist[i].y = Amodelist[j - i -1].y;
slouken@309
   110
    }
slouken@309
   111
    SDL_modearray[j]=NULL;
slouken@0
   112
	
slouken@309
   113
    return SDL_modearray;
slouken@0
   114
}
slouken@0
   115
slouken@0
   116
void ph_FreeVideoModes(_THIS)
slouken@0
   117
{
slouken@266
   118
   return;
slouken@0
   119
}
slouken@0
   120
slouken@309
   121
/* return the mode associated with width, height and bpp */
slouken@309
   122
/* if there is no mode then zero is returned             */
slouken@0
   123
int get_mode(int width, int height, int bpp)
slouken@0
   124
{
slouken@309
   125
    int i;
slouken@0
   126
slouken@309
   127
    if(width<640)
slouken@309
   128
    {
slouken@309
   129
        width=640;
slouken@309
   130
    }
slouken@309
   131
    if(height<480)
slouken@309
   132
    {
slouken@309
   133
        height=480;
slouken@309
   134
    }
slouken@0
   135
slouken@309
   136
    if (PgGetVideoModeList(&mode_list) < 0)
slouken@309
   137
    {
slouken@309
   138
        fprintf(stderr,"error: PgGetVideoModeList failed\n");
slouken@309
   139
        return -1;
slouken@309
   140
    }
slouken@0
   141
slouken@309
   142
    /* search list for exact match */
slouken@309
   143
    for (i=0;i<mode_list.num_modes;i++)
slouken@309
   144
    {
slouken@309
   145
        if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
slouken@309
   146
        {
slouken@309
   147
            fprintf(stderr,"error: PgGetVideoModeInfo failed\n");
slouken@309
   148
            return 0;
slouken@309
   149
        }
slouken@0
   150
slouken@309
   151
        if ((mode_info.width == width) && 
slouken@309
   152
            (mode_info.height == height) && 
slouken@309
   153
            (mode_info.bits_per_pixel == bpp))
slouken@309
   154
        {
slouken@309
   155
            return mode_list.modes[i];
slouken@309
   156
        }
slouken@309
   157
    }
slouken@0
   158
slouken@309
   159
    return (i == mode_list.num_modes) ? 0 : mode_list.modes[i];
slouken@0
   160
}
slouken@0
   161
slouken@0
   162
int get_mode_any_format(int width, int height, int bpp)
slouken@0
   163
/* return the mode associated with width, height and bpp */
slouken@0
   164
/* if requested bpp is not found the mode with closest bpp is returned */
slouken@0
   165
{
slouken@0
   166
    int i, closest, delta, min_delta;
slouken@0
   167
slouken@0
   168
	if (PgGetVideoModeList( &mode_list ) < 0)
slouken@0
   169
	{
slouken@0
   170
	    fprintf(stderr,"error: PgGetVideoModeList failed\n");
slouken@0
   171
	    return -1;
slouken@0
   172
	}
slouken@0
   173
slouken@0
   174
	qsort(mode_list.modes, mode_list.num_modes, sizeof(unsigned short), compare_modes_by_res);
slouken@0
   175
	for(i=0;i<mode_list.num_modes;i++)
slouken@0
   176
	{
slouken@0
   177
       if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
slouken@0
   178
       {
slouken@0
   179
           fprintf(stderr,"error: PgGetVideoModeInfo failed\n");
slouken@0
   180
           return 0;
slouken@0
   181
       }
slouken@0
   182
       if ((mode_info.width == width) &&
slouken@0
   183
           (mode_info.height == height))
slouken@0
   184
           break;
slouken@0
   185
	}
slouken@0
   186
	if (i<mode_list.num_modes)
slouken@0
   187
	{
slouken@315
   188
		/* get closest bpp */
slouken@0
   189
		closest = i++;
slouken@0
   190
		if (mode_info.bits_per_pixel == bpp)
slouken@571
   191
			return mode_list.modes[closest];
slouken@0
   192
slouken@0
   193
		min_delta = abs(mode_info.bits_per_pixel - bpp);
slouken@0
   194
		while(1)
slouken@0
   195
		{
slouken@0
   196
			if (PgGetVideoModeInfo(mode_list.modes[i], &mode_info) < 0)
slouken@0
   197
			{
slouken@0
   198
			    fprintf(stderr,"error: PgGetVideoModeInfo failed\n");
slouken@0
   199
			    return 0;
slouken@0
   200
			}
slouken@0
   201
slouken@0
   202
			if ((mode_info.width != width) ||
slouken@0
   203
				(mode_info.height != height))
slouken@0
   204
				break;
slouken@0
   205
			else if (mode_info.bits_per_pixel == bpp)
slouken@0
   206
			{
slouken@0
   207
				closest = i;
slouken@0
   208
				break;
slouken@0
   209
			}
slouken@0
   210
			else
slouken@0
   211
			{
slouken@0
   212
				delta = abs(mode_info.bits_per_pixel - bpp);
slouken@0
   213
				if (delta < min_delta)
slouken@0
   214
				{
slouken@0
   215
					closest = i;
slouken@0
   216
					min_delta = delta;
slouken@0
   217
				}
slouken@0
   218
				i++;
slouken@0
   219
			}
slouken@0
   220
		}
slouken@571
   221
		return mode_list.modes[closest];
slouken@0
   222
	}
slouken@0
   223
	else
slouken@309
   224
    return 0;
slouken@0
   225
}
slouken@0
   226
slouken@0
   227
int ph_ToggleFullScreen(_THIS, int on)
slouken@0
   228
{
slouken@309
   229
    if (currently_fullscreen)
slouken@309
   230
    {
slouken@309
   231
        return ph_LeaveFullScreen(this);
slouken@309
   232
    }
slouken@309
   233
    else
slouken@309
   234
    {
slouken@309
   235
        return ph_EnterFullScreen(this);
slouken@309
   236
    }
slouken@0
   237
      
slouken@309
   238
    return 0;     
slouken@0
   239
}
slouken@0
   240
slouken@0
   241
int ph_EnterFullScreen(_THIS)
slouken@0
   242
{
slouken@309
   243
    if (!currently_fullscreen)
slouken@309
   244
    {
slouken@315
   245
        if (this->screen)
slouken@309
   246
        {
slouken@315
   247
            if ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL)
slouken@315
   248
            {
slouken@309
   249
#ifdef HAVE_OPENGL
slouken@309
   250
#endif /* HAVE_OPENGL */
slouken@315
   251
                return 0;
slouken@315
   252
            }
slouken@309
   253
        }
slouken@315
   254
slouken@571
   255
        if (OCImage.direct_context==NULL)
slouken@309
   256
        {
slouken@315
   257
            OCImage.direct_context=(PdDirectContext_t*)PdCreateDirectContext();
slouken@315
   258
        }
slouken@0
   259
slouken@315
   260
        if (!OCImage.direct_context)
slouken@315
   261
        {
slouken@571
   262
            fprintf(stderr, "ph_EnterFullScreen(): Can't create direct context !\n");
slouken@571
   263
            return 0;
slouken@315
   264
        }
slouken@0
   265
slouken@571
   266
        OCImage.oldDC=PdDirectStart(OCImage.direct_context);
slouken@0
   267
slouken@315
   268
        currently_fullscreen = 1;
slouken@309
   269
    }
slouken@0
   270
slouken@309
   271
    return 1;
slouken@0
   272
}
slouken@0
   273
slouken@315
   274
int ph_LeaveFullScreen(_THIS)
slouken@0
   275
{
slouken@309
   276
    PgDisplaySettings_t mymode_settings;
slouken@0
   277
       
slouken@309
   278
    if (currently_fullscreen)
slouken@309
   279
    {
slouken@309
   280
        if ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL)
slouken@309
   281
        {
slouken@309
   282
#ifdef HAVE_OPENGL
slouken@309
   283
#endif /* HAVE_OPENGL */
slouken@309
   284
           return 0;
slouken@309
   285
        }
slouken@309
   286
        else
slouken@309
   287
        {
slouken@309
   288
            PdDirectStop(OCImage.direct_context);
slouken@309
   289
            PdReleaseDirectContext(OCImage.direct_context);
slouken@571
   290
            PhDCSetCurrent(OCImage.oldDC);
slouken@571
   291
slouken@315
   292
            currently_fullscreen=0;
slouken@0
   293
slouken@309
   294
            /* Restore old video mode */
slouken@309
   295
            if (old_video_mode != -1)
slouken@309
   296
            {
slouken@309
   297
                mymode_settings.mode= (unsigned short) old_video_mode;
slouken@309
   298
                mymode_settings.refresh= (unsigned short) old_refresh_rate;
slouken@315
   299
                mymode_settings.flags= 0;
slouken@315
   300
                
slouken@309
   301
                if (PgSetVideoMode(&mymode_settings) < 0)
slouken@309
   302
                {
slouken@571
   303
                    fprintf(stderr, "Ph_LeaveFullScreen(): PgSetVideoMode failed !\n");
slouken@571
   304
                    return 0;
slouken@309
   305
                }
slouken@309
   306
            }
slouken@309
   307
slouken@309
   308
            old_video_mode=-1;
slouken@315
   309
            old_refresh_rate=-1;
slouken@309
   310
        }
slouken@309
   311
slouken@309
   312
    }
slouken@309
   313
    return 1;
slouken@0
   314
}