src/video/macrom/SDL_romvideo.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 25 Jul 2004 19:43:56 +0000
changeset 916 46916168361d
parent 774 0c3e00cc9580
child 1032 c1c2efca4548
permissions -rw-r--r--
Date: Sun, 25 Jul 2004 23:10:03 +0800
From: Chris Taylor
Subject: SDL for Macintosh Programmer's Workshop: removed dynamic loading

I'm working on a game called D2X which uses SDL, and I recently ported
it to Mac OS 9. I used MPW to build it. It uses OpenGL. To get it to
work, I had to build SDL so it doesn't dynamically load OpenGL. This is
because I don't think MPW supports dynamic loading. The following patch
makes sure when SDL is built under MPW, dynamic loading of shared
libraries is disabled.
slouken@0
     1
/*
slouken@0
     2
    SDL - Simple DirectMedia Layer
slouken@769
     3
    Copyright (C) 1997-2004 Sam Lantinga
slouken@0
     4
slouken@0
     5
    This library is free software; you can redistribute it and/or
slouken@0
     6
    modify it under the terms of the GNU Library General Public
slouken@0
     7
    License as published by the Free Software Foundation; either
slouken@0
     8
    version 2 of the License, or (at your option) any later version.
slouken@0
     9
slouken@0
    10
    This library is distributed in the hope that it will be useful,
slouken@0
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@0
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@0
    13
    Library General Public License for more details.
slouken@0
    14
slouken@0
    15
    You should have received a copy of the GNU Library General Public
slouken@0
    16
    License along with this library; if not, write to the Free
slouken@0
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@0
    18
slouken@0
    19
    Sam Lantinga
slouken@252
    20
    slouken@libsdl.org
slouken@0
    21
*/
slouken@0
    22
slouken@0
    23
#ifdef SAVE_RCSID
slouken@0
    24
static char rcsid =
slouken@0
    25
 "@(#) $Id$";
slouken@0
    26
#endif
slouken@0
    27
slouken@0
    28
#include <stdio.h>
slouken@0
    29
#include <stdlib.h>
slouken@0
    30
slouken@0
    31
#if TARGET_API_MAC_CARBON
slouken@0
    32
#include <Carbon.h>
slouken@0
    33
/* The fullscreen code requires the QuickTime framework, and the window
slouken@0
    34
   is still at the back on MacOS X, which is where this code is needed.
slouken@0
    35
 */
slouken@0
    36
#if USE_QUICKTIME
slouken@47
    37
#include <QuickTime/Movies.h>
slouken@0
    38
#endif
slouken@0
    39
#else
slouken@0
    40
#include <LowMem.h>
slouken@0
    41
#include <Gestalt.h>
slouken@0
    42
#include <Devices.h>
slouken@0
    43
#include <DiskInit.h>
slouken@0
    44
#include <QDOffscreen.h>
slouken@0
    45
#endif
slouken@0
    46
slouken@0
    47
#include "SDL_video.h"
slouken@0
    48
#include "SDL_error.h"
slouken@0
    49
#include "SDL_syswm.h"
slouken@0
    50
#include "SDL_sysvideo.h"
slouken@0
    51
#include "SDL_romvideo.h"
slouken@0
    52
#include "SDL_macgl_c.h"
slouken@0
    53
#include "SDL_macwm_c.h"
slouken@0
    54
#include "SDL_macmouse_c.h"
slouken@0
    55
#include "SDL_macevents_c.h"
slouken@0
    56
slouken@0
    57
/* Initialization/Query functions */
slouken@0
    58
static int ROM_VideoInit(_THIS, SDL_PixelFormat *vformat);
slouken@0
    59
static SDL_Rect **ROM_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags);
slouken@0
    60
static SDL_Surface *ROM_SetVideoMode(_THIS, SDL_Surface *current, int width, int height, int bpp, Uint32 flags);
slouken@0
    61
static int ROM_SetColors(_THIS, int firstcolor, int ncolors,
slouken@0
    62
			 SDL_Color *colors);
slouken@0
    63
static void ROM_VideoQuit(_THIS);
slouken@0
    64
slouken@0
    65
/* Hardware surface functions */
slouken@0
    66
static int ROM_AllocHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    67
static int ROM_LockHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    68
static void ROM_UnlockHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    69
static void ROM_FreeHWSurface(_THIS, SDL_Surface *surface);
slouken@0
    70
slouken@0
    71
#if !TARGET_API_MAC_CARBON /* This seems not to be available? -sts Aug 2000 */
slouken@0
    72
/* Saved state for the menu bar */
slouken@0
    73
static RgnHandle	gSaveGrayRgn = nil;
slouken@0
    74
static short		gSaveMenuBar = 0;
slouken@0
    75
static Boolean		gSaveCSVis = true;
slouken@0
    76
slouken@0
    77
#if powerc
slouken@0
    78
/* Mixed mode glue to activate the 68K emulator and twiddle a register */
slouken@0
    79
