src/video/photon/SDL_ph_video.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 11 Mar 2002 17:38:56 +0000
changeset 309 2de77f7b7a28
parent 297 f6ffac90895c
child 315 3333b6e68289
permissions -rw-r--r--
*** empty log message ***
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 <stdlib.h>
slouken@0
    29
#include <stdio.h>
slouken@0
    30
#include <unistd.h>
slouken@0
    31
#include <string.h>
slouken@0
    32
#include <sys/ioctl.h>
slouken@0
    33
slouken@0
    34
#include "SDL.h"
slouken@0
    35
#include "SDL_error.h"
slouken@0
    36
#include "SDL_timer.h"
slouken@0
    37
#include "SDL_thread.h"
slouken@0
    38
#include "SDL_video.h"
slouken@0
    39
#include "SDL_mouse.h"
slouken@0
    40
#include "SDL_endian.h"
slouken@0
    41
#include "SDL_sysvideo.h"
slouken@0
    42
#include "SDL_pixels_c.h"
slouken@0
    43
#include "SDL_events_c.h"
slouken@0
    44
#include "SDL_ph_video.h"
slouken@0
    45
#include "SDL_ph_modes_c.h"
slouken@0
    46
#include "SDL_ph_image_c.h"
slouken@0
    47
#include "SDL_ph_events_c.h"
slouken@0
    48
#include "SDL_ph_mouse_c.h"
slouken@19
    49
#include "SDL_ph_wm_c.h"
slouken@0
    50
#include "SDL_phyuv_c.h"
slouken@0
    51
#include "blank_cursor.h"
slouken@0
    52
slouken@0
    53
static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat);
slouken@0
    54
static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
    55
                int width, int height, int bpp, Uint32 flags);
slouken@0
    56
static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors);
slouken@0
    57
static void ph_VideoQuit(_THIS);
slouken@0
    58
static void ph_DeleteDevice(SDL_VideoDevice *device);
slouken@291
    59
slouken@291
    60
#ifdef HAVE_OPENGL
slouken@309
    61
int ph_SetupOpenGLContext(_THIS, int width, int height, int bpp, Uint32 flags);
slouken@266
    62
static void ph_GL_SwapBuffers(_THIS);
slouken@291
    63
static int ph_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value);
slouken@291
    64
#endif /* HAVE_OPENGL */
slouken@266
    65
slouken@0
    66
static int ph_Available(void)
slouken@0
    67
{
slouken@309
    68
    int phstat=-1;
slouken@309
    69
slouken@309
    70
    phstat=PtInit(0);
slouken@309
    71
    if (phstat==0)
slouken@309
    72
    {
slouken@309
    73
       return 1;
slouken@309
    74
    }
slouken@309
    75
    else
slouken@309
    76
    {
slouken@309
    77
       return 0;
slouken@309
    78
    }
slouken@0
    79
}
slouken@0
    80
slouken@0
    81
