Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1485 lines (1169 loc) · 41.6 KB

SDL_dspvideo.c

File metadata and controls

1485 lines (1169 loc) · 41.6 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
/* Initialization/Query functions */
May 28, 2006
May 28, 2006
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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);
Apr 26, 2001
Apr 26, 2001
168
169
/* Hardware surface functions */
May 28, 2006
May 28, 2006
170
171
172
173
174
175
176
177
178
179
180
181
182
183
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);
Apr 26, 2001
Apr 26, 2001
184
Feb 16, 2006
Feb 16, 2006
185
#if SDL_VIDEO_OPENGL
May 28, 2006
May 28, 2006
186
static void DSp_GL_SwapBuffers (_THIS);
Apr 26, 2001
Apr 26, 2001
187
188
189
190
#endif
#if ! TARGET_API_MAC_CARBON
May 28, 2006
May 28, 2006
191
192
193
194
195
#define GetPortPixRowBytes(x) ( (*(x->portPixMap))->rowBytes )
#define GetGDevPixMap(x) ((**(x)).gdPMap)
#define GetPortPixMap(x) ((*(x)).portPixMap)
#define GetPixDepth(y) ((**(y)).pixelSize)
Apr 26, 2001
Apr 26, 2001
196
197
//#define GetPixRowBytes(y) ((**(y)).rowBytes)
//#define GetPixBaseAddr(y) ((**(y)).baseAddr)
May 28, 2006
May 28, 2006
198
199
200
#define GetPixCTab(y) ((**(y)).pmTable)
#define GetPortBitMapForCopyBits(x) (&(((GrafPtr)(x))->portBits))
Apr 26, 2001
Apr 26, 2001
201
#else
May 28, 2006
May 28, 2006
202
203
#define GetPortPixRowBytes(x) (GetPixRowBytes(GetPortPixMap(x)) )
#define GetGDevPixMap(x) ((**(x)).gdPMap)
Apr 26, 2001
Apr 26, 2001
204
205
206
#endif
May 28, 2006
May 28, 2006
207
208
209
210
211
212
213
214
215
216
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
Apr 26, 2001
Apr 26, 2001
217
218
219
} private_hwdata;
May 28, 2006
May 28, 2006
220
typedef private_hwdata private_swdata; /* have same fields */
Apr 26, 2001
Apr 26, 2001
221
222
223
/* Macintosh toolbox driver bootstrap functions */
May 28, 2006
May 28, 2006
224
225
static int
DSp_Available (void)
Apr 26, 2001
Apr 26, 2001
226
{
May 28, 2006
May 28, 2006
227
/* Check for DrawSprocket */
Sep 8, 2005
Sep 8, 2005
228
#if ! TARGET_API_MAC_OSX
May 28, 2006
May 28, 2006
229
230
/* This check is only meaningful if you weak-link DrawSprocketLib */
return ((Ptr) DSpStartup != (Ptr) kUnresolvedCFragSymbolAddress);
Sep 8, 2005
Sep 8, 2005
231
#else
May 28, 2006
May 28, 2006
232
return 1; // DrawSprocket.framework doesn't have it all, but it's there
Sep 8, 2005
Sep 8, 2005
233
#endif
Apr 26, 2001
Apr 26, 2001
234
235
}
May 28, 2006
May 28, 2006
236
237
static void
DSp_DeleteDevice (SDL_VideoDevice * device)
Apr 26, 2001
Apr 26, 2001
238
{
May 28, 2006
May 28, 2006
239
240
241
242
243
244
245
246
247
248
249
250
/* -dw- taking no chances with null pointers */
if (device) {
if (device->hidden) {
if (device->hidden->dspinfo)
SDL_free (device->hidden->dspinfo);
SDL_free (device->hidden);
}
SDL_free (device);
}
Apr 26, 2001
Apr 26, 2001
251
252
}
May 28, 2006
May 28, 2006
253
254
static SDL_VideoDevice *
DSp_CreateDevice (int devindex)
Apr 26, 2001
Apr 26, 2001
255
{
May 28, 2006
May 28, 2006
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
SDL_VideoDevice *device;
/* Initialize all variables that we clean on shutdown */
device = (SDL_VideoDevice *) SDL_malloc (sizeof (SDL_VideoDevice));
if (device) {
SDL_memset (device, 0, sizeof (*device));
device->hidden = (struct SDL_PrivateVideoData *)
SDL_malloc ((sizeof *device->hidden));
if (device->hidden)
SDL_memset (device->hidden, 0, sizeof (*(device->hidden)));
}
if ((device == NULL) || (device->hidden == NULL)) {
SDL_OutOfMemory ();
if (device) {
if (device->hidden)
SDL_free (device->hidden);
SDL_free (device);
}
return (NULL);
}
/* Allocate DrawSprocket information */
device->hidden->dspinfo = (struct DSpInfo *) SDL_malloc ((sizeof *device->
hidden->
dspinfo));
if (device->hidden->dspinfo == NULL) {
SDL_OutOfMemory ();
SDL_free (device->hidden);
SDL_free (device);
return (0);
}
SDL_memset (device->hidden->dspinfo, 0,
(sizeof *device->hidden->dspinfo));
/* 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
310
#if SDL_VIDEO_OPENGL
May 28, 2006
May 28, 2006
311
312
313
314
device->GL_MakeCurrent = Mac_GL_MakeCurrent;
device->GL_SwapBuffers = DSp_GL_SwapBuffers;
device->GL_LoadLibrary = Mac_GL_LoadLibrary;
device->GL_GetProcAddress = Mac_GL_GetProcAddress;
Apr 26, 2001
Apr 26, 2001
315
#endif
May 28, 2006
May 28, 2006
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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;
Apr 26, 2001
Apr 26, 2001
334
335
336
}
VideoBootStrap DSp_bootstrap = {
May 28, 2006
May 28, 2006
337
338
"DSp", "MacOS DrawSprocket",
DSp_Available, DSp_CreateDevice
Apr 26, 2001
Apr 26, 2001
339
340
341
};
/* Use DSp/Display Manager to build mode list for given screen */
May 28, 2006
May 28, 2006
342
343
344
static SDL_Rect **
DSp_BuildModeList (const GDHandle gDevice, int *displayWidth,
int *displayHeight)
Apr 26, 2001
Apr 26, 2001
345
{
May 28, 2006
May 28, 2006
346
347
348
349
350
351
352
353
354
355
356
357
358
359
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
390
391
392
393
394
395
396
397
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
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;
*displayWidth = attributes.displayWidth;
*displayHeight = attributes.displayHeight;
for (i = 0; i < SDL_arraysize (temp_list); i++) {
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 */
mode_list = (SDL_Rect **) SDL_malloc (sizeof (SDL_Rect *) * (i + 1));
if (mode_list) {
/* -dw- new stuff: build in reverse order so largest sizes list first */
for (j = i - 1; j >= 0; j--) {
mode_list[j] = (SDL_Rect *) SDL_malloc (sizeof (SDL_Rect));
if (mode_list[j])
SDL_memcpy (mode_list[j], &(temp_list[j]), sizeof (SDL_Rect));
else {
SDL_OutOfMemory ();
return NULL;
}
}
mode_list[i] = NULL; /* append null to the end */
} else {
SDL_OutOfMemory ();
return NULL;
}
return mode_list;
Apr 26, 2001
Apr 26, 2001
426
427
}
May 28, 2006
May 28, 2006
428
429
static void
DSp_IsHWAvailable (_THIS, SDL_PixelFormat * vformat)
Apr 26, 2001
Apr 26, 2001
430
{
May 28, 2006
May 28, 2006
431
432
433
434
435
/*
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.
*/
Apr 26, 2001
Apr 26, 2001
436
May 28, 2006
May 28, 2006
437
438
439
440
441
442
443
444
445
446
447
448
449
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);
Apr 26, 2001
Apr 26, 2001
450
Sep 8, 2005
Sep 8, 2005
451
#if useDistantHdwrMem && useLocalHdwrMem
May 28, 2006
May 28, 2006
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
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
467
#endif
May 28, 2006
May 28, 2006
468
}
Apr 26, 2001
Apr 26, 2001
469
470
}
May 28, 2006
May 28, 2006
471
472
static int
DSp_GetMainDevice (_THIS, GDHandle * device)
Apr 26, 2001
Apr 26, 2001
473
{
May 28, 2006
May 28, 2006
474
Apr 26, 2001
Apr 26, 2001
475
#if TARGET_API_MAC_OSX
May 28, 2006
May 28, 2006
476
477
478
/* DSpUserSelectContext not available on OS X */
*device = GetMainDevice ();
return 0;
Apr 26, 2001
Apr 26, 2001
479
480
#else
May 28, 2006
May 28, 2006
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
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;
}
SDL_memset (&attrib, 0, sizeof (DSpContextAttributes));
/* 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);
Apr 26, 2001
Apr 26, 2001
535
536
537
#endif
}
May 28, 2006
May 28, 2006
538
539
static int
DSp_VideoInit (_THIS, SDL_PixelFormat * vformat)
Apr 26, 2001
Apr 26, 2001
540
{
May 28, 2006
May 28, 2006
541
542
NumVersion dsp_version = { 0x01, 0x00, 0x00, 0x00 };
Sep 8, 2005
Sep 8, 2005
543
#if UNIVERSAL_INTERFACES_VERSION > 0x0320
May 28, 2006
May 28, 2006
544
dsp_version = DSpGetVersion ();
Sep 8, 2005
Sep 8, 2005
545
#endif
May 28, 2006
May 28, 2006
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
625
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,
&this->info.current_w,
&this->info.current_h);
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);
Apr 26, 2001
Apr 26, 2001
626
627
}
May 28, 2006
May 28, 2006
628
629
static SDL_Rect **
DSp_ListModes (_THIS, SDL_PixelFormat * format, Uint32 flags)
Apr 26, 2001
Apr 26, 2001
630
{
May 28, 2006
May 28, 2006
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
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;
Apr 26, 2001
Apr 26, 2001
649
650
651
}
/* Various screen update functions available */
May 28, 2006
May 28, 2006
652
static void DSp_DirectUpdate (_THIS, int numrects, SDL_Rect * rects);
Apr 26, 2001
Apr 26, 2001
653
Sep 8, 2005
Sep 8, 2005
654
655
#if ! TARGET_API_MAC_OSX
Apr 26, 2001
Apr 26, 2001
656
657
static volatile unsigned int retrace_count = 0; /* -dw- need volatile because it updates asychronously */
May 28, 2006
May 28, 2006
658
659
Boolean
DSp_VBLProc (DSpContextReference context, void *ref_con)
Apr 26, 2001
Apr 26, 2001
660
{
May 28, 2006
May 28, 2006
661
662
663
retrace_count++;
return 1; /* Darrell, is this right? */
Apr 26, 2001
Apr 26, 2001
664
665
}
May 28, 2006
May 28, 2006
666
667
static void
DSp_SetHWError (OSStatus err, int is_agp)
Apr 26, 2001
Apr 26, 2001
668
{
May 28, 2006
May 28, 2006
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
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;
}
SDL_snprintf (message, SDL_arraysize (message), fmt, mem);
SDL_SetError (message);
Apr 26, 2001
Apr 26, 2001
690
}
Sep 8, 2005
Sep 8, 2005
691
#endif // TARGET_API_MAC_OSX
Apr 26, 2001
Apr 26, 2001
692
693
/* put up a dialog to verify display change */
May 28, 2006
May 28, 2006
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
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);
Apr 26, 2001
Apr 26, 2001
714
May 17, 2006
May 17, 2006
715
#if TARGET_API_MAC_CARBON
May 28, 2006
May 28, 2006
716
SetPort (GetDialogPort (dialog));
Sep 8, 2005
Sep 8, 2005
717
#else
May 28, 2006
May 28, 2006
718
SetPort ((WindowPtr) dialog);
Sep 8, 2005
Sep 8, 2005
719
#endif
Apr 26, 2001
Apr 26, 2001
720
May 28, 2006
May 28, 2006
721
722
SetDialogDefaultItem (dialog, bCancel);
SetDialogCancelItem (dialog, bCancel);
Apr 26, 2001
Apr 26, 2001
723
May 28, 2006
May 28, 2006
724
725
SetEventMask (everyEvent);
FlushEvents (everyEvent, 0);
Sep 8, 2005
Sep 8, 2005
726
May 28, 2006
May 28, 2006
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
/* 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);
DisposeDialog (dialog);
SetPort (savePort);
SetEventMask (everyEvent - autoKeyMask);
FlushEvents (everyEvent, 0);
return (item - 1);
Apr 26, 2001
Apr 26, 2001
750
751
}
May 28, 2006
May 28, 2006
752
753
static void
DSp_UnsetVideoMode (_THIS, SDL_Surface * current)
Apr 26, 2001
Apr 26, 2001
754
{
May 28, 2006
May 28, 2006
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
if (current->flags & SDL_INTERNALOPENGL) {
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)
SDL_free (current->hwdata);
DSpContext_SetState (dsp_context, kDSpContextState_Inactive);
DSpContext_Release (dsp_context);
dsp_context = NULL;
}
Apr 26, 2001
Apr 26, 2001
778
779
780
if (SDL_Window != NULL) {
DisposeWindow (SDL_Window);
SDL_Window = NULL;
May 28, 2006
May 28, 2006
781
782
}
Apr 26, 2001
Apr 26, 2001
783
current->pixels = NULL;
May 28, 2006
May 28, 2006
784
current->flags = 0;
Apr 26, 2001
Apr 26, 2001
785
786
}
May 28, 2006
May 28, 2006
787
788
789
790
static SDL_Surface *
DSp_SetVideoMode (_THIS,
SDL_Surface * current, int width, int height, int bpp,
Uint32 flags)
Apr 26, 2001
Apr 26, 2001
791
{
May 28, 2006
May 28, 2006
792
Sep 8, 2005
Sep 8, 2005
793
#if !TARGET_API_MAC_OSX
May 28, 2006
May 28, 2006
794
795
DisplayIDType display_id;
Fixed freq;
Sep 8, 2005
Sep 8, 2005
796
#endif
May 28, 2006
May 28, 2006
797
798
799
800
801
802
803
804
805
806
807
DSpContextAttributes attrib;
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);
Apr 26, 2001
Apr 26, 2001
808
809
810
if (bpp != dsp_old_depth)
DSp_DestroyPalette (this);
May 28, 2006
May 28, 2006
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
867
868
869
870
871
872
873
874
875
876
877
878
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;
}
SDL_memset (&attrib, 0, sizeof (DSpContextAttributes));
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;
#if TARGET_API_MAC_OSX || UNIVERSAL_INTERFACES_VERSION == 0x0320
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;
}
if (DSpFindBestContextOnDisplayID (&attrib, &dsp_context, display_id) !=
noErr) {
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");
Apr 26, 2001
Apr 26, 2001
879
880
881
return NULL;
}
}
May 28, 2006
May 28, 2006
882
883
884
885
886
887
888
889
890
891
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
942
943
944
945
946
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
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);
current->hwdata =
(private_hwdata *) SDL_malloc (sizeof (private_hwdata));
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) {
current->hwdata =
(private_hwdata *) SDL_malloc (sizeof (private_hwdata));
if (current->hwdata == NULL) {
SDL_OutOfMemory ();
return NULL;
}
SDL_memset (current->hwdata, 0, sizeof (private_hwdata));
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_INTERNALOPENGL))
DSpContext_SetVBLProc (dsp_context, DSp_VBLProc, NULL);
#endif
if (bpp == 8)
current->flags |= SDL_HWPALETTE;
if (flags & SDL_INTERNALOPENGL) {
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
987
#if TARGET_API_MAC_CARBON
May 28, 2006
May 28, 2006
988
SetPort (GetWindowPort (SDL_Window));
Sep 8, 2005
Sep 8, 2005
989
#else
May 28, 2006
May 28, 2006
990
SetPort (SDL_Window);
Sep 8, 2005
Sep 8, 2005
991
#endif
May 28, 2006
May 28, 2006
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) {