#define ONEWORDSTUB(p1) \
slouken@0
    80
		{ 0x41FA, 0x0010, 0x209F, (p1), 0x41FA, \
slouken@0
    81
		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
slouken@0
    82
slouken@0
    83
#define TWOWORDSTUB(p1,p2) \
slouken@0
    84
		{ 0x41FA, 0x0012, 0x209F, (p1), (p2), 0x41FA, \
slouken@0
    85
		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
slouken@0
    86
slouken@0
    87
#define THREEWORDSTUB(p1,p2,p3) \
slouken@0
    88
		{ 0x41FA, 0x0014, 0x209F, (p1), (p2), (p3), 0x41FA, \
slouken@0
    89
		  0x0008, 0x2F10, 0x4E75, 0x0000, 0x0000, 0x0000 }
slouken@0
    90
slouken@0
    91
/* ControlStrip inline glue for PowerPC */
slouken@0
    92
static pascal Boolean SBIsControlStripVisible(void)
slouken@0
    93
{
slouken@0
    94
	static short procData[] = TWOWORDSTUB(0x7000, 0xAAF2);
slouken@0
    95
	ProcInfoType procInfo = kD0DispatchedPascalStackBased
slouken@0
    96
				| RESULT_SIZE(SIZE_CODE(sizeof(Boolean)))
slouken@0
    97
            	| DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(kFourByteCode);
slouken@0
    98
            				
slouken@0
    99
	return((Boolean) CallUniversalProc((UniversalProcPtr) procData, procInfo, 0x00));
slouken@0
   100
}
slouken@0
   101
slouken@0
   102
static pascal void SBShowHideControlStrip(Boolean showIt)
slouken@0
   103
{
slouken@0
   104
	static short procData[] = THREEWORDSTUB(0x303C, 0x0101, 0xAAF2);
slouken@0
   105
	ProcInfoType procInfo = kD0DispatchedPascalStackBased
slouken@0
   106
				| DISPATCHED_STACK_ROUTINE_SELECTOR_SIZE(kFourByteCode)
slouken@0
   107
				| DISPATCHED_STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(Boolean)));
slouken@0
   108
slouken@0
   109
	CallUniversalProc((UniversalProcPtr) procData, procInfo, 0x01, showIt);
slouken@0
   110
}
slouken@0
   111
#endif /* powerc */
slouken@0
   112
#endif /* !TARGET_API_MAC_CARBON */
slouken@0
   113
slouken@0
   114
/* Macintosh toolbox driver bootstrap functions */
slouken@0
   115
slouken@0
   116
static int ROM_Available(void)
slouken@0
   117
{
slouken@0
   118
	return(1);
slouken@0
   119
}
slouken@0
   120
slouken@0
   121
static void ROM_DeleteDevice(SDL_VideoDevice *device)
slouken@0
   122
{
slouken@0
   123
	free(device->hidden);
slouken@0
   124
	free(device);
slouken@0
   125
}
slouken@0
   126
slouken@0
   127
static SDL_VideoDevice *ROM_CreateDevice(int devindex)
slouken@0
   128
{
slouken@0
   129
	SDL_VideoDevice *device;
slouken@0
   130
slouken@0
   131
	/* Initialize all variables that we clean on shutdown */
slouken@0
   132
	device = (SDL_VideoDevice *)malloc(sizeof(SDL_VideoDevice));
slouken@0
   133
	if ( device ) {
slouken@0
   134
		memset(device, 0, (sizeof *device));
slouken@0
   135
		device->hidden = (struct SDL_PrivateVideoData *)
slouken@0
   136
				malloc((sizeof *device->hidden));
slouken@0
   137
	}
slouken@0
   138
	if ( (device == NULL) || (device->hidden == NULL) ) {
slouken@0
   139
		SDL_OutOfMemory();
slouken@0
   140
		if ( device ) {
slouken@0
   141
			free(device);
slouken@0
   142
		}
slouken@0
   143
		return(0);
slouken@0
   144
	}
slouken@0
   145
	memset(device->hidden, 0, (sizeof *device->hidden));
slouken@0
   146
slouken@0
   147
	/* Set the function pointers */
slouken@0
   148
	device->VideoInit = ROM_VideoInit;
slouken@0
   149
	device->ListModes = ROM_ListModes;
slouken@0
   150
	device->SetVideoMode = ROM_SetVideoMode;
slouken@0
   151
	device->SetColors = ROM_SetColors;
slouken@0
   152
	device->UpdateRects = NULL;
slouken@0
   153
	device->VideoQuit = ROM_VideoQuit;
slouken@0
   154
	device->AllocHWSurface = ROM_AllocHWSurface;
slouken@0
   155
	device->CheckHWBlit = NULL;
slouken@0
   156
	device->FillHWRect = NULL;
slouken@0
   157
	device->SetHWColorKey = NULL;
slouken@0
   158
	device->SetHWAlpha = NULL;
slouken@0
   159
	device->LockHWSurface = ROM_LockHWSurface;
slouken@0
   160
	device->UnlockHWSurface = ROM_UnlockHWSurface;
slouken@0
   161
	device->FlipHWSurface = NULL;
slouken@0
   162
	device->FreeHWSurface = ROM_FreeHWSurface;
slouken@0
   163
#ifdef HAVE_OPENGL
slouken@0
   164
	device->GL_MakeCurrent = Mac_GL_MakeCurrent;
slouken@0
   165
	device->GL_SwapBuffers = Mac_GL_SwapBuffers;
slouken@916
   166
#ifdef __MWERKS__
slouken@774
   167
	device->GL_LoadLibrary = Mac_GL_LoadLibrary;
slouken@774
   168
	device->GL_GetProcAddress = Mac_GL_GetProcAddress;
slouken@916
   169
#else
slouken@916
   170
	device->GL_LoadLibrary = NULL;
slouken@916
   171
	device->GL_GetProcAddress = NULL;
slouken@916
   172
#endif	// CodeWarrior
slouken@916
   173
#endif	// Have OpenGL
slouken@0
   174
	device->SetCaption = Mac_SetCaption;
slouken@0
   175
	device->SetIcon = NULL;
slouken@0
   176
	device->IconifyWindow = NULL;
slouken@0
   177
	device->GrabInput = NULL;
slouken@0
   178
	device->GetWMInfo = NULL;
slouken@0
   179
	device->FreeWMCursor = Mac_FreeWMCursor;
slouken@0
   180
	device->CreateWMCursor = Mac_CreateWMCursor;
slouken@0
   181
	device->ShowWMCursor = Mac_ShowWMCursor;
slouken@0
   182
	device->WarpWMCursor = Mac_WarpWMCursor;
slouken@0
   183
	device->InitOSKeymap = Mac_InitOSKeymap;
slouken@0
   184
	device->PumpEvents = Mac_PumpEvents;
slouken@0
   185
slouken@0
   186
	device->free = ROM_DeleteDevice;
slouken@0
   187
slouken@0
   188
	return device;
slouken@0
   189
}
slouken@0
   190