static SDL_VideoDevice *ph_CreateDevice(int devindex)
slouken@0
    82
{
slouken@0
    83
    SDL_VideoDevice *device;
slouken@0
    84
slouken@0
    85
    /* Initialize all variables that we clean on shutdown */
slouken@0
    86
    device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
slouken@309
    87
    if (device) {
slouken@0
    88
        memset(device, 0, (sizeof *device));
slouken@0
    89
        device->hidden = (struct SDL_PrivateVideoData *)
slouken@0
    90
                malloc((sizeof *device->hidden));
slouken@0
    91
        device->gl_data = NULL;
slouken@0
    92
    }
slouken@0
    93
    if ( (device == NULL) || (device->hidden == NULL) ) {
slouken@0
    94
        SDL_OutOfMemory();
slouken@0
    95
        ph_DeleteDevice(device);
slouken@0
    96
        return(0);
slouken@0
    97
    }
slouken@0
    98
    memset(device->hidden, 0, (sizeof *device->hidden));
slouken@0
    99
slouken@0
   100
    /* Set the driver flags */
slouken@279
   101
    device->handles_any_size = 1; /* JB not true for fullscreen */
slouken@0
   102
slouken@0
   103
    /* Set the function pointers */
slouken@266
   104
    device->CreateYUVOverlay = ph_CreateYUVOverlay;
slouken@0
   105
    device->VideoInit = ph_VideoInit;
slouken@0
   106
    device->ListModes = ph_ListModes;
slouken@0
   107
    device->SetVideoMode = ph_SetVideoMode;
slouken@309
   108
    device->ToggleFullScreen = ph_ToggleFullScreen;
slouken@0
   109
    device->UpdateMouse = NULL;	
slouken@0
   110
    device->SetColors = ph_SetColors;
slouken@309
   111
    device->UpdateRects = NULL;         /* ph_ResizeImage */
slouken@0
   112
    device->VideoQuit = ph_VideoQuit;
slouken@0
   113
    device->AllocHWSurface = ph_AllocHWSurface;
slouken@0
   114
    device->CheckHWBlit = NULL;
slouken@0
   115
    device->FillHWRect = NULL;
slouken@0
   116
    device->SetHWColorKey = NULL;
slouken@0
   117
    device->SetHWAlpha = NULL;
slouken@0
   118
    device->LockHWSurface = ph_LockHWSurface;
slouken@0
   119
    device->UnlockHWSurface = ph_UnlockHWSurface;
slouken@0
   120
    device->FlipHWSurface = ph_FlipHWSurface;
slouken@0
   121
    device->FreeHWSurface = ph_FreeHWSurface;
slouken@19
   122
    device->SetCaption = ph_SetCaption;
slouken@0
   123
    device->SetIcon = NULL;
slouken@19
   124
    device->IconifyWindow = ph_IconifyWindow;
slouken@283
   125
    device->GrabInput = ph_GrabInput;
slouken@291
   126
    device->GetWMInfo = ph_GetWMInfo;
slouken@0
   127
    device->FreeWMCursor = ph_FreeWMCursor;
slouken@0
   128
    device->CreateWMCursor = ph_CreateWMCursor;
slouken@0
   129
    device->ShowWMCursor = ph_ShowWMCursor;
slouken@0
   130
    device->WarpWMCursor = ph_WarpWMCursor;
slouken@0
   131
    device->CheckMouseMode = ph_CheckMouseMode;
slouken@0
   132
    device->InitOSKeymap = ph_InitOSKeymap;
slouken@0
   133
    device->PumpEvents = ph_PumpEvents;
slouken@0
   134
slouken@279
   135
    /* OpenGL support. */
slouken@266
   136
    device->GL_LoadLibrary = NULL;
slouken@266
   137
    device->GL_GetProcAddress = NULL;
slouken@266
   138
    device->GL_MakeCurrent = NULL;
slouken@279
   139
#ifdef HAVE_OPENGL
slouken@266
   140
    device->GL_SwapBuffers = ph_GL_SwapBuffers;
slouken@291
   141
    device->GL_GetAttribute = ph_GL_GetAttribute;
slouken@279
   142
#else
slouken@279
   143
    device->GL_SwapBuffers = NULL;
slouken@291
   144
    device->GL_GetAttribute = NULL;
slouken@279
   145
#endif /* HAVE_OPENGL */
slouken@266
   146
slouken@0
   147
    device->free = ph_DeleteDevice;
slouken@0
   148
slouken@0
   149
    return device;
slouken@0
   150
}
slouken@0
   151
slouken@19
   152
VideoBootStrap ph_bootstrap = {
slouken@0
   153
        "photon", "QNX Photon video output",
slouken@0
   154
	ph_Available, ph_CreateDevice
slouken@0
   155
};
slouken@0
   156
slouken@0
   157
static void ph_DeleteDevice(SDL_VideoDevice *device)
slouken@0
   158
{
slouken@309
   159
    if (device)
slouken@309
   160
    {
slouken@309
   161
        if (device->hidden)
slouken@309
   162
        {
slouken@0
   163
            free(device->hidden);
slouken@0
   164
            device->hidden = NULL;
slouken@0
   165
        }
slouken@309
   166
        if (device->gl_data)
slouken@309
   167
        {
slouken@0
   168
            free(device->gl_data);
slouken@0
   169
            device->gl_data = NULL;
slouken@0
   170
        }
slouken@0
   171
        free(device);
slouken@0
   172
        device = NULL;
slouken@0
   173
    }
slouken@0
   174
}
slouken@0
   175
