(svn r17714) -Cleanup: [OSX] Apply more coding style.

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
michi_cc 15 years ago
parent 9b54d5bbd7
commit ba6866128d

@ -161,18 +161,15 @@ static CGColorSpaceRef QZ_GetCorrectColorSpace()
{ {
static CGColorSpaceRef colorSpace = NULL; static CGColorSpaceRef colorSpace = NULL;
if (colorSpace == NULL) if (colorSpace == NULL) {
{
CMProfileRef sysProfile; CMProfileRef sysProfile;
if (CMGetSystemProfile(&sysProfile) == noErr) if (CMGetSystemProfile(&sysProfile) == noErr) {
{
colorSpace = CGColorSpaceCreateWithPlatformColorSpace(sysProfile); colorSpace = CGColorSpaceCreateWithPlatformColorSpace(sysProfile);
CMCloseProfile(sysProfile); CMCloseProfile(sysProfile);
} }
if (colorSpace == NULL) if (colorSpace == NULL) error("Could not get system colour space. You might need to recalibrate your monitor.");
error("Could not get system colour space. You might need to recalibrate your monitor.");
} }
return colorSpace; return colorSpace;
@ -231,8 +228,7 @@ static CGColorSpaceRef QZ_GetCorrectColorSpace()
/* Don't do anything if the window is currently being created */ /* Don't do anything if the window is currently being created */
if (driver->setup) return; if (driver->setup) return;
if (!driver->WindowResized()) if (!driver->WindowResized()) error("Cocoa: Failed to resize window.");
error("Cocoa: Failed to resize window.");
} }
- (void)appDidHide:(NSNotification*)note - (void)appDidHide:(NSNotification*)note
@ -261,14 +257,11 @@ static CGColorSpaceRef QZ_GetCorrectColorSpace()
- (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag - (id)initWithContentRect:(NSRect)contentRect styleMask:(NSUInteger)styleMask backing:(NSBackingStoreType)backingType defer:(BOOL)flag
{ {
/* Make our window subclass receive these application notifications */ /* Make our window subclass receive these application notifications */
[ [ NSNotificationCenter defaultCenter ] addObserver:self [ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ];
selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ];
[ [ NSNotificationCenter defaultCenter ] addObserver:self [ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ];
selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ];
[ [ NSNotificationCenter defaultCenter ] addObserver:self [ [ NSNotificationCenter defaultCenter ] addObserver:self selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ];
selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ];
return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ]; return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ];
} }
@ -326,31 +319,23 @@ static CGColorSpaceRef QZ_GetCorrectColorSpace()
- (void)drawRect:(NSRect)invalidRect - (void)drawRect:(NSRect)invalidRect
{ {
CGImageRef fullImage;
CGImageRef clippedImage;
NSRect rect;
const NSRect *dirtyRects;
NSInteger dirtyRectCount;
int n;
CGRect clipRect;
CGRect blitRect;
uint32 blitArea = 0;
NSRect frameRect = [ self frame ];
CGContextRef viewContext = (CGContextRef)[ [ NSGraphicsContext currentContext ] graphicsPort ];
if (driver->cgcontext == NULL) return; if (driver->cgcontext == NULL) return;
CGContextRef viewContext = (CGContextRef)[ [ NSGraphicsContext currentContext ] graphicsPort ];
CGContextSetShouldAntialias(viewContext, FALSE); CGContextSetShouldAntialias(viewContext, FALSE);
CGContextSetInterpolationQuality(viewContext, kCGInterpolationNone); CGContextSetInterpolationQuality(viewContext, kCGInterpolationNone);
/* The obtained 'rect' is actually a union of all dirty rects, let's ask for an explicit list of rects instead */ /* The obtained 'rect' is actually a union of all dirty rects, let's ask for an explicit list of rects instead */
const NSRect *dirtyRects;
NSInteger dirtyRectCount;
[ self getRectsBeingDrawn:&dirtyRects count:&dirtyRectCount ]; [ self getRectsBeingDrawn:&dirtyRects count:&dirtyRectCount ];
/* We need an Image in order to do blitting, but as we don't touch the context between this call and drawing no copying will actually be done here */ /* We need an Image in order to do blitting, but as we don't touch the context between this call and drawing no copying will actually be done here */
fullImage = CGBitmapContextCreateImage(driver->cgcontext); CGImageRef fullImage = CGBitmapContextCreateImage(driver->cgcontext);
/* Calculate total area we are blitting */ /* Calculate total area we are blitting */
for (n = 0; n < dirtyRectCount; n++) { uint32 blitArea = 0;
for (int n = 0; n < dirtyRectCount; n++) {
blitArea += dirtyRects[n].size.width * dirtyRects[n].size.height; blitArea += dirtyRects[n].size.width * dirtyRects[n].size.height;
} }
@ -360,8 +345,11 @@ static CGColorSpaceRef QZ_GetCorrectColorSpace()
* rect separately but if we blit more than that, it's just cheaper to blit the entire union in one pass. * rect separately but if we blit more than that, it's just cheaper to blit the entire union in one pass.
* Feel free to remove or find an even better value than 50% ... / blackis * Feel free to remove or find an even better value than 50% ... / blackis
*/ */
NSRect frameRect = [ self frame ];
if (blitArea / (float)(invalidRect.size.width * invalidRect.size.height) > 0.5f) { if (blitArea / (float)(invalidRect.size.width * invalidRect.size.height) > 0.5f) {
rect = invalidRect; NSRect rect = invalidRect;
CGRect clipRect;
CGRect blitRect;
blitRect.origin.x = rect.origin.x; blitRect.origin.x = rect.origin.x;
blitRect.origin.y = rect.origin.y; blitRect.origin.y = rect.origin.y;
@ -375,12 +363,14 @@ static CGColorSpaceRef QZ_GetCorrectColorSpace()
clipRect.size.height = rect.size.height; clipRect.size.height = rect.size.height;
/* Blit dirty part of image */ /* Blit dirty part of image */
clippedImage = CGImageCreateWithImageInRect(fullImage, clipRect); CGImageRef clippedImage = CGImageCreateWithImageInRect(fullImage, clipRect);
CGContextDrawImage(viewContext, blitRect, clippedImage); CGContextDrawImage(viewContext, blitRect, clippedImage);
CGImageRelease(clippedImage); CGImageRelease(clippedImage);
} else { } else {
for (n = 0; n < dirtyRectCount; n++) { for (int n = 0; n < dirtyRectCount; n++) {
rect = dirtyRects[n]; NSRect rect = dirtyRects[n];
CGRect clipRect;
CGRect blitRect;
blitRect.origin.x = rect.origin.x; blitRect.origin.x = rect.origin.x;
blitRect.origin.y = rect.origin.y; blitRect.origin.y = rect.origin.y;
@ -394,7 +384,7 @@ static CGColorSpaceRef QZ_GetCorrectColorSpace()
clipRect.size.height = rect.size.height; clipRect.size.height = rect.size.height;
/* Blit dirty part of image */ /* Blit dirty part of image */
clippedImage = CGImageCreateWithImageInRect(fullImage, clipRect); CGImageRef clippedImage = CGImageCreateWithImageInRect(fullImage, clipRect);
CGContextDrawImage(viewContext, blitRect, clippedImage); CGContextDrawImage(viewContext, blitRect, clippedImage);
CGImageRelease(clippedImage); CGImageRelease(clippedImage);
} }
@ -408,138 +398,123 @@ static CGColorSpaceRef QZ_GetCorrectColorSpace()
void WindowQuartzSubdriver::GetDeviceInfo() void WindowQuartzSubdriver::GetDeviceInfo()
{ {
CFDictionaryRef cur_mode;
/* Initialize the video settings; this data persists between mode switches */ /* Initialize the video settings; this data persists between mode switches */
cur_mode = CGDisplayCurrentMode(kCGDirectMainDisplay); CFDictionaryRef cur_mode = CGDisplayCurrentMode(kCGDirectMainDisplay);
/* Gather some information that is useful to know about the display */ /* Gather some information that is useful to know about the display */
CFNumberGetValue( CFNumberGetValue(
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayWidth), (const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayWidth),
kCFNumberSInt32Type, &device_width kCFNumberSInt32Type, &this->device_width
); );
CFNumberGetValue( CFNumberGetValue(
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayHeight), (const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayHeight),
kCFNumberSInt32Type, &device_height kCFNumberSInt32Type, &this->device_height
); );
} }
bool WindowQuartzSubdriver::SetVideoMode(int width, int height) bool WindowQuartzSubdriver::SetVideoMode(int width, int height)
{ {
char caption[50]; this->setup = true;
unsigned int style; this->GetDeviceInfo();
NSRect contentRect;
BOOL isCustom = NO;
bool ret;
setup = true;
GetDeviceInfo(); if (width > this->device_width) width = this->device_width;
if (height > this->device_height) height = this->device_height;
if (width > device_width) NSRect contentRect = NSMakeRect(0, 0, width, height);
width = device_width;
if (height > device_height)
height = device_height;
contentRect = NSMakeRect(0, 0, width, height);
/* Check if we should recreate the window */ /* Check if we should recreate the window */
if (window == nil) { if (this->window == nil) {
OTTD_QuartzWindowDelegate *delegate; OTTD_QuartzWindowDelegate *delegate;
/* Set the window style */ /* Set the window style */
style = NSTitledWindowMask; unsigned int style = NSTitledWindowMask;
style |= (NSMiniaturizableWindowMask | NSClosableWindowMask); style |= (NSMiniaturizableWindowMask | NSClosableWindowMask);
style |= NSResizableWindowMask; style |= NSResizableWindowMask;
/* Manually create a window, avoids having a nib file resource */ /* Manually create a window, avoids having a nib file resource */
window = [ [ OTTD_QuartzWindow alloc ] this->window = [ [ OTTD_QuartzWindow alloc ]
initWithContentRect: contentRect initWithContentRect:contentRect
styleMask: style styleMask:style
backing: NSBackingStoreBuffered backing:NSBackingStoreBuffered
defer: NO ]; defer:NO ];
if (window == nil) { if (this->window == nil) {
DEBUG(driver, 0, "Could not create the Cocoa window."); DEBUG(driver, 0, "Could not create the Cocoa window.");
setup = false; this->setup = false;
return false; return false;
} }
[ window setDriver:this ]; [ this->window setDriver:this ];
char caption[50];
snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision); snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
NSString *nsscaption = [ [ NSString alloc ] initWithUTF8String:caption ]; NSString *nsscaption = [ [ NSString alloc ] initWithUTF8String:caption ];
[ window setTitle: nsscaption ]; [ this->window setTitle:nsscaption ];
[ window setMiniwindowTitle: nsscaption ]; [ this->window setMiniwindowTitle:nsscaption ];
[ nsscaption release ]; [ nsscaption release ];
[ window setAcceptsMouseMovedEvents: YES ]; [ this->window setAcceptsMouseMovedEvents:YES ];
[ window setViewsNeedDisplay: NO ]; [ this->window setViewsNeedDisplay:NO ];
[ window useOptimizedDrawing: YES ]; [ this->window useOptimizedDrawing:YES ];
delegate = [ [ OTTD_QuartzWindowDelegate alloc ] init ]; delegate = [ [ OTTD_QuartzWindowDelegate alloc ] init ];
[ delegate setDriver: this ]; [ delegate setDriver:this ];
[ window setDelegate: [ delegate autorelease ] ]; [ this->window setDelegate:[ delegate autorelease ] ];
} else { } else {
/* We already have a window, just change its size */ /* We already have a window, just change its size */
if (!isCustom) { [ this->window setContentSize:contentRect.size ];
[ window setContentSize: contentRect.size ];
// Ensure frame height - title bar height >= view height // Ensure frame height - title bar height >= view height
contentRect.size.height = Clamp(height, 0, [ window frame ].size.height - 22 /* 22 is the height of title bar of window*/); contentRect.size.height = Clamp(height, 0, [ this->window frame ].size.height - 22 /* 22 is the height of title bar of window*/);
if (qzview != nil) { if (this->qzview != nil) {
height = contentRect.size.height; height = contentRect.size.height;
[ qzview setFrameSize: contentRect.size ]; [ this->qzview setFrameSize:contentRect.size ];
}
} }
} }
window_width = width; this->window_width = width;
window_height = height; this->window_height = height;
[ window center ]; [ this->window center ];
/* Only recreate the view if it doesn't already exist */ /* Only recreate the view if it doesn't already exist */
if (qzview == nil) { if (this->qzview == nil) {
qzview = [ [ OTTD_QuartzView alloc ] initWithFrame: contentRect ]; this->qzview = [ [ OTTD_QuartzView alloc ] initWithFrame:contentRect ];
if (qzview == nil) { if (this->qzview == nil) {
DEBUG(driver, 0, "Could not create the Quickdraw view."); DEBUG(driver, 0, "Could not create the Quickdraw view.");
setup = false; this->setup = false;
return false; return false;
} }
[ qzview setDriver: this ]; [ this->qzview setDriver:this ];
[ qzview setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ]; [ this->qzview setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable ];
[ window setContentView: qzview ]; [ this->window setContentView:qzview ];
[ qzview release ]; [ this->qzview release ];
[ window makeKeyAndOrderFront:nil ]; [ this->window makeKeyAndOrderFront:nil ];
} }
ret = WindowResized(); bool ret = WindowResized();
this->UpdatePalette(0, 256);
UpdatePalette(0, 256);
setup = false; this->setup = false;
return ret; return ret;
} }
void WindowQuartzSubdriver::BlitIndexedToView32(int left, int top, int right, int bottom) void WindowQuartzSubdriver::BlitIndexedToView32(int left, int top, int right, int bottom)
{ {
const uint32 *pal = palette; const uint32 *pal = this->palette;
const uint8 *src = (uint8*)pixel_buffer; const uint8 *src = (uint8*)this->pixel_buffer;
uint32 *dst = (uint32*)image_buffer; uint32 *dst = (uint32*)this->image_buffer;
uint width = window_width; uint width = this->window_width;
uint pitch = window_width; uint pitch = this->window_width;
int x;
int y;
for (y = top; y < bottom; y++) { for (int y = top; y < bottom; y++) {
for (x = left; x < right; x++) { for (int x = left; x < right; x++) {
dst[y * pitch + x] = pal[src[y * width + x]]; dst[y * pitch + x] = pal[src[y * width + x]];
} }
} }
@ -548,20 +523,20 @@ void WindowQuartzSubdriver::BlitIndexedToView32(int left, int top, int right, in
WindowQuartzSubdriver::WindowQuartzSubdriver(int bpp) WindowQuartzSubdriver::WindowQuartzSubdriver(int bpp)
{ {
window_width = 0; this->window_width = 0;
window_height = 0; this->window_height = 0;
buffer_depth = bpp; this->buffer_depth = bpp;
image_buffer = NULL; this->image_buffer = NULL;
pixel_buffer = NULL; this->pixel_buffer = NULL;
active = false; this->active = false;
setup = false; this->setup = false;
window = nil; this->window = nil;
qzview = nil; this->qzview = nil;
cgcontext = NULL; this->cgcontext = NULL;
num_dirty_rects = MAX_DIRTY_RECTS; this->num_dirty_rects = MAX_DIRTY_RECTS;
} }
WindowQuartzSubdriver::~WindowQuartzSubdriver() WindowQuartzSubdriver::~WindowQuartzSubdriver()
@ -569,103 +544,92 @@ WindowQuartzSubdriver::~WindowQuartzSubdriver()
QZ_ShowMouse(); QZ_ShowMouse();
/* Release window mode resources */ /* Release window mode resources */
if (window != nil) [ window close ]; if (this->window != nil) [ this->window close ];
CGContextRelease(cgcontext); CGContextRelease(this->cgcontext);
free(image_buffer); free(this->image_buffer);
free(pixel_buffer); free(this->pixel_buffer);
} }
void WindowQuartzSubdriver::Draw() void WindowQuartzSubdriver::Draw()
{ {
int i;
NSRect dirtyrect;
/* Check if we need to do anything */ /* Check if we need to do anything */
if (num_dirty_rects == 0 || if (this->num_dirty_rects == 0 || [ this->window isMiniaturized ]) return;
[ window isMiniaturized ]) {
return; if (this->num_dirty_rects >= MAX_DIRTY_RECTS) {
} this->num_dirty_rects = 1;
this->dirty_rects[0].left = 0;
if (num_dirty_rects >= MAX_DIRTY_RECTS) { this->dirty_rects[0].top = 0;
num_dirty_rects = 1; this->dirty_rects[0].right = this->window_width;
dirty_rects[0].left = 0; this->dirty_rects[0].bottom = this->window_height;
dirty_rects[0].top = 0;
dirty_rects[0].right = window_width;
dirty_rects[0].bottom = window_height;
} }
/* Build the region of dirty rectangles */ /* Build the region of dirty rectangles */
for (i = 0; i < num_dirty_rects; i++) { for (int i = 0; i < this->num_dirty_rects; i++) {
/* We only need to blit in indexed mode since in 32bpp mode the game draws directly to the image. */ /* We only need to blit in indexed mode since in 32bpp mode the game draws directly to the image. */
if (buffer_depth == 8) { if (this->buffer_depth == 8) {
BlitIndexedToView32( BlitIndexedToView32(
dirty_rects[i].left, this->dirty_rects[i].left,
dirty_rects[i].top, this->dirty_rects[i].top,
dirty_rects[i].right, this->dirty_rects[i].right,
dirty_rects[i].bottom this->dirty_rects[i].bottom
); );
} }
dirtyrect.origin.x = dirty_rects[i].left; NSRect dirtyrect;
dirtyrect.origin.y = window_height - dirty_rects[i].bottom; dirtyrect.origin.x = this->dirty_rects[i].left;
dirtyrect.size.width = dirty_rects[i].right - dirty_rects[i].left; dirtyrect.origin.y = this->window_height - this->dirty_rects[i].bottom;
dirtyrect.size.height = dirty_rects[i].bottom - dirty_rects[i].top; dirtyrect.size.width = this->dirty_rects[i].right - this->dirty_rects[i].left;
dirtyrect.size.height = this->dirty_rects[i].bottom - this->dirty_rects[i].top;
/* drawRect will be automatically called by Mac OS X during next update cycle, and then blitting will occur */ /* drawRect will be automatically called by Mac OS X during next update cycle, and then blitting will occur */
[ qzview setNeedsDisplayInRect: dirtyrect ]; [ qzview setNeedsDisplayInRect:dirtyrect ];
} }
//DrawResizeIcon(); //DrawResizeIcon();
num_dirty_rects = 0; this->num_dirty_rects = 0;
} }
void WindowQuartzSubdriver::MakeDirty(int left, int top, int width, int height) void WindowQuartzSubdriver::MakeDirty(int left, int top, int width, int height)
{ {
if (num_dirty_rects < MAX_DIRTY_RECTS) { if (this->num_dirty_rects < MAX_DIRTY_RECTS) {
dirty_rects[num_dirty_rects].left = left; dirty_rects[this->num_dirty_rects].left = left;
dirty_rects[num_dirty_rects].top = top; dirty_rects[this->num_dirty_rects].top = top;
dirty_rects[num_dirty_rects].right = left + width; dirty_rects[this->num_dirty_rects].right = left + width;
dirty_rects[num_dirty_rects].bottom = top + height; dirty_rects[this->num_dirty_rects].bottom = top + height;
} }
num_dirty_rects++; this->num_dirty_rects++;
} }
void WindowQuartzSubdriver::UpdatePalette(uint first_color, uint num_colors) void WindowQuartzSubdriver::UpdatePalette(uint first_color, uint num_colors)
{ {
uint i; if (this->buffer_depth != 8) return;
if (buffer_depth != 8) for (uint i = first_color; i < first_color + num_colors; i++) {
return;
for (i = first_color; i < first_color + num_colors; i++) {
uint32 clr = 0xff000000; uint32 clr = 0xff000000;
clr |= (uint32)_cur_palette[i].r << 16; clr |= (uint32)_cur_palette[i].r << 16;
clr |= (uint32)_cur_palette[i].g << 8; clr |= (uint32)_cur_palette[i].g << 8;
clr |= (uint32)_cur_palette[i].b; clr |= (uint32)_cur_palette[i].b;
palette[i] = clr; this->palette[i] = clr;
} }
num_dirty_rects = MAX_DIRTY_RECTS; this->num_dirty_rects = MAX_DIRTY_RECTS;
} }
uint WindowQuartzSubdriver::ListModes(OTTD_Point *modes, uint max_modes) uint WindowQuartzSubdriver::ListModes(OTTD_Point *modes, uint max_modes)
{ {
return QZ_ListModes(modes, max_modes, kCGDirectMainDisplay, buffer_depth); return QZ_ListModes(modes, max_modes, kCGDirectMainDisplay, this->buffer_depth);
} }
bool WindowQuartzSubdriver::ChangeResolution(int w, int h) bool WindowQuartzSubdriver::ChangeResolution(int w, int h)
{ {
int old_width = window_width; int old_width = this->window_width;
int old_height = window_height; int old_height = this->window_height;
if (SetVideoMode(w, h))
return true;
if (old_width != 0 && old_height != 0) if (this->SetVideoMode(w, h)) return true;
SetVideoMode(old_width, old_height); if (old_width != 0 && old_height != 0) this->SetVideoMode(old_width, old_height);
return false; return false;
} }
@ -673,14 +637,14 @@ bool WindowQuartzSubdriver::ChangeResolution(int w, int h)
/* Convert local coordinate to window server (CoreGraphics) coordinate */ /* Convert local coordinate to window server (CoreGraphics) coordinate */
CGPoint WindowQuartzSubdriver::PrivateLocalToCG(NSPoint *p) CGPoint WindowQuartzSubdriver::PrivateLocalToCG(NSPoint *p)
{ {
CGPoint cgp;
p->y = window_height - p->y; p->y = this->window_height - p->y;
*p = [ qzview convertPoint:*p toView: nil ]; *p = [ this->qzview convertPoint:*p toView:nil ];
*p = [ window convertBaseToScreen:*p ]; *p = [ this->window convertBaseToScreen:*p ];
p->y = device_height - p->y; p->y = this->device_height - p->y;
CGPoint cgp;
cgp.x = p->x; cgp.x = p->x;
cgp.y = p->y; cgp.y = p->y;
@ -689,19 +653,17 @@ CGPoint WindowQuartzSubdriver::PrivateLocalToCG(NSPoint *p)
NSPoint WindowQuartzSubdriver::GetMouseLocation(NSEvent *event) NSPoint WindowQuartzSubdriver::GetMouseLocation(NSEvent *event)
{ {
NSPoint pt; NSPoint pt = [ event locationInWindow ];
pt = [ this->qzview convertPoint:pt fromView:nil ];
pt = [ event locationInWindow ]; pt.y = this->window_height - pt.y;
pt = [ qzview convertPoint:pt fromView:nil ];
pt.y = window_height - pt.y;
return pt; return pt;
} }
bool WindowQuartzSubdriver::MouseIsInsideView(NSPoint *pt) bool WindowQuartzSubdriver::MouseIsInsideView(NSPoint *pt)
{ {
return [ qzview mouse:*pt inRect:[ qzview bounds ] ]; return [ qzview mouse:*pt inRect:[ this->qzview bounds ] ];
} }
@ -711,49 +673,48 @@ bool WindowQuartzSubdriver::MouseIsInsideView(NSPoint *pt)
*/ */
void WindowQuartzSubdriver::SetPortAlphaOpaque() void WindowQuartzSubdriver::SetPortAlphaOpaque()
{ {
uint32 *pixels = (uint32*)image_buffer; uint32 *pixels = (uint32*)this->image_buffer;
uint32 pitch = window_width; uint32 pitch = this->window_width;
int x, y;
for (y = 0; y < window_height; y++) for (int y = 0; y < this->window_height; y++)
for (x = 0; x < window_width; x++) { for (int x = 0; x < this->window_width; x++) {
pixels[y * pitch + x] |= 0xFF000000; pixels[y * pitch + x] |= 0xFF000000;
} }
} }
bool WindowQuartzSubdriver::WindowResized() bool WindowQuartzSubdriver::WindowResized()
{ {
if (window == nil || qzview == nil) return true; if (this->window == nil || this->qzview == nil) return true;
NSRect newframe = [ qzview frame ]; NSRect newframe = [ this->qzview frame ];
window_width = newframe.size.width; this->window_width = newframe.size.width;
window_height = newframe.size.height; this->window_height = newframe.size.height;
/* Create Core Graphics Context */ /* Create Core Graphics Context */
free(image_buffer); free(this->image_buffer);
image_buffer = (uint32*)malloc(window_width * window_height * 4); this->image_buffer = (uint32*)malloc(this->window_width * this->window_height * 4);
CGContextRelease(cgcontext); CGContextRelease(this->cgcontext);
cgcontext = CGBitmapContextCreate( this->cgcontext = CGBitmapContextCreate(
image_buffer, // data this->image_buffer, // data
window_width, // width this->window_width, // width
window_height, // height this->window_height, // height
8, // bits per component 8, // bits per component
window_width * 4, // bytes per row this->window_width * 4, // bytes per row
QZ_GetCorrectColorSpace(), // color space QZ_GetCorrectColorSpace(), // color space
kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host
); );
assert(cgcontext != NULL); assert(this->cgcontext != NULL);
CGContextSetShouldAntialias(cgcontext, FALSE); CGContextSetShouldAntialias(this->cgcontext, FALSE);
CGContextSetAllowsAntialiasing(cgcontext, FALSE); CGContextSetAllowsAntialiasing(this->cgcontext, FALSE);
CGContextSetInterpolationQuality(cgcontext, kCGInterpolationNone); CGContextSetInterpolationQuality(this->cgcontext, kCGInterpolationNone);
if (buffer_depth == 8) { if (this->buffer_depth == 8) {
free(pixel_buffer); free(this->pixel_buffer);
pixel_buffer = malloc(window_width * window_height); this->pixel_buffer = malloc(this->window_width * this->window_height);
if (pixel_buffer == NULL) { if (this->pixel_buffer == NULL) {
DEBUG(driver, 0, "Failed to allocate pixel buffer"); DEBUG(driver, 0, "Failed to allocate pixel buffer");
return false; return false;
} }
@ -762,7 +723,7 @@ bool WindowQuartzSubdriver::WindowResized()
QZ_GameSizeChanged(); QZ_GameSizeChanged();
/* Redraw screen */ /* Redraw screen */
num_dirty_rects = MAX_DIRTY_RECTS; this->num_dirty_rects = MAX_DIRTY_RECTS;
return true; return true;
} }
@ -770,8 +731,6 @@ bool WindowQuartzSubdriver::WindowResized()
CocoaSubdriver *QZ_CreateWindowQuartzSubdriver(int width, int height, int bpp) CocoaSubdriver *QZ_CreateWindowQuartzSubdriver(int width, int height, int bpp)
{ {
WindowQuartzSubdriver *ret;
if (!MacOSVersionIsAtLeast(10, 4, 0)) { if (!MacOSVersionIsAtLeast(10, 4, 0)) {
DEBUG(driver, 0, "The cocoa quartz subdriver requires Mac OS X 10.4 or later."); DEBUG(driver, 0, "The cocoa quartz subdriver requires Mac OS X 10.4 or later.");
return NULL; return NULL;
@ -782,7 +741,7 @@ CocoaSubdriver *QZ_CreateWindowQuartzSubdriver(int width, int height, int bpp)
return NULL; return NULL;
} }
ret = new WindowQuartzSubdriver(bpp); WindowQuartzSubdriver *ret = new WindowQuartzSubdriver(bpp);
if (!ret->ChangeResolution(width, height)) { if (!ret->ChangeResolution(width, height)) {
delete ret; delete ret;

@ -224,8 +224,7 @@ public:
/* Don't do anything if the window is currently being created */ /* Don't do anything if the window is currently being created */
if (driver->setup) return; if (driver->setup) return;
if (!driver->WindowResized()) if (!driver->WindowResized()) error("Cocoa: Failed to resize window.");
error("Cocoa: Failed to resize window.");
} }
- (void)appDidHide:(NSNotification*)note - (void)appDidHide:(NSNotification*)note
@ -255,13 +254,13 @@ public:
{ {
/* Make our window subclass receive these application notifications */ /* Make our window subclass receive these application notifications */
[ [ NSNotificationCenter defaultCenter ] addObserver:self [ [ NSNotificationCenter defaultCenter ] addObserver:self
selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ]; selector:@selector(appDidHide:) name:NSApplicationDidHideNotification object:NSApp ];
[ [ NSNotificationCenter defaultCenter ] addObserver:self [ [ NSNotificationCenter defaultCenter ] addObserver:self
selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ]; selector:@selector(appDidUnhide:) name:NSApplicationDidUnhideNotification object:NSApp ];
[ [ NSNotificationCenter defaultCenter ] addObserver:self [ [ NSNotificationCenter defaultCenter ] addObserver:self
selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ]; selector:@selector(appWillUnhide:) name:NSApplicationWillUnhideNotification object:NSApp ];
return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ]; return [ super initWithContentRect:contentRect styleMask:styleMask backing:backingType defer:flag ];
} }
@ -329,160 +328,132 @@ static bool _resize_icon[] = {
void WindowQuickdrawSubdriver::GetDeviceInfo() void WindowQuickdrawSubdriver::GetDeviceInfo()
{ {
CFDictionaryRef cur_mode;
/* Initialize the video settings; this data persists between mode switches */ /* Initialize the video settings; this data persists between mode switches */
cur_mode = CGDisplayCurrentMode(kCGDirectMainDisplay); CFDictionaryRef cur_mode = CGDisplayCurrentMode(kCGDirectMainDisplay);
/* Gather some information that is useful to know about the display */ /* Gather some information that is useful to know about the display */
CFNumberGetValue( CFNumberGetValue((const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayBitsPerPixel),
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayBitsPerPixel), kCFNumberSInt32Type, &this->device_depth);
kCFNumberSInt32Type, &device_depth
);
CFNumberGetValue( CFNumberGetValue((const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayWidth),
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayWidth), kCFNumberSInt32Type, &this->device_width);
kCFNumberSInt32Type, &device_width
);
CFNumberGetValue( CFNumberGetValue((const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayHeight),
(const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayHeight), kCFNumberSInt32Type, &this->device_height);
kCFNumberSInt32Type, &device_height
);
} }
bool WindowQuickdrawSubdriver::SetVideoMode(int width, int height) bool WindowQuickdrawSubdriver::SetVideoMode(int width, int height)
{ {
char caption[50]; this->setup = true;
NSString *nsscaption; this->GetDeviceInfo();
unsigned int style;
NSRect contentRect;
BOOL isCustom = NO;
bool ret;
setup = true;
GetDeviceInfo(); if (this->buffer_depth > this->device_depth) {
if (buffer_depth > device_depth) {
DEBUG(driver, 0, "Cannot use a blitter with a higer screen depth than the display when running in windowed mode."); DEBUG(driver, 0, "Cannot use a blitter with a higer screen depth than the display when running in windowed mode.");
setup = false; this->setup = false;
return false; return false;
} }
if (width > device_width) if (width > this->device_width) width = this->device_width;
width = device_width; if (height > this->device_height) height = this->device_height;
if (height > device_height)
height = device_height;
contentRect = NSMakeRect(0, 0, width, height); NSRect contentRect = NSMakeRect(0, 0, width, height);
/* Check if we should recreate the window */ /* Check if we should recreate the window */
if (window == nil) { if (this->window == nil) {
OTTD_QuickdrawWindowDelegate *delegate; OTTD_QuickdrawWindowDelegate *delegate;
/* Set the window style */ /* Set the window style */
style = NSTitledWindowMask; unsigned int style = NSTitledWindowMask;
style |= (NSMiniaturizableWindowMask | NSClosableWindowMask); style |= (NSMiniaturizableWindowMask | NSClosableWindowMask);
style |= NSResizableWindowMask; style |= NSResizableWindowMask;
/* Manually create a window, avoids having a nib file resource */ /* Manually create a window, avoids having a nib file resource */
window = [ [ OTTD_QuickdrawWindow alloc ] this->window = [ [ OTTD_QuickdrawWindow alloc ] initWithContentRect:contentRect
initWithContentRect:contentRect styleMask:style backing:NSBackingStoreBuffered defer:NO ];
styleMask:style
backing:NSBackingStoreBuffered
defer:NO ];
if (window == nil) { if (this->window == nil) {
DEBUG(driver, 0, "Could not create the Cocoa window."); DEBUG(driver, 0, "Could not create the Cocoa window.");
setup = false; this->setup = false;
return false; return false;
} }
[ window setDriver:this ]; [ this->window setDriver:this ];
char caption[50];
snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision); snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
nsscaption = [ [ NSString alloc ] initWithUTF8String:caption ]; NSString *nsscaption = [ [ NSString alloc ] initWithUTF8String:caption ];
[ window setTitle:nsscaption ]; [ this->window setTitle:nsscaption ];
[ window setMiniwindowTitle:nsscaption ]; [ this->window setMiniwindowTitle:nsscaption ];
[ nsscaption release ]; [ nsscaption release ];
[ window setAcceptsMouseMovedEvents:YES ]; [ this->window setAcceptsMouseMovedEvents:YES ];
[ window setViewsNeedDisplay:NO ]; [ this->window setViewsNeedDisplay:NO ];
delegate = [ [ OTTD_QuickdrawWindowDelegate alloc ] init ]; delegate = [ [ OTTD_QuickdrawWindowDelegate alloc ] init ];
[ delegate setDriver:this ]; [ delegate setDriver:this ];
[ window setDelegate: [ delegate autorelease ] ]; [ this->window setDelegate: [ delegate autorelease ] ];
} else { } else {
/* We already have a window, just change its size */ /* We already have a window, just change its size */
if (!isCustom) { [ this->window setContentSize:contentRect.size ];
[ window setContentSize:contentRect.size ]; /* Ensure frame height - title bar height >= view height */
// Ensure frame height - title bar height >= view height contentRect.size.height = Clamp(height, 0, [ this->window frame ].size.height - 22); // 22 is the height of title bar of window
contentRect.size.height = Clamp(height, 0, [ window frame ].size.height - 22 /* 22 is the height of title bar of window*/); height = contentRect.size.height;
height = contentRect.size.height; [ this->qdview setFrameSize:contentRect.size ];
[ qdview setFrameSize:contentRect.size ];
}
} }
// Update again // Update again
window_width = width; this->window_width = width;
window_height = height; this->window_height = height;
[ window center ]; [ this->window center ];
/* Only recreate the view if it doesn't already exist */ /* Only recreate the view if it doesn't already exist */
if (qdview == nil) { if (this->qdview == nil) {
qdview = [ [ NSQuickDrawView alloc ] initWithFrame:contentRect ]; this->qdview = [ [ NSQuickDrawView alloc ] initWithFrame:contentRect ];
if (qdview == nil) { if (this->qdview == nil) {
DEBUG(driver, 0, "Could not create the Quickdraw view."); DEBUG(driver, 0, "Could not create the Quickdraw view.");
setup = false; this->setup = false;
return false; return false;
} }
[ qdview setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ]; [ this->qdview setAutoresizingMask: NSViewWidthSizable | NSViewHeightSizable ];
[ [ window contentView ] addSubview:qdview ]; [ [ this->window contentView ] addSubview:this->qdview ];
[ qdview release ]; [ this->qdview release ];
[ window makeKeyAndOrderFront:nil ]; [ this->window makeKeyAndOrderFront:nil ];
} }
ret = WindowResized(); bool ret = this->WindowResized();
this->UpdatePalette(0, 256);
UpdatePalette(0, 256);
setup = false;
this->setup = false;
return ret; return ret;
} }
void WindowQuickdrawSubdriver::Blit32ToView32(int left, int top, int right, int bottom) void WindowQuickdrawSubdriver::Blit32ToView32(int left, int top, int right, int bottom)
{ {
const uint32 *src = (uint32*)pixel_buffer; const uint32 *src = (uint32*)this->pixel_buffer;
uint32 *dst = (uint32*)window_buffer; uint32 *dst = (uint32*)this->window_buffer;
uint width = window_width; uint width = this->window_width;
uint pitch = window_pitch / 4; uint pitch = this->window_pitch / 4;
int y;
dst += top * pitch + left; dst += top * pitch + left;
src += top * width + left; src += top * width + left;
for (y = top; y < bottom; y++, dst+= pitch, src+= width) { for (int y = top; y < bottom; y++, dst+= pitch, src+= width) {
memcpy(dst, src, (right - left) * 4); memcpy(dst, src, (right - left) * 4);
} }
} }
void WindowQuickdrawSubdriver::BlitIndexedToView32(int left, int top, int right, int bottom) void WindowQuickdrawSubdriver::BlitIndexedToView32(int left, int top, int right, int bottom)
{ {
const uint32 *pal = palette32; const uint32 *pal = this->palette32;
const uint8 *src = (uint8*)pixel_buffer; const uint8 *src = (uint8*)this->pixel_buffer;
uint32 *dst = (uint32*)window_buffer; uint32 *dst = (uint32*)this->window_buffer;
uint width = window_width; uint width = this->window_width;
uint pitch = window_pitch / 4; uint pitch = this->window_pitch / 4;
int x;
int y;
for (y = top; y < bottom; y++) { for (int y = top; y < bottom; y++) {
for (x = left; x < right; x++) { for (int x = left; x < right; x++) {
dst[y * pitch + x] = pal[src[y * width + x]]; dst[y * pitch + x] = pal[src[y * width + x]];
} }
} }
@ -490,16 +461,14 @@ void WindowQuickdrawSubdriver::BlitIndexedToView32(int left, int top, int right,
void WindowQuickdrawSubdriver::BlitIndexedToView16(int left, int top, int right, int bottom) void WindowQuickdrawSubdriver::BlitIndexedToView16(int left, int top, int right, int bottom)
{ {
const uint16 *pal = palette16; const uint16 *pal = this->palette16;
const uint8 *src = (uint8*)pixel_buffer; const uint8 *src = (uint8*)this->pixel_buffer;
uint16 *dst = (uint16*)window_buffer; uint16 *dst = (uint16*)this->window_buffer;
uint width = window_width; uint width = this->window_width;
uint pitch = window_pitch / 2; uint pitch = this->window_pitch / 2;
int x;
int y;
for (y = top; y < bottom; y++) { for (int y = top; y < bottom; y++) {
for (x = left; x < right; x++) { for (int x = left; x < right; x++) {
dst[y * pitch + x] = pal[src[y * width + x]]; dst[y * pitch + x] = pal[src[y * width + x]];
} }
} }
@ -508,45 +477,43 @@ void WindowQuickdrawSubdriver::BlitIndexedToView16(int left, int top, int right,
inline void WindowQuickdrawSubdriver::BlitToView(int left, int top, int right, int bottom) inline void WindowQuickdrawSubdriver::BlitToView(int left, int top, int right, int bottom)
{ {
switch (device_depth) { switch (this->device_depth) {
case 32: case 32:
switch (buffer_depth) { switch (this->buffer_depth) {
case 32: case 32:
Blit32ToView32(left, top, right, bottom); this->Blit32ToView32(left, top, right, bottom);
break; break;
case 8: case 8:
BlitIndexedToView32(left, top, right, bottom); this->BlitIndexedToView32(left, top, right, bottom);
break; break;
} }
break; break;
case 16: case 16:
BlitIndexedToView16(left, top, right, bottom); this->BlitIndexedToView16(left, top, right, bottom);
break; break;
} }
} }
void WindowQuickdrawSubdriver::DrawResizeIcon() void WindowQuickdrawSubdriver::DrawResizeIcon()
{ {
int xoff = window_width - _resize_icon_width; int xoff = this->window_width - _resize_icon_width;
int yoff = window_height - _resize_icon_height; int yoff = this->window_height - _resize_icon_height;
int x;
int y;
switch (device_depth) { switch (this->device_depth) {
case 32: case 32:
for (y = 0; y < _resize_icon_height; y++) { for (int y = 0; y < _resize_icon_height; y++) {
uint32 *trg = (uint32*)window_buffer + (yoff + y) * window_pitch / 4 + xoff; uint32 *trg = (uint32*)this->window_buffer + (yoff + y) * this->window_pitch / 4 + xoff;
for (x = 0; x < _resize_icon_width; x++, trg++) { for (int x = 0; x < _resize_icon_width; x++, trg++) {
if (_resize_icon[y * _resize_icon_width + x]) *trg = 0xff000000; if (_resize_icon[y * _resize_icon_width + x]) *trg = 0xff000000;
} }
} }
break; break;
case 16: case 16:
for (y = 0; y < _resize_icon_height; y++) { for (int y = 0; y < _resize_icon_height; y++) {
uint16 *trg = (uint16*)window_buffer + (yoff + y) * window_pitch / 2 + xoff; uint16 *trg = (uint16*)this->window_buffer + (yoff + y) * this->window_pitch / 2 + xoff;
for (x = 0; x < _resize_icon_width; x++, trg++) { for (int x = 0; x < _resize_icon_width; x++, trg++) {
if (_resize_icon[y * _resize_icon_width + x]) *trg = 0x0000; if (_resize_icon[y * _resize_icon_width + x]) *trg = 0x0000;
} }
} }
@ -557,17 +524,17 @@ void WindowQuickdrawSubdriver::DrawResizeIcon()
WindowQuickdrawSubdriver::WindowQuickdrawSubdriver(int bpp) WindowQuickdrawSubdriver::WindowQuickdrawSubdriver(int bpp)
{ {
window_width = 0; this->window_width = 0;
window_height = 0; this->window_height = 0;
buffer_depth = bpp; this->buffer_depth = bpp;
pixel_buffer = NULL; this->pixel_buffer = NULL;
active = false; this->active = false;
setup = false; this->setup = false;
window = nil; this->window = nil;
qdview = nil; this->qdview = nil;
num_dirty_rects = MAX_DIRTY_RECTS; this->num_dirty_rects = MAX_DIRTY_RECTS;
} }
WindowQuickdrawSubdriver::~WindowQuickdrawSubdriver() WindowQuickdrawSubdriver::~WindowQuickdrawSubdriver()
@ -575,121 +542,101 @@ WindowQuickdrawSubdriver::~WindowQuickdrawSubdriver()
QZ_ShowMouse(); QZ_ShowMouse();
/* Release window mode resources */ /* Release window mode resources */
if (window != nil) [ window close ]; if (this->window != nil) [ this->window close ];
free(pixel_buffer); free(this->pixel_buffer);
} }
void WindowQuickdrawSubdriver::Draw() void WindowQuickdrawSubdriver::Draw()
{ {
int i;
RgnHandle dirty, temp;
/* Check if we need to do anything */ /* Check if we need to do anything */
if (num_dirty_rects == 0 || if (this->num_dirty_rects == 0 || [ this->window isMiniaturized ]) return;
[ window isMiniaturized ]) {
return; if (this->num_dirty_rects >= MAX_DIRTY_RECTS) {
this->num_dirty_rects = 1;
this->dirty_rects[0].left = 0;
this->dirty_rects[0].top = 0;
this->dirty_rects[0].right = this->window_width;
this->dirty_rects[0].bottom = this->window_height;
} }
if (num_dirty_rects >= MAX_DIRTY_RECTS) { RgnHandle dirty = NewRgn();
num_dirty_rects = 1; RgnHandle temp = NewRgn();
dirty_rects[0].left = 0;
dirty_rects[0].top = 0;
dirty_rects[0].right = window_width;
dirty_rects[0].bottom = window_height;
}
dirty = NewRgn();
temp = NewRgn();
SetEmptyRgn(dirty); SetEmptyRgn(dirty);
/* Build the region of dirty rectangles */ /* Build the region of dirty rectangles */
for (i = 0; i < num_dirty_rects; i++) { for (int i = 0; i < this->num_dirty_rects; i++) {
BlitToView( this->BlitToView(this->dirty_rects[i].left, this->dirty_rects[i].top,
dirty_rects[i].left, this->dirty_rects[i].right, this->dirty_rects[i].bottom);
dirty_rects[i].top,
dirty_rects[i].right, MacSetRectRgn(temp, this->dirty_rects[i].left, this->dirty_rects[i].top,
dirty_rects[i].bottom this->dirty_rects[i].right, this->dirty_rects[i].bottom);
);
MacSetRectRgn(
temp,
dirty_rects[i].left,
dirty_rects[i].top,
dirty_rects[i].right,
dirty_rects[i].bottom
);
MacUnionRgn(dirty, temp, dirty); MacUnionRgn(dirty, temp, dirty);
} }
DrawResizeIcon(); this->DrawResizeIcon();
/* Flush the dirty region */ /* Flush the dirty region */
QDFlushPortBuffer( (OpaqueGrafPtr*) [ qdview qdPort ], dirty); QDFlushPortBuffer( (OpaqueGrafPtr*) [ this->qdview qdPort ], dirty);
DisposeRgn(dirty); DisposeRgn(dirty);
DisposeRgn(temp); DisposeRgn(temp);
num_dirty_rects = 0; this->num_dirty_rects = 0;
} }
void WindowQuickdrawSubdriver::MakeDirty(int left, int top, int width, int height) void WindowQuickdrawSubdriver::MakeDirty(int left, int top, int width, int height)
{ {
if (num_dirty_rects < MAX_DIRTY_RECTS) { if (this->num_dirty_rects < MAX_DIRTY_RECTS) {
dirty_rects[num_dirty_rects].left = left; this->dirty_rects[this->num_dirty_rects].left = left;
dirty_rects[num_dirty_rects].top = top; this->dirty_rects[this->num_dirty_rects].top = top;
dirty_rects[num_dirty_rects].right = left + width; this->dirty_rects[this->num_dirty_rects].right = left + width;
dirty_rects[num_dirty_rects].bottom = top + height; this->dirty_rects[this->num_dirty_rects].bottom = top + height;
} }
num_dirty_rects++; this->num_dirty_rects++;
} }
void WindowQuickdrawSubdriver::UpdatePalette(uint first_color, uint num_colors) void WindowQuickdrawSubdriver::UpdatePalette(uint first_color, uint num_colors)
{ {
uint i; if (this->buffer_depth != 8) return;
if (buffer_depth != 8)
return;
switch (device_depth) { switch (this->device_depth) {
case 32: case 32:
for (i = first_color; i < first_color + num_colors; i++) { for (uint i = first_color; i < first_color + num_colors; i++) {
uint32 clr32 = 0xff000000; uint32 clr32 = 0xff000000;
clr32 |= (uint32)_cur_palette[i].r << 16; clr32 |= (uint32)_cur_palette[i].r << 16;
clr32 |= (uint32)_cur_palette[i].g << 8; clr32 |= (uint32)_cur_palette[i].g << 8;
clr32 |= (uint32)_cur_palette[i].b; clr32 |= (uint32)_cur_palette[i].b;
palette32[i] = clr32; this->palette32[i] = clr32;
} }
break; break;
case 16: case 16:
for (i = first_color; i < first_color + num_colors; i++) { for (uint i = first_color; i < first_color + num_colors; i++) {
uint16 clr16 = 0x0000; uint16 clr16 = 0x0000;
clr16 |= (uint16)((_cur_palette[i].r >> 3) & 0x1f) << 10; clr16 |= (uint16)((_cur_palette[i].r >> 3) & 0x1f) << 10;
clr16 |= (uint16)((_cur_palette[i].g >> 3) & 0x1f) << 5; clr16 |= (uint16)((_cur_palette[i].g >> 3) & 0x1f) << 5;
clr16 |= (uint16)((_cur_palette[i].b >> 3) & 0x1f); clr16 |= (uint16)((_cur_palette[i].b >> 3) & 0x1f);
palette16[i] = clr16; this->palette16[i] = clr16;
} }
break; break;
} }
num_dirty_rects = MAX_DIRTY_RECTS; this->num_dirty_rects = MAX_DIRTY_RECTS;
} }
uint WindowQuickdrawSubdriver::ListModes(OTTD_Point *modes, uint max_modes) uint WindowQuickdrawSubdriver::ListModes(OTTD_Point *modes, uint max_modes)
{ {
return QZ_ListModes(modes, max_modes, kCGDirectMainDisplay, buffer_depth); return QZ_ListModes(modes, max_modes, kCGDirectMainDisplay, this->buffer_depth);
} }
bool WindowQuickdrawSubdriver::ChangeResolution(int w, int h) bool WindowQuickdrawSubdriver::ChangeResolution(int w, int h)
{ {
int old_width = window_width; int old_width = this->window_width;
int old_height = window_height; int old_height = this->window_height;
if (SetVideoMode(w, h)) if (this->SetVideoMode(w, h)) return true;
return true;
if (old_width != 0 && old_height != 0) if (old_width != 0 && old_height != 0) this->SetVideoMode(old_width, old_height);
SetVideoMode(old_width, old_height);
return false; return false;
} }
@ -697,31 +644,24 @@ bool WindowQuickdrawSubdriver::ChangeResolution(int w, int h)
/* Convert local coordinate to window server (CoreGraphics) coordinate */ /* Convert local coordinate to window server (CoreGraphics) coordinate */
CGPoint WindowQuickdrawSubdriver::PrivateLocalToCG(NSPoint *p) CGPoint WindowQuickdrawSubdriver::PrivateLocalToCG(NSPoint *p)
{ {
CGPoint cgp; *p = [ this->qdview convertPoint:*p toView: nil ];
*p = [ this->window convertBaseToScreen:*p ];
p->y = this->device_height - p->y;
*p = [ qdview convertPoint:*p toView: nil ]; return CGPointMake(p->x, p->y);
*p = [ window convertBaseToScreen:*p ];
p->y = device_height - p->y;
cgp.x = p->x;
cgp.y = p->y;
return cgp;
} }
NSPoint WindowQuickdrawSubdriver::GetMouseLocation(NSEvent *event) NSPoint WindowQuickdrawSubdriver::GetMouseLocation(NSEvent *event)
{ {
NSPoint pt; NSPoint pt = [ event locationInWindow ];
pt = [ this->qdview convertPoint:pt fromView:nil ];
pt = [ event locationInWindow ];
pt = [ qdview convertPoint:pt fromView:nil ];
return pt; return pt;
} }
bool WindowQuickdrawSubdriver::MouseIsInsideView(NSPoint *pt) bool WindowQuickdrawSubdriver::MouseIsInsideView(NSPoint *pt)
{ {
return [ qdview mouse:*pt inRect:[ qdview bounds ] ]; return [ this->qdview mouse:*pt inRect:[ this->qdview bounds ] ];
} }
@ -731,45 +671,42 @@ bool WindowQuickdrawSubdriver::MouseIsInsideView(NSPoint *pt)
*/ */
void WindowQuickdrawSubdriver::SetPortAlphaOpaque() void WindowQuickdrawSubdriver::SetPortAlphaOpaque()
{ {
if (device_depth != 32) if (this->device_depth != 32) return;
return;
uint32 *pixels = (uint32*)window_buffer; uint32 *pixels = (uint32*)this->window_buffer;
uint32 pitch = window_pitch / 4; uint32 pitch = this->window_pitch / 4;
int x, y;
for (y = 0; y < window_height; y++) for (int y = 0; y < this->window_height; y++)
for (x = 0; x < window_width; x++) { for (int x = 0; x < this->window_width; x++) {
pixels[y * pitch + x] |= 0xFF000000; pixels[y * pitch + x] |= 0xFF000000;
} }
} }
bool WindowQuickdrawSubdriver::WindowResized() bool WindowQuickdrawSubdriver::WindowResized()
{ {
if (window == nil || qdview == nil) return true; if (this->window == nil || this->qdview == nil) return true;
NSRect newframe = [ qdview frame ]; NSRect newframe = [ this->qdview frame ];
CGrafPtr thePort = (OpaqueGrafPtr*) [ qdview qdPort ]; CGrafPtr thePort = (OpaqueGrafPtr*) [ this->qdview qdPort ];
int voff, hoff;
LockPortBits(thePort); LockPortBits(thePort);
window_buffer = GetPixBaseAddr(GetPortPixMap(thePort)); this->window_buffer = GetPixBaseAddr(GetPortPixMap(thePort));
window_pitch = GetPixRowBytes(GetPortPixMap(thePort)); this->window_pitch = GetPixRowBytes(GetPortPixMap(thePort));
UnlockPortBits(thePort); UnlockPortBits(thePort);
/* _cocoa_video_data.realpixels now points to the window's pixels /* _cocoa_video_data.realpixels now points to the window's pixels
* We want it to point to the *view's* pixels * We want it to point to the *view's* pixels
*/ */
voff = [ window frame ].size.height - newframe.size.height - newframe.origin.y; int voff = [ this->window frame ].size.height - newframe.size.height - newframe.origin.y;
hoff = [ qdview frame ].origin.x; int hoff = [ this->qdview frame ].origin.x;
window_buffer = (uint8*)window_buffer + (voff * window_pitch) + hoff * (device_depth / 8); this->window_buffer = (uint8*)this->window_buffer + (voff * this->window_pitch) + hoff * (this->device_depth / 8);
window_width = newframe.size.width; this->window_width = newframe.size.width;
window_height = newframe.size.height; this->window_height = newframe.size.height;
free(pixel_buffer); free(this->pixel_buffer);
pixel_buffer = malloc(window_width * window_height * buffer_depth / 8); this->pixel_buffer = malloc(this->window_width * this->window_height * this->buffer_depth / 8);
if (pixel_buffer == NULL) { if (this->pixel_buffer == NULL) {
DEBUG(driver, 0, "Failed to allocate pixel buffer"); DEBUG(driver, 0, "Failed to allocate pixel buffer");
return false; return false;
} }
@ -777,7 +714,7 @@ bool WindowQuickdrawSubdriver::WindowResized()
QZ_GameSizeChanged(); QZ_GameSizeChanged();
/* Redraw screen */ /* Redraw screen */
num_dirty_rects = MAX_DIRTY_RECTS; this->num_dirty_rects = MAX_DIRTY_RECTS;
return true; return true;
} }

Loading…
Cancel
Save