slouken@0
   191
VideoBootStrap TOOLBOX_bootstrap = {
slouken@0
   192
	"toolbox", "MacOS ROM Toolbox",
slouken@0
   193
	ROM_Available, ROM_CreateDevice
slouken@0
   194
};
slouken@0
   195
slouken@0
   196
slouken@0
   197
static int ROM_VideoInit(_THIS, SDL_PixelFormat *vformat)
slouken@0
   198
{
slouken@0
   199
	long info;
slouken@0
   200
	
slouken@0
   201
	/* Check out some things about the system */
slouken@0
   202
	Gestalt(gestaltQuickdrawVersion, &info);
slouken@0
   203
	if ( info == gestaltOriginalQD ) {
slouken@0
   204
		SDL_SetError("Color Quickdraw not available");
slouken@0
   205
		return(-1);
slouken@0
   206
	}
slouken@0
   207
slouken@0
   208
	/* Start ROMintosh events */
slouken@0
   209
	Mac_InitEvents(this);
slouken@0
   210
slouken@0
   211
	/* Get a handle to the main monitor */
slouken@0
   212
	SDL_Display = GetMainDevice();
slouken@0
   213
slouken@0
   214
	/* Determine pixel format */
slouken@0
   215
	vformat->BitsPerPixel = (**(**SDL_Display).gdPMap).pixelSize;
slouken@0
   216
	switch (vformat->BitsPerPixel) {
slouken@0
   217
		case 16:	/* 5-5-5 RGB */
slouken@0
   218
			vformat->Rmask = 0x00007c00;
slouken@0
   219
			vformat->Gmask = 0x000003e0;
slouken@0
   220
			vformat->Bmask = 0x0000001f;
slouken@0
   221
			break;
slouken@0
   222
		default:
slouken@0
   223
			break;
slouken@0
   224
	}
slouken@0
   225
slouken@0
   226
	/* Create our palette */
slouken@0
   227
	SDL_CTab = (CTabHandle)NewHandle(sizeof(ColorSpec)*256 + 8);
slouken@0
   228
	if ( SDL_CTab == nil ) {
slouken@0
   229
		SDL_OutOfMemory();
slouken@0
   230
		return(-1);
slouken@0
   231
	}
slouken@0
   232
	(**SDL_CTab).ctSeed = GetCTSeed();
slouken@0
   233
	(**SDL_CTab).ctFlags = 0;
slouken@0
   234
	(**SDL_CTab).ctSize = 255;
slouken@0
   235
	CTabChanged(SDL_CTab);
slouken@0
   236
	SDL_CPal = NewPalette(256, SDL_CTab, pmExplicit+pmTolerant, 0);
slouken@0
   237
slouken@0
   238
	/* Get a list of available fullscreen modes */
slouken@0
   239
	SDL_modelist = (SDL_Rect **)malloc((1+1)*sizeof(SDL_Rect *));
slouken@0
   240
	if ( SDL_modelist ) {
slouken@0
   241
		SDL_modelist[0] = (SDL_Rect *)malloc(sizeof(SDL_Rect));
slouken@0
   242
		if ( SDL_modelist[0] ) {
slouken@0
   243
			SDL_modelist[0]->x = 0;
slouken@0
   244
			SDL_modelist[0]->y = 0;
slouken@0
   245
			SDL_modelist[0]->w = (**SDL_Display).gdRect.right;
slouken@0
   246
			SDL_modelist[0]->h = (**SDL_Display).gdRect.bottom;
slouken@0
   247
		}
slouken@0
   248
		SDL_modelist[1] = NULL;
slouken@0
   249
	}
slouken@0
   250
slouken@0
   251
	/* Fill in some window manager capabilities */
slouken@0
   252
	this->info.wm_available = 1;
slouken@0
   253
slouken@0
   254
	/* We're done! */
slouken@0
   255
	return(0);
slouken@0
   256
}
slouken@0
   257
slouken@0
   258
static SDL_Rect **ROM_ListModes(_THIS, SDL_PixelFormat *format, Uint32 flags)
slouken@0
   259
{
slouken@0
   260
	if ( this->screen->format->BitsPerPixel == format->BitsPerPixel ) {
slouken@0
   261
		if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
slouken@0
   262
			return(SDL_modelist);
slouken@0
   263
		} else {
slouken@0
   264
			return((SDL_Rect **)-1);
slouken@0
   265
		}
slouken@0
   266
	} else {
slouken@0
   267
		return((SDL_Rect **)0);
slouken@0
   268
	}
