2
0

platformOSX.mm 11 KB

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