slouken@0
   176
static int ph_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@0
   177
{
slouken@309
   178
    PgColor_t ph_palette[_Pg_MAX_PALETTE];
slouken@309
   179
    int i;
slouken@309
   180
    unsigned long *tempptr;
slouken@309
   181
    int rtnval;
slouken@309
   182
    PgVideoModeInfo_t my_mode_info;
slouken@309
   183
    PgHWCaps_t my_hwcaps;
slouken@309
   184
slouken@309
   185
    window=NULL;
slouken@309
   186
    oglctx=NULL;
slouken@0
   187
	
slouken@309
   188
    if (NULL == (event = malloc(EVENT_SIZE)))
slouken@309
   189
    {
slouken@309
   190
        exit(EXIT_FAILURE);
slouken@309
   191
    }
slouken@0
   192
slouken@309
   193
    /* Create the blank cursor */
slouken@309
   194
    SDL_BlankCursor = this->CreateWMCursor(this, blank_cdata, blank_cmask,
slouken@309
   195
                                          (int)BLANK_CWIDTH, (int)BLANK_CHEIGHT,
slouken@309
   196
                                          (int)BLANK_CHOTX, (int)BLANK_CHOTY);
slouken@0
   197
slouken@309
   198
    if (SDL_BlankCursor == NULL)
slouken@309
   199
    {
slouken@309
   200
        printf("ph_VideoInit: could not create blank cursor\n");
slouken@309
   201
    }
slouken@0
   202
slouken@309
   203
    if (PgGetGraphicsHWCaps(&my_hwcaps) < 0)
slouken@309
   204
    {
slouken@309
   205
        fprintf(stderr,"ph_VideoInit: GetGraphicsHWCaps failed!! \n");
slouken@309
   206
    }
slouken@0
   207
slouken@309
   208
    if (PgGetVideoModeInfo(my_hwcaps.current_video_mode, &my_mode_info) < 0)
slouken@309
   209
    {
slouken@309
   210
        fprintf(stderr,"ph_VideoInit:  PgGetVideoModeInfo failed\n");
slouken@309
   211
    }
slouken@0
   212
slouken@309
   213
    /* We need to return BytesPerPixel as it in used by CreateRGBsurface */
slouken@309
   214
    vformat->BitsPerPixel = my_mode_info.bits_per_pixel;
slouken@309
   215
    vformat->BytesPerPixel = my_mode_info.bytes_per_scanline/my_mode_info.width;
slouken@309
   216
         
slouken@309
   217
    /* return a palette if we are in 256 color mode */
slouken@309
   218
    if (vformat->BitsPerPixel == 8)
slouken@309
   219
    {
slouken@309
   220
        vformat->palette = malloc(sizeof(SDL_Palette));
slouken@309
   221
        memset(vformat->palette, 0, sizeof(SDL_Palette));
slouken@309
   222
        vformat->palette->ncolors = 256;
slouken@309
   223
        vformat->palette->colors = (SDL_Color *)malloc(256 *sizeof(SDL_Color));
slouken@0
   224
slouken@309
   225
        /* fill the palette */
slouken@309
   226
        rtnval = PgGetPalette(ph_palette);
slouken@309
   227
        if (rtnval < 0)
slouken@309
   228
        {
slouken@309
   229
	    fprintf(stderr, "ph_VideoInit: PgGetPalette failed\n");
slouken@309
   230
        }
slouken@0
   231
slouken@309
   232
        tempptr = (unsigned long *)vformat->palette->colors;
slouken@0
   233
slouken@309
   234
        for(i=0;i<256; i++)
slouken@309
   235
        {
slouken@309
   236
            *tempptr = (((unsigned long)ph_palette[i]) << 8);
slouken@309
   237
            tempptr++;
slouken@309
   238
        }		
slouken@309
   239
    }
slouken@0
   240
slouken@19
   241
    currently_fullscreen = 0;
slouken@19
   242
    
slouken@19
   243
    this->info.wm_available = 1;
slouken@19
   244
    
slouken@0
   245
    return 0;
slouken@0
   246
}
slouken@0
   247
