Mercurial > sdl-ios-xcode
annotate src/video/macdsp/SDL_dspvideo.c @ 1336:3692456e7b0f
Use SDL_ prefixed versions of C library functions.
FIXME:
Change #include <stdlib.h> to #include "SDL_stdlib.h"
Change #include <string.h> to #include "SDL_string.h"
Make sure nothing else broke because of this...
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 07 Feb 2006 06:59:48 +0000 |
parents | c9b51268668f |
children | 604d73db6802 |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
0 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
0 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
13 Lesser General Public License for more details. |
0 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1135
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
0 | 18 |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 /* | |
24 Written by Darrell Walisser <dwaliss1@purdue.edu> | |
25 | |
26 Implementation notes ---------------------------------------------------------------------- | |
27 | |
28 A bit on GWorlds in VRAM from technote 1182: | |
29 | |
30 There are two important things to note about GWorld's allocated in | |
31 VRAM. First, the base address retrieved through GetPixBaseAddr or | |
32 read directly from the PixMap structure can become invalid anytime | |
33 memory is allocated in VRAM. This can occur either by explicit | |
34 allocations, such as calls to NewGWorld, or by implicit ones, such as | |
35 those associated with the internal texture allocation of OpenGL. The | |
36 stored pixel images themselves will still be valid but may have been | |
37 moved in VRAM, thus rendering any stored base addresses invalid. | |
38 You should never store an image's base address for longer than is | |
39 necessary and especially never across calls to NewGWorld or | |
40 texture-creation routines. | |
41 | |
42 Secondly, an offscreen pixel image allocated in VRAM can be | |
43 purged at system task time by the display driver. This means any | |
44 time your application yields time such by calling WaitNextEvent or | |
45 SystemTask you can lose your VRAM GWorld contents. While this | |
46 happens infrequently, usually associated with display resolution or | |
47 pixel depth changes you must code for this eventuality. This purge | |
48 can occur whether or not the GWorld is locked or not. A return value | |
49 of false from LockPixels, a NULL return value from GetPixBaseAddr | |
50 or NULL in the baseAddr field of the PixMap mean that the pixel | |
51 image has been purged. To reallocate it you can either call | |
52 UpdateGWorld or Dispose your current GWorld through | |
53 DisposeGWorld and reallocate it via NewGWorld. Either way you must | |
54 then rebuild the pixel image. | |
55 | |
56 ------------------------------------------------------------------------------------ | |
57 | |
58 Currently, I don't account for (1). In my testing, NewGWorld never invalidated | |
59 other existing GWorlds in VRAM. However, I do have protection for (2). | |
60 Namely, I am using GetOSEvent() instead of WaitNextEvent() so that there are no | |
61 context switches (the app hogs the CPU). Eventually a book-keeping system should | |
62 be coded to take care of (1) and (2). | |
63 | |
64 ------------------------------------------------------------------------------------ | |
65 | |
66 System requirements (* denotes optional): | |
67 | |
68 1. DrawSprocket 1.7.3 | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
69 2. *MacOS 9 or later (but *not* Mac OS X) for hardware accelerated blit / fill |
0 | 70 3. *May also require certain graphics hardware for (2). I trust that all Apple OEM |
71 hardware will work. Third party accelerators may work if they have QuickDraw | |
72 acceleration in the drivers and the drivers have been updated for OS 9. The current | |
73 Voodoo 3 drivers (1.0b12) do not work. | |
74 | |
75 Coding suggestions: | |
76 | |
77 1. Use SDL_UpdateRects ! | |
78 | |
79 If no QuickDraw acceleration is present, double-buffered surfaces will use a back buffer | |
80 in System memory. I recommend you use SDL_UpdateRects with double-buffered surfaces | |
81 for best performance on these cards, since the overhead is nearly zero for VRAM back buffer. | |
82 | |
83 2. Load most-resident surfaces first. | |
84 | |
85 If you fill up VRAM or AGP memory, there is no contingency for purging to make room for the next one. | |
86 Therefore, you should load the surfaces you plan to use the most frequently first. | |
87 Sooner or later, I will code LRU replacement to help this. | |
88 | |
89 TODO: | |
90 Some kind of posterized mode for resolutions < 640x480. | |
91 Window support / fullscreen toggle. | |
92 Figure out how much VRAM is available. Put in video->info->video_mem. | |
93 Track VRAM usage. | |
94 | |
95 BUGS: | |
96 I can't create a hardware surface the same size as the screen?! How to fix? | |
97 | |
98 | |
99 | |
100 COMPILE OPTIONS: | |
101 | |
102 DSP_TRY_CC_AND_AA - Define if you want to try HWA color-key and alpha blitters | |
103 HW color-key blitting gives substantial improvements, | |
104 but hw alpha is neck-and-neck with SDL's soft bitter. | |
105 | |
106 DSP_NO_SYNC_VBL - Define for HWA double-buffered surfaces: don't sync | |
107 pseudo-flip to monitor redraw. | |
108 | |
109 DSP_NO_SYNC_OPENGL - Define for OpenGL surfaces: don't sync buffer swap. Synching buffer | |
110 swap may result in reduced performance, but can eliminate some | |
111 tearing artifacts. | |
112 CHANGELOG: | |
113 09/17/00 Lots of little tweaks. Build modelist in reverse order so largest contexts | |
114 list first. Compared various methods with ROM methods and fixed rez switch | |
115 crashing bug in GL Tron. (Woohoo!) | |
116 */ | |
117 | |
118 #define DSP_TRY_CC_AND_AA | |
119 | |
120 /* #define DSP_NO_SYNC_VBL */ | |
121 | |
122 #define DSP_NO_SYNC_OPENGL | |
123 | |
124 | |
125 #include <stdio.h> | |
126 #include <stdlib.h> | |
127 | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
128 #if defined(__APPLE__) && defined(__MACH__) |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
129 #include <Carbon/Carbon.h> |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
130 #include <DrawSprocket/DrawSprocket.h> |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
131 #elif TARGET_API_MAC_CARBON && (UNIVERSAL_INTERFACES_VERSION > 0x0335) |
0 | 132 #include <Carbon.h> |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
133 #include <DrawSprocket.h> |
0 | 134 #else |
135 #include <LowMem.h> | |
136 #include <Gestalt.h> | |
137 #include <Devices.h> | |
138 #include <DiskInit.h> | |
139 #include <QDOffscreen.h> | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
140 #include <DrawSprocket.h> |
0 | 141 #endif |
142 | |
143 #include "SDL_video.h" | |
144 #include "SDL_blit.h" | |
145 #include "SDL_error.h" | |
146 #include "SDL_syswm.h" | |
147 #include "SDL_sysvideo.h" | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
148 #include "SDL_pixels_c.h" |
0 | 149 #include "SDL_dspvideo.h" |
150 #include "SDL_macgl_c.h" | |
151 #include "SDL_macwm_c.h" | |
152 #include "SDL_macmouse_c.h" | |
153 #include "SDL_macevents_c.h" | |
154 | |
155 /* Initialization/Query functions */ | |
156 static int DSp_VideoInit(_THIS, SDL_PixelFormat *vformat); | |
157 static SDL_Rect **DSp_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags); | |
158 static SDL_Surface *DSp_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags); | |
159 static int DSp_SetColors(_THIS, int firstcolor, int ncolors, | |
160 SDL_Color *colors); | |
161 static int DSp_CreatePalette(_THIS); | |
162 static int DSp_DestroyPalette(_THIS); | |
163 static void DSp_VideoQuit(_THIS); | |
164 | |
165 static int DSp_GetMainDevice (_THIS, GDHandle *device); | |
166 static void DSp_IsHWAvailable (_THIS, SDL_PixelFormat *vformat); | |
167 static void DSp_DSpUpdate(_THIS, int numrects, SDL_Rect *sdl_rects); | |
168 static void DSp_DirectUpdate(_THIS, int numrects, SDL_Rect *sdl_rects); | |
169 | |
170 /* Hardware surface functions */ | |
171 static int DSp_SetHWAlpha(_THIS, SDL_Surface *surface, UInt8 alpha); | |
172 static int DSp_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key); | |
173 static int DSp_NewHWSurface(_THIS, CGrafPtr *port, int depth, int width, int height); | |
174 static int DSp_AllocHWSurface(_THIS, SDL_Surface *surface); | |
175 static int DSp_LockHWSurface(_THIS, SDL_Surface *surface); | |
176 static void DSp_UnlockHWSurface(_THIS, SDL_Surface *surface); | |
177 static void DSp_FreeHWSurface(_THIS, SDL_Surface *surface); | |
178 static int DSp_FlipHWSurface(_THIS, SDL_Surface *surface); | |
179 static int DSp_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dest); | |
180 static int DSp_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect, | |
181 SDL_Surface *dst, SDL_Rect *dstrect); | |
182 static int DSp_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color); | |
183 | |
184 #ifdef HAVE_OPENGL | |
185 static void DSp_GL_SwapBuffers (_THIS); | |
186 #endif | |
187 | |
188 #if ! TARGET_API_MAC_CARBON | |
189 | |
190 #define GetPortPixRowBytes(x) ( (*(x->portPixMap))->rowBytes ) | |
191 #define GetGDevPixMap(x) ((**(x)).gdPMap) | |
192 #define GetPortPixMap(x) ((*(x)).portPixMap) | |
193 | |
194 #define GetPixDepth(y) ((**(y)).pixelSize) | |
195 //#define GetPixRowBytes(y) ((**(y)).rowBytes) | |
196 //#define GetPixBaseAddr(y) ((**(y)).baseAddr) | |
197 #define GetPixCTab(y) ((**(y)).pmTable) | |
198 #define GetPortBitMapForCopyBits(x) (&(((GrafPtr)(x))->portBits)) | |
199 | |
200 #else | |
201 #define GetPortPixRowBytes(x) (GetPixRowBytes(GetPortPixMap(x)) ) | |
202 #define GetGDevPixMap(x) ((**(x)).gdPMap) | |
203 | |
204 #endif | |
205 | |
206 typedef struct private_hwdata { | |
207 | |
208 GWorldPtr offscreen; // offscreen gworld in VRAM or AGP | |
209 | |
210 #ifdef DSP_TRY_CC_AND_AA | |
211 GWorldPtr mask; // transparent mask | |
212 RGBColor alpha; // alpha color | |
213 RGBColor trans; // transparent color | |
214 #endif | |
215 | |
216 } private_hwdata; | |
217 | |
218 typedef private_hwdata private_swdata ; /* have same fields */ | |
219 | |
220 /* Macintosh toolbox driver bootstrap functions */ | |
221 | |
222 static int DSp_Available(void) | |
223 { | |
224 /* Check for DrawSprocket */ | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
225 #if ! TARGET_API_MAC_OSX |
0 | 226 /* This check is only meaningful if you weak-link DrawSprocketLib */ |
227 return ((Ptr)DSpStartup != (Ptr)kUnresolvedCFragSymbolAddress); | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
228 #else |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
229 return 1; // DrawSprocket.framework doesn't have it all, but it's there |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
230 #endif |
0 | 231 } |
232 | |
233 static void DSp_DeleteDevice(SDL_VideoDevice *device) | |
234 { | |
235 /* -dw- taking no chances with null pointers */ | |
236 if (device) { | |
237 | |
238 if (device->hidden) { | |
239 | |
240 if (device->hidden->dspinfo) | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
241 SDL_free(device->hidden->dspinfo); |
0 | 242 |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
243 SDL_free(device->hidden); |
0 | 244 } |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
245 SDL_free(device); |
0 | 246 } |
247 } | |
248 | |
249 static SDL_VideoDevice *DSp_CreateDevice(int devindex) | |
250 { | |
251 SDL_VideoDevice *device; | |
252 | |
253 /* Initialize all variables that we clean on shutdown */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
254 device = (SDL_VideoDevice *)SDL_malloc(sizeof(SDL_VideoDevice)); |
0 | 255 if ( device ) { |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
256 SDL_memset(device, 0, sizeof (*device)); |
0 | 257 device->hidden = (struct SDL_PrivateVideoData *) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
258 SDL_malloc((sizeof *device->hidden)); |
0 | 259 if (device->hidden) |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
260 SDL_memset(device->hidden, 0, sizeof ( *(device->hidden) ) ); |
0 | 261 } |
262 if ( (device == NULL) || (device->hidden == NULL) ) { | |
263 SDL_OutOfMemory(); | |
264 | |
265 if ( device ) { | |
266 | |
267 if (device->hidden) | |
268 free (device->hidden); | |
269 | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
270 SDL_free(device); |
0 | 271 } |
272 | |
273 return(NULL); | |
274 } | |
275 | |
276 /* Allocate DrawSprocket information */ | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
277 device->hidden->dspinfo = (struct DSpInfo *)SDL_malloc( |
0 | 278 (sizeof *device->hidden->dspinfo)); |
279 if ( device->hidden->dspinfo == NULL ) { | |
280 SDL_OutOfMemory(); | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
281 SDL_free(device->hidden); |
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
282 SDL_free(device); |
0 | 283 return(0); |
284 } | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
285 SDL_memset(device->hidden->dspinfo, 0, (sizeof *device->hidden->dspinfo)); |
0 | 286 |
287 /* Set the function pointers */ | |
288 device->VideoInit = DSp_VideoInit; | |
289 device->ListModes = DSp_ListModes; | |
290 device->SetVideoMode = DSp_SetVideoMode; | |
291 device->SetColors = DSp_SetColors; | |
292 device->UpdateRects = NULL; | |
293 device->VideoQuit = DSp_VideoQuit; | |
294 device->AllocHWSurface = DSp_AllocHWSurface; | |
295 device->CheckHWBlit = NULL; | |
296 device->FillHWRect = NULL; | |
297 device->SetHWColorKey = NULL; | |
298 device->SetHWAlpha = NULL; | |
299 device->LockHWSurface = DSp_LockHWSurface; | |
300 device->UnlockHWSurface = DSp_UnlockHWSurface; | |
301 device->FlipHWSurface = DSp_FlipHWSurface; | |
302 device->FreeHWSurface = DSp_FreeHWSurface; | |
303 #ifdef HAVE_OPENGL | |
304 device->GL_MakeCurrent = Mac_GL_MakeCurrent; | |
305 device->GL_SwapBuffers = DSp_GL_SwapBuffers; | |
1032
c1c2efca4548
Date: Mon, 24 Jan 2005 21:37:56 +0800
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
306 device->GL_LoadLibrary = Mac_GL_LoadLibrary; |
c1c2efca4548
Date: Mon, 24 Jan 2005 21:37:56 +0800
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
307 device->GL_GetProcAddress = Mac_GL_GetProcAddress; |
0 | 308 #endif |
309 device->SetCaption = NULL; | |
310 device->SetIcon = NULL; | |
311 device->IconifyWindow = NULL; | |
312 device->GrabInput = NULL; | |
313 device->GetWMInfo = NULL; | |
314 device->FreeWMCursor = Mac_FreeWMCursor; | |
315 device->CreateWMCursor = Mac_CreateWMCursor; | |
316 device->ShowWMCursor = Mac_ShowWMCursor; | |
317 device->WarpWMCursor = Mac_WarpWMCursor; | |
318 device->InitOSKeymap = Mac_InitOSKeymap; | |
319 device->PumpEvents = Mac_PumpEvents; | |
320 | |
321 device->GrabInput = NULL; | |
322 device->CheckMouseMode = NULL; | |
323 | |
324 device->free = DSp_DeleteDevice; | |
325 | |
326 return device; | |
327 } | |
328 | |
329 VideoBootStrap DSp_bootstrap = { | |
330 "DSp", "MacOS DrawSprocket", | |
331 DSp_Available, DSp_CreateDevice | |
332 }; | |
333 | |
334 /* Use DSp/Display Manager to build mode list for given screen */ | |
335 static SDL_Rect** DSp_BuildModeList (const GDHandle gDevice) | |
336 { | |
337 DSpContextAttributes attributes; | |
338 DSpContextReference context; | |
339 DisplayIDType displayID; | |
340 SDL_Rect temp_list [16]; | |
341 SDL_Rect **mode_list; | |
342 int width, height, i, j; | |
343 | |
344 #if TARGET_API_MAC_OSX | |
345 | |
346 displayID = 0; | |
347 | |
348 #else | |
349 /* Ask Display Manager for integer id of screen device */ | |
350 if ( DMGetDisplayIDByGDevice (gDevice, &displayID, SDL_TRUE) != noErr ) { | |
351 return NULL; | |
352 } | |
353 #endif | |
354 /* Get the first possible DSp context on this device */ | |
355 if ( DSpGetFirstContext (displayID, &context) != noErr ) { | |
356 return NULL; | |
357 } | |
358 | |
359 if ( DSpContext_GetAttributes (context, &attributes) != noErr ) | |
360 return NULL; | |
361 | |
362 for ( i = 0; i < SDL_TABLESIZE(temp_list); i++ ) { | |
363 width = attributes.displayWidth; | |
364 height = attributes.displayHeight; | |
365 | |
366 temp_list [i].x = 0 | attributes.displayBestDepth; | |
367 temp_list [i].y = 0; | |
368 temp_list [i].w = width; | |
369 temp_list [i].h = height; | |
370 | |
371 /* DSp will report many different contexts with the same width and height. */ | |
372 /* They will differ in bit depth and refresh rate. */ | |
373 /* We will ignore them until we reach one with a different width/height */ | |
374 /* When there are no more contexts to look at, we will quit building the list*/ | |
375 while ( width == attributes.displayWidth && height == attributes.displayHeight ) { | |
376 | |
377 OSStatus err = DSpGetNextContext (context, &context); | |
378 if (err != noErr) | |
379 if (err == kDSpContextNotFoundErr) | |
380 goto done; | |
381 else | |
382 return NULL; | |
383 | |
384 if ( DSpContext_GetAttributes (context, &attributes) != noErr ) | |
385 return NULL; | |
386 | |
387 temp_list [i].x |= attributes.displayBestDepth; | |
388 } | |
389 } | |
390 done: | |
391 i++; /* i was not incremented before kicking out of the loop */ | |
392 | |
393 mode_list = (SDL_Rect**) malloc (sizeof (SDL_Rect*) * (i+1)); | |
394 if (mode_list) { | |
395 | |
396 /* -dw- new stuff: build in reverse order so largest sizes list first */ | |
397 for (j = i-1; j >= 0; j--) { | |
398 mode_list [j] = (SDL_Rect*) malloc (sizeof (SDL_Rect)); | |
399 if (mode_list [j]) | |
400 memcpy (mode_list [j], &(temp_list [j]), sizeof (SDL_Rect)); | |
401 else { | |
402 SDL_OutOfMemory (); | |
403 return NULL; | |
404 } | |
405 } | |
406 mode_list [i] = NULL; /* append null to the end */ | |
407 } | |
408 else { | |
409 SDL_OutOfMemory (); | |
410 return NULL; | |
411 } | |
412 | |
413 return mode_list; | |
414 } | |
415 | |
416 static void DSp_IsHWAvailable (_THIS, SDL_PixelFormat *vformat) | |
417 { | |
418 /* | |
419 VRAM GWorlds are only available on OS 9 or later. | |
420 Even with OS 9, some display drivers won't support it, | |
421 so we create a test GWorld and check for errors. | |
422 */ | |
423 | |
424 long versionSystem; | |
425 | |
426 dsp_vram_available = SDL_FALSE; | |
427 dsp_agp_available = SDL_FALSE; | |
428 | |
429 Gestalt ('sysv', &versionSystem); | |
430 if (0x00000860 < (versionSystem & 0x0000FFFF)) { | |
431 | |
432 GWorldPtr offscreen; | |
433 OSStatus err; | |
434 Rect bounds; | |
435 | |
436 SetRect (&bounds, 0, 0, 320, 240); | |
437 | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
438 #if useDistantHdwrMem && useLocalHdwrMem |
0 | 439 err = NewGWorld (&offscreen, vformat->BitsPerPixel, &bounds, NULL, SDL_Display, useDistantHdwrMem | noNewDevice); |
440 if (err == noErr) { | |
441 dsp_vram_available = SDL_TRUE; | |
442 DisposeGWorld (offscreen); | |
443 } | |
444 | |
445 err = NewGWorld (&offscreen, vformat->BitsPerPixel, &bounds, NULL, SDL_Display, useLocalHdwrMem | noNewDevice); | |
446 if (err == noErr) { | |
447 DisposeGWorld (offscreen); | |
448 dsp_agp_available = SDL_TRUE; | |
449 } | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
450 #endif |
0 | 451 } |
452 } | |
453 | |
454 static int DSp_GetMainDevice (_THIS, GDHandle *device) | |
455 { | |
456 | |
457 #if TARGET_API_MAC_OSX | |
458 /* DSpUserSelectContext not available on OS X */ | |
459 *device = GetMainDevice(); | |
460 return 0; | |
461 #else | |
462 | |
463 DSpContextAttributes attrib; | |
464 DSpContextReference context; | |
465 DisplayIDType display_id; | |
466 GDHandle main_device; | |
467 GDHandle device_list; | |
468 | |
469 device_list = GetDeviceList (); | |
470 main_device = GetMainDevice (); | |
471 | |
472 /* Quick check to avoid slower method when only one display exists */ | |
473 if ( (**device_list).gdNextGD == NULL ) { | |
474 *device = main_device; | |
475 return 0; | |
476 } | |
477 | |
478 memset (&attrib, 0, sizeof (DSpContextAttributes)); | |
479 | |
480 /* These attributes are hopefully supported on all devices...*/ | |
481 attrib.displayWidth = 640; | |
482 attrib.displayHeight = 480; | |
483 attrib.displayBestDepth = 8; | |
484 attrib.backBufferBestDepth = 8; | |
485 attrib.displayDepthMask = kDSpDepthMask_All; | |
486 attrib.backBufferDepthMask = kDSpDepthMask_All; | |
487 attrib.colorNeeds = kDSpColorNeeds_Require; | |
488 attrib.pageCount = 1; | |
489 | |
490 if (noErr != DMGetDisplayIDByGDevice (main_device, &display_id, SDL_FALSE)) { | |
491 SDL_SetError ("Display Manager couldn't associate GDevice with a Display ID"); | |
492 return (-1); | |
493 } | |
494 | |
495 /* Put up dialog on main display to select which display to use */ | |
496 if (noErr != DSpUserSelectContext (&attrib, display_id, NULL, &context)) { | |
497 SDL_SetError ("DrawSprocket couldn't create a context"); | |
498 return (-1); | |
499 } | |
500 | |
501 if (noErr != DSpContext_GetDisplayID (context, &display_id)) { | |
502 SDL_SetError ("DrawSprocket couldn't get display ID"); | |
503 return (-1); | |
504 } | |
505 | |
506 if (noErr != DMGetGDeviceByDisplayID (display_id, &main_device, SDL_FALSE)) { | |
507 SDL_SetError ("Display Manager couldn't associate Display ID with GDevice"); | |
508 return (-1); | |
509 } | |
510 | |
511 *device = main_device; | |
512 return (0); | |
513 #endif | |
514 } | |
515 | |
516 static int DSp_VideoInit(_THIS, SDL_PixelFormat *vformat) | |
517 { | |
1135
cf6133247d34
Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents:
1133
diff
changeset
|
518 NumVersion dsp_version = { 0x01, 0x00, 0x00, 0x00 }; |
0 | 519 |
1135
cf6133247d34
Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents:
1133
diff
changeset
|
520 #if UNIVERSAL_INTERFACES_VERSION > 0x0320 |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
521 dsp_version = DSpGetVersion (); |
1135
cf6133247d34
Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents:
1133
diff
changeset
|
522 #endif |
0 | 523 |
524 if ( (dsp_version.majorRev == 1 && dsp_version.minorAndBugRev < 0x73) || | |
525 (dsp_version.majorRev < 1) ) { | |
526 | |
527 /* StandardAlert (kAlertStopAlert, "\pError!", | |
528 "\pI need DrawSprocket 1.7.3 or later!\n" | |
529 "You can find a newer version at http://www.apple.com/swupdates.", | |
530 NULL, NULL); | |
531 */ | |
532 SDL_SetError ("DrawSprocket version is too old. Need 1.7.3 or later."); | |
533 return (-1); | |
534 } | |
535 | |
536 if ( DSpStartup () != noErr ) { | |
537 SDL_SetError ("DrawSprocket couldn't startup"); | |
538 return(-1); | |
539 } | |
540 | |
541 /* Start DSpintosh events */ | |
542 Mac_InitEvents(this); | |
543 | |
544 /* Get a handle to the main monitor, or choose one on multiple monitor setups */ | |
545 if ( DSp_GetMainDevice(this, &SDL_Display) < 0) | |
546 return (-1); | |
547 | |
548 /* Determine pixel format */ | |
549 vformat->BitsPerPixel = GetPixDepth ( (**SDL_Display).gdPMap ); | |
550 dsp_old_depth = vformat->BitsPerPixel; | |
551 | |
552 switch (vformat->BitsPerPixel) { | |
553 case 16: | |
554 vformat->Rmask = 0x00007c00; | |
555 vformat->Gmask = 0x000003e0; | |
556 vformat->Bmask = 0x0000001f; | |
557 break; | |
558 default: | |
559 break; | |
560 } | |
561 | |
562 if ( DSp_CreatePalette (this) < 0 ) { | |
563 | |
564 SDL_SetError ("Could not create palette"); | |
565 return (-1); | |
566 } | |
567 | |
568 /* Get a list of available fullscreen modes */ | |
569 SDL_modelist = DSp_BuildModeList (SDL_Display); | |
570 if (SDL_modelist == NULL) { | |
571 SDL_SetError ("DrawSprocket could not build a mode list"); | |
572 return (-1); | |
573 } | |
574 | |
575 /* Check for VRAM and AGP GWorlds for HW Blitting */ | |
576 DSp_IsHWAvailable (this, vformat); | |
577 | |
578 this->info.wm_available = 0; | |
579 | |
580 if (dsp_vram_available || dsp_agp_available) { | |
581 | |
582 this->info.hw_available = SDL_TRUE; | |
583 | |
584 this->CheckHWBlit = DSp_CheckHWBlit; | |
585 this->info.blit_hw = SDL_TRUE; | |
586 | |
587 this->FillHWRect = DSp_FillHWRect; | |
588 this->info.blit_fill = SDL_TRUE; | |
589 | |
590 #ifdef DSP_TRY_CC_AND_AA | |
591 this->SetHWColorKey = DSp_SetHWColorKey; | |
592 this->info.blit_hw_CC = SDL_TRUE; | |
593 | |
594 this->SetHWAlpha = DSp_SetHWAlpha; | |
595 this->info.blit_hw_A = SDL_TRUE; | |
596 #endif | |
597 | |
598 } | |
599 | |
600 return(0); | |
601 } | |
602 | |
603 static SDL_Rect **DSp_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags) | |
604 { | |
605 static SDL_Rect *dsp_modes[16]; | |
606 int i = 0, j = 0; | |
607 | |
608 if ( format->BitsPerPixel == 0 ) | |
609 return ( (SDL_Rect**) NULL ); | |
610 | |
611 while (SDL_modelist[i] != NULL) { | |
612 | |
613 if (SDL_modelist[i]->x & format->BitsPerPixel) { | |
614 dsp_modes[j] = SDL_modelist[i]; | |
615 j++; | |
616 } | |
617 i++; | |
618 } | |
619 | |
620 dsp_modes[j] = NULL; | |
621 | |
622 return dsp_modes; | |
623 } | |
624 | |
625 /* Various screen update functions available */ | |
626 static void DSp_DirectUpdate(_THIS, int numrects, SDL_Rect *rects); | |
627 | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
628 #if ! TARGET_API_MAC_OSX |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
629 |
0 | 630 static volatile unsigned int retrace_count = 0; /* -dw- need volatile because it updates asychronously */ |
631 | |
632 Boolean DSp_VBLProc ( DSpContextReference context, void *ref_con ) | |
633 { | |
634 retrace_count++; | |
635 | |
636 return 1; /* Darrell, is this right? */ | |
637 } | |
638 | |
639 static void DSp_SetHWError (OSStatus err, int is_agp) | |
640 { | |
641 char message[1024]; | |
642 const char *fmt, *mem; | |
643 | |
644 if ( is_agp ) { | |
645 mem = "AGP Memory"; | |
646 } else { | |
647 mem = "VRAM"; | |
648 } | |
649 switch(err) { | |
650 case memFullErr: | |
651 fmt = "Hardware surface possible but not enough %s available"; | |
652 break; | |
653 case cDepthErr: | |
654 fmt = "Hardware surface possible but invalid color depth"; | |
655 break; | |
656 default: | |
657 fmt = "Hardware surface could not be allocated in %s - unknown error"; | |
658 break; | |
659 } | |
660 sprintf(message, fmt, mem); | |
661 SDL_SetError(message); | |
662 } | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
663 #endif // TARGET_API_MAC_OSX |
0 | 664 |
665 /* put up a dialog to verify display change */ | |
666 static int DSp_ConfirmSwitch () { | |
667 | |
668 /* resource id's for dialog */ | |
669 const int rDialog = 1002; | |
670 const int bCancel = 1; | |
671 const int bOK = 2; | |
672 | |
673 DialogPtr dialog; | |
674 OSStatus err; | |
675 SInt32 response; | |
676 DialogItemIndex item = 0; | |
677 GrafPtr savePort; | |
678 | |
679 GetPort (&savePort); | |
680 | |
681 dialog = GetNewDialog (rDialog, NULL, (WindowPtr) -1); | |
682 if (dialog == NULL) | |
683 return (0); | |
684 | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
685 #if TARGET_API_CARBON |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
686 SetPort (GetDialogPort(dialog)); |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
687 #else |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
688 SetPort ((WindowPtr) dialog); |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
689 #endif |
0 | 690 |
691 SetDialogDefaultItem (dialog, bCancel); | |
692 SetDialogCancelItem (dialog, bCancel); | |
693 | |
694 SetEventMask (everyEvent); | |
695 FlushEvents (everyEvent, 0); | |
696 | |
697 /* On MacOS 8.5 or later, we can make the dialog go away after 15 seconds */ | |
698 /* This is good since it's possible user can't even see the dialog! */ | |
699 /* Requires linking to DialogsLib */ | |
700 err = Gestalt(gestaltSystemVersion,&response); | |
701 if (err == noErr && response >= 0x00000850) { | |
702 SetDialogTimeout(dialog, bCancel, 15); | |
703 } | |
704 | |
705 do { | |
706 | |
707 ModalDialog ( NULL, &item ); | |
708 | |
709 } while ( item != bCancel && item != bOK && err != noErr); | |
710 | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
711 |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
712 DisposeDialog (dialog); |
0 | 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 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 | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
759 #if !TARGET_API_MAC_OSX |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
760 DisplayIDType display_id; |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
761 Fixed freq; |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
762 #endif |
0 | 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 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; | |
1135
cf6133247d34
Mac Classic and CodeWarrior patches.
Ryan C. Gordon <icculus@icculus.org>
parents:
1133
diff
changeset
|
801 #if TARGET_API_MAC_OSX || UNIVERSAL_INTERFACES_VERSION == 0x0320 |
0 | 802 |
803 if ( DSpFindBestContext (&attrib, &dsp_context) != noErr ) { | |
804 SDL_SetError ("DrawSprocket couldn't find a context"); | |
805 return NULL; | |
806 } | |
807 | |
808 #else | |
809 if ( noErr != DMGetDisplayIDByGDevice (SDL_Display, &display_id, SDL_FALSE) ) { | |
810 SDL_SetError ("Display Manager couldn't associate GDevice with display_id"); | |
811 return NULL; | |
812 } | |
1032
c1c2efca4548
Date: Mon, 24 Jan 2005 21:37:56 +0800
Sam Lantinga <slouken@libsdl.org>
parents:
769
diff
changeset
|
813 if ( DSpFindBestContextOnDisplayID(&attrib, &dsp_context, display_id) != noErr ) { |
0 | 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*) 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*) malloc (sizeof (private_hwdata)); | |
889 if (current ->hwdata == NULL) { | |
890 SDL_OutOfMemory (); | |
891 return NULL; | |
892 } | |
893 | |
894 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); | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
945 #if TARGET_API_MAC_CARBON |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
946 SetPort (GetWindowPort(SDL_Window)); |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
947 #else |
0 | 948 SetPort (SDL_Window); |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
949 #endif |
0 | 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 } | |
968 | |
969 #ifdef DSP_TRY_CC_AND_AA | |
970 | |
971 static int DSp_MakeHWMask (_THIS, SDL_Surface *surface) | |
972 { | |
973 GDHandle save_device; | |
974 CGrafPtr save_port; | |
975 GWorldPtr temp; | |
976 RGBColor black = { 0, 0, 0 }; | |
977 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF }; | |
978 Rect rect; | |
979 | |
980 Uint32 depth = GetPixDepth ( GetGDevPixMap (SDL_Display) ); | |
981 | |
982 SetRect (&rect, 0, 0, surface->w, surface->h); | |
983 | |
984 if ( noErr != NewGWorld (&(surface->hwdata->mask), depth, &rect, 0, SDL_Display, 0 ) < 0 ) { | |
985 | |
986 SDL_OutOfMemory (); | |
987 return (-1); | |
988 } | |
989 | |
990 if ( noErr != NewGWorld (&temp, depth, &rect, 0 , SDL_Display, 0 ) ) { | |
991 | |
992 SDL_OutOfMemory (); | |
993 return (-1); | |
994 } | |
995 | |
996 | |
997 GetGWorld (&save_port, &save_device); | |
998 SetGWorld (surface->hwdata->mask, SDL_Display); | |
999 | |
1000 RGBForeColor (&white); | |
1001 PaintRect (&rect); | |
1002 | |
1003 RGBBackColor (&(surface->hwdata->trans)); | |
1004 | |
1005 CopyBits ( GetPortBitMapForCopyBits(surface->hwdata->offscreen), | |
1006 GetPortBitMapForCopyBits(surface->hwdata->mask), | |
1007 &rect, &rect, transparent, NULL ); | |
1008 | |
1009 SetGWorld (surface->hwdata->mask, SDL_Display); | |
1010 SetGWorld (save_port, save_device); | |
1011 return (0); | |
1012 } | |
1013 | |
1014 static int DSp_SetHWAlpha(_THIS, SDL_Surface *surface, UInt8 alpha) | |
1015 { | |
1016 surface->hwdata->alpha.red = (alpha / 255.0) * 65535; | |
1017 surface->hwdata->alpha.blue = (alpha / 255.0) * 65535; | |
1018 surface->hwdata->alpha.green = (alpha / 255.0) * 65535; | |
1019 | |
1020 surface->flags |= SDL_SRCALPHA; | |
1021 | |
1022 if (surface->flags & SDL_SRCCOLORKEY) { | |
1023 return(DSp_MakeHWMask (this, surface)); | |
1024 } | |
1025 return(0); | |
1026 } | |
1027 | |
1028 static int DSp_SetHWColorKey(_THIS, SDL_Surface *surface, Uint32 key) | |
1029 { | |
1030 CGrafPtr save_port; | |
1031 GDHandle save_device; | |
1032 | |
1033 GetGWorld (&save_port, &save_device); | |
1034 SetGWorld (surface->hwdata->offscreen, NULL); | |
1035 | |
1036 Index2Color (key, &(surface->hwdata->trans)); | |
1037 surface->flags |= SDL_SRCCOLORKEY; | |
1038 | |
1039 SetGWorld (save_port, save_device); | |
1040 | |
1041 if ( surface->flags & SDL_SRCALPHA ) { | |
1042 return(DSp_MakeHWMask (this, surface)); | |
1043 } | |
1044 return(0); | |
1045 } | |
1046 | |
1047 #endif /* DSP_TRY_CC_AND_AA */ | |
1048 | |
1049 static int DSp_NewHWSurface(_THIS, CGrafPtr *port, int depth, int width, int height) { | |
1050 | |
1051 OSStatus err; | |
1052 Rect bounds; | |
1053 | |
1054 SetRect (&bounds, 0, 0, width, height); | |
1055 | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
1056 #if useDistantHdwrMem && useLocalHdwrMem |
0 | 1057 if (dsp_vram_available) { |
1058 /* try VRAM */ | |
1059 err = NewGWorld (port, depth, &bounds, 0 , SDL_Display, useDistantHdwrMem | noNewDevice ); | |
1060 if (err != noErr) | |
1061 DSp_SetHWError (err, SDL_FALSE); | |
1062 else | |
1063 return (0); | |
1064 } | |
1065 | |
1066 if (dsp_agp_available) { | |
1067 /* try AGP */ | |
1068 err = NewGWorld (port, depth, &bounds, 0 , SDL_Display, useLocalHdwrMem | noNewDevice ); | |
1069 | |
1070 if (err != noErr) | |
1071 DSp_SetHWError (err, SDL_TRUE); | |
1072 else | |
1073 return (0); | |
1074 } | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
1075 #endif |
0 | 1076 |
1077 return (-1); | |
1078 } | |
1079 | |
1080 static int DSp_AllocHWSurface(_THIS, SDL_Surface *surface) | |
1081 { | |
1082 GWorldPtr temp; | |
1083 | |
1084 if ( DSp_NewHWSurface (this, &temp, surface->format->BitsPerPixel, surface->w, surface->h) < 0 ) | |
1085 return (-1); | |
1086 | |
1087 surface->hwdata = (private_hwdata*) malloc (sizeof (private_hwdata)); | |
1088 if (surface->hwdata == NULL) { | |
1089 SDL_OutOfMemory (); | |
1090 return -1; | |
1091 } | |
1092 | |
1093 memset (surface->hwdata, 0, sizeof(private_hwdata)); | |
1094 surface->hwdata->offscreen = temp; | |
1095 surface->pitch = GetPixRowBytes (GetPortPixMap (temp)) & 0x3FFF; | |
1096 surface->pixels = GetPixBaseAddr (GetPortPixMap (temp)); | |
1097 surface->flags |= SDL_HWSURFACE; | |
1098 #ifdef DSP_TRY_CC_AND_AA | |
1099 surface->flags |= SDL_HWACCEL; | |
1100 #endif | |
1101 return 0; | |
1102 } | |
1103 | |
1104 static void DSp_FreeHWSurface(_THIS, SDL_Surface *surface) | |
1105 { | |
1106 if (surface->hwdata->offscreen != NULL) | |
1107 DisposeGWorld (surface->hwdata->offscreen); | |
1108 free (surface->hwdata); | |
1109 | |
1110 surface->pixels = NULL; | |
1111 } | |
1112 | |
1113 static int DSp_CheckHWBlit(_THIS, SDL_Surface *src, SDL_Surface *dest) | |
1114 { | |
1115 int accelerated; | |
1116 | |
1117 /* Set initial acceleration on */ | |
1118 src->flags |= SDL_HWACCEL; | |
1119 | |
1120 /* Set the surface attributes */ | |
1121 if ( (src->flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { | |
1122 if ( ! this->info.blit_hw_A ) { | |
1123 src->flags &= ~SDL_HWACCEL; | |
1124 } | |
1125 } | |
1126 if ( (src->flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) { | |
1127 if ( ! this->info.blit_hw_CC ) { | |
1128 src->flags &= ~SDL_HWACCEL; | |
1129 } | |
1130 } | |
1131 | |
1132 /* Check to see if final surface blit is accelerated */ | |
1133 accelerated = !!(src->flags & SDL_HWACCEL); | |
1134 if ( accelerated ) { | |
1135 src->map->hw_blit = DSp_HWAccelBlit; | |
1136 } | |
1137 return(accelerated); | |
1138 } | |
1139 | |
1140 static int DSp_HWAccelBlit(SDL_Surface *src, SDL_Rect *srcrect, | |
1141 SDL_Surface *dst, SDL_Rect *dstrect) | |
1142 { | |
1143 CGrafPtr save_port; | |
1144 GDHandle save_device; | |
1145 Rect src_rect, dst_rect; | |
1146 RGBColor black = { 0, 0, 0 }; | |
1147 RGBColor white = { 0xFFFF, 0xFFFF, 0xFFFF }; | |
1148 | |
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 | |
1163 | |
1164 if ( (src->flags & SDL_SRCCOLORKEY) && | |
1165 (src->flags & SDL_SRCALPHA) ) { | |
1166 | |
1167 OpColor (&(src->hwdata->alpha)); | |
1168 | |
1169 CopyDeepMask ( GetPortBitMapForCopyBits(src->hwdata->offscreen), | |
1170 GetPortBitMapForCopyBits(src->hwdata->mask), | |
1171 GetPortBitMapForCopyBits(dst->hwdata->offscreen), | |
1172 &src_rect, &src_rect, &dst_rect, | |
1173 blend, | |
1174 NULL ); | |
1175 } | |
1176 else { | |
1177 | |
1178 if ( src->flags & SDL_SRCCOLORKEY) { | |
1179 RGBBackColor (&(src->hwdata->trans) ); | |
1180 mode = transparent; | |
1181 } | |
1182 else if (src->flags & SDL_SRCALPHA) { | |
1183 | |
1184 OpColor (&(src->hwdata->alpha)); | |
1185 mode = blend; | |
1186 } | |
1187 else { | |
1188 | |
1189 mode = srcCopy; | |
1190 } | |
1191 | |
1192 CopyBits ( GetPortBitMapForCopyBits(src->hwdata->offscreen), | |
1193 GetPortBitMapForCopyBits(dst->hwdata->offscreen), | |
1194 &src_rect, &dst_rect, mode, NULL ); | |
1195 } | |
1196 #else | |
1197 | |
1198 CopyBits ( &(((GrafPtr)(src->hwdata->offscreen))->portBits), | |
1199 &(((GrafPtr)(dst->hwdata->offscreen))->portBits), | |
1200 &src_rect, &dst_rect, srcCopy, NULL ); | |
1201 | |
1202 #endif /* DSP_TRY_CC_AND_AA */ | |
1203 | |
1204 SetGWorld (save_port, save_device); | |
1205 | |
1206 return(0); | |
1207 } | |
1208 | |
1209 static int DSp_FillHWRect(_THIS, SDL_Surface *dst, SDL_Rect *rect, Uint32 color) | |
1210 { | |
1211 CGrafPtr save_port; | |
1212 GDHandle save_device; | |
1213 Rect fill_rect; | |
1214 RGBColor rgb; | |
1215 | |
1216 SetRect (&fill_rect, rect->x, rect->y, rect->x + rect->w, rect->y + rect->h); | |
1217 | |
1218 GetGWorld (&save_port, &save_device); | |
1219 SetGWorld (dst->hwdata->offscreen, NULL); | |
1220 | |
1221 Index2Color (color, &rgb); | |
1222 | |
1223 RGBForeColor (&rgb); | |
1224 PaintRect (&fill_rect); | |
1225 | |
1226 SetGWorld (save_port, save_device); | |
1227 | |
1228 return(0); | |
1229 } | |
1230 | |
1231 static int DSp_FlipHWSurface(_THIS, SDL_Surface *surface) | |
1232 { | |
1233 if ( (surface->flags & SDL_HWSURFACE) ) { | |
1234 CGrafPtr dsp_front_buffer, save_port; | |
1235 Rect rect; | |
1236 | |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
1237 #if ! TARGET_API_MAC_OSX |
0 | 1238 unsigned int old_count; |
1133
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
1239 #endif |
609c060fd2a2
The MacOSX Carbon/Cocoa/X11 all in one library patch. Relevant emails:
Ryan C. Gordon <icculus@icculus.org>
parents:
1032
diff
changeset
|
1240 |
0 | 1241 /* pseudo page flipping for VRAM back buffer*/ |
1242 DSpContext_GetFrontBuffer (dsp_context, &dsp_front_buffer); | |
1243 SetRect (&rect, 0, 0, surface->w-1, surface->h-1); | |
1244 | |
1245 GetPort ((GrafPtr *)&save_port); | |
1246 SetPort ((GrafPtr)dsp_front_buffer); | |
1247 | |
1248 /* wait for retrace */ | |
1249 /* I have tried doing the swap in interrupt routine (VBL Proc) to do */ | |
1250 /* it asynchronously, but apparently CopyBits isn't interrupt safe */ | |
1251 | |
1252 #if ! TARGET_API_MAC_OSX | |
1253 #ifndef DSP_NO_SYNC_VBL | |
1254 old_count = retrace_count; | |
1255 while (old_count == retrace_count) | |
1256 ; | |
1257 #endif | |
1258 #endif | |
1259 | |
1260 CopyBits ( GetPortBitMapForCopyBits(dsp_back_buffer), | |
1261 GetPortBitMapForCopyBits(dsp_front_buffer), | |
1262 &rect, &rect, srcCopy, NULL ); | |
1263 | |
1264 SetPort ((GrafPtr)save_port); | |
1265 | |
1266 } else { | |
1267 /* not really page flipping at all: DSp just blits the dirty rectangles from DSp_UpdateRects */ | |
1268 Boolean busy_flag; | |
1269 DSpContext_SwapBuffers (dsp_context, NULL, &busy_flag); /* this waits for VBL */ | |
1270 DSpContext_GetBackBuffer (dsp_context, kDSpBufferKind_Normal, &dsp_back_buffer); | |
1271 surface->pixels = GetPixBaseAddr( GetPortPixMap(dsp_back_buffer) ); | |
1272 } | |
1273 return(0); | |
1274 } | |
1275 | |
1276 static int DSp_LockHWSurface(_THIS, SDL_Surface *surface) | |
1277 { | |
1278 if ( LockPixels (GetGWorldPixMap (surface->hwdata->offscreen)) ) | |
1279 return 0; | |
1280 else | |
1281 return -1; | |
1282 } | |
1283 | |
1284 static void DSp_UnlockHWSurface(_THIS, SDL_Surface *surface) | |
1285 { | |
1286 UnlockPixels (GetGWorldPixMap (surface->hwdata->offscreen)); | |
1287 } | |
1288 | |
1289 static void DSp_DirectUpdate(_THIS, int numrects, SDL_Rect *sdl_rects) | |
1290 { | |
1291 return; | |
1292 } | |
1293 | |
1294 static void DSp_DSpUpdate(_THIS, int numrects, SDL_Rect *sdl_rects) | |
1295 { | |
1296 #if ! TARGET_API_MAC_OSX /* Unsupported DSp in here */ | |
1297 int i; | |
1298 Rect rect; | |
1299 | |
1300 for (i = 0; i < numrects; i++) { | |
1301 | |
1302 rect.top = sdl_rects[i].y; | |
1303 rect.left = sdl_rects[i].x; | |
1304 rect.bottom = sdl_rects[i].h + sdl_rects[i].y; | |
1305 rect.right = sdl_rects[i].w + sdl_rects[i].x; | |
1306 | |
1307 DSpContext_InvalBackBufferRect (dsp_context, &rect); | |
1308 } | |
1309 #endif | |
1310 } | |
1311 | |
1312 static int DSp_CreatePalette(_THIS) { | |
1313 | |
1314 | |
1315 /* Create our palette */ | |
1316 SDL_CTab = (CTabHandle)NewHandle(sizeof(ColorSpec)*256 + 8); | |
1317 if ( SDL_CTab == nil ) { | |
1318 SDL_OutOfMemory(); | |
1319 return(-1); | |
1320 } | |
1321 (**SDL_CTab).ctSeed = GetCTSeed(); | |
1322 (**SDL_CTab).ctFlags = 0; | |
1323 (**SDL_CTab).ctSize = 255; | |
1324 CTabChanged(SDL_CTab); | |
1325 SDL_CPal = NewPalette(256, SDL_CTab, pmExplicit+pmTolerant, 0); | |
1326 | |
1327 return 0; | |
1328 } | |
1329 | |
1330 static int DSp_DestroyPalette(_THIS) { | |
1331 | |
1332 /* Free palette and restore original one */ | |
1333 if ( SDL_CTab != nil ) { | |
1334 DisposeHandle((Handle)SDL_CTab); | |
1335 SDL_CTab = nil; | |
1336 } | |
1337 if ( SDL_CPal != nil ) { | |
1338 DisposePalette(SDL_CPal); | |
1339 SDL_CPal = nil; | |
1340 } | |
1341 RestoreDeviceClut(SDL_Display); | |
1342 | |
1343 return (0); | |
1344 } | |
1345 | |
1346 static int DSp_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors) | |
1347 { | |
1348 CTabHandle cTab; | |
1349 | |
1350 int i; | |
1351 | |
1352 cTab = SDL_CTab; | |
1353 | |
1354 /* Verify the range of colors */ | |
1355 if ( (firstcolor+ncolors) > ((**cTab).ctSize+1) ) { | |
1356 return(0); | |
1357 } | |
1358 | |
1359 /* Set the screen palette and update the display */ | |
1360 for(i = 0; i < ncolors; i++) { | |
1361 int j = firstcolor + i; | |
1362 (**cTab).ctTable[j].value = j; | |
1363 (**cTab).ctTable[j].rgb.red = colors[i].r << 8 | colors[i].r; | |
1364 (**cTab).ctTable[j].rgb.green = colors[i].g << 8 | colors[i].g; | |
1365 (**cTab).ctTable[j].rgb.blue = colors[i].b << 8 | colors[i].b; | |
1366 } | |
1367 | |
1368 SetGDevice(SDL_Display); | |
1369 SetEntries(0, (**cTab).ctSize, (ColorSpec *)&(**cTab).ctTable); | |
1370 | |
1371 return(1); | |
1372 } | |
1373 | |
1374 void DSp_VideoQuit(_THIS) | |
1375 { | |
1376 int i; | |
1377 | |
1378 /* Free current video mode */ | |
1379 DSp_UnsetVideoMode(this, this->screen); | |
1380 | |
1381 /* Free Palette and restore original */ | |
1382 DSp_DestroyPalette (this); | |
1383 | |
1384 /* Free list of video modes */ | |
1385 if ( SDL_modelist != NULL ) { | |
1386 for ( i=0; SDL_modelist[i]; i++ ) { | |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
1387 SDL_free(SDL_modelist[i]); |
0 | 1388 } |
1336
3692456e7b0f
Use SDL_ prefixed versions of C library functions.
Sam Lantinga <slouken@libsdl.org>
parents:
1312
diff
changeset
|
1389 SDL_free(SDL_modelist); |
0 | 1390 SDL_modelist = NULL; |
1391 } | |
1392 | |
1393 /* Unload DrawSprocket */ | |
1394 DSpShutdown (); | |
1395 } | |
1396 | |
1397 #ifdef HAVE_OPENGL | |
1398 | |
1399 /* swap buffers with v-sync */ | |
1400 static void DSp_GL_SwapBuffers (_THIS) { | |
1401 | |
1402 #ifndef DSP_NO_SYNC_OPENGL | |
1403 | |
1404 unsigned int old_count; | |
1405 | |
1406 old_count = retrace_count; | |
1407 while (old_count == retrace_count) | |
1408 ; | |
1409 #endif | |
1410 | |
1411 aglSwapBuffers (glContext); | |
1412 } | |
1413 | |
1414 #endif |