slouken@0
   269
}
slouken@0
   270
slouken@0
   271
static void ROM_HideMenuBar(_THIS)
slouken@0
   272
{
slouken@0
   273
#if !TARGET_API_MAC_CARBON /* This seems not to be available? -sts Aug 2000 */
slouken@0
   274
	RgnHandle		drawRgn = nil;
slouken@0
   275
	RgnHandle		tempRgn = nil;
slouken@0
   276
	RgnHandle		grayRgn = nil;
slouken@0
   277
	WindowPtr		window = nil;
slouken@0
   278
	GDHandle		gd = nil;
slouken@0
   279
	GrafPtr			savePort;
slouken@0
   280
	long			response;
slouken@0
   281
	short			height;
slouken@0
   282
	EventRecord		theEvent;
slouken@0
   283
slouken@0
   284
	height = GetMBarHeight();
slouken@0
   285
	
slouken@0
   286
	if ( height > 0 ) {
slouken@0
   287
		tempRgn = NewRgn();
slouken@0
   288
		drawRgn = NewRgn();
slouken@0
   289
		gSaveGrayRgn = NewRgn();
slouken@0
   290
		if ( ! tempRgn || ! drawRgn || ! gSaveGrayRgn ) {
slouken@0
   291
			goto CLEANUP;
slouken@0
   292
		}
slouken@0
   293
		grayRgn = GetGrayRgn(); /* No need to check for this */
slouken@0
   294
	
slouken@0
   295
		GetPort(&savePort);
slouken@0
   296
slouken@0
   297
		/* Hide the control strip if it's present, and record its 
slouken@0
   298
		   previous position into the dirty region for redrawing. 
slouken@0
   299
		   This isn't necessary, but may help catch stray bits. */
slouken@0
   300
		CopyRgn(grayRgn, tempRgn);
slouken@0
   301
		if (!Gestalt(gestaltControlStripAttr, &response) && 
slouken@0
   302
			(response & (1L << gestaltControlStripExists))) {
slouken@0
   303
			gSaveCSVis = SBIsControlStripVisible();
slouken@0
   304
			if (gSaveCSVis)
slouken@0
   305
				SBShowHideControlStrip(false);
slouken@0
   306
		}
slouken@0
   307
		DiffRgn(grayRgn, tempRgn, drawRgn);
slouken@0
   308
slouken@0
   309
		/* Save the gray region once the control strip is hidden*/
slouken@0
   310
		CopyRgn(grayRgn, gSaveGrayRgn);
slouken@0
   311
slouken@0
   312
		/* Change the menu height in lowmem */
slouken@0
   313
		gSaveMenuBar = height;
slouken@0
   314
		LMSetMBarHeight(0);
slouken@0
   315
		
slouken@0
   316
		/* Walk the monitor rectangles, and combine any pieces that
slouken@0
   317
		   aren't in GrayRgn: menubar, round corners, fake floaters. */
slouken@0
   318
		for(gd = GetDeviceList(); gd; gd = GetNextDevice(gd)) 
slouken@0
   319
			{
slouken@0
   320
			if (!TestDeviceAttribute(gd, screenDevice)) continue;
slouken@0
   321
			if (!TestDeviceAttribute(gd, screenActive)) continue;
slouken@0
   322
slouken@0
   323
			RectRgn(tempRgn, &(*gd)->gdRect);	/* Get the whole screen */
slouken@0
   324
			DiffRgn(tempRgn, grayRgn, tempRgn); /* Subtract out GrayRgn */
slouken@0
   325
			UnionRgn(tempRgn, drawRgn, drawRgn);/* Combine all the bits */
slouken@0
   326
			}
slouken@0
   327
			
slouken@0
   328
		/* Add the bits into the GrayRgn */
slouken@0
   329
		UnionRgn(drawRgn, grayRgn, grayRgn);
slouken@0
   330
slouken@0
   331
		/* Modify the vis regions of exposed windows */
slouken@0
   332
		window = (FrontWindow()) ? FrontWindow() : (WindowPtr) -1L;
slouken@0
   333
		PaintBehind(window, drawRgn);
slouken@0
   334
		CalcVisBehind(window, drawRgn);
slouken@0
   335
slouken@0
   336
		SetPort(savePort);
slouken@0
   337
		
slouken@0
   338
		/* Yield time so that floaters can catch up */
slouken@0
   339
		EventAvail(0, &theEvent);
slouken@0
   340
		EventAvail(0, &theEvent);
slouken@0
   341
		EventAvail(0, &theEvent);
slouken@0
   342
		EventAvail(0, &theEvent);
slouken@0
   343
		}
slouken@0
   344
slouken@0
   345
CLEANUP:
slouken@0
   346
slouken@0
   347
	if (tempRgn) DisposeRgn(tempRgn);
slouken@0
   348
	if (drawRgn) DisposeRgn(drawRgn);
slouken@0
   349
#endif /* !TARGET_API_MAC_CARBON */
slouken@0
   350
}
slouken@0
   351
	
slouken@0
   352
