Changeset - r3082:12642bff9b64
[Not reviewed]
master
0 1 0
bjarni - 19 years ago 2006-02-24 22:12:11
bjarni@openttd.org
(svn r3671) -Code cleanup: fixed style and removed commented out code in video/cocoa_v.m
1 file changed with 1 insertions and 11 deletions:
0 comments (0 inline, 0 general)
video/cocoa_v.m
Show inline comments
 
@@ -789,261 +789,255 @@ static void QZ_SetPortAlphaOpaque(void)
 

	
 
@implementation OTTD_QuartzWindow
 

	
 
/* we override these methods to fix the miniaturize animation/dock icon bug */
 
- (void)miniaturize:(id)sender
 
{
 
	/* make the alpha channel opaque so anim won't have holes in it */
 
	QZ_SetPortAlphaOpaque ();
 

	
 
	/* window is hidden now */
 
	_cocoa_video_data.active = false;
 

	
 
	QZ_ShowMouse();
 

	
 
	[ super miniaturize:sender ];
 
}
 

	
 
- (void)display
 
{
 
	/* This method fires just before the window deminaturizes from the Dock.
 
	 * We'll save the current visible surface, let the window manager redraw any
 
	 * UI elements, and restore the surface. This way, no expose event
 
	 * is required, and the deminiaturize works perfectly.
 
	 */
 

	
 
	QZ_SetPortAlphaOpaque();
 

	
 
	/* save current visible surface */
 
	[ self cacheImageInRect:[ _cocoa_video_data.qdview frame ] ];
 

	
 
	/* let the window manager redraw controls, border, etc */
 
	[ super display ];
 

	
 
	/* restore visible surface */
 
	[ self restoreCachedImage ];
 

	
 
	/* window is visible again */
 
	_cocoa_video_data.active = true;
 
}
 

	
 
- (void)setFrame:(NSRect)frameRect display:(BOOL)flag
 
{
 
	NSRect newViewFrame;
 
	CGrafPtr thePort;
 

	
 
	[ super setFrame:frameRect display:flag ];
 

	
 
	/* Don't do anything if the window is currently beign created */
 
	if (_cocoa_video_data.issetting) return;
 

	
 
	if (_cocoa_video_data.window == nil) return;
 

	
 
	newViewFrame = [ _cocoa_video_data.qdview frame ];
 

	
 
	/* Update the pixels and pitch */
 
	thePort = [ _cocoa_video_data.qdview qdPort ];
 
	LockPortBits(thePort);
 

	
 
	_cocoa_video_data.realpixels = GetPixBaseAddr(GetPortPixMap(thePort));
 
	_cocoa_video_data.pitch      = GetPixRowBytes(GetPortPixMap(thePort));
 

	
 
	/* _cocoa_video_data.realpixels now points to the window's pixels
 
	 * We want it to point to the *view's* pixels
 
	 */
 
	{
 
		int vOffset = [ _cocoa_video_data.window frame ].size.height - newViewFrame.size.height - newViewFrame.origin.y;
 
		int hOffset = newViewFrame.origin.x;
 

	
 
		_cocoa_video_data.realpixels = (uint8*)_cocoa_video_data.realpixels + (vOffset * _cocoa_video_data.pitch) + hOffset * (_cocoa_video_data.device_bpp / 8);
 
	}
 

	
 
	UnlockPortBits(thePort);
 

	
 
	/* Allocate new buffer */
 
	free(_cocoa_video_data.pixels);
 
	_cocoa_video_data.pixels = (uint8*)malloc(newViewFrame.size.width * newViewFrame.size.height);
 
	assert(_cocoa_video_data.pixels != NULL);
 

	
 

	
 
	/* Tell the game that the resolution changed */
 
	_cocoa_video_data.width = newViewFrame.size.width;
 
	_cocoa_video_data.height = newViewFrame.size.height;
 

	
 
	_screen.width = _cocoa_video_data.width;
 
	_screen.height = _cocoa_video_data.height;
 
	_screen.pitch = _cocoa_video_data.width;
 

	
 
	GameSizeChanged();
 

	
 
	/* Redraw screen */
 
	_cocoa_video_data.num_dirty_rects = MAX_DIRTY_RECTS;
 
}
 

	
 
