(svn r17715) -Cleanup: [OSX] And a final round of coding style and some cleaning.

pull/155/head
michi_cc 15 years ago
parent 997f1c191a
commit 375fd4b2cf

@ -80,9 +80,8 @@ void QZ_ShowMouse()
[ NSCursor unhide ]; [ NSCursor unhide ];
_show_mouse = true; _show_mouse = true;
// Hide the openttd cursor when leaving the window /* Hide the openttd cursor when leaving the window */
if (_cocoa_subdriver != NULL) if (_cocoa_subdriver != NULL) UndrawMouseCursor();
UndrawMouseCursor();
_cursor.in_window = false; _cursor.in_window = false;
} }
} }
@ -90,39 +89,32 @@ void QZ_ShowMouse()
void QZ_HideMouse() void QZ_HideMouse()
{ {
if (_show_mouse) { if (_show_mouse) {
/* /* Don't hide the cursor when compiling in debug mode.
* Don't hide the cursor when compiling in debug mode. * Note: Not hiding the cursor will cause artefacts around it in 8bpp fullscreen mode. */
* Note: Not hiding the cursor will cause artefacts around it in 8bpp fullscreen mode.
*/
#ifndef _DEBUG #ifndef _DEBUG
[ NSCursor hide ]; [ NSCursor hide ];
#endif #endif
_show_mouse = false; _show_mouse = false;
// Show the openttd cursor again /* Show the openttd cursor again */
_cursor.in_window = true; _cursor.in_window = true;
} }
} }
static void QZ_WarpCursor(int x, int y) static void QZ_WarpCursor(int x, int y)
{ {
NSPoint p; assert(_cocoa_subdriver != NULL);
CGPoint cgp;
assert(_cocoa_subdriver);
/* Only allow warping when in foreground */ /* Only allow warping when in foreground */
if (![ NSApp isActive ]) return; if (![ NSApp isActive ]) return;
p = NSMakePoint(x, y); NSPoint p = NSMakePoint(x, y);
cgp = _cocoa_subdriver->PrivateLocalToCG(&p); CGPoint cgp = _cocoa_subdriver->PrivateLocalToCG(&p);
/* this is the magic call that fixes cursor "freezing" after warp */ /* this is the magic call that fixes cursor "freezing" after warp */
CGSetLocalEventsSuppressionInterval(0.0); CGSetLocalEventsSuppressionInterval(0.0);
/* Do the actual warp */ /* Do the actual warp */
CGWarpMouseCursorPosition(cgp); CGWarpMouseCursorPosition(cgp);
/* Generate the mouse moved event */
} }
@ -274,10 +266,9 @@ static const VkMapping _vk_mapping[] = {
static uint32 QZ_MapKey(unsigned short sym) static uint32 QZ_MapKey(unsigned short sym)
{ {
const VkMapping *map;
uint32 key = 0; uint32 key = 0;
for (map = _vk_mapping; map != endof(_vk_mapping); ++map) { for (const VkMapping *map = _vk_mapping; map != endof(_vk_mapping); ++map) {
if (sym == map->vk_from) { if (sym == map->vk_from) {
key = map->map_to; key = map->map_to;
break; break;
@ -323,23 +314,20 @@ static void QZ_DoUnsidedModifiers(unsigned int newMods)
{ {
const int mapping[] = { QZ_CAPSLOCK, QZ_LSHIFT, QZ_LCTRL, QZ_LALT, QZ_LMETA }; const int mapping[] = { QZ_CAPSLOCK, QZ_LSHIFT, QZ_LCTRL, QZ_LALT, QZ_LMETA };
int i;
unsigned int bit;
if (_current_mods == newMods) return; if (_current_mods == newMods) return;
/* Iterate through the bits, testing each against the current modifiers */ /* Iterate through the bits, testing each against the current modifiers */
for (i = 0, bit = NSAlphaShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) { for (unsigned int i = 0, bit = NSAlphaShiftKeyMask; bit <= NSCommandKeyMask; bit <<= 1, ++i) {
unsigned int currentMask, newMask; unsigned int currentMask, newMask;
currentMask = _current_mods & bit; currentMask = _current_mods & bit;
newMask = newMods & bit; newMask = newMods & bit;
if (currentMask && currentMask != newMask) { /* modifier up event */ if (currentMask && currentMask != newMask) { // modifier up event
/* If this was Caps Lock, we need some additional voodoo to make SDL happy (is this needed in ottd?) */ /* If this was Caps Lock, we need some additional voodoo to make SDL happy (is this needed in ottd?) */
if (bit == NSAlphaShiftKeyMask) QZ_KeyEvent(mapping[i], 0, YES); if (bit == NSAlphaShiftKeyMask) QZ_KeyEvent(mapping[i], 0, YES);
QZ_KeyEvent(mapping[i], 0, NO); QZ_KeyEvent(mapping[i], 0, NO);
} else if (newMask && currentMask != newMask) { /* modifier down event */ } else if (newMask && currentMask != newMask) { // modifier down event
QZ_KeyEvent(mapping[i], 0, YES); QZ_KeyEvent(mapping[i], 0, YES);
/* If this was Caps Lock, we need some additional voodoo to make SDL happy (is this needed in ottd?) */ /* If this was Caps Lock, we need some additional voodoo to make SDL happy (is this needed in ottd?) */
if (bit == NSAlphaShiftKeyMask) QZ_KeyEvent(mapping[i], 0, NO); if (bit == NSAlphaShiftKeyMask) QZ_KeyEvent(mapping[i], 0, NO);
@ -402,24 +390,16 @@ static void QZ_MouseButtonEvent(int button, BOOL down)
static bool QZ_PollEvent() static bool QZ_PollEvent()
{ {
NSEvent *event;
NSPoint pt;
NSString *chars;
#ifdef _DEBUG
uint32 et0, et;
#endif
assert(_cocoa_subdriver != NULL); assert(_cocoa_subdriver != NULL);
#ifdef _DEBUG #ifdef _DEBUG
et0 = GetTick(); uint32 et0 = GetTick();
#endif #endif
event = [ NSApp nextEventMatchingMask:NSAnyEventMask NSEvent *event = [ NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[ NSDate distantPast ] untilDate:[ NSDate distantPast ]
inMode:NSDefaultRunLoopMode dequeue:YES ]; inMode:NSDefaultRunLoopMode dequeue:YES ];
#ifdef _DEBUG #ifdef _DEBUG
et = GetTick(); _tEvent += GetTick() - et0;
_tEvent+= et - et0;
#endif #endif
if (event == nil) return false; if (event == nil) return false;
@ -431,13 +411,14 @@ static bool QZ_PollEvent()
QZ_DoUnsidedModifiers( [ event modifierFlags ] ); QZ_DoUnsidedModifiers( [ event modifierFlags ] );
NSString *chars;
NSPoint pt;
switch ([ event type ]) { switch ([ event type ]) {
case NSMouseMoved: case NSMouseMoved:
case NSOtherMouseDragged: case NSOtherMouseDragged:
case NSLeftMouseDragged: case NSLeftMouseDragged:
pt = _cocoa_subdriver->GetMouseLocation(event); pt = _cocoa_subdriver->GetMouseLocation(event);
if (!_cocoa_subdriver->MouseIsInsideView(&pt) && if (!_cocoa_subdriver->MouseIsInsideView(&pt) && !_emulating_right_button) {
!_emulating_right_button) {
QZ_ShowMouse(); QZ_ShowMouse();
[ NSApp sendEvent:event ]; [ NSApp sendEvent:event ];
break; break;
@ -461,8 +442,7 @@ static bool QZ_PollEvent()
pt = _cocoa_subdriver->GetMouseLocation(event); pt = _cocoa_subdriver->GetMouseLocation(event);
if (!([ event modifierFlags ] & keymask) || if (!([ event modifierFlags ] & keymask) || !_cocoa_subdriver->MouseIsInsideView(&pt)) {
!_cocoa_subdriver->MouseIsInsideView(&pt)) {
[ NSApp sendEvent:event ]; [ NSApp sendEvent:event ];
} }
@ -617,14 +597,10 @@ void QZ_GameLoop()
uint32 last_cur_ticks = cur_ticks; uint32 last_cur_ticks = cur_ticks;
uint32 next_tick = cur_ticks + 30; uint32 next_tick = cur_ticks + 30;
uint32 pal_tick = 0; uint32 pal_tick = 0;
#ifdef _DEBUG
uint32 et0, et, st0, st;
#endif
int i;
#ifdef _DEBUG #ifdef _DEBUG
et0 = GetTick(); uint32 et0 = GetTick();
st = 0; uint32 st = 0;
#endif #endif
_screen.dst_ptr = _cocoa_subdriver->GetPixelBuffer(); _screen.dst_ptr = _cocoa_subdriver->GetPixelBuffer();
@ -633,7 +609,7 @@ void QZ_GameLoop()
_cocoa_subdriver->Draw(); _cocoa_subdriver->Draw();
CSleep(1); CSleep(1);
for (i = 0; i < 2; i++) GameLoop(); for (int i = 0; i < 2; i++) GameLoop();
_screen.dst_ptr = _cocoa_subdriver->GetPixelBuffer(); _screen.dst_ptr = _cocoa_subdriver->GetPixelBuffer();
UpdateWindows(); UpdateWindows();
@ -684,7 +660,7 @@ void QZ_GameLoop()
_cocoa_subdriver->Draw(); _cocoa_subdriver->Draw();
} else { } else {
#ifdef _DEBUG #ifdef _DEBUG
st0 = GetTick(); uint32 st0 = GetTick();
#endif #endif
CSleep(1); CSleep(1);
#ifdef _DEBUG #ifdef _DEBUG
@ -698,7 +674,7 @@ void QZ_GameLoop()
} }
#ifdef _DEBUG #ifdef _DEBUG
et = GetTick(); uint32 et = GetTick();
DEBUG(driver, 1, "cocoa_v: nextEventMatchingMask took %i ms total", _tEvent); DEBUG(driver, 1, "cocoa_v: nextEventMatchingMask took %i ms total", _tEvent);
DEBUG(driver, 1, "cocoa_v: game loop took %i ms total (%i ms without sleep)", et - et0, et - et0 - st); DEBUG(driver, 1, "cocoa_v: game loop took %i ms total (%i ms without sleep)", et - et0, et - et0 - st);

@ -25,6 +25,7 @@
#include "../../debug.h" #include "../../debug.h"
#include "../../core/geometry_type.hpp" #include "../../core/geometry_type.hpp"
#include "../../core/sort_func.hpp"
#include "cocoa_v.h" #include "cocoa_v.h"
#include "../../gfx_func.h" #include "../../gfx_func.h"
@ -73,28 +74,28 @@ struct OTTD_QuartzGammaTable {
@end @end
static int CDECL ModeSorter(const OTTD_Point *p1, const OTTD_Point *p2)
{
if (p1->x < p2->x) return -1;
if (p1->x > p2->x) return +1;
if (p1->y < p2->y) return -1;
if (p1->y > p2->y) return +1;
return 0;
}
uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_id, int display_depth) uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_id, int display_depth)
{ {
CFArrayRef mode_list; CFArrayRef mode_list = CGDisplayAvailableModes(display_id);
CFIndex num_modes; CFIndex num_modes = CFArrayGetCount(mode_list);
CFIndex i;
uint count = 0;
mode_list = CGDisplayAvailableModes(display_id);
num_modes = CFArrayGetCount(mode_list);
/* Build list of modes with the requested bpp */ /* Build list of modes with the requested bpp */
for (i = 0; i < num_modes && count < max_modes; i++) { uint count = 0;
CFDictionaryRef onemode; for (CFIndex i = 0; i < num_modes && count < max_modes; i++) {
CFNumberRef number; int intvalue, bpp;
int bpp;
int intvalue;
bool hasMode;
uint16 width, height; uint16 width, height;
onemode = (const __CFDictionary*)CFArrayGetValueAtIndex(mode_list, i); CFDictionaryRef onemode = (const __CFDictionary*)CFArrayGetValueAtIndex(mode_list, i);
number = (const __CFNumber*)CFDictionaryGetValue(onemode, kCGDisplayBitsPerPixel); CFNumberRef number = (const __CFNumber*)CFDictionaryGetValue(onemode, kCGDisplayBitsPerPixel);
CFNumberGetValue(number, kCFNumberSInt32Type, &bpp); CFNumberGetValue(number, kCFNumberSInt32Type, &bpp);
if (bpp != display_depth) continue; if (bpp != display_depth) continue;
@ -108,16 +109,13 @@ uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_i
height = (uint16)intvalue; height = (uint16)intvalue;
/* Check if mode is already in the list */ /* Check if mode is already in the list */
{ bool hasMode = false;
uint i; for (uint i = 0; i < count; i++) {
hasMode = false;
for (i = 0; i < count; i++) {
if (modes[i].x == width && modes[i].y == height) { if (modes[i].x == width && modes[i].y == height) {
hasMode = true; hasMode = true;
break; break;
} }
} }
}
if (hasMode) continue; if (hasMode) continue;
@ -128,37 +126,19 @@ uint QZ_ListModes(OTTD_Point *modes, uint max_modes, CGDirectDisplayID display_i
} }
/* Sort list smallest to largest */ /* Sort list smallest to largest */
{ QSortT(modes, count, &ModeSorter);
uint i, j;
for (i = 0; i < count; i++) {
for (j = 0; j < count-1; j++) {
if (modes[j].x > modes[j + 1].x || (
modes[j].x == modes[j + 1].x &&
modes[j].y > modes[j + 1].y
)) {
uint tmpw = modes[j].x;
uint tmph = modes[j].y;
modes[j].x = modes[j + 1].x;
modes[j].y = modes[j + 1].y;
modes[j + 1].x = tmpw;
modes[j + 1].y = tmph;
}
}
}
}
return count; return count;
} }
/* Small function to test if the main display can display 8 bpp in fullscreen */ /** Small function to test if the main display can display 8 bpp in fullscreen */
bool QZ_CanDisplay8bpp() bool QZ_CanDisplay8bpp()
{ {
OTTD_Point p; OTTD_Point p;
/* We want to know if 8 bpp is possible in fullscreen and not anything about resolutions. /* We want to know if 8 bpp is possible in fullscreen and not anything about
* Because of this we want to fill a list of 1 resolution of 8 bpp on display 0 (main) and return if we found one. */ * resolutions. Because of this we want to fill a list of 1 resolution of 8 bpp
* on display 0 (main) and return if we found one. */
return QZ_ListModes(&p, 1, 0, 8); return QZ_ListModes(&p, 1, 0, 8);
} }
@ -170,17 +150,17 @@ class FullscreenSubdriver: public CocoaSubdriver {
void *screen_buffer; void *screen_buffer;
void *pixel_buffer; void *pixel_buffer;
CGDirectDisplayID display_id; /* 0 == main display (only support single display) */ CGDirectDisplayID display_id; ///< 0 == main display (only support single display)
CFDictionaryRef cur_mode; /* current mode of the display */ CFDictionaryRef cur_mode; ///< current mode of the display
CFDictionaryRef save_mode; /* original mode of the display */ CFDictionaryRef save_mode; ///< original mode of the display
CGDirectPaletteRef palette; /* palette of an 8-bit display */ CGDirectPaletteRef palette; ///< palette of an 8-bit display
#define MAX_DIRTY_RECTS 100 #define MAX_DIRTY_RECTS 100
Rect dirty_rects[MAX_DIRTY_RECTS]; Rect dirty_rects[MAX_DIRTY_RECTS];
int num_dirty_rects; int num_dirty_rects;
/* Gamma functions to try to hide the flash from a rez switch /* Gamma functions to try to hide the flash from a res switch
* Fade the display from normal to black * Fade the display from normal to black
* Save gamma tables for fade back to normal * Save gamma tables for fade back to normal
*/ */
@ -189,15 +169,10 @@ class FullscreenSubdriver: public CocoaSubdriver {
CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE]; CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE];
CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE]; CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE];
CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE]; CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE];
float percent;
int j;
unsigned int actual;
if (CGGetDisplayTransferByTable( unsigned int actual;
display_id, QZ_GAMMA_TABLE_SIZE, if (CGGetDisplayTransferByTable(this->display_id, QZ_GAMMA_TABLE_SIZE, table->red, table->green, table->blue, &actual) != CGDisplayNoErr
table->red, table->green, table->blue, &actual || actual != QZ_GAMMA_TABLE_SIZE) {
) != CGDisplayNoErr ||
actual != QZ_GAMMA_TABLE_SIZE) {
return 1; return 1;
} }
@ -205,17 +180,14 @@ class FullscreenSubdriver: public CocoaSubdriver {
memcpy(greenTable, table->green, sizeof(greenTable)); memcpy(greenTable, table->green, sizeof(greenTable));
memcpy(blueTable, table->blue, sizeof(greenTable)); memcpy(blueTable, table->blue, sizeof(greenTable));
for (percent = 1.0; percent >= 0.0; percent -= 0.01) { for (float percent = 1.0; percent >= 0.0; percent -= 0.01) {
for (j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) { for (int j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) {
redTable[j] = redTable[j] * percent; redTable[j] = redTable[j] * percent;
greenTable[j] = greenTable[j] * percent; greenTable[j] = greenTable[j] * percent;
blueTable[j] = blueTable[j] * percent; blueTable[j] = blueTable[j] * percent;
} }
if (CGSetDisplayTransferByTable( if (CGSetDisplayTransferByTable(this->display_id, QZ_GAMMA_TABLE_SIZE, redTable, greenTable, blueTable) != CGDisplayNoErr) {
display_id, QZ_GAMMA_TABLE_SIZE,
redTable, greenTable, blueTable
) != CGDisplayNoErr) {
CGDisplayRestoreColorSyncSettings(); CGDisplayRestoreColorSyncSettings();
return 1; return 1;
} }
@ -234,24 +206,19 @@ class FullscreenSubdriver: public CocoaSubdriver {
CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE]; CGGammaValue redTable[QZ_GAMMA_TABLE_SIZE];
CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE]; CGGammaValue greenTable[QZ_GAMMA_TABLE_SIZE];
CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE]; CGGammaValue blueTable[QZ_GAMMA_TABLE_SIZE];
float percent;
int j;
memset(redTable, 0, sizeof(redTable)); memset(redTable, 0, sizeof(redTable));
memset(greenTable, 0, sizeof(greenTable)); memset(greenTable, 0, sizeof(greenTable));
memset(blueTable, 0, sizeof(greenTable)); memset(blueTable, 0, sizeof(greenTable));
for (percent = 0.0; percent <= 1.0; percent += 0.01) { for (float percent = 0.0; percent <= 1.0; percent += 0.01) {
for (j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) { for (int j = 0; j < QZ_GAMMA_TABLE_SIZE; j++) {
redTable[j] = table->red[j] * percent; redTable[j] = table->red[j] * percent;
greenTable[j] = table->green[j] * percent; greenTable[j] = table->green[j] * percent;
blueTable[j] = table->blue[j] * percent; blueTable[j] = table->blue[j] * percent;
} }
if (CGSetDisplayTransferByTable( if (CGSetDisplayTransferByTable(this->display_id, QZ_GAMMA_TABLE_SIZE, redTable, greenTable, blueTable) != CGDisplayNoErr) {
display_id, QZ_GAMMA_TABLE_SIZE,
redTable, greenTable, blueTable
) != CGDisplayNoErr) {
CGDisplayRestoreColorSyncSettings(); CGDisplayRestoreColorSyncSettings();
return 1; return 1;
} }
@ -262,33 +229,27 @@ class FullscreenSubdriver: public CocoaSubdriver {
return 0; return 0;
} }
/* Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */ /** Wait for the VBL to occur (estimated since we don't have a hardware interrupt) */
void WaitForVerticalBlank() void WaitForVerticalBlank()
{ {
/* The VBL delay is based on Ian Ollmann's RezLib <iano@cco.caltech.edu> */ /* 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 = (const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayRefreshRate); CFNumberRef refreshRateCFNumber = (const __CFNumber*)CFDictionaryGetValue(this->cur_mode, kCGDisplayRefreshRate);
if (refreshRateCFNumber == NULL) return; if (refreshRateCFNumber == NULL) return;
if (CFNumberGetValue(refreshRateCFNumber, kCFNumberDoubleType, &refreshRate) == 0) double refreshRate;
return; if (CFNumberGetValue(refreshRateCFNumber, kCFNumberDoubleType, &refreshRate) == 0) return;
if (refreshRate == 0) return; if (refreshRate == 0) return;
linesPerSecond = refreshRate * display_height; double linesPerSecond = refreshRate * this->display_height;
target = display_height; double target = this->display_height;
/* Figure out the first delay so we start off about right */ /* Figure out the first delay so we start off about right */
position = CGDisplayBeamPosition(display_id); double position = CGDisplayBeamPosition(this->display_id);
if (position > target) position = 0; if (position > target) position = 0;
adjustment = (target - position) / linesPerSecond; double adjustment = (target - position) / linesPerSecond;
CSleep((uint32)(adjustment * 1000)); CSleep((uint32)(adjustment * 1000));
} }
@ -296,71 +257,63 @@ class FullscreenSubdriver: public CocoaSubdriver {
bool SetVideoMode(int w, int h) bool SetVideoMode(int w, int h)
{ {
boolean_t exact_match;
CFNumberRef number;
int bpp;
int gamma_error;
OTTD_QuartzGammaTable gamma_table;
NSRect screen_rect;
CGError error;
NSPoint pt;
/* Destroy any previous mode */ /* Destroy any previous mode */
if (pixel_buffer != NULL) { if (this->pixel_buffer != NULL) {
free(pixel_buffer); free(this->pixel_buffer);
pixel_buffer = NULL; this->pixel_buffer = NULL;
} }
/* See if requested mode exists */ /* See if requested mode exists */
cur_mode = CGDisplayBestModeForParameters(display_id, display_depth, w, h, &exact_match); boolean_t exact_match;
this->cur_mode = CGDisplayBestModeForParameters(this->display_id, this->display_depth, w, h, &exact_match);
/* If the mode wasn't an exact match, check if it has the right bpp, and update width and height */ /* If the mode wasn't an exact match, check if it has the right bpp, and update width and height */
if (!exact_match) { if (!exact_match) {
number = (const __CFNumber*) CFDictionaryGetValue(cur_mode, kCGDisplayBitsPerPixel); int bpp;
CFNumberRef number = (const __CFNumber*) CFDictionaryGetValue(this->cur_mode, kCGDisplayBitsPerPixel);
CFNumberGetValue(number, kCFNumberSInt32Type, &bpp); CFNumberGetValue(number, kCFNumberSInt32Type, &bpp);
if (bpp != display_depth) { if (bpp != this->display_depth) {
DEBUG(driver, 0, "Failed to find display resolution"); DEBUG(driver, 0, "Failed to find display resolution");
goto ERR_NO_MATCH; goto ERR_NO_MATCH;
} }
number = (const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayWidth); number = (const __CFNumber*)CFDictionaryGetValue(this->cur_mode, kCGDisplayWidth);
CFNumberGetValue(number, kCFNumberSInt32Type, &w); CFNumberGetValue(number, kCFNumberSInt32Type, &w);
number = (const __CFNumber*)CFDictionaryGetValue(cur_mode, kCGDisplayHeight); number = (const __CFNumber*)CFDictionaryGetValue(this->cur_mode, kCGDisplayHeight);
CFNumberGetValue(number, kCFNumberSInt32Type, &h); CFNumberGetValue(number, kCFNumberSInt32Type, &h);
} }
/* Fade display to zero gamma */ /* Fade display to zero gamma */
gamma_error = FadeGammaOut(&gamma_table); OTTD_QuartzGammaTable gamma_table;
int gamma_error = this->FadeGammaOut(&gamma_table);
/* Put up the blanking window (a window above all other windows) */ /* Put up the blanking window (a window above all other windows) */
error = CGDisplayCapture(display_id); if (CGDisplayCapture(this->display_id) != CGDisplayNoErr ) {
if (CGDisplayNoErr != error) {
DEBUG(driver, 0, "Failed capturing display"); DEBUG(driver, 0, "Failed capturing display");
goto ERR_NO_CAPTURE; goto ERR_NO_CAPTURE;
} }
/* Do the physical switch */ /* Do the physical switch */
if (CGDisplaySwitchToMode(display_id, cur_mode) != CGDisplayNoErr) { if (CGDisplaySwitchToMode(this->display_id, this->cur_mode) != CGDisplayNoErr) {
DEBUG(driver, 0, "Failed switching display resolution"); DEBUG(driver, 0, "Failed switching display resolution");
goto ERR_NO_SWITCH; goto ERR_NO_SWITCH;
} }
screen_buffer = CGDisplayBaseAddress(display_id); this->screen_buffer = CGDisplayBaseAddress(this->display_id);
screen_pitch = CGDisplayBytesPerRow(display_id); this->screen_pitch = CGDisplayBytesPerRow(this->display_id);
display_width = CGDisplayPixelsWide(display_id); this->display_width = CGDisplayPixelsWide(this->display_id);
display_height = CGDisplayPixelsHigh(display_id); this->display_height = CGDisplayPixelsHigh(this->display_id);
/* Setup double-buffer emulation */ /* Setup double-buffer emulation */
pixel_buffer = malloc(display_width * display_height * display_depth / 8); this->pixel_buffer = malloc(this->display_width * this->display_height * this->display_depth / 8);
if (pixel_buffer == NULL) { if (this->pixel_buffer == NULL) {
DEBUG(driver, 0, "Failed to allocate memory for double buffering"); DEBUG(driver, 0, "Failed to allocate memory for double buffering");
goto ERR_DOUBLEBUF; goto ERR_DOUBLEBUF;
} }
if (display_depth == 8 && !CGDisplayCanSetPalette(display_id)) { if (this->display_depth == 8 && !CGDisplayCanSetPalette(this->display_id)) {
DEBUG(driver, 0, "Not an indexed display mode."); DEBUG(driver, 0, "Not an indexed display mode.");
goto ERR_NOT_INDEXED; goto ERR_NOT_INDEXED;
} }
@ -377,31 +330,31 @@ class FullscreenSubdriver: public CocoaSubdriver {
* We can hack around this bug by setting the screen rect ourselves. * We can hack around this bug by setting the screen rect ourselves.
* This hack should be removed if/when the bug is fixed. * This hack should be removed if/when the bug is fixed.
*/ */
screen_rect = NSMakeRect(0, 0, display_width, display_height); NSRect screen_rect = NSMakeRect(0, 0, this->display_width, this->display_height);
[ [ NSScreen mainScreen ] setFrame:screen_rect ]; [ [ NSScreen mainScreen ] setFrame:screen_rect ];
pt = [ NSEvent mouseLocation ]; NSPoint pt = [ NSEvent mouseLocation ];
pt.y = display_height - pt.y; pt.y = this->display_height - pt.y;
if (MouseIsInsideView(&pt)) QZ_HideMouse(); if (this->MouseIsInsideView(&pt)) QZ_HideMouse();
UpdatePalette(0, 256); this->UpdatePalette(0, 256);
return true; return true;
/* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */ /* Since the blanking window covers *all* windows (even force quit) correct recovery is crucial */
ERR_NOT_INDEXED: ERR_NOT_INDEXED:
free(pixel_buffer); free(this->pixel_buffer);
pixel_buffer = NULL; this->pixel_buffer = NULL;
ERR_DOUBLEBUF: ERR_DOUBLEBUF:
CGDisplaySwitchToMode(display_id, save_mode); CGDisplaySwitchToMode(this->display_id, this->save_mode);
ERR_NO_SWITCH: ERR_NO_SWITCH:
CGReleaseAllDisplays(); CGReleaseAllDisplays();
ERR_NO_CAPTURE: ERR_NO_CAPTURE:
if (!gamma_error) FadeGammaIn(&gamma_table); if (!gamma_error) this->FadeGammaIn(&gamma_table);
ERR_NO_MATCH: ERR_NO_MATCH:
display_width = 0; this->display_width = 0;
display_height = 0; this->display_height = 0;
return false; return false;
} }
@ -410,33 +363,30 @@ ERR_NO_MATCH:
{ {
/* Release fullscreen resources */ /* Release fullscreen resources */
OTTD_QuartzGammaTable gamma_table; OTTD_QuartzGammaTable gamma_table;
int gamma_error; int gamma_error = this->FadeGammaOut(&gamma_table);
NSRect screen_rect;
gamma_error = FadeGammaOut(&gamma_table);
/* Restore original screen resolution/bpp */ /* Restore original screen resolution/bpp */
CGDisplaySwitchToMode(display_id, save_mode); CGDisplaySwitchToMode(this->display_id, this->save_mode);
CGReleaseAllDisplays(); CGReleaseAllDisplays();
ShowMenuBar(); ShowMenuBar();
/* Reset the main screen's rectangle /* Reset the main screen's rectangle
* See comment in SetVideoMode for why we do this * See comment in SetVideoMode for why we do this */
*/ NSRect screen_rect = NSMakeRect(0, 0, CGDisplayPixelsWide(this->display_id), CGDisplayPixelsHigh(this->display_id));
screen_rect = NSMakeRect(0, 0, CGDisplayPixelsWide(display_id), CGDisplayPixelsHigh(display_id));
[ [ NSScreen mainScreen ] setFrame:screen_rect ]; [ [ NSScreen mainScreen ] setFrame:screen_rect ];
QZ_ShowMouse(); QZ_ShowMouse();
/* Destroy the pixel buffer */ /* Destroy the pixel buffer */
if (pixel_buffer != NULL) { if (this->pixel_buffer != NULL) {
free(pixel_buffer); free(this->pixel_buffer);
pixel_buffer = NULL; this->pixel_buffer = NULL;
} }
if (!gamma_error) FadeGammaIn(&gamma_table); if (!gamma_error) this->FadeGammaIn(&gamma_table);
display_width = 0; this->display_width = 0;
display_height = 0; this->display_height = 0;
} }
public: public:
@ -447,107 +397,101 @@ public:
} }
/* Initialize the video settings; this data persists between mode switches */ /* Initialize the video settings; this data persists between mode switches */
display_id = kCGDirectMainDisplay; this->display_id = kCGDirectMainDisplay;
save_mode = CGDisplayCurrentMode(display_id); this->save_mode = CGDisplayCurrentMode(this->display_id);
if (bpp == 8) palette = CGPaletteCreateDefaultColorPalette(); if (bpp == 8) this->palette = CGPaletteCreateDefaultColorPalette();
display_width = 0; this->display_width = 0;
display_height = 0; this->display_height = 0;
display_depth = bpp; this->display_depth = bpp;
pixel_buffer = NULL; this->pixel_buffer = NULL;
num_dirty_rects = MAX_DIRTY_RECTS; this->num_dirty_rects = MAX_DIRTY_RECTS;
} }
virtual ~FullscreenSubdriver() virtual ~FullscreenSubdriver()
{ {
RestoreVideoMode(); this->RestoreVideoMode();
} }
virtual void Draw() virtual void Draw()
{ {
const uint8 *src = (uint8*) pixel_buffer; const uint8 *src = (uint8 *)this->pixel_buffer;
uint8 *dst = (uint8*) screen_buffer; uint8 *dst = (uint8 *)this->screen_buffer;
uint pitch = screen_pitch; uint pitch = this->screen_pitch;
uint width = display_width; uint width = this->display_width;
uint num_dirty = num_dirty_rects; uint num_dirty = this->num_dirty_rects;
uint bytesperpixel = display_depth / 8; uint bytesperpixel = this->display_depth / 8;
uint i;
/* Check if we need to do anything */ /* Check if we need to do anything */
if (num_dirty == 0) return; if (num_dirty == 0) return;
if (num_dirty >= MAX_DIRTY_RECTS) { if (num_dirty >= MAX_DIRTY_RECTS) {
num_dirty = 1; num_dirty = 1;
dirty_rects[0].left = 0; this->dirty_rects[0].left = 0;
dirty_rects[0].top = 0; this->dirty_rects[0].top = 0;
dirty_rects[0].right = display_width; this->dirty_rects[0].right = this->display_width;
dirty_rects[0].bottom = display_height; this->dirty_rects[0].bottom = this->display_height;
} }
WaitForVerticalBlank(); WaitForVerticalBlank();
/* Build the region of dirty rectangles */ /* Build the region of dirty rectangles */
for (i = 0; i < num_dirty; i++) { for (uint i = 0; i < num_dirty; i++) {
uint y = dirty_rects[i].top; uint y = this->dirty_rects[i].top;
uint left = dirty_rects[i].left; uint left = this->dirty_rects[i].left;
uint length = dirty_rects[i].right - left; uint length = this->dirty_rects[i].right - left;
uint bottom = dirty_rects[i].bottom; uint bottom = this->dirty_rects[i].bottom;
for (; y < bottom; y++) { for (; y < bottom; y++) {
memcpy(dst + y * pitch + left * bytesperpixel, src + y * width * bytesperpixel + left * bytesperpixel, length * bytesperpixel); memcpy(dst + y * pitch + left * bytesperpixel, src + y * width * bytesperpixel + left * bytesperpixel, length * bytesperpixel);
} }
} }
num_dirty_rects = 0; this->num_dirty_rects = 0;
} }
virtual void MakeDirty(int left, int top, int width, int height) virtual void 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++;
} }
virtual void UpdatePalette(uint first_color, uint num_colors) virtual void UpdatePalette(uint first_color, uint num_colors)
{ {
CGTableCount index; if (this->display_depth != 8) return;
CGDeviceColor color;
if (display_depth != 8) for (CGTableCount index = first_color; index < first_color + num_colors; index++) {
return;
for (index = first_color; index < first_color+num_colors; index++) {
/* Clamp colors between 0.0 and 1.0 */ /* Clamp colors between 0.0 and 1.0 */
CGDeviceColor color;
color.red = _cur_palette[index].r / 255.0; color.red = _cur_palette[index].r / 255.0;
color.blue = _cur_palette[index].b / 255.0; color.blue = _cur_palette[index].b / 255.0;
color.green = _cur_palette[index].g / 255.0; color.green = _cur_palette[index].g / 255.0;
CGPaletteSetColorAtIndex(palette, color, index); CGPaletteSetColorAtIndex(this->palette, color, index);
} }
CGDisplaySetPalette(display_id, palette); CGDisplaySetPalette(this->display_id, this->palette);
} }
virtual uint ListModes(OTTD_Point *modes, uint max_modes) virtual uint ListModes(OTTD_Point *modes, uint max_modes)
{ {
return QZ_ListModes(modes, max_modes, display_id, display_depth); return QZ_ListModes(modes, max_modes, this->display_id, this->display_depth);
} }
virtual bool ChangeResolution(int w, int h) virtual bool ChangeResolution(int w, int h)
{ {
int old_width = display_width; int old_width = this->display_width;
int old_height = display_height; int old_height = this->display_height;
if (SetVideoMode(w, h)) if (SetVideoMode(w, h)) return true;
return true;
if (old_width != 0 && old_height != 0) if (old_width != 0 && old_height != 0) SetVideoMode(old_width, old_height);
SetVideoMode(old_width, old_height);
return false; return false;
} }
@ -559,17 +503,17 @@ public:
virtual int GetWidth() virtual int GetWidth()
{ {
return display_width; return this->display_width;
} }
virtual int GetHeight() virtual int GetHeight()
{ {
return display_height; return this->display_height;
} }
virtual void *GetPixelBuffer() virtual void *GetPixelBuffer()
{ {
return pixel_buffer; return this->pixel_buffer;
} }
/* /*
@ -578,27 +522,20 @@ public:
*/ */
virtual CGPoint PrivateLocalToCG(NSPoint *p) virtual CGPoint PrivateLocalToCG(NSPoint *p)
{ {
CGPoint cgp; return CGPointMake(p->x, p->y);
cgp.x = p->x;
cgp.y = p->y;
return cgp;
} }
virtual NSPoint GetMouseLocation(NSEvent *event) virtual NSPoint GetMouseLocation(NSEvent *event)
{ {
NSPoint pt; NSPoint pt = [ NSEvent mouseLocation ];
pt.y = this->display_height - pt.y;
pt = [ NSEvent mouseLocation ];
pt.y = display_height - pt.y;
return pt; return pt;
} }
virtual bool MouseIsInsideView(NSPoint *pt) virtual bool MouseIsInsideView(NSPoint *pt)
{ {
return pt->x >= 0 && pt->y >= 0 && pt->x < display_width && pt->y < display_height; return pt->x >= 0 && pt->y >= 0 && pt->x < this->display_width && pt->y < this->display_height;
} }
virtual bool IsActive() virtual bool IsActive()
@ -609,9 +546,7 @@ public:
CocoaSubdriver *QZ_CreateFullscreenSubdriver(int width, int height, int bpp) CocoaSubdriver *QZ_CreateFullscreenSubdriver(int width, int height, int bpp)
{ {
FullscreenSubdriver *ret; FullscreenSubdriver *ret = new FullscreenSubdriver(bpp);
ret = new FullscreenSubdriver(bpp);
if (!ret->ChangeResolution(width, height)) { if (!ret->ChangeResolution(width, height)) {
delete ret; delete ret;

Loading…
Cancel
Save