src/video/cocoa_v.mm
changeset 7170 923946ec324f
parent 7136 eb853b098323
child 7176 8fd82c59560b
equal deleted inserted replaced
7169:314b046af3a1 7170:923946ec324f
    63 # endif
    63 # endif
    64 #endif
    64 #endif
    65 
    65 
    66 
    66 
    67 #include "../stdafx.h"
    67 #include "../stdafx.h"
       
    68 #include "../openttd.h"
    68 #include "../debug.h"
    69 #include "../debug.h"
    69 #include "../macros.h"
    70 #include "../macros.h"
    70 #include "../os/macosx/splash.h"
    71 #include "../os/macosx/splash.h"
    71 #include "../variables.h"
    72 #include "../variables.h"
       
    73 #include "../gfx.h"
    72 #include "cocoa_v.h"
    74 #include "cocoa_v.h"
    73 #include "cocoa_keys.h"
    75 #include "cocoa_keys.h"
    74 #include "../blitter/factory.hpp"
    76 #include "../blitter/factory.hpp"
    75 #include "../fileio.h"
    77 #include "../fileio.h"
    76 
    78 
   129 static void QZ_UnsetVideoMode();
   131 static void QZ_UnsetVideoMode();
   130 static void QZ_UpdatePalette(uint start, uint count);
   132 static void QZ_UpdatePalette(uint start, uint count);
   131 static void QZ_WarpCursor(int x, int y);
   133 static void QZ_WarpCursor(int x, int y);
   132 static void QZ_ShowMouse();
   134 static void QZ_ShowMouse();
   133 static void QZ_HideMouse();
   135 static void QZ_HideMouse();
   134 static void CocoaVideoFullScreen(bool full_screen);
       
   135 
   136 
   136 
   137 
   137 static NSAutoreleasePool *_ottd_autorelease_pool;
   138 static NSAutoreleasePool *_ottd_autorelease_pool;
   138 static OTTDMain *_ottd_main;
   139 static OTTDMain *_ottd_main;
   139 
   140 
   140 
   141 
   141 static struct CocoaVideoData {
   142 static struct VideoDriver_Cocoa::Data {
   142 	bool isset;
   143 	bool isset;
   143 	bool issetting;
   144 	bool issetting;
   144 
   145 
   145 	CGDirectDisplayID  display_id;         /* 0 == main display (only support single display) */
   146 	CGDirectDisplayID  display_id;         /* 0 == main display (only support single display) */
   146 	CFDictionaryRef    mode;               /* current mode of the display */
   147 	CFDictionaryRef    mode;               /* current mode of the display */
   364 		case QZ_TAB: _cocoa_video_data.tab_is_down = down; break;
   365 		case QZ_TAB: _cocoa_video_data.tab_is_down = down; break;
   365 
   366 
   366 		case QZ_RETURN:
   367 		case QZ_RETURN:
   367 		case QZ_f:
   368 		case QZ_f:
   368 			if (down && (_cocoa_video_data.current_mods & NSCommandKeyMask)) {
   369 			if (down && (_cocoa_video_data.current_mods & NSCommandKeyMask)) {
   369 				CocoaVideoFullScreen(!_fullscreen);
   370 				_video_driver->ToggleFullscreen(!_fullscreen);
   370 			}
   371 			}
   371 			break;
   372 			break;
   372 	}
   373 	}
   373 
   374 
   374 	if (down) {
   375 	if (down) {
  1317 	}
  1318 	}
  1318 
  1319 
  1319 	return 0;
  1320 	return 0;
  1320 }
  1321 }
  1321 
  1322 
  1322 static const char* QZ_SetVideoFullScreen(int width, int height)
  1323 static const char* QZ_SetVideoToggleFullscreen(int width, int height)
  1323 {
  1324 {
  1324 	const char* errstr = "QZ_SetVideoFullScreen error";
  1325 	const char* errstr = "QZ_SetVideoToggleFullscreen error";
  1325 	int exact_match;
  1326 	int exact_match;
  1326 	CFNumberRef number;
  1327 	CFNumberRef number;
  1327 	int bpp;
  1328 	int bpp;
  1328 	int gamma_error;
  1329 	int gamma_error;
  1329 	OTTD_QuartzGammaTable gamma_table;
  1330 	OTTD_QuartzGammaTable gamma_table;
  1705 	const char *ret;
  1706 	const char *ret;
  1706 
  1707 
  1707 	_cocoa_video_data.issetting = true;
  1708 	_cocoa_video_data.issetting = true;
  1708 	if (fullscreen) {
  1709 	if (fullscreen) {
  1709 		/* Setup full screen video */
  1710 		/* Setup full screen video */
  1710 		ret = QZ_SetVideoFullScreen(width, height);
  1711 		ret = QZ_SetVideoToggleFullscreen(width, height);
  1711 	} else {
  1712 	} else {
  1712 		/* Setup windowed video */
  1713 		/* Setup windowed video */
  1713 		ret = QZ_SetVideoWindowed(width, height);
  1714 		ret = QZ_SetVideoWindowed(width, height);
  1714 	}
  1715 	}
  1715 	_cocoa_video_data.issetting = false;
  1716 	_cocoa_video_data.issetting = false;
  1968 
  1969 
  1969 /******************************************************************************
  1970 /******************************************************************************
  1970  *                             Video driver interface                         *
  1971  *                             Video driver interface                         *
  1971  ******************************************************************************/
  1972  ******************************************************************************/
  1972 
  1973 
  1973 static void CocoaVideoStop()
  1974 static FVideoDriver_Cocoa iFVideoDriver_Cocoa;
       
  1975 
       
  1976 void VideoDriver_Cocoa::Stop()
  1974 {
  1977 {
  1975 	if (!_cocoa_video_started) return;
  1978 	if (!_cocoa_video_started) return;
  1976 
  1979 
  1977 	if (_cocoa_video_data.isset) QZ_UnsetVideoMode();
  1980 	if (_cocoa_video_data.isset) QZ_UnsetVideoMode();
  1978 
  1981 
  1979 	[_ottd_main release];
  1982 	[_ottd_main release];
  1980 
  1983 
  1981 	_cocoa_video_started = false;
  1984 	_cocoa_video_started = false;
  1982 }
  1985 }
  1983 
  1986 
  1984 static const char *CocoaVideoStart(const char * const *parm)
  1987 const char *VideoDriver_Cocoa::Start(const char * const *parm)
  1985 {
  1988 {
  1986 	const char *ret;
  1989 	const char *ret;
  1987 
  1990 
  1988 	if (_cocoa_video_started) return "Already started";
  1991 	if (_cocoa_video_started) return "Already started";
  1989 	_cocoa_video_started = true;
  1992 	_cocoa_video_started = true;
  1996 	if (_cocoa_video_dialog) return NULL;
  1999 	if (_cocoa_video_dialog) return NULL;
  1997 
  2000 
  1998 	QZ_VideoInit();
  2001 	QZ_VideoInit();
  1999 
  2002 
  2000 	ret = QZ_SetVideoMode(_cur_resolution[0], _cur_resolution[1], _fullscreen);
  2003 	ret = QZ_SetVideoMode(_cur_resolution[0], _cur_resolution[1], _fullscreen);
  2001 	if (ret != NULL) CocoaVideoStop();
  2004 	if (ret != NULL) VideoDriver_Cocoa::Stop();
  2002 
  2005 
  2003 	return ret;
  2006 	return ret;
  2004 }
  2007 }
  2005 
  2008 
  2006 static void CocoaVideoMakeDirty(int left, int top, int width, int height)
  2009 void VideoDriver_Cocoa::MakeDirty(int left, int top, int width, int height)
  2007 {
  2010 {
  2008 	if (_cocoa_video_data.num_dirty_rects < MAX_DIRTY_RECTS) {
  2011 	if (_cocoa_video_data.num_dirty_rects < MAX_DIRTY_RECTS) {
  2009 		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].left = left;
  2012 		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].left = left;
  2010 		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].top = top;
  2013 		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].top = top;
  2011 		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].right = left + width;
  2014 		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].right = left + width;
  2012 		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].bottom = top + height;
  2015 		_cocoa_video_data.dirty_rects[_cocoa_video_data.num_dirty_rects].bottom = top + height;
  2013 	}
  2016 	}
  2014 	_cocoa_video_data.num_dirty_rects++;
  2017 	_cocoa_video_data.num_dirty_rects++;
  2015 }
  2018 }
  2016 
  2019 
  2017 static void CocoaVideoMainLoop()
  2020 void VideoDriver_Cocoa::MainLoop()
  2018 {
  2021 {
  2019 	/* Start the main event loop */
  2022 	/* Start the main event loop */
  2020 	[NSApp run];
  2023 	[NSApp run];
  2021 }
  2024 }
  2022 
  2025 
  2023 static bool CocoaVideoChangeRes(int w, int h)
  2026 bool VideoDriver_Cocoa::ChangeResolution(int w, int h)
  2024 {
  2027 {
  2025 	const char *ret = QZ_SetVideoModeAndRestoreOnFailure((uint)w, (uint)h, _cocoa_video_data.fullscreen);
  2028 	const char *ret = QZ_SetVideoModeAndRestoreOnFailure((uint)w, (uint)h, _cocoa_video_data.fullscreen);
  2026 	if (ret != NULL) {
  2029 	if (ret != NULL) {
  2027 		DEBUG(driver, 0, "cocoa_v: CocoaVideoChangeRes failed with message: %s", ret);
  2030 		DEBUG(driver, 0, "cocoa_v: VideoDriver_Cocoa::ChangeResolution failed with message: %s", ret);
  2028 	}
  2031 	}
  2029 
  2032 
  2030 	return ret == NULL;
  2033 	return ret == NULL;
  2031 }
  2034 }
  2032 
  2035 
  2033 static void CocoaVideoFullScreen(bool full_screen)
  2036 void VideoDriver_Cocoa::ToggleFullscreen(bool full_screen)
  2034 {
  2037 {
  2035 	const char *ret = QZ_SetVideoModeAndRestoreOnFailure(_cocoa_video_data.width, _cocoa_video_data.height, full_screen);
  2038 	const char *ret = QZ_SetVideoModeAndRestoreOnFailure(_cocoa_video_data.width, _cocoa_video_data.height, full_screen);
  2036 	if (ret != NULL) {
  2039 	if (ret != NULL) {
  2037 		DEBUG(driver, 0, "cocoa_v: CocoaVideoFullScreen failed with message: %s", ret);
  2040 		DEBUG(driver, 0, "cocoa_v: VideoDriver_Cocoa::ToggleFullscreen failed with message: %s", ret);
  2038 	}
  2041 	}
  2039 
  2042 
  2040 	_fullscreen = _cocoa_video_data.fullscreen;
  2043 	_fullscreen = _cocoa_video_data.fullscreen;
  2041 }
  2044 }
  2042 
       
  2043 const HalVideoDriver _cocoa_video_driver = {
       
  2044 	CocoaVideoStart,
       
  2045 	CocoaVideoStop,
       
  2046 	CocoaVideoMakeDirty,
       
  2047 	CocoaVideoMainLoop,
       
  2048 	CocoaVideoChangeRes,
       
  2049 	CocoaVideoFullScreen,
       
  2050 };
       
  2051 
  2045 
  2052 
  2046 
  2053 /* This is needed since sometimes assert is called before the videodriver is initialized */
  2047 /* This is needed since sometimes assert is called before the videodriver is initialized */
  2054 void CocoaDialog(const char* title, const char* message, const char* buttonLabel)
  2048 void CocoaDialog(const char* title, const char* message, const char* buttonLabel)
  2055 {
  2049 {
  2056 	bool wasstarted;
  2050 	bool wasstarted;
  2057 
  2051 
  2058 	_cocoa_video_dialog = true;
  2052 	_cocoa_video_dialog = true;
  2059 
  2053 
  2060 	wasstarted = _cocoa_video_started;
  2054 	wasstarted = _cocoa_video_started;
  2061 	if (!_cocoa_video_started && CocoaVideoStart(NULL) != NULL) {
  2055 	if (!_cocoa_video_started && VideoDriver_Cocoa::Start(NULL) != NULL) {
  2062 		fprintf(stderr, "%s: %s\n", title, message);
  2056 		fprintf(stderr, "%s: %s\n", title, message);
  2063 		return;
  2057 		return;
  2064 	}
  2058 	}
  2065 
  2059 
  2066 	NSRunAlertPanel([NSString stringWithCString: title], [NSString stringWithCString: message], [NSString stringWithCString: buttonLabel], nil, nil);
  2060 	NSRunAlertPanel([NSString stringWithCString: title], [NSString stringWithCString: message], [NSString stringWithCString: buttonLabel], nil, nil);
  2067 
  2061 
  2068 	if (!wasstarted) CocoaVideoStop();
  2062 	if (!wasstarted) VideoDriver_Cocoa::Stop();
  2069 
  2063 
  2070 	_cocoa_video_dialog = false;
  2064 	_cocoa_video_dialog = false;
  2071 }
  2065 }
  2072 
  2066 
  2073 /* This is needed since OS X application bundles do not have a
  2067 /* This is needed since OS X application bundles do not have a