Skip to content

Latest commit

 

History

History
1411 lines (1093 loc) · 39.5 KB

SDL_dspvideo.c

File metadata and controls

1411 lines (1093 loc) · 39.5 KB
 
Apr 26, 2001
Apr 26, 2001
1
2
/*
SDL - Simple DirectMedia Layer
Feb 1, 2006
Feb 1, 2006
3
Copyright (C) 1997-2006 Sam Lantinga
Apr 26, 2001
Apr 26, 2001
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
Apr 26, 2001
Apr 26, 2001
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
Apr 26, 2001
Apr 26, 2001
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Apr 26, 2001
Apr 26, 2001
14
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Apr 26, 2001
Apr 26, 2001
18
19
Sam Lantinga
Dec 14, 2001
Dec 14, 2001
20
slouken@libsdl.org
Apr 26, 2001
Apr 26, 2001
21
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 26, 2001
Apr 26, 2001
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Written by Darrell Walisser <dwaliss1@purdue.edu>
Implementation notes ----------------------------------------------------------------------
A bit on GWorlds in VRAM from technote 1182:
There are two important things to note about GWorld's allocated in
VRAM. First, the base address retrieved through GetPixBaseAddr or
read directly from the PixMap structure can become invalid anytime
memory is allocated in VRAM. This can occur either by explicit
allocations, such as calls to NewGWorld, or by implicit ones, such as
those associated with the internal texture allocation of OpenGL. The
stored pixel images themselves will still be valid but may have been
moved in VRAM, thus rendering any stored base addresses invalid.
You should never store an image's base address for longer than is
necessary and especially never across calls to NewGWorld or
texture-creation routines.
Secondly, an offscreen pixel image allocated in VRAM can be
purged at system task time by the display driver. This means any
time your application yields time such by calling WaitNextEvent or
SystemTask you can lose your VRAM GWorld contents. While this
happens infrequently, usually associated with display resolution or
pixel depth changes you must code for this eventuality. This purge
can occur whether or not the GWorld is locked or not. A return value
of false from LockPixels, a NULL return value from GetPixBaseAddr
or NULL in the baseAddr field of the PixMap mean that the pixel
image has been purged. To reallocate it you can either call
UpdateGWorld or Dispose your current GWorld through
DisposeGWorld and reallocate it via NewGWorld. Either way you must
then rebuild the pixel image.
------------------------------------------------------------------------------------
Currently, I don't account for (1). In my testing, NewGWorld never invalidated
other existing GWorlds in VRAM. However, I do have protection for (2).
Namely, I am using GetOSEvent() instead of WaitNextEvent() so that there are no
context switches (the app hogs the CPU). Eventually a book-keeping system should
be coded to take care of (1) and (2).
------------------------------------------------------------------------------------
System requirements (* denotes optional):
1. DrawSprocket 1.7.3
Sep 8, 2005
Sep 8, 2005
70
2. *MacOS 9 or later (but *not* Mac OS X) for hardware accelerated blit / fill
Apr 26, 2001
Apr 26, 2001
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
3. *May also require certain graphics hardware for (2). I trust that all Apple OEM
hardware will work. Third party accelerators may work if they have QuickDraw
acceleration in the drivers and the drivers have been updated for OS 9. The current
Voodoo 3 drivers (1.0b12) do not work.
Coding suggestions:
1. Use SDL_UpdateRects !
If no QuickDraw acceleration is present, double-buffered surfaces will use a back buffer
in System memory. I recommend you use SDL_UpdateRects with double-buffered surfaces
for best performance on these cards, since the overhead is nearly zero for VRAM back buffer.
2. Load most-resident surfaces first.
If you fill up VRAM or AGP memory, there is no contingency for purging to make room for the next one.
Therefore, you should load the surfaces you plan to use the most frequently first.
Sooner or later, I will code LRU replacement to help this.
TODO:
Some kind of posterized mode for resolutions < 640x480.
Window support / fullscreen toggle.
Figure out how much VRAM is available. Put in video->info->video_mem.
Track VRAM usage.
BUGS:
I can't create a hardware surface the same size as the screen?! How to fix?
COMPILE OPTIONS:
DSP_TRY_CC_AND_AA - Define if you want to try HWA color-key and alpha blitters
HW color-key blitting gives substantial improvements,
but hw alpha is neck-and-neck with SDL's soft bitter.
DSP_NO_SYNC_VBL - Define for HWA double-buffered surfaces: don't sync
pseudo-flip to monitor redraw.
DSP_NO_SYNC_OPENGL - Define for OpenGL surfaces: don't sync buffer swap. Synching buffer
swap may result in reduced performance, but can eliminate some
tearing artifacts.
CHANGELOG:
09/17/00 Lots of little tweaks. Build modelist in reverse order so largest contexts
list first. Compared various methods with ROM methods and fixed rez switch
crashing bug in GL Tron. (Woohoo!)
*/
#define DSP_TRY_CC_AND_AA
/* #define DSP_NO_SYNC_VBL */
#define DSP_NO_SYNC_OPENGL
Sep 8, 2005
Sep 8, 2005
126
127
128
129
#if defined(__APPLE__) && defined(__MACH__)
#include <Carbon/Carbon.h>
#include <DrawSprocket/DrawSprocket.h>
#elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335)
Apr 26, 2001
Apr 26, 2001
130
#include <Carbon.h>
Sep 8, 2005
Sep 8, 2005
131
#include <DrawSprocket.h>
Apr 26, 2001
Apr 26, 2001
132
133
134
135
136
137
#else
#include <LowMem.h>
#include <Gestalt.h>
#include <Devices.h>
#include <DiskInit.h>
#include <QDOffscreen.h>
Sep 8, 2005
Sep 8, 2005
138
#include <DrawSprocket.h>
Apr 26, 2001
Apr 26, 2001
139
140
141
142
#endif
#include "SDL_video.h"
#include "SDL_syswm.h"
Feb 16, 2006
Feb 16, 2006
143
144
145
#include "../SDL_sysvideo.h"
#include "../SDL_blit.h"
#include "../SDL_pixels_c.h"
Apr 26, 2001
Apr 26, 2001
146
#include "SDL_dspvideo.h"
Feb 24, 2006
Feb 24, 2006
147
148
149
150
#include "../maccommon/SDL_macgl_c.h"
#include "../maccommon/SDL_macwm_c.h"
#include "../maccommon/SDL_macmouse_c.h"
#include "../maccommon/SDL_macevents_c.h"
Apr 26, 2001
Apr 26, 2001
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* Initialization/Query functions */
static int DSp_VideoInit(_THIS, SDL_PixelFormat *vformat);
static SDL_Rect **DSp_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
static SDL_Surface *DSp_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
static int DSp_SetColors(_THIS, int firstcolor, int ncolors,
SDL_Color *colors);
static int DSp_CreatePalette(_THIS);
static int DSp_DestroyPalette(_THIS);
static void DSp_VideoQuit(_THIS);
static int DSp_GetMainDevice (_THIS, GDHandle *device);
static void DSp_IsHWAvailable (_THIS, SDL_PixelFormat *vformat);
static void DSp_DSpUpdate(_THIS, int numrects, SDL_Rect *sdl_rects);
static void DSp_DirectUpdate(_THIS, int numrects, SDL_Rect *sdl_rects);
/* Hardware surface functions */
static int DSp_SetHWAlpha(_THIS, SDL_Surface *surface, UInt8 alpha);
static int DSp_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key);
static int DSp_NewHWSurface(_THIS, CGrafPtr *port, int depth, int width, int height);
static int DSp_AllocHWSurface(_THIS, SDL_Surface *surface);
static int DSp_LockHWSurface(_THIS, SDL_Surface *surface);
static void DSp_UnlockHWSurface(_THIS, SDL_Surface *surface);
static void DSp_FreeHWSurface(_THIS, SDL_Surface *surface);
static int DSp_FlipHWSurface(_THIS, SDL_Surface *surface);
static int DSp_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dest);
static int DSp_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect,
SDL_Surface *dst, SDL_Rect *dstrect);
static int DSp_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color);
Feb 16, 2006
Feb 16, 2006
181
#if SDL_VIDEO_OPENGL
Apr 26, 2001
Apr 26, 2001
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
static void DSp_GL_SwapBuffers (_THIS);
#endif
#if ! TARGET_API_MAC_CARBON
#define GetPortPixRowBytes(x) ( (*(x->portPixMap))->rowBytes )
#define GetGDevPixMap(x) ((**(x)).gdPMap)
#define GetPortPixMap(x) ((*(x)).portPixMap)
#define GetPixDepth(y) ((**(y)).pixelSize)
//#define GetPixRowBytes(y) ((**(y)).rowBytes)
//#define GetPixBaseAddr(y) ((**(y)).baseAddr)
#define GetPixCTab(y) ((**(y)).pmTable)
#define GetPortBitMapForCopyBits(x) (&(((GrafPtr)(x))->portBits))
#else
#define GetPortPixRowBytes(x) (GetPixRowBytes(GetPortPixMap(x)) )
#define GetGDevPixMap(x) ((**(x)).gdPMap)
#endif
typedef struct private_hwdata {
GWorldPtr offscreen; // offscreen gworld in VRAM or AGP
#ifdef DSP_TRY_CC_AND_AA
GWorldPtr mask; // transparent mask
RGBColor alpha; // alpha color
RGBColor trans; // transparent color
#endif
} private_hwdata;
typedef private_hwdata private_swdata ; /* have same fields */
/* Macintosh toolbox driver bootstrap functions */
static int DSp_Available(void)
{
/* Check for DrawSprocket */
Sep 8, 2005
Sep 8, 2005
222
#if ! TARGET_API_MAC_OSX
Apr 26, 2001
Apr 26, 2001
223
224
/* This check is only meaningful if you weak-link DrawSprocketLib */
return ((Ptr)DSpStartup != (Ptr)kUnresolvedCFragSymbolAddress);
Sep 8, 2005
Sep 8, 2005
225
226
227
#else
return 1; // DrawSprocket.framework doesn't have it all, but it's there
#endif
Apr 26, 2001
Apr 26, 2001
228
229
230
231
232
233
234
235
236
237
}
static void DSp_DeleteDevice(SDL_VideoDevice *device)
{
/* -dw- taking no chances with null pointers */
if (device) {
if (device->hidden) {
if (device->hidden->dspinfo)
Feb 7, 2006
Feb 7, 2006
238
SDL_free(device->hidden->dspinfo);
Apr 26, 2001
Apr 26, 2001
239
Feb 7, 2006
Feb 7, 2006
240
SDL_free(device->hidden);
Apr 26, 2001
Apr 26, 2001
241
}
Feb 7, 2006
Feb 7, 2006
242
SDL_free(device);
Apr 26, 2001
Apr 26, 2001
243
244
245
246
247
248
249
250
}
}
static SDL_VideoDevice *DSp_CreateDevice(int devindex)
{
SDL_VideoDevice *device;
/* Initialize all variables that we clean on shutdown */
Feb 7, 2006
Feb 7, 2006
251
device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice));
Apr 26, 2001
Apr 26, 2001
252
if ( device ) {
Feb 7, 2006
Feb 7, 2006
253
SDL_memset(device, 0, sizeof (*device));
Apr 26, 2001
Apr 26, 2001
254
device->hidden = (struct SDL_PrivateVideoData *)
Feb 7, 2006
Feb 7, 2006
255
SDL_malloc((sizeof *device->hidden));
Apr 26, 2001
Apr 26, 2001
256
if (device->hidden)
Feb 7, 2006
Feb 7, 2006
257
SDL_memset(device->hidden, 0, sizeof ( *(device->hidden) ) );
Apr 26, 2001
Apr 26, 2001
258
259
260
261
262
263
264
}
if ( (device == NULL) || (device->hidden == NULL) ) {
SDL_OutOfMemory();
if ( device ) {
if (device->hidden)
Feb 7, 2006
Feb 7, 2006
265
SDL_free(device->hidden);
Apr 26, 2001
Apr 26, 2001
266
Feb 7, 2006
Feb 7, 2006
267
SDL_free(device);
Apr 26, 2001
Apr 26, 2001
268
269
270
271
272
273
}
return(NULL);
}
/* Allocate DrawSprocket information */
Feb 7, 2006
Feb 7, 2006
274
device->hidden->dspinfo = (struct DSpInfo *)SDL_malloc(
Apr 26, 2001
Apr 26, 2001
275
276
277
(sizeof *device->hidden->dspinfo));
if ( device->hidden->dspinfo == NULL ) {
SDL_OutOfMemory();
Feb 7, 2006
Feb 7, 2006
278
279
SDL_free(device->hidden);
SDL_free(device);
Apr 26, 2001
Apr 26, 2001
280
281
return(0);
}
Feb 7, 2006
Feb 7, 2006
282
SDL_memset(device->hidden->dspinfo, 0, (sizeof *device->hidden->dspinfo));
Apr 26, 2001
Apr 26, 2001
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* Set the function pointers */
device->VideoInit = DSp_VideoInit;
device->ListModes = DSp_ListModes;
device->SetVideoMode = DSp_SetVideoMode;
device->SetColors = DSp_SetColors;
device->UpdateRects = NULL;
device->VideoQuit = DSp_VideoQuit;
device->AllocHWSurface = DSp_AllocHWSurface;
device->CheckHWBlit = NULL;
device->FillHWRect = NULL;
device->SetHWColorKey = NULL;
device->SetHWAlpha = NULL;
device->LockHWSurface = DSp_LockHWSurface;
device->UnlockHWSurface = DSp_UnlockHWSurface;
device->FlipHWSurface = DSp_FlipHWSurface;
device->FreeHWSurface = DSp_FreeHWSurface;
Feb 16, 2006
Feb 16, 2006
300
#if SDL_VIDEO_OPENGL
Apr 26, 2001
Apr 26, 2001
301
302
device->GL_MakeCurrent = Mac_GL_MakeCurrent;
device->GL_SwapBuffers = DSp_GL_SwapBuffers;
Jan 25, 2005
Jan 25, 2005
303
304
device->GL_LoadLibrary = Mac_GL_LoadLibrary;
device->GL_GetProcAddress = Mac_GL_GetProcAddress;
Apr 26, 2001
Apr 26, 2001
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#endif
device->SetCaption = NULL;
device->SetIcon = NULL;
device->IconifyWindow = NULL;
device->GrabInput = NULL;
device->GetWMInfo = NULL;
device->FreeWMCursor = Mac_FreeWMCursor;
device->CreateWMCursor = Mac_CreateWMCursor;
device->ShowWMCursor = Mac_ShowWMCursor;
device->WarpWMCursor = Mac_WarpWMCursor;
device->InitOSKeymap = Mac_InitOSKeymap;
device->PumpEvents = Mac_PumpEvents;
device->GrabInput = NULL;
device->CheckMouseMode = NULL;
device->free = DSp_DeleteDevice;
return device;
}
VideoBootStrap DSp_bootstrap = {
"DSp", "MacOS DrawSprocket",
DSp_Available, DSp_CreateDevice
};
/* Use DSp/Display Manager to build mode list for given screen */
static SDL_Rect** DSp_BuildModeList (const GDHandle gDevice)
{
DSpContextAttributes attributes;
DSpContextReference context;
DisplayIDType displayID;
SDL_Rect temp_list [16];
SDL_Rect **mode_list;
int width, height, i, j;
#if TARGET_API_MAC_OSX
displayID = 0;
#else
/* Ask Display Manager for integer id of screen device */
if ( DMGetDisplayIDByGDevice (gDevice, &displayID, SDL_TRUE) != noErr ) {
return NULL;
}
#endif
/* Get the first possible DSp context on this device */
if ( DSpGetFirstContext (displayID, &context) != noErr ) {
return NULL;
}
if ( DSpContext_GetAttributes (context, &attributes) != noErr )
return NULL;
Feb 19, 2006
Feb 19, 2006
359
for ( i = 0; i < SDL_arraysize(temp_list); i++ ) {
Apr 26, 2001
Apr 26, 2001
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
width = attributes.displayWidth;
height = attributes.displayHeight;
temp_list [i].x = 0 | attributes.displayBestDepth;
temp_list [i].y = 0;
temp_list [i].w = width;
temp_list [i].h = height;
/* DSp will report many different contexts with the same width and height. */
/* They will differ in bit depth and refresh rate. */
/* We will ignore them until we reach one with a different width/height */
/* When there are no more contexts to look at, we will quit building the list*/
while ( width == attributes.displayWidth && height == attributes.displayHeight ) {
OSStatus err = DSpGetNextContext (context, &context);
if (err != noErr)
if (err == kDSpContextNotFoundErr)
goto done;
else
return NULL;
if ( DSpContext_GetAttributes (context, &attributes) != noErr )
return NULL;
temp_list [i].x |= attributes.displayBestDepth;
}
}
done:
i++; /* i was not incremented before kicking out of the loop */
Feb 7, 2006
Feb 7, 2006
390
mode_list = (SDL_Rect**) SDL_malloc (sizeof (SDL_Rect*) * (i+1));
Apr 26, 2001
Apr 26, 2001
391
392
393
394
if (mode_list) {
/* -dw- new stuff: build in reverse order so largest sizes list first */
for (j = i-1; j >= 0; j--) {
Feb 7, 2006
Feb 7, 2006
395
mode_list [j] = (SDL_Rect*) SDL_malloc (sizeof (SDL_Rect));
Apr 26, 2001
Apr 26, 2001
396
if (mode_list [j])
Feb 7, 2006
Feb 7, 2006
397
SDL_memcpy (mode_list [j], &(temp_list [j]), sizeof (SDL_Rect));
Apr 26, 2001
Apr 26, 2001
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
else {
SDL_OutOfMemory ();
return NULL;
}
}
mode_list [i] = NULL; /* append null to the end */
}
else {
SDL_OutOfMemory ();
return NULL;
}
return mode_list;
}
static void DSp_IsHWAvailable (_THIS, SDL_PixelFormat *vformat)
{
/*
VRAM GWorlds are only available on OS 9 or later.
Even with OS 9, some display drivers won't support it,
so we create a test GWorld and check for errors.
*/
long versionSystem;
dsp_vram_available = SDL_FALSE;
dsp_agp_available = SDL_FALSE;
Gestalt ('sysv', &versionSystem);
if (0x00000860 < (versionSystem & 0x0000FFFF)) {
GWorldPtr offscreen;
OSStatus err;
Rect bounds;
SetRect (&bounds, 0, 0, 320, 240);
Sep 8, 2005
Sep 8, 2005
435
#if useDistantHdwrMem && useLocalHdwrMem
Apr 26, 2001
Apr 26, 2001
436
437
438
439
440
441
442
443
444
445
446
err = NewGWorld (&offscreen, vformat->BitsPerPixel, &bounds, NULL, SDL_Display, useDistantHdwrMem | noNewDevice);
if (err == noErr) {
dsp_vram_available = SDL_TRUE;
DisposeGWorld (offscreen);
}
err = NewGWorld (&offscreen, vformat->BitsPerPixel, &bounds, NULL, SDL_Display, useLocalHdwrMem | noNewDevice);
if (err == noErr) {
DisposeGWorld (offscreen);
dsp_agp_available = SDL_TRUE;
}
Sep 8, 2005
Sep 8, 2005
447
#endif
Apr 26, 2001
Apr 26, 2001
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
}
}
static int DSp_GetMainDevice (_THIS, GDHandle *device)
{
#if TARGET_API_MAC_OSX
/* DSpUserSelectContext not available on OS X */
*device = GetMainDevice();
return 0;
#else
DSpContextAttributes attrib;
DSpContextReference context;
DisplayIDType display_id;
GDHandle main_device;
GDHandle device_list;
device_list = GetDeviceList ();
main_device = GetMainDevice ();
/* Quick check to avoid slower method when only one display exists */
if ( (**device_list).gdNextGD == NULL ) {
*device = main_device;
return 0;
}
Feb 7, 2006
Feb 7, 2006
475
SDL_memset (&attrib, 0, sizeof (DSpContextAttributes));
Apr 26, 2001
Apr 26, 2001
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* These attributes are hopefully supported on all devices...*/
attrib.displayWidth = 640;
attrib.displayHeight = 480;
attrib.displayBestDepth = 8;
attrib.backBufferBestDepth = 8;
attrib.displayDepthMask = kDSpDepthMask_All;
attrib.backBufferDepthMask = kDSpDepthMask_All;
attrib.colorNeeds = kDSpColorNeeds_Require;
attrib.pageCount = 1;
if (noErr != DMGetDisplayIDByGDevice (main_device, &display_id, SDL_FALSE)) {
SDL_SetError ("Display Manager couldn't associate GDevice with a Display ID");
return (-1);
}
/* Put up dialog on main display to select which display to use */
if (noErr != DSpUserSelectContext (&attrib, display_id, NULL, &context)) {
SDL_SetError ("DrawSprocket couldn't create a context");
return (-1);
}
if (noErr != DSpContext_GetDisplayID (context, &display_id)) {
SDL_SetError ("DrawSprocket couldn't get display ID");
return (-1);
}
if (noErr != DMGetGDeviceByDisplayID (display_id, &main_device, SDL_FALSE)) {
SDL_SetError ("Display Manager couldn't associate Display ID with GDevice");
return (-1);
}
*device = main_device;
return (0);
#endif
}
static int DSp_VideoInit(_THIS, SDL_PixelFormat *vformat)
{
Sep 8, 2005
Sep 8, 2005
515
NumVersion dsp_version = { 0x01, 0x00, 0x00, 0x00 };
Apr 26, 2001
Apr 26, 2001
516
Sep 8, 2005
Sep 8, 2005
517
#if UNIVERSAL_INTERFACES_VERSION > 0x0320
Sep 8, 2005
Sep 8, 2005
518
dsp_version = DSpGetVersion ();
Sep 8, 2005
Sep 8, 2005
519
#endif
Apr 26, 2001
Apr 26, 2001
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
if ( (dsp_version.majorRev == 1 && dsp_version.minorAndBugRev < 0x73) ||
(dsp_version.majorRev < 1) ) {
/* StandardAlert (kAlertStopAlert, "\pError!",
"\pI need DrawSprocket 1.7.3 or later!\n"
"You can find a newer version at http://www.apple.com/swupdates.",
NULL, NULL);
*/
SDL_SetError ("DrawSprocket version is too old. Need 1.7.3 or later.");
return (-1);
}
if ( DSpStartup () != noErr ) {
SDL_SetError ("DrawSprocket couldn't startup");
return(-1);
}
/* Start DSpintosh events */
Mac_InitEvents(this);
/* Get a handle to the main monitor, or choose one on multiple monitor setups */
if ( DSp_GetMainDevice(this, &SDL_Display) < 0)
return (-1);
/* Determine pixel format */
vformat->BitsPerPixel = GetPixDepth ( (**SDL_Display).gdPMap );
dsp_old_depth = vformat->BitsPerPixel;
switch (vformat->BitsPerPixel) {
case 16:
vformat->Rmask = 0x00007c00;
vformat->Gmask = 0x000003e0;
vformat->Bmask = 0x0000001f;
break;
default:
break;
}
if ( DSp_CreatePalette (this) < 0 ) {
SDL_SetError ("Could not create palette");
return (-1);
}
/* Get a list of available fullscreen modes */
SDL_modelist = DSp_BuildModeList (SDL_Display);
if (SDL_modelist == NULL) {
SDL_SetError ("DrawSprocket could not build a mode list");
return (-1);
}
/* Check for VRAM and AGP GWorlds for HW Blitting */
DSp_IsHWAvailable (this, vformat);
this->info.wm_available = 0;
if (dsp_vram_available || dsp_agp_available) {
this->info.hw_available = SDL_TRUE;
this->CheckHWBlit = DSp_CheckHWBlit;
this->info.blit_hw = SDL_TRUE;
this->FillHWRect = DSp_FillHWRect;
this->info.blit_fill = SDL_TRUE;
#ifdef DSP_TRY_CC_AND_AA
this->SetHWColorKey = DSp_SetHWColorKey;
this->info.blit_hw_CC = SDL_TRUE;
this->SetHWAlpha = DSp_SetHWAlpha;
this->info.blit_hw_A = SDL_TRUE;
#endif
}
return(0);
}
static SDL_Rect **DSp_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
{
static SDL_Rect *dsp_modes[16];
int i = 0, j = 0;
if ( format->BitsPerPixel == 0 )
return ( (SDL_Rect**) NULL );
while (SDL_modelist[i] != NULL) {
if (SDL_modelist[i]->x & format->BitsPerPixel) {
dsp_modes[j] = SDL_modelist[i];
j++;
}
i++;
}
dsp_modes[j] = NULL;
return dsp_modes;
}
/* Various screen update functions available */
static void DSp_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
Sep 8, 2005
Sep 8, 2005
625
626
#if ! TARGET_API_MAC_OSX
Apr 26, 2001
Apr 26, 2001
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
static volatile unsigned int retrace_count = 0; /* -dw- need volatile because it updates asychronously */
Boolean DSp_VBLProc ( DSpContextReference context, void *ref_con )
{
retrace_count++;
return 1; /* Darrell, is this right? */
}
static void DSp_SetHWError (OSStatus err, int is_agp)
{
char message[1024];
const char *fmt, *mem;
if ( is_agp ) {
mem = "AGP Memory";
} else {
mem = "VRAM";
}
switch(err) {
case memFullErr:
fmt = "Hardware surface possible but not enough %s available";
break;
case cDepthErr:
fmt = "Hardware surface possible but invalid color depth";
break;
default:
fmt = "Hardware surface could not be allocated in %s - unknown error";
break;
}
Feb 7, 2006
Feb 7, 2006
657
SDL_snprintf(message, SDL_arraysize(message), fmt, mem);
Apr 26, 2001
Apr 26, 2001
658
659
SDL_SetError(message);
}
Sep 8, 2005
Sep 8, 2005
660
#endif // TARGET_API_MAC_OSX
Apr 26, 2001
Apr 26, 2001
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
/* put up a dialog to verify display change */
static int DSp_ConfirmSwitch () {
/* resource id's for dialog */
const int rDialog = 1002;
const int bCancel = 1;
const int bOK = 2;
DialogPtr dialog;
OSStatus err;
SInt32 response;
DialogItemIndex item = 0;
GrafPtr savePort;
GetPort (&savePort);
dialog = GetNewDialog (rDialog, NULL, (WindowPtr) -1);
if (dialog == NULL)
return (0);
Sep 8, 2005
Sep 8, 2005
682
683
684
685
686
#if TARGET_API_CARBON
SetPort (GetDialogPort(dialog));
#else
SetPort ((WindowPtr) dialog);
#endif
Apr 26, 2001
Apr 26, 2001
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
SetDialogDefaultItem (dialog, bCancel);
SetDialogCancelItem (dialog, bCancel);
SetEventMask (everyEvent);
FlushEvents (everyEvent, 0);
/* On MacOS 8.5 or later, we can make the dialog go away after 15 seconds */
/* This is good since it's possible user can't even see the dialog! */
/* Requires linking to DialogsLib */
err = Gestalt(gestaltSystemVersion,&response);
if (err == noErr && response >= 0x00000850) {
SetDialogTimeout(dialog, bCancel, 15);
}
do {
ModalDialog ( NULL, &item );
} while ( item != bCancel && item != bOK && err != noErr);
Sep 8, 2005
Sep 8, 2005
708
709
DisposeDialog (dialog);
Apr 26, 2001
Apr 26, 2001
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
SetPort (savePort);
SetEventMask(everyEvent - autoKeyMask);
FlushEvents(everyEvent, 0);
return (item - 1);
}
static void DSp_UnsetVideoMode(_THIS, SDL_Surface *current)
{
if ( current->flags & SDL_OPENGL ) {
Mac_GL_Quit (this);
}
if (dsp_context != NULL) {
GWorldPtr front;
DSpContext_GetFrontBuffer (dsp_context, &front);
if (front != dsp_back_buffer)
DisposeGWorld (dsp_back_buffer);
if (current->hwdata)
Feb 7, 2006
Feb 7, 2006
735
SDL_free(current->hwdata);
Apr 26, 2001
Apr 26, 2001
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
DSpContext_SetState (dsp_context, kDSpContextState_Inactive );
DSpContext_Release (dsp_context);
dsp_context = NULL;
}
if (SDL_Window != NULL) {
DisposeWindow (SDL_Window);
SDL_Window = NULL;
}
current->pixels = NULL;
current->flags = 0;
}
static SDL_Surface *DSp_SetVideoMode(_THIS,
SDL_Surface *current, int width, int height, int bpp, Uint32 flags)
{
Sep 8, 2005
Sep 8, 2005
756
757
#if !TARGET_API_MAC_OSX
DisplayIDType display_id;
Apr 26, 2001
Apr 26, 2001
758
Fixed freq;
Sep 8, 2005
Sep 8, 2005
759
760
#endif
DSpContextAttributes attrib;
Apr 26, 2001
Apr 26, 2001
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
OSStatus err;
UInt32 rmask = 0, gmask = 0, bmask = 0;
int page_count;
int double_buf;
int hw_surface;
int use_dsp_back_buffer;
DSp_UnsetVideoMode (this, current);
if (bpp != dsp_old_depth)
DSp_DestroyPalette (this);
double_buf = (flags & SDL_DOUBLEBUF) != 0;
hw_surface = (flags & SDL_HWSURFACE) != 0;
use_dsp_back_buffer = !dsp_vram_available || !hw_surface ;
current->flags |= SDL_FULLSCREEN;
rebuild:
if ( double_buf && use_dsp_back_buffer ) {
page_count = 2;
} else {
page_count = 1;
}
Feb 7, 2006
Feb 7, 2006
788
SDL_memset (&attrib, 0, sizeof (DSpContextAttributes));
Apr 26, 2001
Apr 26, 2001
789
790
791
792
793
794
795
796
797
attrib.displayWidth = width;
attrib.displayHeight = height;
attrib.displayBestDepth = bpp;
attrib.backBufferBestDepth = bpp;
attrib.displayDepthMask = kDSpDepthMask_All;
attrib.backBufferDepthMask = kDSpDepthMask_All;
attrib.colorNeeds = kDSpColorNeeds_Require;
attrib.colorTable = 0;
attrib.pageCount = page_count;
Sep 8, 2005
Sep 8, 2005
798
#if TARGET_API_MAC_OSX || UNIVERSAL_INTERFACES_VERSION == 0x0320
Apr 26, 2001
Apr 26, 2001
799
800
801
802
803
804
805
806
807
808
809
if ( DSpFindBestContext (&attrib, &dsp_context) != noErr ) {
SDL_SetError ("DrawSprocket couldn't find a context");
return NULL;
}
#else
if ( noErr != DMGetDisplayIDByGDevice (SDL_Display, &display_id, SDL_FALSE) ) {
SDL_SetError ("Display Manager couldn't associate GDevice with display_id");
return NULL;
}
Jan 25, 2005
Jan 25, 2005
810
if ( DSpFindBestContextOnDisplayID(&attrib, &dsp_context, display_id) != noErr ) {
Apr 26, 2001
Apr 26, 2001
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
SDL_SetError ("DrawSprocket couldn't find a suitable context on given display");
return NULL;
}
#endif
if ( DSpContext_Reserve (dsp_context, &attrib) != noErr ) {
SDL_SetError ("DrawSprocket couldn't get the needed resources to build the display");
return NULL;
}
if ( (err = DSpContext_SetState (dsp_context, kDSpContextState_Active)) != noErr ) {
if (err == kDSpConfirmSwitchWarning) {
if ( ! DSp_ConfirmSwitch () ) {
DSpContext_Release (dsp_context);
dsp_context = NULL;
SDL_SetError ("User cancelled display switch");
return NULL;
}
else
/* Have to reactivate context. Why? */
DSpContext_SetState (dsp_context, kDSpContextState_Active);
}
else {
SDL_SetError ("DrawSprocket couldn't activate the context");
return NULL;
}
}
if (bpp != dsp_old_depth) {
DSp_CreatePalette (this);
/* update format if display depth changed */
if (bpp == 16) {
rmask = 0x00007c00;
gmask = 0x000003e0;
bmask = 0x0000001f;
}
if ( ! SDL_ReallocFormat (current, bpp, rmask, gmask, bmask, 0 ) ) {
SDL_SetError ("Could not reallocate video format.");
return(NULL);
}
}
if (!double_buf) {
/* single-buffer context */
DSpContext_GetFrontBuffer (dsp_context, &dsp_back_buffer);
Feb 7, 2006
Feb 7, 2006
867
current->hwdata = (private_hwdata*) SDL_malloc (sizeof (private_hwdata));
Apr 26, 2001
Apr 26, 2001
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
if (current ->hwdata == NULL) {
SDL_OutOfMemory ();
return NULL;
}
current->hwdata->offscreen = dsp_back_buffer;
current->flags |= SDL_HWSURFACE;
this->UpdateRects = DSp_DirectUpdate;
}
else if ( use_dsp_back_buffer ) {
DSpContext_GetBackBuffer (dsp_context, kDSpBufferKind_Normal, &dsp_back_buffer);
current->flags |= SDL_DOUBLEBUF | SDL_SWSURFACE; /* only front buffer is in VRAM */
this->UpdateRects = DSp_DSpUpdate;
}
else if ( DSp_NewHWSurface(this, &dsp_back_buffer, bpp, width-1, height-1) == 0 ) {
Feb 7, 2006
Feb 7, 2006
885
current->hwdata = (private_hwdata*) SDL_malloc (sizeof (private_hwdata));
Apr 26, 2001
Apr 26, 2001
886
887
888
889
890
if (current ->hwdata == NULL) {
SDL_OutOfMemory ();
return NULL;
}
Feb 7, 2006
Feb 7, 2006
891
SDL_memset (current->hwdata, 0, sizeof (private_hwdata));
Apr 26, 2001
Apr 26, 2001
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
current->hwdata->offscreen = dsp_back_buffer;
current->flags |= SDL_DOUBLEBUF | SDL_HWSURFACE;
this->UpdateRects = DSp_DirectUpdate; /* hardware doesn't do update rects, must be page-flipped */
}
else {
DSpContext_Release (dsp_context);
use_dsp_back_buffer = SDL_TRUE;
goto rebuild;
}
current->pitch = GetPortPixRowBytes(dsp_back_buffer) & 0x3FFF;
current->pixels = GetPixBaseAddr(GetPortPixMap(dsp_back_buffer));
current->w = width;
current->h = height;
#if ! TARGET_API_MAC_OSX
if (use_dsp_back_buffer) {
DSpContext_GetMonitorFrequency (dsp_context, &freq);
DSpContext_SetMaxFrameRate (dsp_context, freq >> 16);
}
if ( (current->flags & SDL_HWSURFACE) || (current->flags & SDL_OPENGL) )
DSpContext_SetVBLProc (dsp_context, DSp_VBLProc, NULL);
#endif
if (bpp == 8)
current->flags |= SDL_HWPALETTE;
if (flags & SDL_OPENGL) {
Rect rect;
RGBColor rgb = { 0.0, 0.0, 0.0 };
GrafPtr save_port;
SetRect (&rect, 0, 0, width, height);
SDL_Window = NewCWindow(nil, &( (**SDL_Display).gdRect), "\p", SDL_TRUE, plainDBox, (WindowPtr)-1, SDL_FALSE, 0);
if (SDL_Window == NULL) {
SDL_SetError ("DSp_SetVideoMode : OpenGL window could not be created.");
return NULL;
}
/* Set window color to black to avoid white flash*/
GetPort (&save_port);
Sep 8, 2005
Sep 8, 2005
942
943
944
#if TARGET_API_MAC_CARBON
SetPort (GetWindowPort(SDL_Window));
#else
Apr 26, 2001
Apr 26, 2001
945
SetPort (SDL_Window);
Sep 8, 2005
Sep 8, 2005
946
#endif
Apr 26, 2001
Apr 26, 2001
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
RGBForeColor (&rgb);
PaintRect (&rect);
SetPort (save_port);
SetPortWindowPort (SDL_Window);
SelectWindow (SDL_Window);
if ( Mac_GL_Init (this) < 0 ) {
SDL_SetError ("DSp_SetVideoMode : could not create OpenGL context.");
return NULL;
}
current->flags |= SDL_OPENGL;
}
return current;
}
#ifdef DSP_TRY_CC_AND_AA
static int DSp_MakeHWMask (_THIS, SDL_Surface *surface)
{
GDHandle save_device;
CGrafPtr save_port;
GWorldPtr temp;
RGBColor black = { 0, 0, 0 };
RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF };
Rect rect;
Uint32 depth = GetPixDepth ( GetGDevPixMap (SDL_Display) );
SetRect (&rect, 0, 0, surface->w, surface->h);
if ( noErr != NewGWorld (&(surface->hwdata->mask), depth, &rect, 0, SDL_Display, 0 ) < 0 ) {
SDL_OutOfMemory ();
return (-1);
}
if ( noErr != NewGWorld (&temp, depth, &rect, 0 , SDL_Display, 0 ) ) {
SDL_OutOfMemory ();
return (-1);
}
GetGWorld (&save_port, &save_device);
SetGWorld (surface->hwdata->mask, SDL_Display);
RGBForeColor (&white);
PaintRect (&rect);
RGBBackColor (&(surface->hwdata->trans));