comparison src/video/macdsp/SDL_dspvideo.c @ 1662:782fd950bd46 SDL-1.3

Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API. WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid. The code is now run through a consistent indent format: indent -i4 -nut -nsc -br -ce The headers are being converted to automatically generate doxygen documentation.
author Sam Lantinga <slouken@libsdl.org>
date Sun, 28 May 2006 13:04:16 +0000
parents 14717b52abc0
children 4da1ee79c9af
comparison
equal deleted inserted replaced
1661:281d3f4870e5 1662:782fd950bd46
148 #include "../maccommon/SDL_macwm_c.h" 148 #include "../maccommon/SDL_macwm_c.h"
149 #include "../maccommon/SDL_macmouse_c.h" 149 #include "../maccommon/SDL_macmouse_c.h"
150 #include "../maccommon/SDL_macevents_c.h" 150 #include "../maccommon/SDL_macevents_c.h"
151 151
152 /* Initialization/Query functions */ 152 /* Initialization/Query functions */
153 static int DSp_VideoInit(_THIS, SDL_PixelFormat *vformat); 153 static int DSp_VideoInit (_THIS, SDL_PixelFormat * vformat);
154 static SDL_Rect **DSp_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags); 154 static SDL_Rect **DSp_ListModes (_THIS, SDL_PixelFormat * format,
155 static SDL_Surface *DSp_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags); 155 Uint32 flags);
156 static int DSp_SetColors(_THIS, int firstcolor, int ncolors, 156 static SDL_Surface *DSp_SetVideoMode (_THIS, SDL_Surface * current, int width,
157 SDL_Color *colors); 157 int height, int bpp, Uint32 flags);
158 static int DSp_CreatePalette(_THIS); 158 static int DSp_SetColors (_THIS, int firstcolor, int ncolors,
159 static int DSp_DestroyPalette(_THIS); 159 SDL_Color * colors);
160 static void DSp_VideoQuit(_THIS); 160 static int DSp_CreatePalette (_THIS);
161 161 static int DSp_DestroyPalette (_THIS);
162 static int DSp_GetMainDevice (_THIS, GDHandle *device); 162 static void DSp_VideoQuit (_THIS);
163 static void DSp_IsHWAvailable (_THIS, SDL_PixelFormat *vformat); 163
164 static void DSp_DSpUpdate(_THIS, int numrects, SDL_Rect *sdl_rects); 164 static int DSp_GetMainDevice (_THIS, GDHandle * device);
165 static void DSp_DirectUpdate(_THIS, int numrects, SDL_Rect *sdl_rects); 165 static void DSp_IsHWAvailable (_THIS, SDL_PixelFormat * vformat);
166 static void DSp_DSpUpdate (_THIS, int numrects, SDL_Rect * sdl_rects);
167 static void DSp_DirectUpdate (_THIS, int numrects, SDL_Rect * sdl_rects);
166 168
167 /* Hardware surface functions */ 169 /* Hardware surface functions */
168 static int DSp_SetHWAlpha(_THIS, SDL_Surface *surface, UInt8 alpha); 170 static int DSp_SetHWAlpha (_THIS, SDL_Surface * surface, UInt8 alpha);
169 static int DSp_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key); 171 static int DSp_SetHWColorKey (_THIS, SDL_Surface * surface, Uint32 key);
170 static int DSp_NewHWSurface(_THIS, CGrafPtr *port, int depth, int width, int height); 172 static int DSp_NewHWSurface (_THIS, CGrafPtr * port, int depth, int width,
171 static int DSp_AllocHWSurface(_THIS, SDL_Surface *surface); 173 int height);
172 static int DSp_LockHWSurface(_THIS, SDL_Surface *surface); 174 static int DSp_AllocHWSurface (_THIS, SDL_Surface * surface);
173 static void DSp_UnlockHWSurface(_THIS, SDL_Surface *surface); 175 static int DSp_LockHWSurface (_THIS, SDL_Surface * surface);
174 static void DSp_FreeHWSurface(_THIS, SDL_Surface *surface); 176 static void DSp_UnlockHWSurface (_THIS, SDL_Surface * surface);
175 static int DSp_FlipHWSurface(_THIS, SDL_Surface *surface); 177 static void DSp_FreeHWSurface (_THIS, SDL_Surface * surface);
176 static int DSp_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dest); 178 static int DSp_FlipHWSurface (_THIS, SDL_Surface * surface);
177 static int DSp_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect, 179 static int DSp_CheckHWBlit (_THIS, SDL_Surface * src, SDL_Surface * dest);
178 SDL_Surface *dst, SDL_Rect *dstrect); 180 static int DSp_HWAccelBlit (SDL_Surface * src, SDL_Rect * srcrect,
179 static int DSp_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color); 181 SDL_Surface * dst, SDL_Rect * dstrect);
182 static int DSp_FillHWRect (_THIS, SDL_Surface * dst, SDL_Rect * rect,
183 Uint32 color);
180 184
181 #if SDL_VIDEO_OPENGL 185 #if SDL_VIDEO_OPENGL
182 static void DSp_GL_SwapBuffers (_THIS); 186 static void DSp_GL_SwapBuffers (_THIS);
183 #endif 187 #endif
184 188
185 #if ! TARGET_API_MAC_CARBON 189 #if ! TARGET_API_MAC_CARBON
186 190
187 #define GetPortPixRowBytes(x) ( (*(x->portPixMap))->rowBytes ) 191 #define GetPortPixRowBytes(x) ( (*(x->portPixMap))->rowBytes )
188 #define GetGDevPixMap(x) ((**(x)).gdPMap) 192 #define GetGDevPixMap(x) ((**(x)).gdPMap)
189 #define GetPortPixMap(x) ((*(x)).portPixMap) 193 #define GetPortPixMap(x) ((*(x)).portPixMap)
190 194
191 #define GetPixDepth(y) ((**(y)).pixelSize) 195 #define GetPixDepth(y) ((**(y)).pixelSize)
192 //#define GetPixRowBytes(y) ((**(y)).rowBytes) 196 //#define GetPixRowBytes(y) ((**(y)).rowBytes)
193 //#define GetPixBaseAddr(y) ((**(y)).baseAddr) 197 //#define GetPixBaseAddr(y) ((**(y)).baseAddr)
194 #define GetPixCTab(y) ((**(y)).pmTable) 198 #define GetPixCTab(y) ((**(y)).pmTable)
195 #define GetPortBitMapForCopyBits(x) (&(((GrafPtr)(x))->portBits)) 199 #define GetPortBitMapForCopyBits(x) (&(((GrafPtr)(x))->portBits))
196 200
197 #else 201 #else
198 #define GetPortPixRowBytes(x) (GetPixRowBytes(GetPortPixMap(x)) ) 202 #define GetPortPixRowBytes(x) (GetPixRowBytes(GetPortPixMap(x)) )
199 #define GetGDevPixMap(x) ((**(x)).gdPMap) 203 #define GetGDevPixMap(x) ((**(x)).gdPMap)
200 204
201 #endif 205 #endif
202 206
203 typedef struct private_hwdata { 207 typedef struct private_hwdata
204 208 {
205 GWorldPtr offscreen; // offscreen gworld in VRAM or AGP 209
206 210 GWorldPtr offscreen; // offscreen gworld in VRAM or AGP
207 #ifdef DSP_TRY_CC_AND_AA 211
208 GWorldPtr mask; // transparent mask 212 #ifdef DSP_TRY_CC_AND_AA
209 RGBColor alpha; // alpha color 213 GWorldPtr mask; // transparent mask
210 RGBColor trans; // transparent color 214 RGBColor alpha; // alpha color
211 #endif 215 RGBColor trans; // transparent color
212 216 #endif
217
213 } private_hwdata; 218 } private_hwdata;
214 219
215 typedef private_hwdata private_swdata ; /* have same fields */ 220 typedef private_hwdata private_swdata; /* have same fields */
216 221
217 /* Macintosh toolbox driver bootstrap functions */ 222 /* Macintosh toolbox driver bootstrap functions */
218 223
219 static int DSp_Available(void) 224 static int
220 { 225 DSp_Available (void)
221 /* Check for DrawSprocket */ 226 {
227 /* Check for DrawSprocket */
222 #if ! TARGET_API_MAC_OSX 228 #if ! TARGET_API_MAC_OSX
223 /* This check is only meaningful if you weak-link DrawSprocketLib */ 229 /* This check is only meaningful if you weak-link DrawSprocketLib */
224 return ((Ptr)DSpStartup != (Ptr)kUnresolvedCFragSymbolAddress); 230 return ((Ptr) DSpStartup != (Ptr) kUnresolvedCFragSymbolAddress);
225 #else 231 #else
226 return 1; // DrawSprocket.framework doesn't have it all, but it's there 232 return 1; // DrawSprocket.framework doesn't have it all, but it's there
227 #endif 233 #endif
228 } 234 }
229 235
230 static void DSp_DeleteDevice(SDL_VideoDevice *device) 236 static void
231 { 237 DSp_DeleteDevice (SDL_VideoDevice * device)
232 /* -dw- taking no chances with null pointers */ 238 {
233 if (device) { 239 /* -dw- taking no chances with null pointers */
234 240 if (device) {
235 if (device->hidden) { 241
236 242 if (device->hidden) {
237 if (device->hidden->dspinfo) 243
238 SDL_free(device->hidden->dspinfo); 244 if (device->hidden->dspinfo)
239 245 SDL_free (device->hidden->dspinfo);
240 SDL_free(device->hidden); 246
241 } 247 SDL_free (device->hidden);
242 SDL_free(device); 248 }
243 } 249 SDL_free (device);
244 } 250 }
245 251 }
246 static SDL_VideoDevice *DSp_CreateDevice(int devindex) 252
247 { 253 static SDL_VideoDevice *
248 SDL_VideoDevice *device; 254 DSp_CreateDevice (int devindex)
249 255 {
250 /* Initialize all variables that we clean on shutdown */ 256 SDL_VideoDevice *device;
251 device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice)); 257
252 if ( device ) { 258 /* Initialize all variables that we clean on shutdown */
253 SDL_memset(device, 0, sizeof (*device)); 259 device = (SDL_VideoDevice *) SDL_malloc (sizeof (SDL_VideoDevice));
254 device->hidden = (struct SDL_PrivateVideoData *) 260 if (device) {
255 SDL_malloc((sizeof *device->hidden)); 261 SDL_memset (device, 0, sizeof (*device));
256 if (device->hidden) 262 device->hidden = (struct SDL_PrivateVideoData *)
257 SDL_memset(device->hidden, 0, sizeof ( *(device->hidden) ) ); 263 SDL_malloc ((sizeof *device->hidden));
258 } 264 if (device->hidden)
259 if ( (device == NULL) || (device->hidden == NULL) ) { 265 SDL_memset (device->hidden, 0, sizeof (*(device->hidden)));
260 SDL_OutOfMemory(); 266 }
261 267 if ((device == NULL) || (device->hidden == NULL)) {
262 if ( device ) { 268 SDL_OutOfMemory ();
263 269
264 if (device->hidden) 270 if (device) {
265 SDL_free(device->hidden); 271
266 272 if (device->hidden)
267 SDL_free(device); 273 SDL_free (device->hidden);
268 } 274
269 275 SDL_free (device);
270 return(NULL); 276 }
271 } 277
272 278 return (NULL);
273 /* Allocate DrawSprocket information */ 279 }
274 device->hidden->dspinfo = (struct DSpInfo *)SDL_malloc( 280
275 (sizeof *device->hidden->dspinfo)); 281 /* Allocate DrawSprocket information */
276 if ( device->hidden->dspinfo == NULL ) { 282 device->hidden->dspinfo = (struct DSpInfo *) SDL_malloc ((sizeof *device->
277 SDL_OutOfMemory(); 283 hidden->
278 SDL_free(device->hidden); 284 dspinfo));
279 SDL_free(device); 285 if (device->hidden->dspinfo == NULL) {
280 return(0); 286 SDL_OutOfMemory ();
281 } 287 SDL_free (device->hidden);
282 SDL_memset(device->hidden->dspinfo, 0, (sizeof *device->hidden->dspinfo)); 288 SDL_free (device);
283 289 return (0);
284 /* Set the function pointers */ 290 }
285 device->VideoInit = DSp_VideoInit; 291 SDL_memset (device->hidden->dspinfo, 0,
286 device->ListModes = DSp_ListModes; 292 (sizeof *device->hidden->dspinfo));
287 device->SetVideoMode = DSp_SetVideoMode; 293
288 device->SetColors = DSp_SetColors; 294 /* Set the function pointers */
289 device->UpdateRects = NULL; 295 device->VideoInit = DSp_VideoInit;
290 device->VideoQuit = DSp_VideoQuit; 296 device->ListModes = DSp_ListModes;
291 device->AllocHWSurface = DSp_AllocHWSurface; 297 device->SetVideoMode = DSp_SetVideoMode;
292 device->CheckHWBlit = NULL; 298 device->SetColors = DSp_SetColors;
293 device->FillHWRect = NULL; 299 device->UpdateRects = NULL;
294 device->SetHWColorKey = NULL; 300 device->VideoQuit = DSp_VideoQuit;
295 device->SetHWAlpha = NULL; 301 device->AllocHWSurface = DSp_AllocHWSurface;
296 device->LockHWSurface = DSp_LockHWSurface; 302 device->CheckHWBlit = NULL;
297 device->UnlockHWSurface = DSp_UnlockHWSurface; 303 device->FillHWRect = NULL;
298 device->FlipHWSurface = DSp_FlipHWSurface; 304 device->SetHWColorKey = NULL;
299 device->FreeHWSurface = DSp_FreeHWSurface; 305 device->SetHWAlpha = NULL;
306 device->LockHWSurface = DSp_LockHWSurface;
307 device->UnlockHWSurface = DSp_UnlockHWSurface;
308 device->FlipHWSurface = DSp_FlipHWSurface;
309 device->FreeHWSurface = DSp_FreeHWSurface;
300 #if SDL_VIDEO_OPENGL 310 #if SDL_VIDEO_OPENGL
301 device->GL_MakeCurrent = Mac_GL_MakeCurrent; 311 device->GL_MakeCurrent = Mac_GL_MakeCurrent;
302 device->GL_SwapBuffers = DSp_GL_SwapBuffers; 312 device->GL_SwapBuffers = DSp_GL_SwapBuffers;
303 device->GL_LoadLibrary = Mac_GL_LoadLibrary; 313 device->GL_LoadLibrary = Mac_GL_LoadLibrary;
304 device->GL_GetProcAddress = Mac_GL_GetProcAddress; 314 device->GL_GetProcAddress = Mac_GL_GetProcAddress;
305 #endif 315 #endif
306 device->SetCaption = NULL; 316 device->SetCaption = NULL;
307 device->SetIcon = NULL; 317 device->SetIcon = NULL;
308 device->IconifyWindow = NULL; 318 device->IconifyWindow = NULL;
309 device->GrabInput = NULL; 319 device->GrabInput = NULL;
310 device->GetWMInfo = NULL; 320 device->GetWMInfo = NULL;
311 device->FreeWMCursor = Mac_FreeWMCursor; 321 device->FreeWMCursor = Mac_FreeWMCursor;
312 device->CreateWMCursor = Mac_CreateWMCursor; 322 device->CreateWMCursor = Mac_CreateWMCursor;
313 device->ShowWMCursor = Mac_ShowWMCursor; 323 device->ShowWMCursor = Mac_ShowWMCursor;
314 device->WarpWMCursor = Mac_WarpWMCursor; 324 device->WarpWMCursor = Mac_WarpWMCursor;
315 device->InitOSKeymap = Mac_InitOSKeymap; 325 device->InitOSKeymap = Mac_InitOSKeymap;
316 device->PumpEvents = Mac_PumpEvents; 326 device->PumpEvents = Mac_PumpEvents;
317 327
318 device->GrabInput = NULL; 328 device->GrabInput = NULL;
319 device->CheckMouseMode = NULL; 329 device->CheckMouseMode = NULL;
320 330
321 device->free = DSp_DeleteDevice; 331 device->free = DSp_DeleteDevice;
322 332
323 return device; 333 return device;
324 } 334 }
325 335
326 VideoBootStrap DSp_bootstrap = { 336 VideoBootStrap DSp_bootstrap = {
327 "DSp", "MacOS DrawSprocket", 337 "DSp", "MacOS DrawSprocket",
328 DSp_Available, DSp_CreateDevice 338 DSp_Available, DSp_CreateDevice
329 }; 339 };
330 340
331 /* Use DSp/Display Manager to build mode list for given screen */ 341 /* Use DSp/Display Manager to build mode list for given screen */
332 static SDL_Rect** DSp_BuildModeList (const GDHandle gDevice, int *displayWidth, int *displayHeight) 342 static SDL_Rect **
333 { 343 DSp_BuildModeList (const GDHandle gDevice, int *displayWidth,
334 DSpContextAttributes attributes; 344 int *displayHeight)
335 DSpContextReference context; 345 {
336 DisplayIDType displayID; 346 DSpContextAttributes attributes;
337 SDL_Rect temp_list [16]; 347 DSpContextReference context;
338 SDL_Rect **mode_list; 348 DisplayIDType displayID;
339 int width, height, i, j; 349 SDL_Rect temp_list[16];
340 350 SDL_Rect **mode_list;
341 #if TARGET_API_MAC_OSX 351 int width, height, i, j;
342 352
343 displayID = 0; 353 #if TARGET_API_MAC_OSX
344 354
345 #else 355 displayID = 0;
346 /* Ask Display Manager for integer id of screen device */ 356
347 if ( DMGetDisplayIDByGDevice (gDevice, &displayID, SDL_TRUE) != noErr ) { 357 #else
348 return NULL; 358 /* Ask Display Manager for integer id of screen device */
349 } 359 if (DMGetDisplayIDByGDevice (gDevice, &displayID, SDL_TRUE) != noErr) {
350 #endif 360 return NULL;
351 /* Get the first possible DSp context on this device */ 361 }
352 if ( DSpGetFirstContext (displayID, &context) != noErr ) { 362 #endif
353 return NULL; 363 /* Get the first possible DSp context on this device */
354 } 364 if (DSpGetFirstContext (displayID, &context) != noErr) {
355 365 return NULL;
356 if ( DSpContext_GetAttributes (context, &attributes) != noErr ) 366 }
357 return NULL; 367
358 368 if (DSpContext_GetAttributes (context, &attributes) != noErr)
359 *displayWidth = attributes.displayWidth; 369 return NULL;
360 *displayHeight = attributes.displayHeight; 370
361 371 *displayWidth = attributes.displayWidth;
362 for ( i = 0; i < SDL_arraysize(temp_list); i++ ) { 372 *displayHeight = attributes.displayHeight;
363 width = attributes.displayWidth; 373
364 height = attributes.displayHeight; 374 for (i = 0; i < SDL_arraysize (temp_list); i++) {
365 375 width = attributes.displayWidth;
366 temp_list [i].x = 0 | attributes.displayBestDepth; 376 height = attributes.displayHeight;
367 temp_list [i].y = 0; 377
368 temp_list [i].w = width; 378 temp_list[i].x = 0 | attributes.displayBestDepth;
369 temp_list [i].h = height; 379 temp_list[i].y = 0;
370 380 temp_list[i].w = width;
371 /* DSp will report many different contexts with the same width and height. */ 381 temp_list[i].h = height;
372 /* They will differ in bit depth and refresh rate. */ 382
373 /* We will ignore them until we reach one with a different width/height */ 383 /* DSp will report many different contexts with the same width and height. */
374 /* When there are no more contexts to look at, we will quit building the list*/ 384 /* They will differ in bit depth and refresh rate. */
375 while ( width == attributes.displayWidth && height == attributes.displayHeight ) { 385 /* We will ignore them until we reach one with a different width/height */
376 386 /* When there are no more contexts to look at, we will quit building the list */
377 OSStatus err = DSpGetNextContext (context, &context); 387 while (width == attributes.displayWidth
378 if (err != noErr) 388 && height == attributes.displayHeight) {
379 if (err == kDSpContextNotFoundErr) 389
380 goto done; 390 OSStatus err = DSpGetNextContext (context, &context);
381 else 391 if (err != noErr)
382 return NULL; 392 if (err == kDSpContextNotFoundErr)
383 393 goto done;
384 if ( DSpContext_GetAttributes (context, &attributes) != noErr ) 394 else
385 return NULL; 395 return NULL;
386 396
387 temp_list [i].x |= attributes.displayBestDepth; 397 if (DSpContext_GetAttributes (context, &attributes) != noErr)
388 } 398 return NULL;
389 } 399
390 done: 400 temp_list[i].x |= attributes.displayBestDepth;
391 i++; /* i was not incremented before kicking out of the loop */ 401 }
392 402 }
393 mode_list = (SDL_Rect**) SDL_malloc (sizeof (SDL_Rect*) * (i+1)); 403 done:
394 if (mode_list) { 404 i++; /* i was not incremented before kicking out of the loop */
395 405
396 /* -dw- new stuff: build in reverse order so largest sizes list first */ 406 mode_list = (SDL_Rect **) SDL_malloc (sizeof (SDL_Rect *) * (i + 1));
397 for (j = i-1; j >= 0; j--) { 407 if (mode_list) {
398 mode_list [j] = (SDL_Rect*) SDL_malloc (sizeof (SDL_Rect)); 408
399 if (mode_list [j]) 409 /* -dw- new stuff: build in reverse order so largest sizes list first */
400 SDL_memcpy (mode_list [j], &(temp_list [j]), sizeof (SDL_Rect)); 410 for (j = i - 1; j >= 0; j--) {
401 else { 411 mode_list[j] = (SDL_Rect *) SDL_malloc (sizeof (SDL_Rect));
402 SDL_OutOfMemory (); 412 if (mode_list[j])
403 return NULL; 413 SDL_memcpy (mode_list[j], &(temp_list[j]), sizeof (SDL_Rect));
404 } 414 else {
405 } 415 SDL_OutOfMemory ();
406 mode_list [i] = NULL; /* append null to the end */ 416 return NULL;
407 } 417 }
408 else { 418 }
409 SDL_OutOfMemory (); 419 mode_list[i] = NULL; /* append null to the end */
410 return NULL; 420 } else {
411 } 421 SDL_OutOfMemory ();
412 422 return NULL;
413 return mode_list; 423 }
414 } 424
415 425 return mode_list;
416 static void DSp_IsHWAvailable (_THIS, SDL_PixelFormat *vformat) 426 }
417 { 427
418 /* 428 static void
419 VRAM GWorlds are only available on OS 9 or later. 429 DSp_IsHWAvailable (_THIS, SDL_PixelFormat * vformat)
420 Even with OS 9, some display drivers won't support it, 430 {
421 so we create a test GWorld and check for errors. 431 /*
422 */ 432 VRAM GWorlds are only available on OS 9 or later.
423 433 Even with OS 9, some display drivers won't support it,
424 long versionSystem; 434 so we create a test GWorld and check for errors.
425 435 */
426 dsp_vram_available = SDL_FALSE; 436
427 dsp_agp_available = SDL_FALSE; 437 long versionSystem;
428 438
429 Gestalt ('sysv', &versionSystem); 439 dsp_vram_available = SDL_FALSE;
430 if (0x00000860 < (versionSystem & 0x0000FFFF)) { 440 dsp_agp_available = SDL_FALSE;
431 441
432 GWorldPtr offscreen; 442 Gestalt ('sysv', &versionSystem);
433 OSStatus err; 443 if (0x00000860 < (versionSystem & 0x0000FFFF)) {
434 Rect bounds; 444
435 445 GWorldPtr offscreen;
436 SetRect (&bounds, 0, 0, 320, 240); 446 OSStatus err;
437 447 Rect bounds;
448
449 SetRect (&bounds, 0, 0, 320, 240);
450
438 #if useDistantHdwrMem && useLocalHdwrMem 451 #if useDistantHdwrMem && useLocalHdwrMem
439 err = NewGWorld (&offscreen, vformat->BitsPerPixel, &bounds, NULL, SDL_Display, useDistantHdwrMem | noNewDevice); 452 err =
440 if (err == noErr) { 453 NewGWorld (&offscreen, vformat->BitsPerPixel, &bounds, NULL,
441 dsp_vram_available = SDL_TRUE; 454 SDL_Display, useDistantHdwrMem | noNewDevice);
442 DisposeGWorld (offscreen); 455 if (err == noErr) {
443 } 456 dsp_vram_available = SDL_TRUE;
444 457 DisposeGWorld (offscreen);
445 err = NewGWorld (&offscreen, vformat->BitsPerPixel, &bounds, NULL, SDL_Display, useLocalHdwrMem | noNewDevice); 458 }
446 if (err == noErr) { 459
447 DisposeGWorld (offscreen); 460 err =
448 dsp_agp_available = SDL_TRUE; 461 NewGWorld (&offscreen, vformat->BitsPerPixel, &bounds, NULL,
449 } 462 SDL_Display, useLocalHdwrMem | noNewDevice);
450 #endif 463 if (err == noErr) {
451 } 464 DisposeGWorld (offscreen);
452 } 465 dsp_agp_available = SDL_TRUE;
453 466 }
454 static int DSp_GetMainDevice (_THIS, GDHandle *device) 467 #endif
455 { 468 }
456 469 }
470
471 static int
472 DSp_GetMainDevice (_THIS, GDHandle * device)
473 {
474
457 #if TARGET_API_MAC_OSX 475 #if TARGET_API_MAC_OSX
458 /* DSpUserSelectContext not available on OS X */ 476 /* DSpUserSelectContext not available on OS X */
459 *device = GetMainDevice(); 477 *device = GetMainDevice ();
478 return 0;
479 #else
480
481 DSpContextAttributes attrib;
482 DSpContextReference context;
483 DisplayIDType display_id;
484 GDHandle main_device;
485 GDHandle device_list;
486
487 device_list = GetDeviceList ();
488 main_device = GetMainDevice ();
489
490 /* Quick check to avoid slower method when only one display exists */
491 if ((**device_list).gdNextGD == NULL) {
492 *device = main_device;
460 return 0; 493 return 0;
494 }
495
496 SDL_memset (&attrib, 0, sizeof (DSpContextAttributes));
497
498 /* These attributes are hopefully supported on all devices... */
499 attrib.displayWidth = 640;
500 attrib.displayHeight = 480;
501 attrib.displayBestDepth = 8;
502 attrib.backBufferBestDepth = 8;
503 attrib.displayDepthMask = kDSpDepthMask_All;
504 attrib.backBufferDepthMask = kDSpDepthMask_All;
505 attrib.colorNeeds = kDSpColorNeeds_Require;
506 attrib.pageCount = 1;
507
508 if (noErr !=
509 DMGetDisplayIDByGDevice (main_device, &display_id, SDL_FALSE)) {
510 SDL_SetError
511 ("Display Manager couldn't associate GDevice with a Display ID");
512 return (-1);
513 }
514
515 /* Put up dialog on main display to select which display to use */
516 if (noErr != DSpUserSelectContext (&attrib, display_id, NULL, &context)) {
517 SDL_SetError ("DrawSprocket couldn't create a context");
518 return (-1);
519 }
520
521 if (noErr != DSpContext_GetDisplayID (context, &display_id)) {
522 SDL_SetError ("DrawSprocket couldn't get display ID");
523 return (-1);
524 }
525
526 if (noErr !=
527 DMGetGDeviceByDisplayID (display_id, &main_device, SDL_FALSE)) {
528 SDL_SetError
529 ("Display Manager couldn't associate Display ID with GDevice");
530 return (-1);
531 }
532
533 *device = main_device;
534 return (0);
535 #endif
536 }
537
538 static int
539 DSp_VideoInit (_THIS, SDL_PixelFormat * vformat)
540 {
541 NumVersion dsp_version = { 0x01, 0x00, 0x00, 0x00 };
542
543 #if UNIVERSAL_INTERFACES_VERSION > 0x0320
544 dsp_version = DSpGetVersion ();
545 #endif
546
547 if ((dsp_version.majorRev == 1 && dsp_version.minorAndBugRev < 0x73) ||
548 (dsp_version.majorRev < 1)) {
549
550 /* StandardAlert (kAlertStopAlert, "\pError!",
551 "\pI need DrawSprocket 1.7.3 or later!\n"
552 "You can find a newer version at http://www.apple.com/swupdates.",
553 NULL, NULL);
554 */
555 SDL_SetError
556 ("DrawSprocket version is too old. Need 1.7.3 or later.");
557 return (-1);
558 }
559
560 if (DSpStartup () != noErr) {
561 SDL_SetError ("DrawSprocket couldn't startup");
562 return (-1);
563 }
564
565 /* Start DSpintosh events */
566 Mac_InitEvents (this);
567
568 /* Get a handle to the main monitor, or choose one on multiple monitor setups */
569 if (DSp_GetMainDevice (this, &SDL_Display) < 0)
570 return (-1);
571
572 /* Determine pixel format */
573 vformat->BitsPerPixel = GetPixDepth ((**SDL_Display).gdPMap);
574 dsp_old_depth = vformat->BitsPerPixel;
575
576 switch (vformat->BitsPerPixel) {
577 case 16:
578 vformat->Rmask = 0x00007c00;
579 vformat->Gmask = 0x000003e0;
580 vformat->Bmask = 0x0000001f;
581 break;
582 default:
583 break;
584 }
585
586 if (DSp_CreatePalette (this) < 0) {
587 SDL_SetError ("Could not create palette");
588 return (-1);
589 }
590
591 /* Get a list of available fullscreen modes */
592 SDL_modelist = DSp_BuildModeList (SDL_Display,
593 &this->info.current_w,
594 &this->info.current_h);
595 if (SDL_modelist == NULL) {
596 SDL_SetError ("DrawSprocket could not build a mode list");
597 return (-1);
598 }
599
600 /* Check for VRAM and AGP GWorlds for HW Blitting */
601 DSp_IsHWAvailable (this, vformat);
602
603 this->info.wm_available = 0;
604
605 if (dsp_vram_available || dsp_agp_available) {
606
607 this->info.hw_available = SDL_TRUE;
608
609 this->CheckHWBlit = DSp_CheckHWBlit;
610 this->info.blit_hw = SDL_TRUE;
611
612 this->FillHWRect = DSp_FillHWRect;
613 this->info.blit_fill = SDL_TRUE;
614
615 #ifdef DSP_TRY_CC_AND_AA
616 this->SetHWColorKey = DSp_SetHWColorKey;
617 this->info.blit_hw_CC = SDL_TRUE;
618
619 this->SetHWAlpha = DSp_SetHWAlpha;
620 this->info.blit_hw_A = SDL_TRUE;
621 #endif
622
623 }
624
625 return (0);
626 }
627
628 static SDL_Rect **
629 DSp_ListModes (_THIS, SDL_PixelFormat * format, Uint32 flags)
630 {
631 static SDL_Rect *dsp_modes[16];
632 int i = 0, j = 0;
633
634 if (format->BitsPerPixel == 0)
635 return ((SDL_Rect **) NULL);
636
637 while (SDL_modelist[i] != NULL) {
638
639 if (SDL_modelist[i]->x & format->BitsPerPixel) {
640 dsp_modes[j] = SDL_modelist[i];
641 j++;
642 }
643 i++;
644 }
645
646 dsp_modes[j] = NULL;
647
648 return dsp_modes;
649 }
650
651 /* Various screen update functions available */
652 static void DSp_DirectUpdate (_THIS, int numrects, SDL_Rect * rects);
653
654 #if ! TARGET_API_MAC_OSX
655
656 static volatile unsigned int retrace_count = 0; /* -dw- need volatile because it updates asychronously */
657
658 Boolean
659 DSp_VBLProc (DSpContextReference context, void *ref_con)
660 {
661 retrace_count++;
662
663 return 1; /* Darrell, is this right? */
664 }
665
666 static void
667 DSp_SetHWError (OSStatus err, int is_agp)
668 {
669 char message[1024];
670 const char *fmt, *mem;
671
672 if (is_agp) {
673 mem = "AGP Memory";
674 } else {
675 mem = "VRAM";
676 }
677 switch (err) {
678 case memFullErr:
679 fmt = "Hardware surface possible but not enough %s available";
680 break;
681 case cDepthErr:
682 fmt = "Hardware surface possible but invalid color depth";
683 break;
684 default:
685 fmt = "Hardware surface could not be allocated in %s - unknown error";
686 break;
687 }
688 SDL_snprintf (message, SDL_arraysize (message), fmt, mem);
689 SDL_SetError (message);
690 }
691 #endif // TARGET_API_MAC_OSX
692
693 /* put up a dialog to verify display change */
694 static int
695 DSp_ConfirmSwitch ()
696 {
697
698 /* resource id's for dialog */
699 const int rDialog = 1002;
700 const int bCancel = 1;
701 const int bOK = 2;
702
703 DialogPtr dialog;
704 OSStatus err;
705 SInt32 response;
706 DialogItemIndex item = 0;
707 GrafPtr savePort;
708
709 GetPort (&savePort);
710
711 dialog = GetNewDialog (rDialog, NULL, (WindowPtr) - 1);
712 if (dialog == NULL)
713 return (0);
714
715 #if TARGET_API_MAC_CARBON
716 SetPort (GetDialogPort (dialog));
461 #else 717 #else
462 718 SetPort ((WindowPtr) dialog);
463 DSpContextAttributes attrib; 719 #endif
464 DSpContextReference context; 720
465 DisplayIDType display_id; 721 SetDialogDefaultItem (dialog, bCancel);
466 GDHandle main_device; 722 SetDialogCancelItem (dialog, bCancel);
467 GDHandle device_list; 723
468 724 SetEventMask (everyEvent);
469 device_list = GetDeviceList (); 725 FlushEvents (everyEvent, 0);
470 main_device = GetMainDevice (); 726
471 727 /* On MacOS 8.5 or later, we can make the dialog go away after 15 seconds */
472 /* Quick check to avoid slower method when only one display exists */ 728 /* This is good since it's possible user can't even see the dialog! */
473 if ( (**device_list).gdNextGD == NULL ) { 729 /* Requires linking to DialogsLib */
474 *device = main_device; 730 err = Gestalt (gestaltSystemVersion, &response);
475 return 0; 731 if (err == noErr && response >= 0x00000850) {
476 } 732 SetDialogTimeout (dialog, bCancel, 15);
477 733 }
478 SDL_memset (&attrib, 0, sizeof (DSpContextAttributes)); 734
479 735 do {
480 /* These attributes are hopefully supported on all devices...*/ 736
481 attrib.displayWidth = 640; 737 ModalDialog (NULL, &item);
482 attrib.displayHeight = 480; 738
483 attrib.displayBestDepth = 8; 739 }
484 attrib.backBufferBestDepth = 8; 740 while (item != bCancel && item != bOK && err != noErr);
485 attrib.displayDepthMask = kDSpDepthMask_All; 741
486 attrib.backBufferDepthMask = kDSpDepthMask_All; 742
487 attrib.colorNeeds = kDSpColorNeeds_Require; 743 DisposeDialog (dialog);
488 attrib.pageCount = 1; 744 SetPort (savePort);
489 745
490 if (noErr != DMGetDisplayIDByGDevice (main_device, &display_id, SDL_FALSE)) { 746 SetEventMask (everyEvent - autoKeyMask);
491 SDL_SetError ("Display Manager couldn't associate GDevice with a Display ID"); 747 FlushEvents (everyEvent, 0);
492 return (-1); 748
493 } 749 return (item - 1);
494 750 }
495 /* Put up dialog on main display to select which display to use */ 751
496 if (noErr != DSpUserSelectContext (&attrib, display_id, NULL, &context)) { 752 static void
497 SDL_SetError ("DrawSprocket couldn't create a context"); 753 DSp_UnsetVideoMode (_THIS, SDL_Surface * current)
498 return (-1); 754 {
499 } 755
500 756
501 if (noErr != DSpContext_GetDisplayID (context, &display_id)) { 757 if (current->flags & SDL_INTERNALOPENGL) {
502 SDL_SetError ("DrawSprocket couldn't get display ID"); 758 Mac_GL_Quit (this);
503 return (-1); 759 }
504 } 760
505 761 if (dsp_context != NULL) {
506 if (noErr != DMGetGDeviceByDisplayID (display_id, &main_device, SDL_FALSE)) { 762
507 SDL_SetError ("Display Manager couldn't associate Display ID with GDevice"); 763 GWorldPtr front;
508 return (-1); 764 DSpContext_GetFrontBuffer (dsp_context, &front);
509 } 765
510 766 if (front != dsp_back_buffer)
511 *device = main_device; 767 DisposeGWorld (dsp_back_buffer);
512 return (0); 768
513 #endif 769 if (current->hwdata)
514 } 770 SDL_free (current->hwdata);
515 771
516 static int DSp_VideoInit(_THIS, SDL_PixelFormat *vformat) 772 DSpContext_SetState (dsp_context, kDSpContextState_Inactive);
517 { 773 DSpContext_Release (dsp_context);
518 NumVersion dsp_version = { 0x01, 0x00, 0x00, 0x00 }; 774
519 775 dsp_context = NULL;
520 #if UNIVERSAL_INTERFACES_VERSION > 0x0320 776 }
521 dsp_version = DSpGetVersion (); 777
522 #endif
523
524 if ( (dsp_version.majorRev == 1 && dsp_version.minorAndBugRev < 0x73) ||
525 (dsp_version.majorRev < 1) ) {
526
527 /* StandardAlert (kAlertStopAlert, "\pError!",
528 "\pI need DrawSprocket 1.7.3 or later!\n"
529 "You can find a newer version at http://www.apple.com/swupdates.",
530 NULL, NULL);
531 */
532 SDL_SetError ("DrawSprocket version is too old. Need 1.7.3 or later.");
533 return (-1);
534 }
535
536 if ( DSpStartup () != noErr ) {
537 SDL_SetError ("DrawSprocket couldn't startup");
538 return(-1);
539 }
540
541 /* Start DSpintosh events */
542 Mac_InitEvents(this);
543
544 /* Get a handle to the main monitor, or choose one on multiple monitor setups */
545 if ( DSp_GetMainDevice(this, &SDL_Display) < 0)
546 return (-1);
547
548 /* Determine pixel format */
549 vformat->BitsPerPixel = GetPixDepth ( (**SDL_Display).gdPMap );
550 dsp_old_depth = vformat->BitsPerPixel;
551
552 switch (vformat->BitsPerPixel) {
553 case 16:
554 vformat->Rmask = 0x00007c00;
555 vformat->Gmask = 0x000003e0;
556 vformat->Bmask = 0x0000001f;
557 break;
558 default:
559 break;
560 }
561
562 if ( DSp_CreatePalette (this) < 0 ) {
563 SDL_SetError ("Could not create palette");
564 return (-1);
565 }
566
567 /* Get a list of available fullscreen modes */
568 SDL_modelist = DSp_BuildModeList (SDL_Display,
569 &this->info.current_w, &this->info.current_h);
570 if (SDL_modelist == NULL) {
571 SDL_SetError ("DrawSprocket could not build a mode list");
572 return (-1);
573 }
574
575 /* Check for VRAM and AGP GWorlds for HW Blitting */
576 DSp_IsHWAvailable (this, vformat);
577
578 this->info.wm_available = 0;
579
580 if (dsp_vram_available || dsp_agp_available) {
581
582 this->info.hw_available = SDL_TRUE;
583
584 this->CheckHWBlit = DSp_CheckHWBlit;
585 this->info.blit_hw = SDL_TRUE;
586
587 this->FillHWRect = DSp_FillHWRect;
588 this->info.blit_fill = SDL_TRUE;
589
590 #ifdef DSP_TRY_CC_AND_AA
591 this->SetHWColorKey = DSp_SetHWColorKey;
592 this->info.blit_hw_CC = SDL_TRUE;
593
594 this->SetHWAlpha = DSp_SetHWAlpha;
595 this->info.blit_hw_A = SDL_TRUE;
596 #endif
597
598 }
599
600 return(0);
601 }
602
603 static SDL_Rect **DSp_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
604 {
605 static SDL_Rect *dsp_modes[16];
606 int i = 0, j = 0;
607
608 if ( format->BitsPerPixel == 0 )
609 return ( (SDL_Rect**) NULL );
610
611 while (SDL_modelist[i] != NULL) {
612
613 if (SDL_modelist[i]->x & format->BitsPerPixel) {
614 dsp_modes[j] = SDL_modelist[i];
615 j++;
616 }
617 i++;
618 }
619
620 dsp_modes[j] = NULL;
621
622 return dsp_modes;
623 }
624
625 /* Various screen update functions available */
626 static void DSp_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
627
628 #if ! TARGET_API_MAC_OSX
629
630 static volatile unsigned int retrace_count = 0; /* -dw- need volatile because it updates asychronously */
631
632 Boolean DSp_VBLProc ( DSpContextReference context, void *ref_con )
633 {
634 retrace_count++;
635
636 return 1; /* Darrell, is this right? */
637 }
638
639 static void DSp_SetHWError (OSStatus err, int is_agp)
640 {
641 char message[1024];
642 const char *fmt, *mem;
643
644 if ( is_agp ) {
645 mem = "AGP Memory";
646 } else {
647 mem = "VRAM";
648 }
649 switch(err) {
650 case memFullErr:
651 fmt = "Hardware surface possible but not enough %s available";
652 break;
653 case cDepthErr:
654 fmt = "Hardware surface possible but invalid color depth";
655 break;
656 default:
657 fmt = "Hardware surface could not be allocated in %s - unknown error";
658 break;
659 }
660 SDL_snprintf(message, SDL_arraysize(message), fmt, mem);
661 SDL_SetError(message);
662 }
663 #endif // TARGET_API_MAC_OSX
664
665 /* put up a dialog to verify display change */
666 static int DSp_ConfirmSwitch () {
667
668 /* resource id's for dialog */
669 const int rDialog = 1002;
670 const int bCancel = 1;
671 const int bOK = 2;
672
673 DialogPtr dialog;
674 OSStatus err;
675 SInt32 response;
676 DialogItemIndex item = 0;
677 GrafPtr savePort;
678
679 GetPort (&savePort);
680
681 dialog = GetNewDialog (rDialog, NULL, (WindowPtr) -1);
682 if (dialog == NULL)
683 return (0);
684
685 #if TARGET_API_MAC_CARBON
686 SetPort (GetDialogPort(dialog));
687 #else
688 SetPort ((WindowPtr) dialog);
689 #endif
690
691 SetDialogDefaultItem (dialog, bCancel);
692 SetDialogCancelItem (dialog, bCancel);
693
694 SetEventMask (everyEvent);
695 FlushEvents (everyEvent, 0);
696
697 /* On MacOS 8.5 or later, we can make the dialog go away after 15 seconds */
698 /* This is good since it's possible user can't even see the dialog! */
699 /* Requires linking to DialogsLib */
700 err = Gestalt(gestaltSystemVersion,&response);
701 if (err == noErr && response >= 0x00000850) {
702 SetDialogTimeout(dialog, bCancel, 15);
703 }
704
705 do {
706
707 ModalDialog ( NULL, &item );
708
709 } while ( item != bCancel && item != bOK && err != noErr);
710
711
712 DisposeDialog (dialog);
713 SetPort (savePort);
714
715 SetEventMask(everyEvent - autoKeyMask);
716 FlushEvents(everyEvent, 0);
717
718 return (item - 1);
719 }
720
721 static void DSp_UnsetVideoMode(_THIS, SDL_Surface *current)
722 {
723
724
725 if ( current->flags & SDL_INTERNALOPENGL ) {
726 Mac_GL_Quit (this);
727 }
728
729 if (dsp_context != NULL) {
730
731 GWorldPtr front;
732 DSpContext_GetFrontBuffer (dsp_context, &front);
733
734 if (front != dsp_back_buffer)
735 DisposeGWorld (dsp_back_buffer);
736
737 if (current->hwdata)
738 SDL_free(current->hwdata);
739
740 DSpContext_SetState (dsp_context, kDSpContextState_Inactive );
741 DSpContext_Release (dsp_context);
742
743 dsp_context = NULL;
744 }
745
746 if (SDL_Window != NULL) { 778 if (SDL_Window != NULL) {
747 DisposeWindow (SDL_Window); 779 DisposeWindow (SDL_Window);
748 SDL_Window = NULL; 780 SDL_Window = NULL;
749 } 781 }
750 782
751 current->pixels = NULL; 783 current->pixels = NULL;
752 current->flags = 0; 784 current->flags = 0;
753 } 785 }
754 786
755 static SDL_Surface *DSp_SetVideoMode(_THIS, 787 static SDL_Surface *
756 SDL_Surface *current, int width, int height, int bpp, Uint32 flags) 788 DSp_SetVideoMode (_THIS,
757 { 789 SDL_Surface * current, int width, int height, int bpp,
758 790 Uint32 flags)
791 {
792
759 #if !TARGET_API_MAC_OSX 793 #if !TARGET_API_MAC_OSX
760 DisplayIDType display_id; 794 DisplayIDType display_id;
761 Fixed freq; 795 Fixed freq;
762 #endif 796 #endif
763 DSpContextAttributes attrib; 797 DSpContextAttributes attrib;
764 OSStatus err; 798 OSStatus err;
765 UInt32 rmask = 0, gmask = 0, bmask = 0; 799 UInt32 rmask = 0, gmask = 0, bmask = 0;
766 800
767 int page_count; 801 int page_count;
768 int double_buf; 802 int double_buf;
769 int hw_surface; 803 int hw_surface;
770 int use_dsp_back_buffer; 804 int use_dsp_back_buffer;
771 805
772 DSp_UnsetVideoMode (this, current); 806 DSp_UnsetVideoMode (this, current);
773 807
774 if (bpp != dsp_old_depth) 808 if (bpp != dsp_old_depth)
775 DSp_DestroyPalette (this); 809 DSp_DestroyPalette (this);
776 810
777 double_buf = (flags & SDL_DOUBLEBUF) != 0; 811 double_buf = (flags & SDL_DOUBLEBUF) != 0;
778 hw_surface = (flags & SDL_HWSURFACE) != 0; 812 hw_surface = (flags & SDL_HWSURFACE) != 0;
779 use_dsp_back_buffer = !dsp_vram_available || !hw_surface ; 813 use_dsp_back_buffer = !dsp_vram_available || !hw_surface;
780 814
781 current->flags |= SDL_FULLSCREEN; 815 current->flags |= SDL_FULLSCREEN;
782 816
783 rebuild: 817 rebuild:
784 818
785 if ( double_buf && use_dsp_back_buffer ) { 819 if (double_buf && use_dsp_back_buffer) {
786 page_count = 2; 820 page_count = 2;
787 } else { 821 } else {
788 page_count = 1; 822 page_count = 1;
789 } 823 }
790 824
791 SDL_memset (&attrib, 0, sizeof (DSpContextAttributes)); 825 SDL_memset (&attrib, 0, sizeof (DSpContextAttributes));
792 attrib.displayWidth = width; 826 attrib.displayWidth = width;
793 attrib.displayHeight = height; 827 attrib.displayHeight = height;
794 attrib.displayBestDepth = bpp; 828 attrib.displayBestDepth = bpp;
795 attrib.backBufferBestDepth = bpp; 829 attrib.backBufferBestDepth = bpp;
796 attrib.displayDepthMask = kDSpDepthMask_All; 830 attrib.displayDepthMask = kDSpDepthMask_All;
797 attrib.backBufferDepthMask = kDSpDepthMask_All; 831 attrib.backBufferDepthMask = kDSpDepthMask_All;
798 attrib.colorNeeds = kDSpColorNeeds_Require; 832 attrib.colorNeeds = kDSpColorNeeds_Require;
799 attrib.colorTable = 0; 833 attrib.colorTable = 0;
800 attrib.pageCount = page_count; 834 attrib.pageCount = page_count;
801 #if TARGET_API_MAC_OSX || UNIVERSAL_INTERFACES_VERSION == 0x0320 835 #if TARGET_API_MAC_OSX || UNIVERSAL_INTERFACES_VERSION == 0x0320
802 836
803 if ( DSpFindBestContext (&attrib, &dsp_context) != noErr ) { 837 if (DSpFindBestContext (&attrib, &dsp_context) != noErr) {
804 SDL_SetError ("DrawSprocket couldn't find a context"); 838 SDL_SetError ("DrawSprocket couldn't find a context");
839 return NULL;
840 }
841 #else
842 if (noErr !=
843 DMGetDisplayIDByGDevice (SDL_Display, &display_id, SDL_FALSE)) {
844 SDL_SetError
845 ("Display Manager couldn't associate GDevice with display_id");
846 return NULL;
847 }
848 if (DSpFindBestContextOnDisplayID (&attrib, &dsp_context, display_id) !=
849 noErr) {
850 SDL_SetError
851 ("DrawSprocket couldn't find a suitable context on given display");
852 return NULL;
853 }
854 #endif
855 if (DSpContext_Reserve (dsp_context, &attrib) != noErr) {
856 SDL_SetError
857 ("DrawSprocket couldn't get the needed resources to build the display");
858 return NULL;
859 }
860
861 if ((err =
862 DSpContext_SetState (dsp_context, kDSpContextState_Active)) != noErr)
863 {
864
865 if (err == kDSpConfirmSwitchWarning) {
866
867 if (!DSp_ConfirmSwitch ()) {
868
869 DSpContext_Release (dsp_context);
870 dsp_context = NULL;
871 SDL_SetError ("User cancelled display switch");
872 return NULL;
873 } else
874 /* Have to reactivate context. Why? */
875 DSpContext_SetState (dsp_context, kDSpContextState_Active);
876
877 } else {
878 SDL_SetError ("DrawSprocket couldn't activate the context");
805 return NULL; 879 return NULL;
806 } 880 }
807 881 }
808 #else 882
809 if ( noErr != DMGetDisplayIDByGDevice (SDL_Display, &display_id, SDL_FALSE) ) { 883
810 SDL_SetError ("Display Manager couldn't associate GDevice with display_id"); 884 if (bpp != dsp_old_depth) {
811 return NULL; 885
812 } 886 DSp_CreatePalette (this);
813 if ( DSpFindBestContextOnDisplayID(&attrib, &dsp_context, display_id) != noErr ) { 887
814 SDL_SetError ("DrawSprocket couldn't find a suitable context on given display"); 888 /* update format if display depth changed */
815 return NULL; 889 if (bpp == 16) {
816 } 890
817 891 rmask = 0x00007c00;
818 #endif 892 gmask = 0x000003e0;
819 if ( DSpContext_Reserve (dsp_context, &attrib) != noErr ) { 893 bmask = 0x0000001f;
820 SDL_SetError ("DrawSprocket couldn't get the needed resources to build the display"); 894 }
821 return NULL; 895 if (!SDL_ReallocFormat (current, bpp, rmask, gmask, bmask, 0)) {
822 } 896
823 897 SDL_SetError ("Could not reallocate video format.");
824 if ( (err = DSpContext_SetState (dsp_context, kDSpContextState_Active)) != noErr ) { 898 return (NULL);
825 899 }
826 if (err == kDSpConfirmSwitchWarning) { 900 }
827 901
828 if ( ! DSp_ConfirmSwitch () ) { 902 if (!double_buf) {
829 903
830 DSpContext_Release (dsp_context); 904 /* single-buffer context */
831 dsp_context = NULL; 905 DSpContext_GetFrontBuffer (dsp_context, &dsp_back_buffer);
832 SDL_SetError ("User cancelled display switch"); 906
833 return NULL; 907 current->hwdata =
834 } 908 (private_hwdata *) SDL_malloc (sizeof (private_hwdata));
835 else 909 if (current->hwdata == NULL) {
836 /* Have to reactivate context. Why? */ 910 SDL_OutOfMemory ();
837 DSpContext_SetState (dsp_context, kDSpContextState_Active); 911 return NULL;
838 912 }
839 } 913 current->hwdata->offscreen = dsp_back_buffer;
840 else { 914 current->flags |= SDL_HWSURFACE;
841 SDL_SetError ("DrawSprocket couldn't activate the context"); 915 this->UpdateRects = DSp_DirectUpdate;
842 return NULL; 916 } else if (use_dsp_back_buffer) {
843 } 917
844 } 918 DSpContext_GetBackBuffer (dsp_context, kDSpBufferKind_Normal,
845 919 &dsp_back_buffer);
846 920
847 if (bpp != dsp_old_depth) { 921 current->flags |= SDL_DOUBLEBUF | SDL_SWSURFACE; /* only front buffer is in VRAM */
848 922 this->UpdateRects = DSp_DSpUpdate;
849 DSp_CreatePalette (this); 923 } else if (DSp_NewHWSurface
850 924 (this, &dsp_back_buffer, bpp, width - 1, height - 1) == 0) {
851 /* update format if display depth changed */ 925
852 if (bpp == 16) { 926 current->hwdata =
853 927 (private_hwdata *) SDL_malloc (sizeof (private_hwdata));
854 rmask = 0x00007c00; 928 if (current->hwdata == NULL) {
855 gmask = 0x000003e0; 929 SDL_OutOfMemory ();
856 bmask = 0x0000001f; 930 return NULL;
857 } 931 }
858 if ( ! SDL_ReallocFormat (current, bpp, rmask, gmask, bmask, 0 ) ) { 932
859 933 SDL_memset (current->hwdata, 0, sizeof (private_hwdata));
860 SDL_SetError ("Could not reallocate video format."); 934 current->hwdata->offscreen = dsp_back_buffer;
861 return(NULL); 935 current->flags |= SDL_DOUBLEBUF | SDL_HWSURFACE;
862 } 936 this->UpdateRects = DSp_DirectUpdate; /* hardware doesn't do update rects, must be page-flipped */
863 } 937 } else {
864 938
865 if (!double_buf) { 939 DSpContext_Release (dsp_context);
866 940 use_dsp_back_buffer = SDL_TRUE;
867 /* single-buffer context */ 941 goto rebuild;
868 DSpContext_GetFrontBuffer (dsp_context, &dsp_back_buffer); 942 }
869 943
870 current->hwdata = (private_hwdata*) SDL_malloc (sizeof (private_hwdata)); 944 current->pitch = GetPortPixRowBytes (dsp_back_buffer) & 0x3FFF;
871 if (current ->hwdata == NULL) { 945 current->pixels = GetPixBaseAddr (GetPortPixMap (dsp_back_buffer));
872 SDL_OutOfMemory (); 946
873 return NULL; 947 current->w = width;
874 } 948 current->h = height;
875 current->hwdata->offscreen = dsp_back_buffer; 949
876 current->flags |= SDL_HWSURFACE; 950 #if ! TARGET_API_MAC_OSX
877 this->UpdateRects = DSp_DirectUpdate; 951
878 } 952 if (use_dsp_back_buffer) {
879 else if ( use_dsp_back_buffer ) { 953
880 954 DSpContext_GetMonitorFrequency (dsp_context, &freq);
881 DSpContext_GetBackBuffer (dsp_context, kDSpBufferKind_Normal, &dsp_back_buffer); 955 DSpContext_SetMaxFrameRate (dsp_context, freq >> 16);
882 956 }
883 current->flags |= SDL_DOUBLEBUF | SDL_SWSURFACE; /* only front buffer is in VRAM */ 957
884 this->UpdateRects = DSp_DSpUpdate; 958
885 } 959 if ((current->flags & SDL_HWSURFACE)
886 else if ( DSp_NewHWSurface(this, &dsp_back_buffer, bpp, width-1, height-1) == 0 ) { 960 || (current->flags & SDL_INTERNALOPENGL))
887 961 DSpContext_SetVBLProc (dsp_context, DSp_VBLProc, NULL);
888 current->hwdata = (private_hwdata*) SDL_malloc (sizeof (private_hwdata)); 962 #endif
889 if (current ->hwdata == NULL) { 963
890 SDL_OutOfMemory (); 964 if (bpp == 8)
891 return NULL; 965 current->flags |= SDL_HWPALETTE;
892 } 966
893 967 if (flags & SDL_INTERNALOPENGL) {
894 SDL_memset (current->hwdata, 0, sizeof (private_hwdata)); 968
895 current->hwdata->offscreen = dsp_back_buffer; 969 Rect rect;
896 current->flags |= SDL_DOUBLEBUF | SDL_HWSURFACE; 970 RGBColor rgb = { 0.0, 0.0, 0.0 };
897 this->UpdateRects = DSp_DirectUpdate; /* hardware doesn't do update rects, must be page-flipped */ 971 GrafPtr save_port;
898 } 972
899 else { 973 SetRect (&rect, 0, 0, width, height);
900 974 SDL_Window =
901 DSpContext_Release (dsp_context); 975 NewCWindow (nil, &((**SDL_Display).gdRect), "\p", SDL_TRUE,
902 use_dsp_back_buffer = SDL_TRUE; 976 plainDBox, (WindowPtr) - 1, SDL_FALSE, 0);
903 goto rebuild; 977
904 } 978 if (SDL_Window == NULL) {
905 979
906 current->pitch = GetPortPixRowBytes(dsp_back_buffer) & 0x3FFF; 980 SDL_SetError
907 current->pixels = GetPixBaseAddr(GetPortPixMap(dsp_back_buffer)); 981 ("DSp_SetVideoMode : OpenGL window could not be created.");
908 982 return NULL;
909 current->w = width; 983 }
910 current->h = height; 984
911 985 /* Set window color to black to avoid white flash */
912 #if ! TARGET_API_MAC_OSX 986 GetPort (&save_port);
913
914 if (use_dsp_back_buffer) {
915
916 DSpContext_GetMonitorFrequency (dsp_context, &freq);
917 DSpContext_SetMaxFrameRate (dsp_context, freq >> 16);
918 }
919
920
921 if ( (current->flags & SDL_HWSURFACE) || (current->flags & SDL_INTERNALOPENGL) )
922 DSpContext_SetVBLProc (dsp_context, DSp_VBLProc, NULL);
923 #endif
924
925 if (bpp == 8)
926 current->flags |= SDL_HWPALETTE;
927
928 if (flags & SDL_INTERNALOPENGL) {
929
930 Rect rect;
931 RGBColor rgb = { 0.0, 0.0, 0.0 };
932 GrafPtr save_port;
933
934 SetRect (&rect, 0, 0, width, height);
935 SDL_Window = NewCWindow(nil, &( (**SDL_Display).gdRect), "\p", SDL_TRUE, plainDBox, (WindowPtr)-1, SDL_FALSE, 0);
936
937 if (SDL_Window == NULL) {
938
939 SDL_SetError ("DSp_SetVideoMode : OpenGL window could not be created.");
940 return NULL;
941 }
942
943 /* Set window color to black to avoid white flash*/
944 GetPort (&save_port);
945 #if TARGET_API_MAC_CARBON 987 #if TARGET_API_MAC_CARBON
946 SetPort (GetWindowPort(SDL_Window)); 988 SetPort (GetWindowPort (SDL_Window));
947 #else 989 #else
948 SetPort (SDL_Window); 990 SetPort (SDL_Window);
949 #endif 991 #endif
950 RGBForeColor (&rgb); 992 RGBForeColor (&rgb);
951 PaintRect (&rect); 993 PaintRect (&rect);
952 SetPort (save_port); 994 SetPort (save_port);
953 995
954 SetPortWindowPort (SDL_Window); 996 SetPortWindowPort (SDL_Window);
955 SelectWindow (SDL_Window); 997 SelectWindow (SDL_Window);
956 998
957 if ( Mac_GL_Init (this) < 0 ) { 999 if (Mac_GL_Init (this) < 0) {
958 1000
959 SDL_SetError ("DSp_SetVideoMode : could not create OpenGL context."); 1001 SDL_SetError
960 return NULL; 1002 ("DSp_SetVideoMode : could not create OpenGL context.");
961 } 1003 return NULL;
962 1004 }
963 current->flags |= SDL_INTERNALOPENGL; 1005
964 } 1006 current->flags |= SDL_INTERNALOPENGL;
965 1007 }
966 return current; 1008
1009 return current;
967 } 1010 }
968 1011
969 #ifdef DSP_TRY_CC_AND_AA 1012 #ifdef DSP_TRY_CC_AND_AA
970 1013
971 static int DSp_MakeHWMask (_THIS, SDL_Surface *surface) 1014 static int
1015 DSp_MakeHWMask (_THIS, SDL_Surface * surface)
972 { 1016 {
973 GDHandle save_device; 1017 GDHandle save_device;
974 CGrafPtr save_port; 1018 CGrafPtr save_port;
975 GWorldPtr temp; 1019 GWorldPtr temp;
976 RGBColor black = { 0, 0, 0 }; 1020 RGBColor black = { 0, 0, 0 };
977 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF }; 1021 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF };
978 Rect rect; 1022 Rect rect;
979 1023
980 Uint32 depth = GetPixDepth ( GetGDevPixMap (SDL_Display) ); 1024 Uint32 depth = GetPixDepth (GetGDevPixMap (SDL_Display));
981 1025
982 SetRect (&rect, 0, 0, surface->w, surface->h); 1026 SetRect (&rect, 0, 0, surface->w, surface->h);
983 1027
984 if ( noErr != NewGWorld (&(surface->hwdata->mask), depth, &rect, 0, SDL_Display, 0 ) < 0 ) { 1028 if (noErr !=
985 1029 NewGWorld (&(surface->hwdata->mask), depth, &rect, 0, SDL_Display,
1030 0) < 0) {
1031
986 SDL_OutOfMemory (); 1032 SDL_OutOfMemory ();
987 return (-1); 1033 return (-1);
988 } 1034 }
989 1035
990 if ( noErr != NewGWorld (&temp, depth, &rect, 0 , SDL_Display, 0 ) ) { 1036 if (noErr != NewGWorld (&temp, depth, &rect, 0, SDL_Display, 0)) {
991 1037
992 SDL_OutOfMemory (); 1038 SDL_OutOfMemory ();
993 return (-1); 1039 return (-1);
994 } 1040 }
995 1041
996 1042
997 GetGWorld (&save_port, &save_device); 1043 GetGWorld (&save_port, &save_device);
998 SetGWorld (surface->hwdata->mask, SDL_Display); 1044 SetGWorld (surface->hwdata->mask, SDL_Display);
999 1045
1000 RGBForeColor (&white); 1046 RGBForeColor (&white);
1001 PaintRect (&rect); 1047 PaintRect (&rect);
1002 1048
1003 RGBBackColor (&(surface->hwdata->trans)); 1049 RGBBackColor (&(surface->hwdata->trans));
1004 1050
1005 CopyBits ( GetPortBitMapForCopyBits(surface->hwdata->offscreen), 1051 CopyBits (GetPortBitMapForCopyBits (surface->hwdata->offscreen),
1006 GetPortBitMapForCopyBits(surface->hwdata->mask), 1052 GetPortBitMapForCopyBits (surface->hwdata->mask),
1007 &rect, &rect, transparent, NULL ); 1053 &rect, &rect, transparent, NULL);
1008 1054
1009 SetGWorld (surface->hwdata->mask, SDL_Display); 1055 SetGWorld (surface->hwdata->mask, SDL_Display);
1010 SetGWorld (save_port, save_device); 1056 SetGWorld (save_port, save_device);
1011 return (0); 1057 return (0);
1012 } 1058 }
1013 1059
1014 static int DSp_SetHWAlpha(_THIS, SDL_Surface *surface, UInt8 alpha) 1060 static int
1015 { 1061 DSp_SetHWAlpha (_THIS, SDL_Surface * surface, UInt8 alpha)
1016 surface->hwdata->alpha.red = (alpha / 255.0) * 65535; 1062 {
1017 surface->hwdata->alpha.blue = (alpha / 255.0) * 65535; 1063 surface->hwdata->alpha.red = (alpha / 255.0) * 65535;
1064 surface->hwdata->alpha.blue = (alpha / 255.0) * 65535;
1018 surface->hwdata->alpha.green = (alpha / 255.0) * 65535; 1065 surface->hwdata->alpha.green = (alpha / 255.0) * 65535;
1019 1066
1020 surface->flags |= SDL_SRCALPHA; 1067 surface->flags |= SDL_SRCALPHA;
1021 1068
1022 if (surface->flags & SDL_SRCCOLORKEY) { 1069 if (surface->flags & SDL_SRCCOLORKEY) {
1023 return(DSp_MakeHWMask (this, surface)); 1070 return (DSp_MakeHWMask (this, surface));
1024 } 1071 }
1025 return(0); 1072 return (0);
1026 } 1073 }
1027 1074
1028 static int DSp_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key) 1075 static int
1076 DSp_SetHWColorKey (_THIS, SDL_Surface * surface, Uint32 key)
1029 { 1077 {
1030 CGrafPtr save_port; 1078 CGrafPtr save_port;
1031 GDHandle save_device; 1079 GDHandle save_device;
1032 1080
1033 GetGWorld (&save_port, &save_device); 1081 GetGWorld (&save_port, &save_device);
1034 SetGWorld (surface->hwdata->offscreen, NULL); 1082 SetGWorld (surface->hwdata->offscreen, NULL);
1035 1083
1036 Index2Color (key, &(surface->hwdata->trans)); 1084 Index2Color (key, &(surface->hwdata->trans));
1037 surface->flags |= SDL_SRCCOLORKEY; 1085 surface->flags |= SDL_SRCCOLORKEY;
1038 1086
1039 SetGWorld (save_port, save_device); 1087 SetGWorld (save_port, save_device);
1040 1088
1041 if ( surface->flags & SDL_SRCALPHA ) { 1089 if (surface->flags & SDL_SRCALPHA) {
1042 return(DSp_MakeHWMask (this, surface)); 1090 return (DSp_MakeHWMask (this, surface));
1043 } 1091 }
1044 return(0); 1092 return (0);
1045 } 1093 }
1046 1094
1047 #endif /* DSP_TRY_CC_AND_AA */ 1095 #endif /* DSP_TRY_CC_AND_AA */
1048 1096
1049 static int DSp_NewHWSurface(_THIS, CGrafPtr *port, int depth, int width, int height) { 1097 static int
1050 1098 DSp_NewHWSurface (_THIS, CGrafPtr * port, int depth, int width, int height)
1051 OSStatus err; 1099 {
1052 Rect bounds; 1100
1053 1101 OSStatus err;
1054 SetRect (&bounds, 0, 0, width, height); 1102 Rect bounds;
1055 1103
1056 #if useDistantHdwrMem && useLocalHdwrMem 1104 SetRect (&bounds, 0, 0, width, height);
1105
1106 #if useDistantHdwrMem && useLocalHdwrMem
1057 if (dsp_vram_available) { 1107 if (dsp_vram_available) {
1058 /* try VRAM */ 1108 /* try VRAM */
1059 err = NewGWorld (port, depth, &bounds, 0 , SDL_Display, useDistantHdwrMem | noNewDevice ); 1109 err =
1060 if (err != noErr) 1110 NewGWorld (port, depth, &bounds, 0, SDL_Display,
1061 DSp_SetHWError (err, SDL_FALSE); 1111 useDistantHdwrMem | noNewDevice);
1062 else 1112 if (err != noErr)
1063 return (0); 1113 DSp_SetHWError (err, SDL_FALSE);
1064 } 1114 else
1065 1115 return (0);
1116 }
1117
1066 if (dsp_agp_available) { 1118 if (dsp_agp_available) {
1067 /* try AGP */ 1119 /* try AGP */
1068 err = NewGWorld (port, depth, &bounds, 0 , SDL_Display, useLocalHdwrMem | noNewDevice ); 1120 err =
1069 1121 NewGWorld (port, depth, &bounds, 0, SDL_Display,
1070 if (err != noErr) 1122 useLocalHdwrMem | noNewDevice);
1071 DSp_SetHWError (err, SDL_TRUE); 1123
1072 else 1124 if (err != noErr)
1073 return (0); 1125 DSp_SetHWError (err, SDL_TRUE);
1074 } 1126 else
1075 #endif 1127 return (0);
1076 1128 }
1077 return (-1); 1129 #endif
1078 } 1130
1079 1131 return (-1);
1080 static int DSp_AllocHWSurface(_THIS, SDL_Surface *surface) 1132 }
1081 { 1133
1082 GWorldPtr temp; 1134 static int
1083 1135 DSp_AllocHWSurface (_THIS, SDL_Surface * surface)
1084 if ( DSp_NewHWSurface (this, &temp, surface->format->BitsPerPixel, surface->w, surface->h) < 0 ) 1136 {
1085 return (-1); 1137 GWorldPtr temp;
1086 1138
1087 surface->hwdata = (private_hwdata*) SDL_malloc (sizeof (private_hwdata)); 1139 if (DSp_NewHWSurface
1088 if (surface->hwdata == NULL) { 1140 (this, &temp, surface->format->BitsPerPixel, surface->w,
1089 SDL_OutOfMemory (); 1141 surface->h) < 0)
1090 return -1; 1142 return (-1);
1091 } 1143
1092 1144 surface->hwdata = (private_hwdata *) SDL_malloc (sizeof (private_hwdata));
1093 SDL_memset (surface->hwdata, 0, sizeof(private_hwdata)); 1145 if (surface->hwdata == NULL) {
1094 surface->hwdata->offscreen = temp; 1146 SDL_OutOfMemory ();
1095 surface->pitch = GetPixRowBytes (GetPortPixMap (temp)) & 0x3FFF; 1147 return -1;
1096 surface->pixels = GetPixBaseAddr (GetPortPixMap (temp)); 1148 }
1097 surface->flags |= SDL_HWSURFACE; 1149
1098 #ifdef DSP_TRY_CC_AND_AA 1150 SDL_memset (surface->hwdata, 0, sizeof (private_hwdata));
1099 surface->flags |= SDL_HWACCEL; 1151 surface->hwdata->offscreen = temp;
1100 #endif 1152 surface->pitch = GetPixRowBytes (GetPortPixMap (temp)) & 0x3FFF;
1101 return 0; 1153 surface->pixels = GetPixBaseAddr (GetPortPixMap (temp));
1102 } 1154 surface->flags |= SDL_HWSURFACE;
1103 1155 #ifdef DSP_TRY_CC_AND_AA
1104 static void DSp_FreeHWSurface(_THIS, SDL_Surface *surface) 1156 surface->flags |= SDL_HWACCEL;
1105 { 1157 #endif
1106 if (surface->hwdata->offscreen != NULL) 1158 return 0;
1107 DisposeGWorld (surface->hwdata->offscreen); 1159 }
1108 SDL_free(surface->hwdata); 1160
1161 static void
1162 DSp_FreeHWSurface (_THIS, SDL_Surface * surface)
1163 {
1164 if (surface->hwdata->offscreen != NULL)
1165 DisposeGWorld (surface->hwdata->offscreen);
1166 SDL_free (surface->hwdata);
1109 1167
1110 surface->pixels = NULL; 1168 surface->pixels = NULL;
1111 } 1169 }
1112 1170
1113 static int DSp_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dest) 1171 static int
1114 { 1172 DSp_CheckHWBlit (_THIS, SDL_Surface * src, SDL_Surface * dest)
1115 int accelerated; 1173 {
1116 1174 int accelerated;
1117 /* Set initial acceleration on */ 1175
1118 src->flags |= SDL_HWACCEL; 1176 /* Set initial acceleration on */
1119 1177 src->flags |= SDL_HWACCEL;
1120 /* Set the surface attributes */ 1178
1121 if ( (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { 1179 /* Set the surface attributes */
1122 if ( ! this->info.blit_hw_A ) { 1180 if ((src->flags & SDL_SRCALPHA) == SDL_SRCALPHA) {
1123 src->flags &= ~SDL_HWACCEL; 1181 if (!this->info.blit_hw_A) {
1124 } 1182 src->flags &= ~SDL_HWACCEL;
1125 } 1183 }
1126 if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) { 1184 }
1127 if ( ! this->info.blit_hw_CC ) { 1185 if ((src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY) {
1128 src->flags &= ~SDL_HWACCEL; 1186 if (!this->info.blit_hw_CC) {
1129 } 1187 src->flags &= ~SDL_HWACCEL;
1130 } 1188 }
1131 1189 }
1132 /* Check to see if final surface blit is accelerated */ 1190
1133 accelerated = !!(src->flags & SDL_HWACCEL); 1191 /* Check to see if final surface blit is accelerated */
1134 if ( accelerated ) { 1192 accelerated = !!(src->flags & SDL_HWACCEL);
1135 src->map->hw_blit = DSp_HWAccelBlit; 1193 if (accelerated) {
1136 } 1194 src->map->hw_blit = DSp_HWAccelBlit;
1137 return(accelerated); 1195 }
1138 } 1196 return (accelerated);
1139 1197 }
1140 static int DSp_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect, 1198
1141 SDL_Surface *dst, SDL_Rect *dstrect) 1199 static int
1142 { 1200 DSp_HWAccelBlit (SDL_Surface * src, SDL_Rect * srcrect,
1143 CGrafPtr save_port; 1201 SDL_Surface * dst, SDL_Rect * dstrect)
1144 GDHandle save_device; 1202 {
1145 Rect src_rect, dst_rect; 1203 CGrafPtr save_port;
1204 GDHandle save_device;
1205 Rect src_rect, dst_rect;
1146 RGBColor black = { 0, 0, 0 }; 1206 RGBColor black = { 0, 0, 0 };
1147 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF }; 1207 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF };
1148 1208
1149 #ifdef DSP_TRY_CC_AND_AA
1150 UInt32 mode;
1151 #endif
1152
1153 SetRect (&src_rect, srcrect->x, srcrect->y, srcrect->x + srcrect->w, srcrect->y + srcrect->h);
1154 SetRect (&dst_rect, dstrect->x, dstrect->y, dstrect->x + dstrect->w, dstrect->y + dstrect->h);
1155
1156 GetGWorld (&save_port, &save_device);
1157 SetGWorld (dst->hwdata->offscreen, NULL);
1158
1159 RGBForeColor (&black);
1160 RGBBackColor (&white);
1161
1162 #ifdef DSP_TRY_CC_AND_AA 1209 #ifdef DSP_TRY_CC_AND_AA
1163 1210 UInt32 mode;
1164 if ( (src->flags & SDL_SRCCOLORKEY) && 1211 #endif
1165 (src->flags & SDL_SRCALPHA) ) { 1212
1166 1213 SetRect (&src_rect, srcrect->x, srcrect->y, srcrect->x + srcrect->w,
1167 OpColor (&(src->hwdata->alpha)); 1214 srcrect->y + srcrect->h);
1168 1215 SetRect (&dst_rect, dstrect->x, dstrect->y, dstrect->x + dstrect->w,
1169 CopyDeepMask ( GetPortBitMapForCopyBits(src->hwdata->offscreen), 1216 dstrect->y + dstrect->h);
1170 GetPortBitMapForCopyBits(src->hwdata->mask), 1217
1171 GetPortBitMapForCopyBits(dst->hwdata->offscreen), 1218 GetGWorld (&save_port, &save_device);
1172 &src_rect, &src_rect, &dst_rect, 1219 SetGWorld (dst->hwdata->offscreen, NULL);
1173 blend, 1220
1174 NULL ); 1221 RGBForeColor (&black);
1175 } 1222 RGBBackColor (&white);
1176 else { 1223
1177 1224 #ifdef DSP_TRY_CC_AND_AA
1178 if ( src->flags & SDL_SRCCOLORKEY) { 1225
1179 RGBBackColor (&(src->hwdata->trans) ); 1226 if ((src->flags & SDL_SRCCOLORKEY) && (src->flags & SDL_SRCALPHA)) {
1180 mode = transparent; 1227
1181 } 1228 OpColor (&(src->hwdata->alpha));
1182 else if (src->flags & SDL_SRCALPHA) { 1229
1183 1230 CopyDeepMask (GetPortBitMapForCopyBits (src->hwdata->offscreen),
1184 OpColor (&(src->hwdata->alpha)); 1231 GetPortBitMapForCopyBits (src->hwdata->mask),
1185 mode = blend; 1232 GetPortBitMapForCopyBits (dst->hwdata->offscreen),
1186 } 1233 &src_rect, &src_rect, &dst_rect, blend, NULL);
1187 else { 1234 } else {
1188 1235
1189 mode = srcCopy; 1236 if (src->flags & SDL_SRCCOLORKEY) {
1190 } 1237 RGBBackColor (&(src->hwdata->trans));
1191 1238 mode = transparent;
1192 CopyBits ( GetPortBitMapForCopyBits(src->hwdata->offscreen), 1239 } else if (src->flags & SDL_SRCALPHA) {
1193 GetPortBitMapForCopyBits(dst->hwdata->offscreen), 1240
1194 &src_rect, &dst_rect, mode, NULL ); 1241 OpColor (&(src->hwdata->alpha));
1195 } 1242 mode = blend;
1243 } else {
1244
1245 mode = srcCopy;
1246 }
1247
1248 CopyBits (GetPortBitMapForCopyBits (src->hwdata->offscreen),
1249 GetPortBitMapForCopyBits (dst->hwdata->offscreen),
1250 &src_rect, &dst_rect, mode, NULL);
1251 }
1196 #else 1252 #else
1197 1253
1198 CopyBits ( &(((GrafPtr)(src->hwdata->offscreen))->portBits), 1254 CopyBits (&(((GrafPtr) (src->hwdata->offscreen))->portBits),
1199 &(((GrafPtr)(dst->hwdata->offscreen))->portBits), 1255 &(((GrafPtr) (dst->hwdata->offscreen))->portBits),
1200 &src_rect, &dst_rect, srcCopy, NULL ); 1256 &src_rect, &dst_rect, srcCopy, NULL);
1201 1257
1202 #endif /* DSP_TRY_CC_AND_AA */ 1258 #endif /* DSP_TRY_CC_AND_AA */
1203 1259
1204 SetGWorld (save_port, save_device); 1260 SetGWorld (save_port, save_device);
1205 1261
1206 return(0); 1262 return (0);
1207 } 1263 }
1208 1264
1209 static int DSp_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color) 1265 static int
1210 { 1266 DSp_FillHWRect (_THIS, SDL_Surface * dst, SDL_Rect * rect, Uint32 color)
1211 CGrafPtr save_port; 1267 {
1212 GDHandle save_device; 1268 CGrafPtr save_port;
1213 Rect fill_rect; 1269 GDHandle save_device;
1214 RGBColor rgb; 1270 Rect fill_rect;
1215 1271 RGBColor rgb;
1216 SetRect (&fill_rect, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); 1272
1217 1273 SetRect (&fill_rect, rect->x, rect->y, rect->x + rect->w,
1218 GetGWorld (&save_port, &save_device); 1274 rect->y + rect->h);
1219 SetGWorld (dst->hwdata->offscreen, NULL); 1275
1276 GetGWorld (&save_port, &save_device);
1277 SetGWorld (dst->hwdata->offscreen, NULL);
1220 1278
1221 Index2Color (color, &rgb); 1279 Index2Color (color, &rgb);
1222 1280
1223 RGBForeColor (&rgb); 1281 RGBForeColor (&rgb);
1224 PaintRect (&fill_rect); 1282 PaintRect (&fill_rect);
1225 1283
1226 SetGWorld (save_port, save_device); 1284 SetGWorld (save_port, save_device);
1227 1285
1228 return(0); 1286 return (0);
1229 } 1287 }
1230 1288
1231 static int DSp_FlipHWSurface(_THIS, SDL_Surface *surface) 1289 static int
1232 { 1290 DSp_FlipHWSurface (_THIS, SDL_Surface * surface)
1233 if ( (surface->flags & SDL_HWSURFACE) ) { 1291 {
1234 CGrafPtr dsp_front_buffer, save_port; 1292 if ((surface->flags & SDL_HWSURFACE)) {
1235 Rect rect; 1293 CGrafPtr dsp_front_buffer, save_port;
1236 1294 Rect rect;
1237 #if ! TARGET_API_MAC_OSX 1295
1238 unsigned int old_count; 1296 #if ! TARGET_API_MAC_OSX
1239 #endif 1297 unsigned int old_count;
1240 1298 #endif
1241 /* pseudo page flipping for VRAM back buffer*/ 1299
1242 DSpContext_GetFrontBuffer (dsp_context, &dsp_front_buffer); 1300 /* pseudo page flipping for VRAM back buffer */
1243 SetRect (&rect, 0, 0, surface->w-1, surface->h-1); 1301 DSpContext_GetFrontBuffer (dsp_context, &dsp_front_buffer);
1244 1302 SetRect (&rect, 0, 0, surface->w - 1, surface->h - 1);
1245 GetPort ((GrafPtr *)&save_port); 1303
1246 SetPort ((GrafPtr)dsp_front_buffer); 1304 GetPort ((GrafPtr *) & save_port);
1247 1305 SetPort ((GrafPtr) dsp_front_buffer);
1248 /* wait for retrace */ 1306
1249 /* I have tried doing the swap in interrupt routine (VBL Proc) to do */ 1307 /* wait for retrace */
1250 /* it asynchronously, but apparently CopyBits isn't interrupt safe */ 1308 /* I have tried doing the swap in interrupt routine (VBL Proc) to do */
1251 1309 /* it asynchronously, but apparently CopyBits isn't interrupt safe */
1252 #if ! TARGET_API_MAC_OSX 1310
1253 #ifndef DSP_NO_SYNC_VBL 1311 #if ! TARGET_API_MAC_OSX
1254 old_count = retrace_count; 1312 #ifndef DSP_NO_SYNC_VBL
1255 while (old_count == retrace_count) 1313 old_count = retrace_count;
1256 ; 1314 while (old_count == retrace_count);
1257 #endif 1315 #endif
1258 #endif 1316 #endif
1259 1317
1260 CopyBits ( GetPortBitMapForCopyBits(dsp_back_buffer), 1318 CopyBits (GetPortBitMapForCopyBits (dsp_back_buffer),
1261 GetPortBitMapForCopyBits(dsp_front_buffer), 1319 GetPortBitMapForCopyBits (dsp_front_buffer),
1262 &rect, &rect, srcCopy, NULL ); 1320 &rect, &rect, srcCopy, NULL);
1263 1321
1264 SetPort ((GrafPtr)save_port); 1322 SetPort ((GrafPtr) save_port);
1265 1323
1266 } else { 1324 } else {
1267 /* not really page flipping at all: DSp just blits the dirty rectangles from DSp_UpdateRects */ 1325 /* not really page flipping at all: DSp just blits the dirty rectangles from DSp_UpdateRects */
1268 Boolean busy_flag; 1326 Boolean busy_flag;
1269 DSpContext_SwapBuffers (dsp_context, NULL, &busy_flag); /* this waits for VBL */ 1327 DSpContext_SwapBuffers (dsp_context, NULL, &busy_flag); /* this waits for VBL */
1270 DSpContext_GetBackBuffer (dsp_context, kDSpBufferKind_Normal, &dsp_back_buffer); 1328 DSpContext_GetBackBuffer (dsp_context, kDSpBufferKind_Normal,
1271 surface->pixels = GetPixBaseAddr( GetPortPixMap(dsp_back_buffer) ); 1329 &dsp_back_buffer);
1272 } 1330 surface->pixels = GetPixBaseAddr (GetPortPixMap (dsp_back_buffer));
1273 return(0); 1331 }
1274 } 1332 return (0);
1275 1333 }
1276 static int DSp_LockHWSurface(_THIS, SDL_Surface *surface) 1334
1277 { 1335 static int
1278 if ( LockPixels (GetGWorldPixMap (surface->hwdata->offscreen)) ) 1336 DSp_LockHWSurface (_THIS, SDL_Surface * surface)
1279 return 0; 1337 {
1280 else 1338 if (LockPixels (GetGWorldPixMap (surface->hwdata->offscreen)))
1281 return -1; 1339 return 0;
1282 } 1340 else
1283 1341 return -1;
1284 static void DSp_UnlockHWSurface(_THIS, SDL_Surface *surface) 1342 }
1285 { 1343
1286 UnlockPixels (GetGWorldPixMap (surface->hwdata->offscreen)); 1344 static void
1287 } 1345 DSp_UnlockHWSurface (_THIS, SDL_Surface * surface)
1288 1346 {
1289 static void DSp_DirectUpdate(_THIS, int numrects, SDL_Rect *sdl_rects) 1347 UnlockPixels (GetGWorldPixMap (surface->hwdata->offscreen));
1290 { 1348 }
1291 return; 1349
1292 } 1350 static void
1293 1351 DSp_DirectUpdate (_THIS, int numrects, SDL_Rect * sdl_rects)
1294 static void DSp_DSpUpdate(_THIS, int numrects, SDL_Rect *sdl_rects) 1352 {
1295 { 1353 return;
1296 #if ! TARGET_API_MAC_OSX /* Unsupported DSp in here */ 1354 }
1297 int i; 1355
1298 Rect rect; 1356 static void
1299 1357 DSp_DSpUpdate (_THIS, int numrects, SDL_Rect * sdl_rects)
1300 for (i = 0; i < numrects; i++) { 1358 {
1301 1359 #if ! TARGET_API_MAC_OSX /* Unsupported DSp in here */
1302 rect.top = sdl_rects[i].y; 1360 int i;
1303 rect.left = sdl_rects[i].x; 1361 Rect rect;
1304 rect.bottom = sdl_rects[i].h + sdl_rects[i].y; 1362
1305 rect.right = sdl_rects[i].w + sdl_rects[i].x; 1363 for (i = 0; i < numrects; i++) {
1306 1364
1307 DSpContext_InvalBackBufferRect (dsp_context, &rect); 1365 rect.top = sdl_rects[i].y;
1308 } 1366 rect.left = sdl_rects[i].x;
1309 #endif 1367 rect.bottom = sdl_rects[i].h + sdl_rects[i].y;
1310 } 1368 rect.right = sdl_rects[i].w + sdl_rects[i].x;
1311 1369
1312 static int DSp_CreatePalette(_THIS) { 1370 DSpContext_InvalBackBufferRect (dsp_context, &rect);
1313 1371 }
1314 1372 #endif
1315 /* Create our palette */ 1373 }
1316 SDL_CTab = (CTabHandle)NewHandle(sizeof(ColorSpec)*256 + 8); 1374
1317 if ( SDL_CTab == nil ) { 1375 static int
1318 SDL_OutOfMemory(); 1376 DSp_CreatePalette (_THIS)
1319 return(-1); 1377 {
1320 } 1378
1321 (**SDL_CTab).ctSeed = GetCTSeed(); 1379
1322 (**SDL_CTab).ctFlags = 0; 1380 /* Create our palette */
1323 (**SDL_CTab).ctSize = 255; 1381 SDL_CTab = (CTabHandle) NewHandle (sizeof (ColorSpec) * 256 + 8);
1324 CTabChanged(SDL_CTab); 1382 if (SDL_CTab == nil) {
1325 SDL_CPal = NewPalette(256, SDL_CTab, pmExplicit+pmTolerant, 0); 1383 SDL_OutOfMemory ();
1326 1384 return (-1);
1327 return 0; 1385 }
1328 } 1386 (**SDL_CTab).ctSeed = GetCTSeed ();
1329 1387 (**SDL_CTab).ctFlags = 0;
1330 static int DSp_DestroyPalette(_THIS) { 1388 (**SDL_CTab).ctSize = 255;
1331 1389 CTabChanged (SDL_CTab);
1332 /* Free palette and restore original one */ 1390 SDL_CPal = NewPalette (256, SDL_CTab, pmExplicit + pmTolerant, 0);
1333 if ( SDL_CTab != nil ) { 1391
1334 DisposeHandle((Handle)SDL_CTab); 1392 return 0;
1335 SDL_CTab = nil; 1393 }
1336 } 1394
1337 if ( SDL_CPal != nil ) { 1395 static int
1338 DisposePalette(SDL_CPal); 1396 DSp_DestroyPalette (_THIS)
1339 SDL_CPal = nil; 1397 {
1340 } 1398
1341 RestoreDeviceClut(SDL_Display); 1399 /* Free palette and restore original one */
1342 1400 if (SDL_CTab != nil) {
1343 return (0); 1401 DisposeHandle ((Handle) SDL_CTab);
1344 } 1402 SDL_CTab = nil;
1345 1403 }
1346 static int DSp_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) 1404 if (SDL_CPal != nil) {
1347 { 1405 DisposePalette (SDL_CPal);
1348 CTabHandle cTab; 1406 SDL_CPal = nil;
1349 1407 }
1350 int i; 1408 RestoreDeviceClut (SDL_Display);
1351 1409
1352 cTab = SDL_CTab; 1410 return (0);
1353 1411 }
1354 /* Verify the range of colors */ 1412
1355 if ( (firstcolor+ncolors) > ((**cTab).ctSize+1) ) { 1413 static int
1356 return(0); 1414 DSp_SetColors (_THIS, int firstcolor, int ncolors, SDL_Color * colors)
1357 } 1415 {
1358 1416 CTabHandle cTab;
1359 /* Set the screen palette and update the display */ 1417
1360 for(i = 0; i < ncolors; i++) { 1418 int i;
1361 int j = firstcolor + i; 1419
1362 (**cTab).ctTable[j].value = j; 1420 cTab = SDL_CTab;
1363 (**cTab).ctTable[j].rgb.red = colors[i].r << 8 | colors[i].r; 1421
1364 (**cTab).ctTable[j].rgb.green = colors[i].g << 8 | colors[i].g; 1422 /* Verify the range of colors */
1365 (**cTab).ctTable[j].rgb.blue = colors[i].b << 8 | colors[i].b; 1423 if ((firstcolor + ncolors) > ((**cTab).ctSize + 1)) {
1366 } 1424 return (0);
1367 1425 }
1368 SetGDevice(SDL_Display); 1426
1369 SetEntries(0, (**cTab).ctSize, (ColorSpec *)&(**cTab).ctTable); 1427 /* Set the screen palette and update the display */
1370 1428 for (i = 0; i < ncolors; i++) {
1371 return(1); 1429 int j = firstcolor + i;
1372 } 1430 (**cTab).ctTable[j].value = j;
1373 1431 (**cTab).ctTable[j].rgb.red = colors[i].r << 8 | colors[i].r;
1374 void DSp_VideoQuit(_THIS) 1432 (**cTab).ctTable[j].rgb.green = colors[i].g << 8 | colors[i].g;
1375 { 1433 (**cTab).ctTable[j].rgb.blue = colors[i].b << 8 | colors[i].b;
1376 int i; 1434 }
1377 1435
1378 /* Free current video mode */ 1436 SetGDevice (SDL_Display);
1379 DSp_UnsetVideoMode(this, this->screen); 1437 SetEntries (0, (**cTab).ctSize, (ColorSpec *) & (**cTab).ctTable);
1380 1438
1381 /* Free Palette and restore original */ 1439 return (1);
1382 DSp_DestroyPalette (this); 1440 }
1383 1441
1384 /* Free list of video modes */ 1442 void
1385 if ( SDL_modelist != NULL ) { 1443 DSp_VideoQuit (_THIS)
1386 for ( i=0; SDL_modelist[i]; i++ ) { 1444 {
1387 SDL_free(SDL_modelist[i]); 1445 int i;
1388 } 1446
1389 SDL_free(SDL_modelist); 1447 /* Free current video mode */
1390 SDL_modelist = NULL; 1448 DSp_UnsetVideoMode (this, this->screen);
1391 } 1449
1392 1450 /* Free Palette and restore original */
1393 /* Unload DrawSprocket */ 1451 DSp_DestroyPalette (this);
1394 DSpShutdown (); 1452
1453 /* Free list of video modes */
1454 if (SDL_modelist != NULL) {
1455 for (i = 0; SDL_modelist[i]; i++) {
1456 SDL_free (SDL_modelist[i]);
1457 }
1458 SDL_free (SDL_modelist);
1459 SDL_modelist = NULL;
1460 }
1461
1462 /* Unload DrawSprocket */
1463 DSpShutdown ();
1395 } 1464 }
1396 1465
1397 #if SDL_VIDEO_OPENGL 1466 #if SDL_VIDEO_OPENGL
1398 1467
1399 /* swap buffers with v-sync */ 1468 /* swap buffers with v-sync */
1400 static void DSp_GL_SwapBuffers (_THIS) { 1469 static void
1401 1470 DSp_GL_SwapBuffers (_THIS)
1402 #ifndef DSP_NO_SYNC_OPENGL 1471 {
1403 1472
1404 unsigned int old_count; 1473 #ifndef DSP_NO_SYNC_OPENGL
1405 1474
1406 old_count = retrace_count; 1475 unsigned int old_count;
1407 while (old_count == retrace_count) 1476
1408 ; 1477 old_count = retrace_count;
1409 #endif 1478 while (old_count == retrace_count);
1410 1479 #endif
1411 aglSwapBuffers (glContext); 1480
1412 } 1481 aglSwapBuffers (glContext);
1413 1482 }
1414 #endif 1483
1484 #endif
1485 /* vi: set ts=4 sw=4 expandtab: */