comparison src/video/macdsp/SDL_dspvideo.c @ 1895:c121d94672cb

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