- (void)appDidHide:(NSNotification*)note
 
{
 
	_cocoa_video_data.active = false;
 
//	DEBUG(driver, 1)("cocoa_v: appDidHide");
 
}
 

	
 

	
 
- (void)appWillUnhide:(NSNotification*)note
 
{
 
	QZ_SetPortAlphaOpaque ();
 

	
 
	/* save current visible surface */
 
	[ self cacheImageInRect:[ _cocoa_video_data.qdview frame ] ];
 
}
 

	
 
- (void)appDidUnhide:(NSNotification*)note
 
{
 
	/* restore cached image, since it may not be current, post expose event too */
 
	[ self restoreCachedImage ];
 

	
 
	_cocoa_video_data.active = true;
 
//	DEBUG(driver, 1)("cocoa_v: appDidUnhide");
 
}
 

	
 

	
 
- (id)initWithContentRect:(NSRect)contentRect styleMask:(unsigned int)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag
 
{
 
	/* Make our window subclass receive these application notifications */
 
	[ [ NSNotificationCenter defaultCenter ] addObserver:self
 
	selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ];
 

	
 
	[ [ NSNotificationCenter defaultCenter ] addObserver:self
 
	selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ];
 

	
 
	[ [ NSNotificationCenter defaultCenter ] addObserver:self
 
	selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ];
 

	
 
	return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ];
 
}
 

	
 
@end
 

	
 
@implementation OTTD_QuartzWindowDelegate
 
- (BOOL)windowShouldClose:(id)sender
 
{
 
	QZ_AskQuit();
 

	
 
	return NO;
 
}
 

	
 
- (void)windowDidBecomeKey:(NSNotification*)aNotification
 
{
 
	_cocoa_video_data.active = true;
 
//	DEBUG(driver, 1)("cocoa_v: windowDidBecomeKey");
 
}
 

	
 
- (void)windowDidResignKey:(NSNotification*)aNotification
 
{
 
	_cocoa_video_data.active = false;
 
//	DEBUG(driver, 1)("cocoa_v: windowDidResignKey");
 
}
 

	
 
- (void)windowDidBecomeMain:(NSNotification*)aNotification
 
{
 
	_cocoa_video_data.active = true;
 
//	DEBUG(driver, 1)("cocoa_v: windowDidBecomeMain");
 
}
 

	
 
- (void)windowDidResignMain:(NSNotification*)aNotification
 
{
 
	_cocoa_video_data.active = false;
 
//	DEBUG(driver, 1)("cocoa_v: windowDidResignMain");
 
}
 

	
 
@end
 

	
 

	
 
static void QZ_UpdateWindowPalette(uint start, uint count)
 
{
 
	uint i;
 

	
 
	switch (_cocoa_video_data.device_bpp) {
 
		case 32:
 
			for (i = start; i < start + count; i++) {
 
				uint32 clr32 = 0xff000000;
 
				clr32 |= (uint32)_cur_palette[i].r << 16;
 
				clr32 |= (uint32)_cur_palette[i].g << 8;
 
				clr32 |= (uint32)_cur_palette[i].b;
 
				_cocoa_video_data.palette32[i] = clr32;
 
			}
 
			break;
 
		case 16:
 
			for (i = start; i < start + count; i++) {
 
				uint16 clr16 = 0x0000;
 
				clr16 |= (uint16)((_cur_palette[i].r >> 3) & 0x1f) << 10;
 
				clr16 |= (uint16)((_cur_palette[i].g >> 3) & 0x1f) << 5;
 
				clr16 |= (uint16)((_cur_palette[i].b >> 3) & 0x1f);
 
				_cocoa_video_data.palette16[i] = clr16;
 
			}
 
			break;
 
	}
 

	
 
	_cocoa_video_data.num_dirty_rects = MAX_DIRTY_RECTS;
 
}
 

	
 