slouken@0
   248
static SDL_Surface *ph_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
   249
                int width, int height, int bpp, Uint32 flags)
slouken@0
   250
{
slouken@0
   251
    PgDisplaySettings_t settings;
slouken@309
   252
    int mode;
slouken@309
   253
    PtArg_t arg[32];
slouken@309
   254
    PhDim_t dim;
slouken@266
   255
    int rtnval;
slouken@266
   256
    PgColor_t ph_palette[_Pg_MAX_PALETTE];
slouken@266
   257
    int i;
slouken@266
   258
    unsigned long *tempptr;
slouken@309
   259
    int pargc;
slouken@0
   260
slouken@266
   261
    dim.w=width;
slouken@266
   262
    dim.h=height;
slouken@0
   263
slouken@0
   264
    /* Lock the event thread, in multi-threading environments */
slouken@0
   265
    SDL_Lock_EventThread();
slouken@0
   266
slouken@309
   267
    /* create window if no OpenGL support selected */
slouken@309
   268
    if ((flags & SDL_OPENGL)!=SDL_OPENGL)
slouken@0
   269
    {
slouken@309
   270
        pargc=0;
slouken@309
   271
slouken@309
   272
        PtSetArg(&arg[pargc++], Pt_ARG_DIM, &dim, 0);
slouken@309
   273
        PtSetArg(&arg[pargc++], Pt_ARG_RESIZE_FLAGS, Pt_FALSE, Pt_RESIZE_XY_AS_REQUIRED);
slouken@309
   274
slouken@309
   275
        if (window!=NULL)
slouken@0
   276
        {
slouken@309
   277
            PtUnrealizeWidget(window);
slouken@309
   278
            PtDestroyWidget(window);
slouken@309
   279
            window=NULL;
slouken@309
   280
        }
slouken@309
   281
slouken@309
   282
        window=PtCreateWidget(PtWindow, NULL, pargc-1, arg);
slouken@309
   283
        PtRealizeWidget(window);
slouken@309
   284
slouken@309
   285
        PtFlush();
slouken@309
   286
    }
slouken@309
   287
slouken@309
   288
#ifdef HAVE_OPENGL
slouken@309
   289
    if (flags & SDL_OPENGL)
slouken@309
   290
    {
slouken@309
   291
        /* ph_SetupOpenGLContext creates also window as need */
slouken@309
   292
        if (ph_SetupOpenGLContext(this, width, height, bpp, flags)==0)
slouken@309
   293
        {
slouken@309
   294
            current->flags=flags;
slouken@309
   295
            /* setup OGL update function ... ugly method */
slouken@309
   296
            ph_ResizeImage(this, current, flags); 
slouken@0
   297
        }
slouken@0
   298
        else
slouken@0
   299
        {
slouken@309
   300
            /* if context creation fail, report no OpenGL to high level */
slouken@309
   301
            current->flags=(flags & (~SDL_OPENGL));
slouken@309
   302
        }
slouken@309
   303
#else
slouken@309
   304
    if (flags & SDL_OPENGL) /* if no built-in OpenGL support */
slouken@309
   305
    {
slouken@309
   306
        fprintf(stderr, "error: no OpenGL support, try to recompile library.\n");
slouken@309
   307
        current->flags=(flags & (~SDL_OPENGL));
slouken@309
   308
        return NULL;
slouken@309
   309
#endif /* HAVE_OPENGL */
slouken@309
   310
    }
slouken@309
   311
    else
slouken@309
   312
    {
slouken@309
   313
        /* Initialize the window */
slouken@309
   314
        if (flags & SDL_FULLSCREEN) /* Direct Context , assume SDL_HWSURFACE also set */
slouken@309
   315
        {
slouken@309
   316
            /* Get the video mode and set it */
slouken@309
   317
            if (flags & SDL_ANYFORMAT)
slouken@0
   318
            {
slouken@309
   319
                if ((mode = get_mode_any_format(width, height, bpp)) == 0)
slouken@309
   320
                {
slouken@309
   321
                    fprintf(stderr,"error: get_mode_any_format failed\n");
slouken@309
   322
                    exit(1);
slouken@309
   323
                }
slouken@309
   324
            }
slouken@309
   325
            else
slouken@309
   326
            {
slouken@309
   327
                if ((mode = get_mode(width, height, bpp)) == 0)
slouken@309
   328
                {
slouken@309
   329
                    fprintf(stderr,"error: get_mode failed\n");
slouken@309
   330
                    exit(1);
slouken@309
   331
                }
slouken@309
   332
            }
slouken@309
   333
            settings.mode = mode;
slouken@309
   334
            settings.refresh = 0;
slouken@309
   335
            settings.flags  = 0;       
slouken@309
   336
slouken@309
   337
            if (PgSetVideoMode( &settings ) < 0)
slouken@309
   338
            {
slouken@309
   339
                fprintf(stderr,"error: PgSetVideoMode failed\n");
slouken@309
   340
            }
slouken@309
   341
slouken@309
   342
            /* Get the true height and width */
slouken@309
   343
slouken@309
   344
            current->flags = (flags & (~SDL_RESIZABLE)); /* no resize for Direct Context */
slouken@309
   345
slouken@309
   346
            /* Begin direct mode */
slouken@309
   347
slouken@309
   348
            ph_EnterFullScreen(this);
slouken@309
   349
slouken@309
   350
        } /* end fullscreen flag */
slouken@309
   351
        else
slouken@309
   352
        {
slouken@309
   353
            if (flags & SDL_HWSURFACE)  /* Use offscreen memory iff SDL_HWSURFACE flag is set */
slouken@309
   354
            {
slouken@309
   355
                /* Hardware surface is Offsceen Context.  ph_ResizeImage handles the switch */
slouken@309
   356
                current->flags = (flags & (~SDL_RESIZABLE)); /* no stretch blit in offscreen context */
slouken@309
   357
            }
slouken@309
   358
            else /* must be SDL_SWSURFACE */
slouken@309
   359
            {
slouken@309
   360
                current->flags = (flags | SDL_RESIZABLE); /* yes we can resize as this is a software surface */
slouken@0
   361
            }
slouken@0
   362
        }
slouken@266
   363
slouken@309
   364
	/* If we are setting video to use the palette make sure we have allocated memory for it */
slouken@309
   365
	if (bpp==8)
slouken@309
   366
	{
slouken@309
   367
            current->format->palette = malloc(sizeof(SDL_Palette));
slouken@309
   368
            memset(current->format->palette, 0, sizeof(SDL_Palette));
slouken@309
   369
            current->format->palette->ncolors = 256;
slouken@309
   370
            current->format->palette->colors = (SDL_Color *)malloc(256 *sizeof(SDL_Color));
slouken@309
   371
            /* fill the palette */
slouken@309
   372
            rtnval = PgGetPalette(ph_palette);
slouken@309
   373
slouken@309
   374
            tempptr = (unsigned long *)current->format->palette->colors;
slouken@309
   375
slouken@309
   376
            for(i=0; i<256; i++)
slouken@309
   377
            {
slouken@309
   378
                *tempptr = (((unsigned long)ph_palette[i]) << 8);
slouken@309
   379
                tempptr++;
slouken@309
   380
            }
slouken@0
   381
        }
slouken@0
   382
slouken@0
   383
    }
slouken@0
   384
slouken@309
   385
    current->w = width;
slouken@309
   386
    current->h = height;
slouken@309
   387
    current->format->BitsPerPixel = bpp;
slouken@309
   388
    current->format->BytesPerPixel = (bpp+7)/8;
slouken@309
   389
    current->pitch = SDL_CalculatePitch(current);
slouken@309
   390
    /* Must call at least once it setup image planes */
slouken@309
   391
    ph_ResizeImage(this, current, flags);
slouken@0
   392
slouken@0
   393
    SDL_Unlock_EventThread();
slouken@0
   394
slouken@0
   395
    /* We're done! */
slouken@0
   396
    return(current);
slouken@0
   397
}
slouken@0
   398
slouken@0
   399
static void ph_VideoQuit(_THIS)
slouken@0
   400
{
slouken@309
   401
    PhRegion_t region_info;
slouken@309
   402
slouken@283
   403
    ph_DestroyImage(this, SDL_VideoSurface); 
slouken@0
   404
slouken@279
   405
    if (currently_fullscreen)
slouken@279
   406
    {
slouken@309
   407
        PdDirectStop(directContext);
slouken@309
   408
        PdReleaseDirectContext(directContext);
slouken@279
   409
        directContext=0;	
slouken@309
   410
        currently_fullscreen=0;
slouken@279
   411
    }
slouken@309
   412
slouken@309
   413
#ifdef HAVE_OPENGL
slouken@309
   414
    if (((this->screen->flags & SDL_FULLSCREEN)==SDL_FULLSCREEN) &&
slouken@309
   415
        ((this->screen->flags & SDL_OPENGL)==SDL_OPENGL))
slouken@309
   416
    {
slouken@309
   417
        region_info.cursor_type=Ph_CURSOR_POINTER;
slouken@309
   418
        region_info.rid=PtWidgetRid(window);
slouken@309
   419
        PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
slouken@309
   420
    }
slouken@309
   421
slouken@309
   422
    PtFlush();
slouken@309
   423
#endif /* HAVE_OPENGL */
slouken@309
   424
    
slouken@309
   425
    if (window)
slouken@309
   426
    {
slouken@309
   427
        PtUnrealizeWidget(window);
slouken@309
   428
        PtDestroyWidget(window);
slouken@309
   429
        window=NULL;
slouken@309
   430
    }
slouken@309
   431
slouken@309
   432
#ifdef HAVE_OPENGL
slouken@309
   433
    if (oglctx)
slouken@309
   434
    {
slouken@309
   435
        PhDCSetCurrent(NULL);
slouken@309
   436
        PhDCRelease(oglctx);
slouken@309
   437
        oglctx=NULL;
slouken@309
   438
    }
slouken@309
   439
#endif /* HAVE_OPENGL */
slouken@0
   440
}
slouken@0
   441
slouken@0
   442
static int ph_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
   443
{
slouken@0
   444
	PgColor_t *in, *out;
slouken@0
   445
	int i, j;
slouken@0
   446
	int alloct_all = 1;
slouken@0
   447
slouken@0
   448
    	colors = this->screen->format->palette->colors;
slouken@0
   449
slouken@0
   450
	in = alloca( ncolors*sizeof(PgColor_t)  );
slouken@0
   451
	if ( in == NULL  ) {
slouken@0
   452
		return 0;
slouken@0
   453
	}
slouken@0
   454
	memset(in,0,ncolors*sizeof(PgColor_t));
slouken@0
   455
slouken@0
   456
    out = alloca( ncolors*sizeof(PgColor_t)  );
slouken@0
   457
    if ( out == NULL  ) {
slouken@0
   458
		return 0;
slouken@0
   459
    }
slouken@0
   460
	
slouken@0
   461
	for (i=0,j=firstcolor;i<ncolors;i++,j++)
slouken@0
   462
	{
slouken@0
   463
		in[i] |= colors[j].r<<16 ;
slouken@0
   464
		in[i] |= colors[j].g<<8 ;
slouken@0
   465
		in[i] |= colors[j].b ; 
slouken@0
   466
	}
slouken@0
   467
slouken@0
   468
	if ( (this->screen->flags & SDL_HWPALETTE) == SDL_HWPALETTE ) 
slouken@0
   469
	{
slouken@0
   470
		if( PgSetPalette( in, 0, 0, ncolors, Pg_PALSET_HARD, 0) < 0 )
slouken@0
   471
		{
slouken@0
   472
			fprintf(stderr,"error: PgSetPalette(..,Pg_PALSET_HARD)  failed\n");
slouken@0
   473
			return 0;
slouken@0
   474
		}  
slouken@0
   475
	}
slouken@0
   476
	else 
slouken@0
   477
	{
slouken@0
   478
		if ( PgColorMatch(ncolors, in, out) < 0 )
slouken@0
   479
        {
slouken@0
   480
            fprintf(stderr,"error: PgColorMatch failed\n");
slouken@0
   481
            return 0;
slouken@0
   482
        }
slouken@0
   483
		for (i=0;i<ncolors;i++)
slouken@0
   484
		{
slouken@0
   485
			if (memcmp(&in[i],&out[i],sizeof(PgColor_t)))
slouken@0
   486
			{
slouken@0
   487
				alloct_all = 0;
slouken@0
   488
				break;
slouken@0
   489
			}
slouken@0
   490
		}
slouken@0
   491
        if( PgSetPalette( out, 0, 0, ncolors, Pg_PALSET_SOFT, 0) < 0 )
slouken@0
   492
        {
slouken@0
   493
            fprintf(stderr,"error: PgSetPalette(..,Pg_PALSET_SOFT) failed\n");
slouken@0
   494
            return 0;
slouken@0
   495
        }
slouken@0
   496
	}
slouken@0
   497
	return alloct_all;
slouken@0
   498
}
slouken@0
   499
slouken@279
   500
#ifdef HAVE_OPENGL
slouken@309
   501
slouken@309
   502
int ph_SetupOpenGLContext(_THIS, int width, int height, int bpp, Uint32 flags)
slouken@309
   503
{
slouken@309
   504
    PtArg_t args[8];
slouken@309
   505
    PhDim_t dim;
slouken@309
   506
    uint64_t OGLAttrib[PH_OGL_MAX_ATTRIBS];
slouken@309
   507
    int OGLargc;
slouken@309
   508
    int pargc;
slouken@309
   509
slouken@309
   510
    dim.w=width;
slouken@309
   511
    dim.h=height;
slouken@309
   512
    
slouken@309
   513
    if (oglctx!=NULL)
slouken@309
   514
    {
slouken@309
   515
       PhDCSetCurrent(NULL);
slouken@309
   516
       PhDCRelease(oglctx);
slouken@309
   517
       oglctx=NULL;
slouken@309
   518
    }
slouken@309
   519
slouken@309
   520
    OGLargc=0;
slouken@309
   521
    if (this->gl_config.depth_size)
slouken@309
   522
    {
slouken@309
   523
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_DEPTH_BITS;
slouken@309
   524
        OGLAttrib[OGLargc++]=this->gl_config.depth_size;
slouken@309
   525
    }
slouken@309
   526
    if (this->gl_config.stencil_size)
slouken@309
   527
    {
slouken@309
   528
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_STENCIL_BITS;
slouken@309
   529
        OGLAttrib[OGLargc++]=this->gl_config.stencil_size;
slouken@309
   530
    }
slouken@309
   531
    OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FORCE_SW;
slouken@309
   532
    if (flags & SDL_FULLSCREEN)
slouken@309
   533
    {
slouken@309
   534
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN;
slouken@309
   535
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_DIRECT;
slouken@309
   536
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN_BEST;
slouken@309
   537
        OGLAttrib[OGLargc++]=PHOGL_ATTRIB_FULLSCREEN_CENTER;
slouken@309
   538
    }
slouken@309
   539
    OGLAttrib[OGLargc++]=PHOGL_ATTRIB_NONE;
slouken@309
   540
slouken@309
   541
    if (this->gl_config.double_buffer)
slouken@309
   542
    {
slouken@309
   543
        oglctx=PdCreateOpenGLContext(2, &dim, 0, OGLAttrib);
slouken@309
   544
    }
slouken@309
   545
    else
slouken@309
   546
    {
slouken@309
   547
        oglctx=PdCreateOpenGLContext(1, &dim, 0, OGLAttrib);
slouken@309
   548
    }
slouken@309
   549
    if (oglctx==NULL)
slouken@309
   550
    {
slouken@309
   551
        fprintf(stderr,"error: cannot create OpenGL context.\n");
slouken@309
   552
        return (-1);
slouken@309
   553
    }
slouken@309
   554
slouken@309
   555
    PhDCSetCurrent(oglctx);
slouken@309
   556
slouken@309
   557
    pargc=0;
slouken@309
   558
slouken@309
   559
    PtSetArg(&args[pargc++], Pt_ARG_DIM, &dim, 0);
slouken@309
   560
    PtSetArg(&args[pargc++], Pt_ARG_RESIZE_FLAGS, Pt_FALSE, Pt_RESIZE_XY_AS_REQUIRED);
slouken@309
   561
    PtSetArg(&args[pargc++], Pt_ARG_FILL_COLOR, Pg_BLACK, 0);
slouken@309
   562
slouken@309
   563
    if (flags & SDL_FULLSCREEN)
slouken@309
   564
    {
slouken@309
   565
        PhPoint_t pos;
slouken@309
   566
slouken@309
   567
        pos.x=0;
slouken@309
   568
        pos.y=0;
slouken@309
   569
slouken@309
   570
        PtSetArg(&args[pargc++], Pt_ARG_WINDOW_RENDER_FLAGS, Pt_FALSE, ~0);
slouken@309
   571
        PtSetArg(&args[pargc++], Pt_ARG_WINDOW_MANAGED_FLAGS,Pt_TRUE, Ph_WM_FFRONT | Ph_WM_CLOSE | Ph_WM_TOFRONT | Ph_WM_CONSWITCH);
slouken@309
   572
        PtSetArg(&args[pargc++], Pt_ARG_WINDOW_STATE, Pt_TRUE, Ph_WM_STATE_ISFRONT | Ph_WM_STATE_ISFOCUS);
slouken@309
   573
        PtSetArg(&args[pargc++], Pt_ARG_POS, &pos, 0);
slouken@309
   574
    }
slouken@309
   575
slouken@309
   576
    if (window!=NULL)
slouken@309
   577
    {
slouken@309
   578
        PtUnrealizeWidget(window);
slouken@309
   579
        PtDestroyWidget(window);
slouken@309
   580
        window=NULL;
slouken@309
   581
    }
slouken@309
   582
slouken@309
   583
    window=PtCreateWidget(PtWindow, NULL, pargc-1, args);
slouken@309
   584
    PtRealizeWidget(window);
slouken@309
   585
slouken@309
   586
    /* disable mouse for fullscreen */
slouken@309
   587
    if (flags & SDL_FULLSCREEN)
slouken@309
   588
    {
slouken@309
   589
        PhRegion_t region_info;
slouken@309
   590
slouken@309
   591
        region_info.cursor_type=Ph_CURSOR_NONE;
slouken@309
   592
        region_info.rid=PtWidgetRid(window);
slouken@309
   593
        PhRegionChange(Ph_REGION_CURSOR, 0, &region_info, NULL, NULL);
slouken@309
   594
    }
slouken@309
   595
slouken@309
   596
    PtFlush();
slouken@309
   597
slouken@309
   598
    return 0;
slouken@309
   599
}
slouken@309
   600
slouken@266
   601
void ph_GL_SwapBuffers(_THIS)
slouken@266
   602
{
slouken@291
   603
    PgSetRegion(PtWidgetRid(window));
slouken@309
   604
    PdOpenGLContextSwapBuffers(oglctx);
slouken@266
   605
}
slouken@266
   606
slouken@291
   607
int ph_GL_GetAttribute(_THIS, SDL_GLattr attrib, int* value)
slouken@0
   608
{
slouken@291
   609
    switch (attrib)
slouken@291
   610
    {
slouken@291
   611
        case SDL_GL_DOUBLEBUFFER:
slouken@291
   612
             *value=this->gl_config.double_buffer;
slouken@291
   613
             break;
slouken@291
   614
        case SDL_GL_STENCIL_SIZE:
slouken@291
   615
             *value=this->gl_config.stencil_size;
slouken@291
   616
             break;
slouken@291
   617
        case SDL_GL_DEPTH_SIZE:
slouken@291
   618
             *value=this->gl_config.depth_size;
slouken@291
   619
             break;
slouken@291
   620
        default:
slouken@291
   621
             *value=0;
slouken@291
   622
             return(-1);
slouken@291
   623
    }
slouken@291
   624
    return 0;
slouken@291
   625
}
slouken@0
   626
slouken@291
   627
#endif /* HAVE_OPENGL */