platformOSX.mm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. // If we are not going to full screen mode, get a new frame offset that accounts
  111. // for the title bar height
  112. if (!_fullscreen)
  113. {
  114. frame = [NSWindow frameRectForContentRect:frame styleMask:NSWindowStyleMaskTitled];
  115. // Set the new window frame
  116. [_window setFrame:frame display:YES];
  117. }
  118. else
  119. {
  120. #if __MAC_OS_X_VERSION_MAX_ALLOWED < 1070
  121. NSRect mainDisplayRect = [[NSScreen mainScreen] frame];
  122. // Update the frame of the torqueView to match the window
  123. NSRect viewFrame = NSMakeRect(0, 0, mainDisplayRect.size.width, mainDisplayRect.size.height);
  124. [_torqueView setFrame:viewFrame];
  125. [_torqueView updateContext];
  126. // Otherwise, just go straight full screen
  127. [_torqueView enterFullScreenMode:[NSScreen mainScreen] withOptions:nil];
  128. #else
  129. // Otherwise, just go straight full screen
  130. [_window toggleFullScreen:self];
  131. #endif
  132. }
  133. // Update the frame of the torqueView to match the window
  134. NSRect viewFrame = NSMakeRect(0, 0, width, height);
  135. [_torqueView setFrame:viewFrame];
  136. [_torqueView updateContext];
  137. [_window makeKeyAndOrderFront:NSApp];
  138. [_window makeFirstResponder:_torqueView];
  139. }
  140. //-----------------------------------------------------------------------------
  141. -(Point2I&) getWindowSize
  142. {
  143. return _windowSize;
  144. }
  145. //-----------------------------------------------------------------------------
  146. - (U32)windowWidth
  147. {
  148. return (U32)_windowSize.x;
  149. }
  150. //-----------------------------------------------------------------------------
  151. - (U32)windowHeight
  152. {
  153. return (U32)_windowSize.y;
  154. }
  155. #pragma mark ---- Singleton Functions ----
  156. //-----------------------------------------------------------------------------
  157. + (id)sharedPlatState
  158. {
  159. @synchronized(self)
  160. {
  161. if (tempSharedPlatState == nil)
  162. tempSharedPlatState = (osxPlatState *) [[super allocWithZone:NULL] init];
  163. }
  164. return tempSharedPlatState;
  165. }
  166. //-----------------------------------------------------------------------------
  167. + (id)allocWithZone:(NSZone *)zone
  168. {
  169. return [[self sharedPlatState] retain];
  170. }
  171. //-----------------------------------------------------------------------------
  172. - (id)copyWithZone:(NSZone *)zone
  173. {
  174. return self;
  175. }
  176. //-----------------------------------------------------------------------------
  177. - (id)retain
  178. {
  179. return self;
  180. }
  181. //-----------------------------------------------------------------------------
  182. - (NSUInteger)retainCount
  183. {
  184. // Denotes an object that cannot be released
  185. return UINT_MAX;
  186. }
  187. //-----------------------------------------------------------------------------
  188. - (oneway void)release
  189. {
  190. // never release
  191. }
  192. //-----------------------------------------------------------------------------
  193. - (id)autorelease
  194. {
  195. return self;
  196. }
  197. #pragma mark ---- Torque 2D Functions ----
  198. //-----------------------------------------------------------------------------
  199. // Initializes the Game and any additional inits you need to perform
  200. - (BOOL) initializeTorque2D
  201. {
  202. return Game->mainInitialize(_argc, _argv);
  203. }
  204. //-----------------------------------------------------------------------------
  205. // Runs the main Game instance and any other looping calls you need to perform
  206. - (void) mainTorqueLoop:(NSTimer *)obj
  207. {
  208. if(Game->isRunning())
  209. {
  210. Game->mainLoop();
  211. }
  212. else
  213. {
  214. [NSApp terminate:self];
  215. }
  216. }
  217. //-----------------------------------------------------------------------------
  218. // Entry function for running Torque 2D
  219. - (void) runTorque2D
  220. {
  221. BOOL initialResult = [self initializeTorque2D];
  222. if (initialResult)
  223. {
  224. // Setting the interval to 1ms to handle Box2D and rendering
  225. _osxTimer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(mainTorqueLoop:) userInfo:self repeats:YES];
  226. }
  227. else
  228. {
  229. [NSApp terminate:self];
  230. }
  231. }
  232. //-----------------------------------------------------------------------------
  233. // Shut down the main Game instancea nd perform any additional cleanup
  234. - (void) shutDownTorque2D
  235. {
  236. // Shutdown the game
  237. Game->mainShutdown();
  238. // Perform any platform cleanup
  239. }
  240. @end
  241. #pragma mark ---- Platform Namespace Functions ----
  242. //-----------------------------------------------------------------------------
  243. // Used for initializing the OS X platform code
  244. void Platform::init()
  245. {
  246. // Set the global script variable $Platform to "macos"
  247. Con::setVariable("$Platform", "macos");
  248. // Initialize standard libraries (namespaces)
  249. Video::init();
  250. Input::init();
  251. // Initialize OS X specific libraries and services
  252. Con::printSeparator();
  253. }
  254. //-----------------------------------------------------------------------------
  255. // OS X processing
  256. void Platform::process()
  257. {
  258. }
  259. //-----------------------------------------------------------------------------
  260. // Shuts down the OS X platform layer code
  261. void Platform::shutdown()
  262. {
  263. Input::destroy();
  264. Video::destroy();
  265. }
  266. //-----------------------------------------------------------------------------
  267. // Completely closes and restarts the simulation
  268. void Platform::restartInstance()
  269. {
  270. // Returns the NSBundle that corresponds to the directory where the current app executable is located.
  271. NSBundle* mainAppBundle = [NSBundle mainBundle];
  272. // Returns the file URL of the receiver's executable file.
  273. // Not currently used, but left here for reference
  274. //NSURL* execURL = [mainAppBundle executableURL];
  275. // Returns the full pathname of the receiver's executable file.
  276. NSString* execString = [mainAppBundle executablePath];
  277. // Create a mutable string we can build into an executable command
  278. NSMutableString* mut = [[[NSMutableString alloc] init] autorelease];
  279. // Base string is the executable path
  280. [mut appendString:execString];
  281. // append ampersand so that we can launch without blocking.
  282. // encase in quotes so that spaces in the path are accepted.
  283. [mut insertString:@"\"" atIndex:0];
  284. [mut appendString:@"\" & "];
  285. [mut appendString:@"\\0"];
  286. // Convert to a C string
  287. const char* execCString = [mut UTF8String];
  288. // Echo the command before we run it
  289. Con::printf("---- %s -----", execCString);
  290. // Run the restart command and hope for the best
  291. system(execCString);
  292. }
  293. //-----------------------------------------------------------------------------
  294. // Starts the quit process for the main Game instance
  295. void Platform::postQuitMessage(const U32 in_quitVal)
  296. {
  297. Event quitEvent;
  298. quitEvent.type = QuitEventType;
  299. Game->postEvent(quitEvent);
  300. }
  301. //-----------------------------------------------------------------------------
  302. // Kicks off the termination process for the NSApp
  303. void Platform::forceShutdown(S32 returnValue)
  304. {
  305. // Note: AppCode states terminate is deprecated, which is not true
  306. [NSApp performSelector:@selector(terminate:) withObject:nil afterDelay:0.0];
  307. }
  308. //-----------------------------------------------------------------------------
  309. void Platform::debugBreak()
  310. {
  311. raise(SIGTRAP);
  312. }
  313. //-----------------------------------------------------------------------------
  314. void Platform::outputDebugString(const char *string)
  315. {
  316. fprintf(stderr, string, NULL );
  317. fprintf(stderr, "\n" );
  318. fflush(stderr);
  319. }