static inline void QZ_WindowBlitIndexedPixelsToView32(uint left, uint top, uint right, uint bottom)
 
{
 
	const uint32* pal = _cocoa_video_data.palette32;
 
	const uint8* src = _cocoa_video_data.pixels;
 
	uint32* dst = (uint32*)_cocoa_video_data.realpixels;
 
	uint width = _cocoa_video_data.width;
 
	uint pitch = _cocoa_video_data.pitch / 4;
 
	uint x;
 
	uint y;
 

	
 
	for (y = top; y < bottom; y++) {
 
		for (x = left; x < right; x++) {
 
			dst[y * pitch + x] = pal[src[y * width + x]];
 
		}
 
	}
 
}
 

	
 
static inline void QZ_WindowBlitIndexedPixelsToView16(uint left, uint top, uint right, uint bottom)
 
{
 
	const uint16* pal = _cocoa_video_data.palette16;
 
	const uint8* src = _cocoa_video_data.pixels;
 
	uint16* dst = (uint16*)_cocoa_video_data.realpixels;
 
	uint width = _cocoa_video_data.width;
 
	uint pitch = _cocoa_video_data.pitch / 2;
 
	uint x;
 
	uint y;
 

	
 
	for (y = top; y < bottom; y++) {
 
		for (x = left; x < right; x++) {
 
			dst[y * pitch + x] = pal[src[y * width + x]];
 
		}
 
	}
 
}
 

	
 
static inline void QZ_WindowBlitIndexedPixelsToView(int left, int top, int right, int bottom)
 
{
 
	switch (_cocoa_video_data.device_bpp) {
 
		case 32: QZ_WindowBlitIndexedPixelsToView32(left, top, right, bottom); break;
 
		case 16: QZ_WindowBlitIndexedPixelsToView16(left, top, right, bottom); break;
 
	}
 
}
 

	
 
static bool _resize_icon[] = {
 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1,
 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0,
 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1,
 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1,
 
	0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0,
 
	0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
 
	0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
 
	0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
 
	0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
 
	0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
 
	0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1,
 
	0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1,
 
	0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0,
 
	1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0
 
};
 

	
 
static void QZ_DrawResizeIcon(void)
 
{
 
@@ -1390,195 +1384,193 @@ static const char* QZ_SetVideoFullScreen
 
	[ [ NSScreen mainScreen ] setFrame:screen_rect ];
 

	
 
	/* we're fullscreen, so flag all input states... */
 
	_cocoa_video_data.active = true;
 

	
 

	
 
	pt = [ NSEvent mouseLocation ];
 
	pt.y = CGDisplayPixelsHigh(_cocoa_video_data.display_id) - pt.y;
 
	if (QZ_MouseIsInsideView(&pt)) QZ_HideMouse();
 

	
 
	return NULL;
 

	
 
/* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */
 
ERR_NOT_INDEXED:
 
	free(_cocoa_video_data.pixels);
 
	_cocoa_video_data.pixels = NULL;
 
ERR_DOUBLEBUF:
 
	CGDisplaySwitchToMode(_cocoa_video_data.display_id, _cocoa_video_data.save_mode);
 
ERR_NO_SWITCH:
 
	CGReleaseAllDisplays();
 
ERR_NO_CAPTURE:
 
	if (!gamma_error) QZ_FadeGammaIn(&gamma_table);
 
ERR_NO_MATCH:
 
	return errstr;
 
}
 

	
 

	
 
static void QZ_UpdateFullscreenPalette(uint first_color, uint num_colors)
 
{
 
	CGTableCount  index;
 
	CGDeviceColor color;
 

	
 
	for (index = first_color; index < first_color+num_colors; index++) {
 
		/* Clamp colors between 0.0 and 1.0 */
 
		color.red   = _cur_palette[index].r / 255.0;
 
		color.blue  = _cur_palette[index].b / 255.0;
 
		color.green = _cur_palette[index].g / 255.0;
 

	
 
		CGPaletteSetColorAtIndex(_cocoa_video_data.palette, color, index);
 
	}
 

	
 
	CGDisplaySetPalette(_cocoa_video_data.display_id, _cocoa_video_data.palette);
 
}
 

	
 
/* Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */
 
static void QZ_WaitForVerticalBlank(void)
 
{
 
	/* The VBL delay is based on Ian Ollmann's RezLib <iano@cco.caltech.edu> */
 
	double refreshRate;
 
	double linesPerSecond;
 
	double target;
 
	double position;
 
	double adjustment;
 
	CFNumberRef refreshRateCFNumber;
 

	
 
	refreshRateCFNumber = CFDictionaryGetValue(_cocoa_video_data.mode, kCGDisplayRefreshRate);
 
	if (refreshRateCFNumber == NULL) return;
 

	
 
	if (CFNumberGetValue(refreshRateCFNumber, kCFNumberDoubleType, &refreshRate) == 0)
 
		return;
 

	
 
	if (refreshRate == 0) return;
 

	
 
	linesPerSecond = refreshRate * _cocoa_video_data.height;
 
	target = _cocoa_video_data.height;
 

	
 
	/* Figure out the first delay so we start off about right */
 
	position = CGDisplayBeamPosition(_cocoa_video_data.display_id);
 
	if (position > target) position = 0;
 

	
 
	adjustment = (target - position) / linesPerSecond;
 

	
 
	CSleep((uint32)(adjustment * 1000));
 
}
 

	
 
static void QZ_DrawScreen(void)
 
{
 
	const uint8* src;
 
	uint8* dst;
 
	uint height;
 
	uint width;
 
	uint pitch;
 
	uint y;
 
	uint num_dirty_rects;
 
	uint length_drawn;
 
	uint left;
 
	uint i;
 

	
 
	src = _cocoa_video_data.pixels;
 
	dst = (uint8*)_cocoa_video_data.realpixels;
 
	height = _cocoa_video_data.height;
 
	width  = _cocoa_video_data.width;
 
	pitch  = _cocoa_video_data.pitch;
 
	num_dirty_rects = _cocoa_video_data.num_dirty_rects;
 

	
 
	/* Check if we need to do anything */
 
	if (_cocoa_video_data.num_dirty_rects == 0 ) {
 
		return;
 
	}
 
	if (_cocoa_video_data.num_dirty_rects == 0 ) return;
 

	
 
	if (num_dirty_rects >= MAX_DIRTY_RECTS) {
 
		num_dirty_rects = 1;
 
		_cocoa_video_data.dirty_rects[0].left = 0;
 
		_cocoa_video_data.dirty_rects[0].top = 0;
 
		_cocoa_video_data.dirty_rects[0].right = _cocoa_video_data.width;
 
		_cocoa_video_data.dirty_rects[0].bottom = _cocoa_video_data.height;
 
	}
 

	
 
	QZ_WaitForVerticalBlank();
 
	/* Build the region of dirty rectangles */
 
	for (i = 0; i < num_dirty_rects; i++) {
 

	
 
		y = _cocoa_video_data.dirty_rects[i].top;
 
		left = _cocoa_video_data.dirty_rects[i].left;
 
		length_drawn = _cocoa_video_data.dirty_rects[i].right - left + 1;
 
		height = _cocoa_video_data.dirty_rects[i].bottom;
 
		for (; y <= height; y++) memcpy(dst + y * pitch + left, src + y * width +left, length_drawn);
 
	}
 

	
 
	_cocoa_video_data.num_dirty_rects = 0;
 
}
 

	
 
static int QZ_ListFullscreenModes(OTTDPoint* mode_list, int max_modes)
 
