platformOSX.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #import "platformOSX/platformOSX.h"
  23. #include "platform/platformVideo.h"
  24. #include "game/gameInterface.h"
  25. #pragma mark ---- OSXPlatState Implementation ----
  26. @interface osxPlatState (PrivateMethods)
  27. - (void)mainTorqueLoop:(NSTimer *)obj;
  28. @end
  29. @implementation osxPlatState
  30. @synthesize window = _window;
  31. @synthesize torqueView = _torqueView;
  32. @synthesize cgDisplay = _cgDisplay;
  33. @synthesize applicationID = _applicationID;
  34. @synthesize alertSemaphore = _alertSemaphore;
  35. @synthesize fullScreen = _fullscreen;
  36. @synthesize argc = _argc;
  37. @synthesize argv = _argv;
  38. @synthesize platformRandom = _platformRandom;
  39. @synthesize desktopBitsPixel = _desktopBitsPixel;
  40. @synthesize desktopWidth = _desktopWidth;
  41. @synthesize desktopHeight = _desktopHeight;
  42. @synthesize currentSimTime = _currentSimTime;
  43. @synthesize lastTimeTick = _lastTimeTick;
  44. @synthesize sleepTicks = _sleepTicks;
  45. @synthesize mainCSDirectory = _mainCSDirectory;
  46. @synthesize backgrounded = _backgrounded;
  47. @synthesize minimized = _minimized;
  48. @synthesize mouseLocked = _mouseLocked;
  49. @synthesize windowTitle = _windowTitle;
  50. @synthesize quit = _quit;
  51. @synthesize osxTimer = _osxTimer;
  52. static osxPlatState * tempSharedPlatState = nil;
  53. //-----------------------------------------------------------------------------
  54. - (id)init
  55. {
  56. self = [super init];
  57. if (self)
  58. {
  59. // Default window behaviors
  60. _backgrounded = false;
  61. _minimized = false;
  62. _mouseLocked = true;
  63. _fullscreen = false;
  64. _quit = false;
  65. // Default window resolution
  66. _desktopBitsPixel = 32;
  67. _desktopWidth = 1024;
  68. _desktopHeight = 768;
  69. _windowSize.x = 1024;
  70. _windowSize.y = 768;
  71. _windowTitle = [[NSString alloc] initWithString:@"Torque 2D OS X"];
  72. // Default window
  73. _window = nil;
  74. // Default system variables
  75. _currentSimTime = 0;
  76. _sleepTicks = 0;
  77. _lastTimeTick = 0;
  78. _argc = 0;
  79. _alertSemaphore = Semaphore::createSemaphore(0);
  80. _platformRandom = new RandomLCG();
  81. }
  82. return self;
  83. }
  84. //-----------------------------------------------------------------------------
  85. - (void)dealloc
  86. {
  87. if (_window)
  88. [_window release];
  89. if (_windowTitle)
  90. [_windowTitle release];
  91. if (_mainCSDirectory)
  92. [_mainCSDirectory release];
  93. if (_platformRandom)
  94. delete _platformRandom;
  95. [super dealloc];
  96. }
  97. -(void) updateWindowTitle:(const char*)title
  98. {
  99. _windowTitle = [NSString stringWithFormat:@"%s", title];
  100. [_window setTitle:_windowTitle];
  101. }
  102. //-----------------------------------------------------------------------------
  103. - (void)setWindowSize:(int)width height:(int)height
  104. {
  105. // Store the width and height in the state
  106. _windowSize.x = width;
  107. _windowSize.y = height;
  108. // Get the window's current frame
  109. NSRect frame = NSMakeRect([_window frame].origin.x, [_window frame].origin.y, width, height);
  110. // Get the starting position of the bar height
  111. F32 barOffset = frame.size.height;
  112. // If we are not going to full screen mode, get a new frame offset that accounts
  113. // for the title bar height
  114. if (!_fullscreen)
  115. {
  116. frame = [NSWindow frameRectForContentRect:frame styleMask:NSTitledWindowMask];
  117. // Set the new window frame
  118. [_window setFrame:frame display:YES];
  119. // Get the new position of the title bar
  120. barOffset -= frame.size.height;
  121. #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1070
  122. // Update the frame of the torqueView to match the window
  123. frame = NSMakeRect([_window frame].origin.x, [_window frame].origin.y, width, height);
  124. NSRect viewFrame = NSMakeRect(0, 0, frame.size.width, frame.size.height);
  125. [_torqueView setFrame:viewFrame];
  126. [_torqueView updateContext];
  127. #endif
  128. }
  129. else
  130. {
  131. #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1070
  132. NSRect mainDisplayRect = [[NSScreen mainScreen] frame];
  133. // Update the frame of the torqueView to match the window
  134. NSRect viewFrame = NSMakeRect(0, 0, mainDisplayRect.size.width, mainDisplayRect.size.height);
  135. [_torqueView setFrame:viewFrame];
  136. [_torqueView updateContext];
  137. // Otherwise, just go straight full screen
  138. [_torqueView enterFullScreenMode:[NSScreen mainScreen] withOptions:nil];
  139. #else
  140. // Otherwise, just go straight full screen
  141. [_window toggleFullScreen:self];
  142. #endif
  143. }
  144. // Update the frame of the torqueView to match the window
  145. frame = NSMakeRect([_window frame].origin.x, [_window frame].origin.y, width, height);
  146. NSRect viewFrame = NSMakeRect(0, 0, frame.size.width, frame.size.height);
  147. [_torqueView setFrame:viewFrame];
  148. [_torqueView updateContext];
  149. [_window makeKeyAndOrderFront:NSApp];
  150. [_window makeFirstResponder:_torqueView];
  151. }
  152. //-----------------------------------------------------------------------------
  153. -(Point2I&) getWindowSize
  154. {
  155. return _windowSize;
  156. }
  157. //-----------------------------------------------------------------------------
  158. - (U32)windowWidth
  159. {
  160. return (U32)_windowSize.x;
  161. }
  162. //-----------------------------------------------------------------------------
  163. - (U32)windowHeight
  164. {
  165. return (U32)_windowSize.y;
  166. }
  167. #pragma mark ---- Singleton Functions ----
  168. //-----------------------------------------------------------------------------
  169. + (id)sharedPlatState
  170. {
  171. @synchronized(self)
  172. {
  173. if (tempSharedPlatState == nil)
  174. tempSharedPlatState = (osxPlatState *) [[super allocWithZone:NULL] init];
  175. }
  176. return tempSharedPlatState;
  177. }
  178. //-----------------------------------------------------------------------------
  179. + (id)allocWithZone:(NSZone *)zone
  180. {
  181. return [[self sharedPlatState] retain];
  182. }
  183. //-----------------------------------------------------------------------------
  184. - (id)copyWithZone:(NSZone *)zone
  185. {
  186. return self;
  187. }
  188. //-----------------------------------------------------------------------------
  189. - (id)retain
  190. {
  191. return self;
  192. }
  193. //-----------------------------------------------------------------------------
  194. - (NSUInteger)retainCount
  195. {
  196. // Denotes an object that cannot be released
  197. return UINT_MAX;
  198. }
  199. //-----------------------------------------------------------------------------
  200. - (oneway void)release
  201. {
  202. // never release
  203. }
  204. //-----------------------------------------------------------------------------
  205. - (id)autorelease
  206. {
  207. return self;
  208. }
  209. #pragma mark ---- Torque 2D Functions ----
  210. //-----------------------------------------------------------------------------
  211. // Initializes the Game and any additional inits you need to perform
  212. - (BOOL) initializeTorque2D
  213. {
  214. return Game->mainInitialize(_argc, _argv);
  215. }
  216. //-----------------------------------------------------------------------------
  217. // Runs the main Game instance and any other looping calls you need to perform
  218. - (void) mainTorqueLoop:(NSTimer *)obj
  219. {
  220. if(Game->isRunning())
  221. {
  222. Game->mainLoop();
  223. }
  224. else
  225. {
  226. [NSApp terminate:self];
  227. }
  228. }
  229. //-----------------------------------------------------------------------------
  230. // Entry function for running Torque 2D
  231. - (void) runTorque2D
  232. {
  233. BOOL initialResult = [self initializeTorque2D];
  234. if (initialResult)
  235. {
  236. // Setting the interval to 1ms to handle Box2D and rendering
  237. _osxTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(mainTorqueLoop:) userInfo:self repeats:YES];
  238. }
  239. else
  240. {
  241. [NSApp terminate:self];
  242. }
  243. }
  244. //-----------------------------------------------------------------------------
  245. // Shut down the main Game instancea nd perform any additional cleanup
  246. - (void) shutDownTorque2D
  247. {
  248. // Shutdown the game
  249. Game->mainShutdown();
  250. // Perform any platform cleanup
  251. }
  252. @end
  253. #pragma mark ---- Platform Namespace Functions ----
  254. //-----------------------------------------------------------------------------
  255. // Used for initializing the OS X platform code
  256. void Platform::init()
  257. {
  258. // Set the global script variable $Platform to "macos"
  259. Con::setVariable("$Platform", "macos");
  260. // Initialize standard libraries (namespaces)
  261. Video::init();
  262. Input::init();
  263. // Initialize OS X specific libraries and services
  264. Con::printSeparator();
  265. }
  266. //-----------------------------------------------------------------------------
  267. // OS X processing
  268. void Platform::process()
  269. {
  270. }
  271. //-----------------------------------------------------------------------------
  272. // Shuts down the OS X platform layer code
  273. void Platform::shutdown()
  274. {
  275. Input::destroy();
  276. Video::destroy();
  277. }
  278. //-----------------------------------------------------------------------------
  279. // Completely closes and restarts the simulation
  280. void Platform::restartInstance()
  281. {
  282. // Returns the NSBundle that corresponds to the directory where the current app executable is located.
  283. NSBundle* mainAppBundle = [NSBundle mainBundle];
  284. // Returns the file URL of the receiver's executable file.
  285. // Not currently used, but left here for reference
  286. //NSURL* execURL = [mainAppBundle executableURL];
  287. // Returns the full pathname of the receiver's executable file.
  288. NSString* execString = [mainAppBundle executablePath];
  289. // Create a mutable string we can build into an executable command
  290. NSMutableString* mut = [[[NSMutableString alloc] init] autorelease];
  291. // Base string is the executable path
  292. [mut appendString:execString];
  293. // append ampersand so that we can launch without blocking.
  294. // encase in quotes so that spaces in the path are accepted.
  295. [mut insertString:@"\"" atIndex:0];
  296. [mut appendString:@"\" & "];
  297. [mut appendString:@"\\0"];
  298. // Convert to a C string
  299. const char* execCString = [mut UTF8String];
  300. // Echo the command before we run it
  301. Con::printf("---- %s -----", execCString);
  302. // Run the restart command and hope for the best
  303. system(execCString);
  304. }
  305. //-----------------------------------------------------------------------------
  306. // Starts the quit process for the main Game instance
  307. void Platform::postQuitMessage(const U32 in_quitVal)
  308. {
  309. Event quitEvent;
  310. quitEvent.type = QuitEventType;
  311. Game->postEvent(quitEvent);
  312. }
  313. //-----------------------------------------------------------------------------
  314. // Kicks off the termination process for the NSApp
  315. void Platform::forceShutdown(S32 returnValue)
  316. {
  317. // Note: AppCode states terminate is deprecated, which is not true
  318. [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];
  319. }
  320. //-----------------------------------------------------------------------------
  321. void Platform::debugBreak()
  322. {
  323. raise(SIGTRAP);
  324. }
  325. //-----------------------------------------------------------------------------
  326. void Platform::outputDebugString(const char *string)
  327. {
  328. fprintf(stderr, string, NULL );
  329. fprintf(stderr, "\n" );
  330. fflush(stderr);
  331. }