static void ROM_ShowMenuBar(_THIS)
slouken@0
   353
{
slouken@0
   354
#if !TARGET_API_MAC_CARBON /* This seems not to be available? -sts Aug 2000 */
slouken@0
   355
	RgnHandle		drawRgn = nil;
slouken@0
   356
	RgnHandle		menuRgn = nil;
slouken@0
   357
	RgnHandle		tempRgn = nil;
slouken@0
   358
	RgnHandle		grayRgn = nil;
slouken@0
   359
	WindowPtr		window = nil;
slouken@0
   360
	GrafPtr			wMgrPort;
slouken@0
   361
	GrafPtr			savePort;
slouken@0
   362
	Rect			menuRect;
slouken@0
   363
	long			response;
slouken@0
   364
	short			height;
slouken@0
   365
	EventRecord		theEvent;
slouken@0
   366
	RGBColor		saveRGB;
slouken@0
   367
	RGBColor		blackRGB = { 0, 0, 0 };
slouken@0
   368
slouken@0
   369
	height = GetMBarHeight();
slouken@0
   370
	
slouken@0
   371
	if ((height <= 0) && (gSaveMenuBar > 0)) {
slouken@0
   372
		drawRgn = NewRgn();
slouken@0
   373
		menuRgn = NewRgn();
slouken@0
   374
		tempRgn = NewRgn();
slouken@0
   375
		if ( ! tempRgn || ! drawRgn || ! gSaveGrayRgn ) {
slouken@0
   376
			goto CLEANUP;
slouken@0
   377
		}
slouken@0
   378
		grayRgn = GetGrayRgn(); /* No need to check for this */
slouken@0
   379
	
slouken@0
   380
		GetPort(&savePort);
slouken@0
   381
		GetWMgrPort(&wMgrPort);
slouken@0
   382
slouken@0
   383
		/* Set the height properly */
slouken@0
   384
		LMSetMBarHeight(gSaveMenuBar);
slouken@0
   385
slouken@0
   386
		/* Restore the old GrayRgn: rounded corners, etc, but not
slouken@0
   387
		   the menubar -- subtract that out first! */
slouken@0
   388
		if (gSaveGrayRgn)
slouken@0
   389
			{
slouken@0
   390
			menuRect = (*GetMainDevice())->gdRect;
slouken@0
   391
			menuRect.bottom = menuRect.top + gSaveMenuBar;
slouken@0
   392
			RectRgn(menuRgn, &menuRect);
slouken@0
   393
slouken@0
   394
			DiffRgn(grayRgn, gSaveGrayRgn, drawRgn); 	/* What do we inval? */
slouken@0
   395
			DiffRgn(drawRgn, menuRgn, drawRgn);			/* Clip out the menu */
slouken@0
   396
			
slouken@0
   397
			/* Now redraw the corners and other bits black */
slouken@0
   398
			SetPort(wMgrPort);
slouken@0
   399
			GetClip(tempRgn);
slouken@0
   400
			SetClip(drawRgn);
slouken@0
   401
			GetForeColor(&saveRGB);
slouken@0
   402
			RGBForeColor(&blackRGB);
slouken@0
   403
			PaintRgn(drawRgn);
slouken@0
   404
			RGBForeColor(&saveRGB);
slouken@0
   405
			SetClip(tempRgn);
slouken@0
   406
			SetPort(savePort);
slouken@0
   407
			
slouken@0
   408
			UnionRgn(drawRgn, menuRgn, drawRgn);		/* Put back the menu */
slouken@0
   409
slouken@0
   410
			/* Now actually restore the GrayRgn */
slouken@0
   411
			CopyRgn(gSaveGrayRgn, grayRgn);
slouken@0
   412
			DisposeRgn(gSaveGrayRgn);
slouken@0
   413
			gSaveGrayRgn = nil;
slouken@0
   414
			}
slouken@0
   415
slouken@0
   416
		/* Modify the vis regions of exposed windows and draw menubar */
slouken@0
   417
		window = (FrontWindow()) ? FrontWindow() : (WindowPtr) -1L;
slouken@0
   418
		PaintBehind(window, drawRgn);
slouken@0
   419
		CalcVisBehind(window, drawRgn);
slouken@0
   420
		DrawMenuBar();
slouken@0
   421
slouken@0
   422
		SetPort(savePort);
slouken@0
   423
		gSaveMenuBar = 0;
slouken@0
   424
slouken@0
   425
		/* Now show the control strip if it's present */
slouken@0
   426
		if (!Gestalt(gestaltControlStripAttr, &response) && 
slouken@0
   427
				(response & (1L << gestaltControlStripExists)))
slouken@0
   428
			{
slouken@0
   429
			if (gSaveCSVis && !SBIsControlStripVisible())
slouken@0
   430
				SBShowHideControlStrip(true);
slouken@0
   431
			gSaveCSVis = true;
slouken@0
   432
			}
slouken@0
   433
slouken@0
   434
		/* Yield time so that floaters can catch up */
slouken@0
   435
		EventAvail(0, &theEvent);
slouken@0
   436
		EventAvail(0, &theEvent);
slouken@0
   437
		EventAvail(0, &theEvent);
slouken@0
   438
		EventAvail(0, &theEvent);
slouken@0
   439
		}
slouken@0
   440
slouken@0
   441
CLEANUP:
slouken@0
   442
slouken@0
   443
	if (drawRgn) DisposeRgn(drawRgn);
slouken@0
   444
	if (menuRgn) DisposeRgn(menuRgn);
slouken@0
   445
	if (tempRgn) DisposeRgn(tempRgn);
slouken@0
   446
#endif /* !TARGET_API_MAC_CARBON */
slouken@0
   447
}
slouken@0
   448