{
 
	CFIndex num_modes;
 
	CFIndex i;
 
	int list_size = 0;
 

	
 
	num_modes = CFArrayGetCount(_cocoa_video_data.mode_list);
 

	
 
	/* Build list of modes with the requested bpp */
 
	for (i = 0; i < num_modes && list_size < max_modes; i++) {
 
		CFDictionaryRef onemode;
 
		CFNumberRef     number;
 
		int bpp;
 
		int intvalue;
 
		bool hasMode;
 
		uint16 width, height;
 

	
 
		onemode = CFArrayGetValueAtIndex(_cocoa_video_data.mode_list, i);
 
		number = CFDictionaryGetValue(onemode, kCGDisplayBitsPerPixel);
 
		CFNumberGetValue (number, kCFNumberSInt32Type, &bpp);
 

	
 
		if (bpp != 8) continue;
 

	
 
		number = CFDictionaryGetValue(onemode, kCGDisplayWidth);
 
		CFNumberGetValue(number, kCFNumberSInt32Type, &intvalue);
 
		width = (uint16)intvalue;
 

	
 
		number = CFDictionaryGetValue(onemode, kCGDisplayHeight);
 
		CFNumberGetValue(number, kCFNumberSInt32Type, &intvalue);
 
		height = (uint16)intvalue;
 

	
 
		/* Check if mode is already in the list */
 
		{
 
			int i;
 
			hasMode = false;
 
			for (i = 0; i < list_size; i++) {
 
				if (mode_list[i].x == width &&  mode_list[i].y == height) {
 
					hasMode = true;
 
					break;
 
				}
 
			}
 
		}
 

	
 
		if (hasMode) continue;
 

	
 
		/* Add mode to the list */
 
		mode_list[list_size].x = width;
 
		mode_list[list_size].y = height;
 
		list_size++;
 
	}
 

	
 
	/* Sort list smallest to largest */
 
	{
 
		int i, j;
 
		for (i = 0; i < list_size; i++) {
 
			for (j = 0; j < list_size-1; j++) {
 
				if (mode_list[j].x > mode_list[j + 1].x || (
 
							mode_list[j].x == mode_list[j + 1].x &&
 
							mode_list[j].y >  mode_list[j + 1].y
 
						)) {
 
					uint tmpw = mode_list[j].x;
 
					uint tmph = mode_list[j].y;
 

	
 
					mode_list[j].x = mode_list[j + 1].x;
 
					mode_list[j].y = mode_list[j + 1].y;
 

	
 
					mode_list[j + 1].x = tmpw;
 
					mode_list[j + 1].y = tmph;
 
				}
 
			}
 
		}
 
	}
 

	
 
@@ -1714,234 +1706,232 @@ static const char* QZ_SetVideoMode(uint 
 

	
 
	/* Tell the game that the resolution has changed */
 
	_screen.width = _cocoa_video_data.width;
 
	_screen.height = _cocoa_video_data.height;
 
	_screen.pitch = _cocoa_video_data.width;
 

	
 
	QZ_UpdateVideoModes();
 
	GameSizeChanged();
 

	
 
	QZ_InitPalette();
 

	
 
	return NULL;
 
}
 

	
 
static const char* QZ_SetVideoModeAndRestoreOnFailure(uint width, uint height, bool fullscreen)
 
{
 
	bool wasset = _cocoa_video_data.isset;
 
	uint32 oldwidth = _cocoa_video_data.width;
 
	uint32 oldheight = _cocoa_video_data.height;
 
	bool oldfullscreen = _cocoa_video_data.fullscreen;
 
	const char *ret;
 

	
 
	ret = QZ_SetVideoMode(width, height, fullscreen);
 
	if (ret != NULL && wasset) QZ_SetVideoMode(oldwidth, oldheight, oldfullscreen);
 

	
 
	return ret;
 
}
 

	
 
static void QZ_VideoInit(void)
 
{
 
	memset(&_cocoa_video_data, 0, sizeof(_cocoa_video_data));
 

	
 
	/* Initialize the video settings; this data persists between mode switches */
 
	_cocoa_video_data.display_id = kCGDirectMainDisplay;
 
	_cocoa_video_data.save_mode  = CGDisplayCurrentMode(_cocoa_video_data.display_id);
 
	_cocoa_video_data.mode_list  = CGDisplayAvailableModes(_cocoa_video_data.display_id);
 
	_cocoa_video_data.palette    = CGPaletteCreateDefaultColorPalette();
 

	
 
	/* Gather some information that is useful to know about the display */
 
	/* Maybe this should be moved to QZ_SetVideoMode, in case this is changed after startup */
 
	CFNumberGetValue(
 
		CFDictionaryGetValue(_cocoa_video_data.save_mode, kCGDisplayBitsPerPixel),
 
		kCFNumberSInt32Type, &_cocoa_video_data.device_bpp
 
	);
 

	
 
	CFNumberGetValue(
 
		CFDictionaryGetValue(_cocoa_video_data.save_mode, kCGDisplayWidth),
 
		kCFNumberSInt32Type, &_cocoa_video_data.device_width
 
	);
 

	
 
	CFNumberGetValue(
 
		CFDictionaryGetValue(_cocoa_video_data.save_mode, kCGDisplayHeight),
 
		kCFNumberSInt32Type, &_cocoa_video_data.device_height
 
	);
 

	
 
	_cocoa_video_data.cursor_visible = true;
 

	
 
	/* register for sleep notifications so wake from sleep generates SDL_VIDEOEXPOSE */
 
//	QZ_RegisterForSleepNotifications();
 
}
 

	
 

	
 
/* Convert local coordinate to window server (CoreGraphics) coordinate */
 
static CGPoint QZ_PrivateLocalToCG(NSPoint* p)
 
{
 
	CGPoint cgp;
 

	
 
	if (!_cocoa_video_data.fullscreen) {
 
		*p = [ _cocoa_video_data.qdview convertPoint:*p toView: nil ];
 
		*p = [ _cocoa_video_data.window convertBaseToScreen:*p ];
 
		p->y = _cocoa_video_data.device_height - p->y;
 
	}
 

	
 
	cgp.x = p->x;
 
	cgp.y = p->y;
 

	
 
	return cgp;
 
}
 

	
 
static void QZ_WarpCursor(int x, int y)
 
{
 
	NSPoint p;
 
	CGPoint cgp;
 

	
 
	/* Only allow warping when in foreground */
 
	if (![ NSApp isActive ]) return;
 

	
 
	p = NSMakePoint(x, y);
 
	cgp = QZ_PrivateLocalToCG(&p);
 

	
 
	/* this is the magic call that fixes cursor "freezing" after warp */
 
	CGSetLocalEventsSuppressionInterval(0.0);
 
	/* Do the actual warp */
 
	CGWarpMouseCursorPosition(cgp);
 

	
 
	/* Generate the mouse moved event */
 
//	SDL_PrivateMouseMotion(0, 0, x, y);
 
}
 

	
 
static void QZ_ShowMouse(void)
 
{
 
	if (!_cocoa_video_data.cursor_visible) {
 
		[ NSCursor unhide ];
 
		_cocoa_video_data.cursor_visible = true;
 
	}
 
}
 

	
 
static void QZ_HideMouse(void)
 
{
 
	if (_cocoa_video_data.cursor_visible) {
 
#ifndef _DEBUG
 
		[ NSCursor hide ];
 
#endif
 
		_cocoa_video_data.cursor_visible = false;
 
	}
 
}
 

	
 

	
 
/******************************************************************************
 
 *                             OS X application creation                      *
 
 ******************************************************************************/
 

	
 
/* The main class of the application, the application's delegate */
 
@implementation OTTDMain
 
/* Called when the internal event loop has just started running */
 
- (void) applicationDidFinishLaunching: (NSNotification*) note
 
{
 
	/* Hand off to main application code */
 
	QZ_GameLoop();
 

	
 
	/* We're done, thank you for playing */
 
	[ NSApp stop:_ottd_main ];
 
}
 

	
 
/* Display the in game quit confirmation dialog */
 
- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication*) sender
 
