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
 
@@ -837,165 +837,159 @@ static void QZ_SetPortAlphaOpaque(void)
 
	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]];
 
		}
 
	}
 
@@ -1438,99 +1432,97 @@ static void QZ_WaitForVerticalBlank(void
 
	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);
 
@@ -1762,138 +1754,136 @@ static void QZ_VideoInit(void)
 
	);
 

	
 
	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];
0 comments (0 inline, 0 general)