slouken@0
   449
/* Various screen update functions available */
slouken@0
   450
static void ROM_DirectUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
   451
static void ROM_WindowUpdate(_THIS, int numrects, SDL_Rect *rects);
slouken@0
   452
slouken@0
   453
static void ROM_UnsetVideoMode(_THIS, SDL_Surface *current)
slouken@0
   454
{
slouken@0
   455
	/* Free the current window, if any */
slouken@0
   456
	if ( SDL_Window != nil ) {
slouken@0
   457
		GWorldPtr memworld;
slouken@0
   458
		
slouken@0
   459
		/* Handle OpenGL support */
slouken@0
   460
		Mac_GL_Quit(this);
slouken@0
   461
slouken@0
   462
		memworld = (GWorldPtr)GetWRefCon(SDL_Window);
slouken@0
   463
		if ( memworld != nil ) {
slouken@0
   464
			UnlockPixels(GetGWorldPixMap(memworld));
slouken@0
   465
			DisposeGWorld(memworld);
slouken@0
   466
		}
slouken@0
   467
		if ( (current->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
slouken@0
   468
#if USE_QUICKTIME
slouken@0
   469
			EndFullScreen(fullscreen_ctx, nil);
slouken@0
   470
			SDL_Window = nil;
slouken@0
   471
#else
slouken@0
   472
			ROM_ShowMenuBar(this);
slouken@0
   473
#endif
slouken@0
   474
		}
slouken@0
   475
	}
slouken@0
   476
	current->pixels = NULL;
slouken@0
   477
	current->flags &= ~(SDL_HWSURFACE|SDL_FULLSCREEN);
slouken@0
   478
}
slouken@0
   479
slouken@0
   480
static SDL_Surface *ROM_SetVideoMode(_THIS, SDL_Surface *current,
slouken@0
   481
				int width, int height, int bpp, Uint32 flags)
slouken@0
   482
{
slouken@0
   483
	Rect wrect, orect;
slouken@0
   484
#if TARGET_API_MAC_CARBON
slouken@0
   485
	Rect tmprect;
slouken@0
   486
#endif
slouken@0
   487
slouken@0
   488
	/* Free any previous video mode */
slouken@0
   489
	ROM_UnsetVideoMode(this, current);
slouken@0
   490
slouken@0
   491
	/* Create the ROM window and SDL video surface */
slouken@0
   492
	current->flags = 0;		/* Clear flags */
slouken@0
   493
	current->w = width;
slouken@0
   494
	current->h = height;
slouken@0
   495
	SetRect(&wrect, 0, 0, width, height);
slouken@0
   496
	if ( SDL_Window ) {
slouken@0
   497
		/* If we recreate the window, don't move it around */
slouken@0
   498
#if TARGET_API_MAC_CARBON
slouken@0
   499
		orect = *GetWindowPortBounds(SDL_Window, &tmprect);
slouken@0
   500
#else
slouken@0
   501
		orect = SDL_Window->portRect;
slouken@0
   502
#endif
slouken@0
   503
		OffsetRect(&wrect, orect.left, orect.top);
slouken@0
   504
	} else {
slouken@0
   505
		/* Center the window the first time we show it */
slouken@0
   506
		OffsetRect(&wrect,
slouken@0
   507
		(SDL_modelist[0]->w-width)/2, (SDL_modelist[0]->h-height)/2);
slouken@0
   508
	}
slouken@0
   509
slouken@0
   510
#if MACOSX && !USE_QUICKTIME
slouken@0
   511
	/* Hum.. fullscreen mode is broken */
slouken@0
   512
	flags &= ~SDL_FULLSCREEN;
slouken@0
   513
#endif
slouken@0
   514
	if ( (flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
slouken@0
   515
		/* Create the fullscreen window and use screen bits */
slouken@0
   516
		current->flags |= SDL_HWSURFACE|SDL_FULLSCREEN;
slouken@0
   517
		if ( SDL_Window ) {
slouken@0
   518
			DisposeWindow(SDL_Window);
slouken@0
   519
		}
slouken@0
   520
#if USE_QUICKTIME
slouken@0
   521
		BeginFullScreen(&fullscreen_ctx, nil, 0,0, &SDL_Window, nil, 0);
slouken@0
   522
#else
slouken@0
   523
		SDL_Window = NewCWindow(nil, &wrect, "\p", true, plainDBox,
slouken@0
   524
						(WindowPtr)-1, false, 0);
slouken@0
   525
		ROM_HideMenuBar(this);
slouken@0
   526
#endif
slouken@0
   527
		current->pitch = (**(**SDL_Display).gdPMap).rowBytes & 0x3FFF;
slouken@0
   528
		current->pixels = (**(**SDL_Display).gdPMap).baseAddr;
slouken@0
   529
		this->UpdateRects = ROM_DirectUpdate;
slouken@0
   530
	} else {
slouken@0
   531
		GWorldPtr memworld;
slouken@0
   532
		PixMapHandle pixmap;
slouken@0
   533
		int style;
slouken@0
   534
slouken@0
   535
		style = noGrowDocProc;
slouken@0
   536
		if ( flags & SDL_NOFRAME ) {
slouken@0
   537
			style = plainDBox;
slouken@0
   538
			current->flags |= SDL_NOFRAME;
slouken@0
   539
		} else
slouken@0
   540
		if ( flags & SDL_RESIZABLE ) {
slouken@0
   541
			style = zoomDocProc;
slouken@0
   542
			current->flags |= SDL_RESIZABLE;
slouken@0
   543
		}
slouken@0
   544
		if ( SDL_Window && (style == current_style) ) {
slouken@0
   545
			/* Resize existing window, if necessary */
slouken@0
   546
			if ( ((orect.right-orect.left) != width) ||
slouken@0
   547
			     ((orect.bottom-orect.top) != height) ) {
slouken@0
   548
				SizeWindow(SDL_Window, width, height, false);
slouken@0
   549
			}
slouken@0
   550
		} else {
slouken@0
   551
			/* Recreate the window in the new style */
slouken@0
   552
			if ( SDL_Window ) {
slouken@0
   553
				DisposeWindow(SDL_Window);
slouken@0
   554
			}
slouken@0
   555
			SDL_Window = NewCWindow(nil, &wrect, "\p", true,
slouken@0
   556
			                        style, (WindowPtr)-1, true, 0);
slouken@0
   557
slouken@0
   558
			/* Set the window title, if any */
slouken@0
   559
			{ char *title;
slouken@0
   560
				SDL_WM_GetCaption(&title, NULL);
slouken@0
   561
				if ( title ) {
slouken@0
   562
					Mac_SetCaption(this, title, NULL);
slouken@0
   563
				}
slouken@0
   564
			}
slouken@0
   565
		}
slouken@0
   566
		current_style = style;
slouken@0
   567
		SetPalette(SDL_Window, SDL_CPal, false);
slouken@0
   568
		ActivatePalette(SDL_Window);
slouken@0
   569
		if ( NewGWorld(&memworld, 0,
slouken@0
   570
#if TARGET_API_MAC_CARBON
slouken@0
   571
			       GetWindowPortBounds(SDL_Window, &tmprect),
slouken@0
   572
#else
slouken@0
   573
			       &SDL_Window->portRect,
slouken@0
   574
#endif
slouken@0
   575
			       SDL_CTab, nil, 0) != noErr ) {
slouken@0
   576
			SDL_SetError("NewGWorld() failed");
slouken@0
   577
			return(NULL);
slouken@0
   578
		}
slouken@0
   579
		SetWRefCon(SDL_Window, (long)memworld);
slouken@0
   580
		pixmap = GetGWorldPixMap(memworld);
slouken@0
   581
		LockPixels(pixmap);
slouken@0
   582
		current->pitch = (**pixmap).rowBytes & 0x3FFF;
slouken@0
   583
		current->pixels = GetPixBaseAddr(pixmap);
slouken@0
   584
		this->UpdateRects = ROM_WindowUpdate;
slouken@0
   585
	}
slouken@0
   586
	SetPortWindowPort(SDL_Window);
slouken@0
   587
	SelectWindow(SDL_Window);
slouken@0
   588
slouken@0
   589
	/* Handle OpenGL support */
slouken@0
   590
	if ( flags & SDL_OPENGL ) {
slouken@0
   591
		if ( Mac_GL_Init(this) == 0 ) {
slouken@0
   592
			current->flags |= SDL_OPENGL;
slouken@0
   593
		} else {
slouken@0
   594
			current = NULL;
slouken@0
   595
		}
slouken@0
   596
	}
slouken@0
   597
	
slouken@0
   598
	if ( (flags & SDL_HWPALETTE) && (flags & SDL_FULLSCREEN) )
slouken@0
   599
	   current->flags |= SDL_HWPALETTE;
slouken@0
   600
	   
slouken@0
   601
	/* We're live! */
slouken@0
   602
	return(current);
slouken@0
   603
}
slouken@0
   604
slouken@0
   605
/* We don't actually allow hardware surfaces other than the main one */
slouken@0
   606
static int ROM_AllocHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   607
{
slouken@0
   608
	return(-1);
slouken@0
   609
}
slouken@0
   610
static void ROM_FreeHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   611
{
slouken@0
   612
	return;
slouken@0
   613
}
slouken@0
   614
static int ROM_LockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   615
{
slouken@0
   616
	return(0);
slouken@0
   617
}
slouken@0
   618
static void ROM_UnlockHWSurface(_THIS, SDL_Surface *surface)
slouken@0
   619
{
slouken@0
   620
	return;
slouken@0
   621
}
slouken@0
   622
slouken@0
   623
static void ROM_DirectUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   624
{
slouken@0
   625
	/* The application is already updating the visible video memory */
slouken@0
   626
	return;
slouken@0
   627
}
slouken@0
   628
slouken@0
   629
static void ROM_WindowUpdate(_THIS, int numrects, SDL_Rect *rects)
slouken@0
   630
{
slouken@0
   631
	GWorldPtr memworld;
slouken@0
   632
	GrafPtr saveport;
slouken@0
   633
	CGrafPtr thePort;
slouken@0
   634
	const BitMap *memBits;
slouken@0
   635
	const BitMap *winBits;
slouken@0
   636
	int i;
slouken@0
   637
	Rect update;
slouken@0
   638
	
slouken@0
   639
	/* Copy from the offscreen GWorld to the window port */
slouken@0
   640
	GetPort(&saveport);
slouken@0
   641
	SetPortWindowPort(SDL_Window);
slouken@0
   642
	thePort = GetWindowPort(SDL_Window);
slouken@0
   643
	memworld = (GWorldPtr)GetWRefCon(SDL_Window);
slouken@0
   644
#if TARGET_API_MAC_CARBON
slouken@0
   645
	memBits = GetPortBitMapForCopyBits((CGrafPtr) memworld);
slouken@0
   646
#else
slouken@0
   647
	memBits = &((GrafPtr)memworld)->portBits;
slouken@0
   648
#endif
slouken@0
   649
#if TARGET_API_MAC_CARBON
slouken@0
   650
	winBits = GetPortBitMapForCopyBits(thePort);
slouken@0
   651
#else
slouken@0
   652
	winBits = &SDL_Window->portBits;
slouken@0
   653
#endif
slouken@0
   654
	for ( i=0; i<numrects; ++i ) {
slouken@0
   655
		update.left = rects[i].x;
slouken@0
   656
		update.right = rects[i].x+rects[i].w;
slouken@0
   657
		update.top = rects[i].y;
slouken@0
   658
		update.bottom = rects[i].y+rects[i].h;
slouken@0
   659
		CopyBits(memBits, winBits,
slouken@0
   660
			 &update, &update, srcCopy, nil);
slouken@0
   661
	}
slouken@0
   662
#if TARGET_API_MAC_CARBON
slouken@0
   663
	if ( QDIsPortBuffered(thePort) ) {
slouken@0
   664
		QDFlushPortBuffer(thePort, NULL);
slouken@0
   665
	}
slouken@0
   666
#endif
slouken@0
   667
	SetPort(saveport);
slouken@0
   668
}
slouken@0
   669
slouken@0
   670
static int ROM_SetColors(_THIS, int firstcolor, int ncolors, SDL_Color *colors)
slouken@0
   671
{
slouken@0
   672
	CTabHandle cTab;
slouken@0
   673
	int i;
slouken@0
   674
slouken@0
   675
	/* Get the colortable from the either the display or window */
slouken@0
   676
	if ( (this->screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) {
slouken@0
   677
		cTab = (**(**SDL_Display).gdPMap).pmTable;
slouken@0
   678
	} else {
slouken@0
   679
		cTab = SDL_CTab;
slouken@0
   680
	}
slouken@0
   681
slouken@0
   682
	/* Verify the range of colors */
slouken@0
   683
	if ( (firstcolor+ncolors) > ((**cTab).ctSize+1) ) {
slouken@0
   684
		return(0);
slouken@0
   685
	}
slouken@0
   686
	
slouken@0
   687
	/* Set the screen palette and update the display */
slouken@0
   688
	for ( i=0; i< ncolors; ++i ) {
slouken@0
   689
	        int j = firstcolor + i;
slouken@0
   690
		(**cTab).ctTable[j].value = j;
slouken@0
   691
		(**cTab).ctTable[j].rgb.red = colors[i].r << 8 | colors[i].r;
slouken@0
   692
		(**cTab).ctTable[j].rgb.green = colors[i].g << 8 | colors[i].g;
slouken@0
   693
		(**cTab).ctTable[j].rgb.blue = colors[i].b << 8 | colors[i].b;
slouken@0
   694
	}
slouken@0
   695
//	if ( (this->screen->flags & SDL_FULLSCREEN) == SDL_FULLSCREEN ) 
slouken@0
   696
{
slouken@0
   697
		GDevice **odisplay;
slouken@0
   698
		odisplay = GetGDevice();
slouken@0
   699
		SetGDevice(SDL_Display);
slouken@0
   700
		SetEntries(0, (**cTab).ctSize, (ColorSpec *)&(**cTab).ctTable);
slouken@0
   701
		SetGDevice(odisplay);
slouken@0
   702
	}
slouken@0
   703
	return(1);
slouken@0
   704
}
slouken@0
   705
slouken@0
   706
void ROM_VideoQuit(_THIS)
slouken@0
   707
{
slouken@0
   708
	int i;
slouken@0
   709
slouken@0
   710
	/* Free current video mode */
slouken@0
   711
	ROM_UnsetVideoMode(this, this->screen);
slouken@0
   712
	if ( SDL_Window ) {
slouken@0
   713
		DisposeWindow(SDL_Window);
slouken@0
   714
		SDL_Window = nil;
slouken@0
   715
	}
slouken@0
   716
slouken@0
   717
	/* Free palette and restore original one */
slouken@0
   718
	if ( SDL_CTab != nil ) {
slouken@0
   719
		DisposeHandle((Handle)SDL_CTab);
slouken@0
   720
		SDL_CTab = nil;
slouken@0
   721
	}
slouken@0
   722
	if ( SDL_CPal != nil ) {
slouken@0
   723
		DisposePalette(SDL_CPal);
slouken@0
   724
		SDL_CPal = nil;
slouken@0
   725
	}
slouken@0
   726
	RestoreDeviceClut(GetMainDevice());
slouken@0
   727
slouken@0
   728
	/* Free list of video modes */
slouken@0
   729
	if ( SDL_modelist != NULL ) {
slouken@0
   730
		for ( i=0; SDL_modelist[i]; ++i ) {
slouken@0
   731
			free(SDL_modelist[i]);
slouken@0
   732
		}
slouken@0
   733
		free(SDL_modelist);
slouken@0
   734
		SDL_modelist = NULL;
slouken@0
   735
	}
slouken@0
   736
}
slouken@0
   737