{
 
//	DEBUG(driver, 1)("cocoa_v: applicationShouldTerminate");
 

	
 
	QZ_AskQuit();
 

	
 
	return NSTerminateCancel;		// NSTerminateLater ?
 
}
 
@end
 

	
 
static void setApplicationMenu(void)
 
{
 
	/* warning: this code is very odd */
 
	NSMenu *appleMenu;
 
	NSMenuItem *menuItem;
 
	NSString *title;
 
	NSString *appName;
 

	
 
	appName = @"OTTD";
 
	appleMenu = [[NSMenu alloc] initWithTitle:appName];
 

	
 
	/* Add menu items */
 
	title = [@"About " stringByAppendingString:appName];
 
	[appleMenu addItemWithTitle:title action:@selector(orderFrontStandardAboutPanel:) keyEquivalent:@""];
 

	
 
	[appleMenu addItem:[NSMenuItem separatorItem]];
 

	
 
	title = [@"Hide " stringByAppendingString:appName];
 
	[appleMenu addItemWithTitle:title action:@selector(hide:) keyEquivalent:@"h"];
 

	
 
	menuItem = (NSMenuItem*)[appleMenu addItemWithTitle:@"Hide Others" action:@selector(hideOtherApplications:) keyEquivalent:@"h"];
 
	[menuItem setKeyEquivalentModifierMask:(NSAlternateKeyMask|NSCommandKeyMask)];
 

	
 
	[appleMenu addItemWithTitle:@"Show All" action:@selector(unhideAllApplications:) keyEquivalent:@""];
 

	
 
	[appleMenu addItem:[NSMenuItem separatorItem]];
 

	
 
	title = [@"Quit " stringByAppendingString:appName];
 
	[appleMenu addItemWithTitle:title action:@selector(terminate:) keyEquivalent:@"q"];
 

	
 

	
 
	/* Put menu into the menubar */
 
	menuItem = [[NSMenuItem alloc] initWithTitle:@"" action:nil keyEquivalent:@""];
 
	[menuItem setSubmenu:appleMenu];
 
	[[NSApp mainMenu] addItem:menuItem];
 

	
 
	/* Tell the application object that this is now the application menu */
 
	[NSApp setAppleMenu:appleMenu];
 

	
 
	/* Finally give up our references to the objects */
 
	[appleMenu release];
 
	[menuItem release];
 
}
 

	
 
