platformOSX.mm 11 KB

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