/* Create a window menu */
 
static void setupWindowMenu(void)
 
{
 
	NSMenu* windowMenu;
 
	NSMenuItem* windowMenuItem;
 
	NSMenuItem* menuItem;
 

	
 
	windowMenu = [[NSMenu alloc] initWithTitle:@"Window"];
 

	
 
	/* "Minimize" item */
 
	menuItem = [[NSMenuItem alloc] initWithTitle:@"Minimize" action:@selector(performMiniaturize:) keyEquivalent:@"m"];
 
	[windowMenu addItem:menuItem];
 
	[menuItem release];
 

	
 
	/* Put menu into the menubar */
 
	windowMenuItem = [[NSMenuItem alloc] initWithTitle:@"Window" action:nil keyEquivalent:@""];
 
	[windowMenuItem setSubmenu:windowMenu];
 
	[[NSApp mainMenu] addItem:windowMenuItem];
 

	
 
	/* Tell the application object that this is now the window menu */
 
	[NSApp setWindowsMenu:windowMenu];
 

	
 
	/* Finally give up our references to the objects */
 
	[windowMenu release];
 
	[windowMenuItem release];
 
}
 

	
 
static void setupApplication(void)
 
{
 
	CPSProcessSerNum PSN;
 

	
 
	/* Ensure the application object is initialised */
 
	[NSApplication sharedApplication];
 

	
 
	/* Tell the dock about us */
 
	if (!CPSGetCurrentProcess(&PSN) &&
 
			!CPSEnableForegroundOperation(&PSN, 0x03, 0x3C, 0x2C, 0x1103) &&
 
			!CPSSetFrontProcess(&PSN)) {
 
		[NSApplication sharedApplication];
 
	}
 

	
 
	/* Set up the menubar */
 
	[NSApp setMainMenu:[[NSMenu alloc] init]];
 
	setApplicationMenu();
 
	setupWindowMenu();
0 comments (0 inline, 0 general)