defaultGame.cc 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  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. #include "game/defaultGame.h"
  23. #include "platform/types.h"
  24. #include "platform/Tickable.h"
  25. #include "platform/platform.h"
  26. #include "platform/platformVideo.h"
  27. #include "platform/platformInput.h"
  28. #include "platform/platformAudio.h"
  29. #include "platform/event.h"
  30. #include "game/gameInterface.h"
  31. #include "collection/vector.h"
  32. #include "math/mMath.h"
  33. #include "graphics/dgl.h"
  34. #include "graphics/gBitmap.h"
  35. #include "io/resource/resourceManager.h"
  36. #include "io/fileStream.h"
  37. #include "graphics/TextureManager.h"
  38. #include "console/console.h"
  39. #include "sim/simBase.h"
  40. #include "gui/guiCanvas.h"
  41. #include "input/actionMap.h"
  42. #include "network/connectionProtocol.h"
  43. #include "io/bitStream.h"
  44. #include "network/telnetConsole.h"
  45. #include "debug/telnetDebugger.h"
  46. #include "console/consoleTypes.h"
  47. #include "math/mathTypes.h"
  48. #include "graphics/TextureManager.h"
  49. #include "io/resource/resourceManager.h"
  50. #include "platform/platformVideo.h"
  51. #include "network/netStringTable.h"
  52. #include "memory/frameAllocator.h"
  53. #include "game/version.h"
  54. #include "debug/profiler.h"
  55. #include "network/serverQuery.h"
  56. #include "game/defaultGame.h"
  57. #include "platform/nativeDialogs/msgBox.h"
  58. #include "platform/nativeDialogs/fileDialog.h"
  59. #include "memory/safeDelete.h"
  60. #include <stdio.h>
  61. #ifndef _NETWORK_PROCESS_LIST_H_
  62. #include "network/networkProcessList.h"
  63. #endif
  64. #ifndef _REMOTE_DEBUGGER_BRIDGE_H_
  65. #include "debug/remote/RemoteDebuggerBridge.h"
  66. #endif
  67. #ifndef _MODULE_MANAGER_H
  68. #include "module/moduleManager.h"
  69. #endif
  70. #ifndef _ASSET_MANAGER_H_
  71. #include "assets/assetManager.h"
  72. #endif
  73. #ifndef _PARTICLE_SYSTEM_H_
  74. #include "2d/core/ParticleSystem.h"
  75. #endif
  76. #ifdef TORQUE_OS_IOS
  77. #include "platformiOS/iOSProfiler.h"
  78. #endif
  79. // Script binding.
  80. #include "platform/platform_ScriptBinding.h"
  81. //-----------------------------------------------------------------------------
  82. DefaultGame GameObject;
  83. DemoNetInterface GameNetInterface;
  84. NetworkProcessList gServerProcessList(true);
  85. StringTableEntry gMasterAddress;
  86. static F32 gTimeScale = 1.0;
  87. static U32 gTimeAdvance = 0;
  88. static U32 gFrameSkip = 0;
  89. static U32 gFrameCount = 0;
  90. // Reset frames stats.
  91. static F32 framePeriod = 0.0f;
  92. static F32 frameTotalTime = 0.0f;
  93. static F32 frameTotalLastTime = 0.0f;
  94. static U32 frameTotalCount = 0;
  95. //-----------------------------------------------------------------------------
  96. bool initializeLibraries()
  97. {
  98. PlatformAssert::create();
  99. Con::init();
  100. Sim::init();
  101. if(!Net::init())
  102. {
  103. printf("Network Error : Unable to initialize the network... aborting.");
  104. return false;
  105. }
  106. gMasterAddress = StringTable->EmptyString;
  107. Con::addVariable( "MasterServerAddress", TypeString, &gMasterAddress );
  108. // Create the stock colors.
  109. StockColor::create();
  110. #if defined(TORQUE_OS_IOS) || defined(TORQUE_OS_ANDROID) || defined(TORQUE_OS_EMSCRIPTEN)
  111. //3MB default is way too big for iPhone!!!
  112. #ifdef TORQUE_SHIPPING
  113. FrameAllocator::init(256 * 1024); //256KB for now... but let's test and see!
  114. #else
  115. FrameAllocator::init(512 * 1024); //512KB for now... but let's test and see!
  116. #endif //TORQUE_SHIPPING
  117. #else
  118. FrameAllocator::init(3 << 20); // 3 meg frame allocator buffer
  119. #endif //TORQUE_OS_IOS
  120. TextureManager::create();
  121. ResManager::create();
  122. // Register known file types here
  123. ResourceManager->registerExtension(".jpg", constructBitmapJPEG);
  124. ResourceManager->registerExtension(".jpeg", constructBitmapJPEG);
  125. ResourceManager->registerExtension(".png", constructBitmapPNG);
  126. ResourceManager->registerExtension(".uft", constructNewFont);
  127. ResourceManager->registerExtension(".fnt", constructBMFont);
  128. #ifdef TORQUE_OS_IOS
  129. ResourceManager->registerExtension(".pvr", constructBitmapPVR);
  130. #endif
  131. Platform::initConsole();
  132. NetStringTable::create();
  133. TelnetConsole::create();
  134. TelnetDebugger::create();
  135. Processor::init();
  136. Math::init();
  137. Platform::init(); // platform specific initialization
  138. // Initialize the particle system.
  139. ParticleSystem::Init();
  140. #if defined(TORQUE_OS_IOS) && defined(_USE_STORE_KIT)
  141. storeInit();
  142. #endif // TORQUE_OS_IOS && _USE_STORE_KIT
  143. return true;
  144. }
  145. //--------------------------------------------------------------------------
  146. void shutdownLibraries()
  147. {
  148. // Purge any resources on the timeout list...
  149. if (ResourceManager)
  150. ResourceManager->purge();
  151. TelnetDebugger::destroy();
  152. TelnetConsole::destroy();
  153. Sim::shutdown();
  154. Platform::shutdown();
  155. NetStringTable::destroy();
  156. Con::shutdown();
  157. ResManager::destroy();
  158. TextureManager::destroy();
  159. // Destroy the stock colors.
  160. StockColor::destroy();
  161. _StringTable::destroy();
  162. // asserts should be destroyed LAST
  163. FrameAllocator::destroy();
  164. PlatformAssert::destroy();
  165. Net::shutdown();
  166. // Destroy the particle system.
  167. ParticleSystem::destroy();
  168. #ifdef _USE_STORE_KIT
  169. storeCleanup();
  170. #endif // _USE_STORE_KIT
  171. }
  172. //--------------------------------------------------------------------------
  173. bool initializeGame(int argc, const char **argv)
  174. {
  175. Con::addVariable("timeScale", TypeF32, &gTimeScale);
  176. Con::addVariable("timeAdvance", TypeS32, &gTimeAdvance);
  177. Con::addVariable("frameSkip", TypeS32, &gFrameSkip);
  178. initMessageBoxVars();
  179. // Register the module manager.
  180. ModuleDatabase.registerObject( "ModuleDatabase" );
  181. // Register the asset database.
  182. AssetDatabase.registerObject( "AssetDatabase" );
  183. // Register the asset database as a module listener.
  184. ModuleDatabase.addListener( &AssetDatabase );
  185. ActionMap* globalMap = new ActionMap;
  186. globalMap->registerObject("GlobalActionMap");
  187. Sim::getActiveActionMapSet()->pushObject(globalMap);
  188. // Let the remote debugger process the command-line.
  189. RemoteDebuggerBridge::processCommandLine( argc, argv );
  190. if(argc > 2 && dStricmp(argv[1], "-project") == 0)
  191. {
  192. char playerPath[1024];
  193. Platform::makeFullPathName(argv[2], playerPath, sizeof(playerPath));
  194. Platform::setCurrentDirectory(playerPath);
  195. argv += 2;
  196. argc -= 2;
  197. }
  198. // Scan executable location and all sub-directories.
  199. ResourceManager->setWriteablePath(Platform::getCurrentDirectory());
  200. ResourceManager->addPath( Platform::getCurrentDirectory() );
  201. FileStream scriptFileStream;
  202. Stream* scriptStream;
  203. const char* defaultScriptName = "main.cs";
  204. bool useDefaultScript = true;
  205. // Check if any command-line parameters were passed (the first is just the app name).
  206. if (argc > 1)
  207. {
  208. // If so, check if the first parameter is a file to open.
  209. if ( (scriptFileStream.open(argv[1], FileStream::Read)) && dStrncmp(argv[1], "", 1) )
  210. {
  211. // If it opens, we assume it is the script to run.
  212. useDefaultScript = false;
  213. scriptStream = &scriptFileStream;
  214. }
  215. }
  216. if (useDefaultScript)
  217. {
  218. bool success = false;
  219. success = scriptFileStream.open(defaultScriptName, FileStream::Read);
  220. if( !success )
  221. {
  222. char msg[1024];
  223. dSprintf(msg, sizeof(msg), "Failed to open \"%s\".", defaultScriptName);
  224. printf(" Error : %s", msg);
  225. return false;
  226. }
  227. scriptStream = &scriptFileStream;
  228. }
  229. // Create a script buffer.
  230. const U32 size = scriptStream->getStreamSize();
  231. char* pScriptBuffer = new char[size + 1];
  232. // Read script.
  233. scriptStream->read(size, pScriptBuffer);
  234. scriptFileStream.close();
  235. pScriptBuffer[size] = 0;
  236. char buffer[1024], *ptr;
  237. Platform::makeFullPathName(useDefaultScript ? defaultScriptName : argv[1], buffer, sizeof(buffer), Platform::getCurrentDirectory());
  238. ptr = dStrrchr( buffer, '/' );
  239. if ( ptr )
  240. *ptr = 0;
  241. Platform::setMainDotCsDir(buffer);
  242. Platform::setCurrentDirectory(buffer);
  243. const S32 errorHash = Con::getIntVariable("$ScriptErrorHash");
  244. Con::evaluate(pScriptBuffer, false, useDefaultScript ? defaultScriptName : argv[1]);
  245. delete[] pScriptBuffer;
  246. // Did an error occur?
  247. if ( errorHash != Con::getIntVariable("$ScriptErrorHash") )
  248. {
  249. printf( "Quitting as an error occurred parsing the root script '%s'.", useDefaultScript ? defaultScriptName : argv[1] );
  250. return false;
  251. }
  252. return true;
  253. }
  254. //--------------------------------------------------------------------------
  255. void shutdownGame()
  256. {
  257. // Perform pre-exit callback.
  258. if( Con::isFunction("onPreExit") )
  259. Con::executef(1, "onPreExit");
  260. // Perform the exit callback.
  261. if( Con::isFunction("onExit") )
  262. Con::executef(1, "onExit");
  263. // Unregister the module database.
  264. ModuleDatabase.unregisterObject();
  265. // Unregister the asset database.
  266. AssetDatabase.unregisterObject();
  267. }
  268. //--------------------------------------------------------------------------
  269. bool DefaultGame::mainInitialize(int argc, const char **argv)
  270. {
  271. if(!initializeLibraries())
  272. return false;
  273. #ifdef TORQUE_OS_EMSCRIPTEN
  274. // temp hack
  275. argc = 0;
  276. #endif
  277. // Set up the command line args for the console scripts...
  278. Con::setIntVariable("$GameProject::argc", argc);
  279. U32 i;
  280. for (i = 0; i < (U32)argc; i++)
  281. Con::setVariable(avar("$GameProject::argv%d", i), argv[i]);
  282. if (initializeGame(argc, argv) == false)
  283. {
  284. //Using printf cos Con:: is not around here.
  285. printf("\nApplication failed to start! Make sure your resources are in the correct place.");
  286. shutdownGame();
  287. shutdownLibraries();
  288. return false;
  289. }
  290. // Start processing ticks.
  291. setProcessTicks( true );
  292. #ifdef TORQUE_OS_IOS
  293. // Torque 2D does not have true, GameKit networking support.
  294. // The old socket network code is untested, undocumented and likely broken.
  295. // This will eventually be replaced with GameKit.
  296. // For now, it is confusing to even have a checkbox in the editor that no one uses or understands.
  297. // If you are one of the few that uses this, just replace the false; with the commented line. -MP 1.5
  298. //-Mat this is a bit of a hack, but if we don't want the network, we shut it off now.
  299. // We can't do it until we've run the entry script, otherwise the script variable will not have ben loaded
  300. bool usesNet = false; //dAtob( Con::getVariable( "$pref::iOS::UseNetwork" ) );
  301. if( !usesNet ) {
  302. Net::shutdown();
  303. }
  304. #ifdef TORQUE_OS_IOS_PROFILE
  305. iPhoneProfilerProfilerInit();
  306. #endif
  307. #endif
  308. #ifdef TORQUE_OS_ANDROID
  309. //-Mat this is a bit of a hack, but if we don't want the network, we shut it off now.
  310. // We can't do it until we've run the entry script, otherwise the script variable will not have ben loaded
  311. bool usesNet = false; //dAtob( Con::getVariable( "$pref::iOS::UseNetwork" ) );
  312. if( !usesNet ) {
  313. Net::shutdown();
  314. }
  315. #ifdef TORQUE_OS_ANDROID_PROFILE
  316. AndroidProfilerProfilerInit();
  317. #endif
  318. #endif
  319. return true;
  320. }
  321. //--------------------------------------------------------------------------
  322. void DefaultGame::processTick( void )
  323. {
  324. Con::setVariable( "Sim::Time", avar("%4.1f", (F32)Platform::getVirtualMilliseconds() / 1000.0f ) );
  325. // Update the frame variables periodically.
  326. static F32 lastFrameUpdate = frameTotalTime;
  327. if ( (frameTotalTime - lastFrameUpdate) > 0.25f )
  328. {
  329. Con::setVariable( "fps::framePeriod", avar("%4.1f", 1.0f / framePeriod) );
  330. Con::setVariable( "fps::frameCount", avar("%u", frameTotalCount) );
  331. lastFrameUpdate = frameTotalTime;
  332. }
  333. }
  334. //--------------------------------------------------------------------------
  335. void DefaultGame::advanceTime( F32 timeDelta )
  336. {
  337. // Update total frame time.
  338. frameTotalTime += timeDelta;
  339. // Update frame total count.
  340. frameTotalCount++;
  341. // Have we already processed a single frame?
  342. if ( frameTotalCount > 1 )
  343. {
  344. // Yes, so set the time-bias to use.
  345. const F32 timeBias = 0.01f;
  346. // Calculate the current frame period.
  347. framePeriod = framePeriod * ( 1.0f - timeBias ) + ( frameTotalTime - frameTotalLastTime ) * timeBias;
  348. }
  349. // Update last total frame time.
  350. frameTotalLastTime = frameTotalTime;
  351. }
  352. //--------------------------------------------------------------------------
  353. void DefaultGame::mainLoop( void )
  354. {
  355. #ifdef TORQUE_OS_IOS_PROFILE
  356. iPhoneProfilerStart("MAIN_LOOP");
  357. #endif
  358. #ifdef TORQUE_OS_ANDROID_PROFILE
  359. AndroidProfilerStart("MAIN_LOOP");
  360. #endif
  361. PROFILE_START(MainLoop);
  362. #ifdef TORQUE_ALLOW_JOURNALING
  363. PROFILE_START(JournalMain);
  364. Game->journalProcess();
  365. PROFILE_END();
  366. #endif // TORQUE_ALLOW_JOURNALING
  367. PROFILE_START(NetProcessMain);
  368. Net::process(); // read in all events
  369. PROFILE_END();
  370. PROFILE_START(PlatformProcessMain);
  371. Platform::process(); // keys, etc.
  372. PROFILE_END();
  373. PROFILE_START(TelconsoleProcessMain);
  374. TelConsole->process();
  375. PROFILE_END();
  376. PROFILE_START(TelDebuggerProcessMain);
  377. TelDebugger->process();
  378. PROFILE_END();
  379. PROFILE_START(TimeManagerProcessMain);
  380. TimeManager::process(); // guaranteed to produce an event
  381. PROFILE_END();
  382. PROFILE_START(GameProcessEvents);
  383. Game->processEvents(); // process all non-sim posted events.
  384. PROFILE_END();
  385. PROFILE_END();
  386. #ifdef TORQUE_OS_IOS_PROFILE
  387. iPhoneProfilerEnd("MAIN_LOOP");
  388. if(iPhoneProfilerGetCount() >= 60){
  389. iPhoneProfilerPrintAllResults();
  390. iPhoneProfilerProfilerInit();
  391. }
  392. #endif
  393. #ifdef TORQUE_OS_ANDROID_PROFILE
  394. AndroidProfilerEnd("MAIN_LOOP");
  395. if(AndroidProfilerGetCount() >= 60){
  396. AndroidProfilerPrintAllResults();
  397. AndroidProfilerProfilerInit();
  398. }
  399. #endif
  400. }
  401. //-----------------------------------------------------------------------------
  402. void DefaultGame::mainShutdown( void )
  403. {
  404. // Stop processing ticks.
  405. setProcessTicks( false );
  406. shutdownGame();
  407. shutdownLibraries();
  408. if( Game->requiresRestart() )
  409. Platform::restartInstance();
  410. }
  411. //--------------------------------------------------------------------------
  412. void DefaultGame::gameReactivate( void )
  413. {
  414. if ( !Input::isEnabled() )
  415. Input::enable();
  416. if ( !Input::isActive() )
  417. Input::reactivate();
  418. TextureManager::mDGLRender = true;
  419. if ( Canvas )
  420. Canvas->resetUpdateRegions();
  421. }
  422. //--------------------------------------------------------------------------
  423. void DefaultGame::gameDeactivate( const bool noRender )
  424. {
  425. if ( Input::isActive() )
  426. Input::deactivate();
  427. if ( Input::isEnabled() )
  428. Input::disable();
  429. if ( noRender )
  430. TextureManager::mDGLRender = false;
  431. }
  432. //--------------------------------------------------------------------------
  433. void DefaultGame::textureKill()
  434. {
  435. TextureManager::killManager();
  436. }
  437. //--------------------------------------------------------------------------
  438. void DefaultGame::textureResurrect()
  439. {
  440. TextureManager::resurrectManager();
  441. }
  442. //--------------------------------------------------------------------------
  443. void DefaultGame::refreshWindow()
  444. {
  445. if(Canvas)
  446. Canvas->resetUpdateRegions();
  447. }
  448. //--------------------------------------------------------------------------
  449. void DefaultGame::processQuitEvent()
  450. {
  451. setRunning(false);
  452. }
  453. //--------------------------------------------------------------------------
  454. void DefaultGame::processTimeEvent(TimeEvent *event)
  455. {
  456. PROFILE_START(ProcessTimeEvent);
  457. U32 elapsedTime = event->elapsedTime;
  458. if(elapsedTime > 1024)
  459. {
  460. elapsedTime = 0;
  461. }
  462. if(gTimeAdvance)
  463. {
  464. elapsedTime = gTimeAdvance;
  465. }
  466. else if ( mNotEqual( gTimeScale, 1.0f ) )
  467. {
  468. elapsedTime = (U32) (elapsedTime * gTimeScale);
  469. }
  470. Platform::advanceTime(elapsedTime);
  471. bool tickPass;
  472. PROFILE_START(ServerProcess);
  473. #ifdef TORQUE_OS_IOS_PROFILE
  474. iPhoneProfilerStart("SERVER_PROC");
  475. #endif
  476. #ifdef TORQUE_OS_ANDROID_PROFILE
  477. AndroidProfilerStart("SERVER_PROC");
  478. #endif
  479. tickPass = gServerProcessList.advanceTime(elapsedTime);
  480. #ifdef TORQUE_OS_IOS_PROFILE
  481. iPhoneProfilerEnd("SERVER_PROC");
  482. #endif
  483. #ifdef TORQUE_OS_ANDROID_PROFILE
  484. AndroidProfilerEnd("SERVER_PROC");
  485. #endif
  486. PROFILE_END();
  487. PROFILE_START(ServerNetProcess);
  488. // only send packets if a tick happened
  489. if(tickPass)
  490. GNet->processServer();
  491. PROFILE_END();
  492. PROFILE_START(SimAdvanceTime);
  493. #ifdef TORQUE_OS_IOS_PROFILE
  494. iPhoneProfilerStart("SIM_TIME");
  495. #endif
  496. #ifdef TORQUE_OS_ANDROID_PROFILE
  497. AndroidProfilerStart("SIM_TIME");
  498. #endif
  499. Sim::advanceTime(elapsedTime);
  500. #ifdef TORQUE_OS_IOS_PROFILE
  501. iPhoneProfilerEnd("SIM_TIME");
  502. #endif
  503. #ifdef TORQUE_OS_ANDROID_PROFILE
  504. AndroidProfilerEnd("SIM_TIME");
  505. #endif
  506. PROFILE_END();
  507. PROFILE_START(ClientProcess);
  508. #ifdef TORQUE_OS_IOS_PROFILE
  509. iPhoneProfilerStart("CLIENT_PROC");
  510. #endif
  511. #ifdef TORQUE_OS_ANDROID_PROFILE
  512. AndroidProfilerStart("CLIENT_PROC");
  513. #endif
  514. PROFILE_START(TickableAdvanceTime);
  515. Tickable::advanceTime(elapsedTime);
  516. PROFILE_END();
  517. // Milliseconds between audio updates.
  518. const U32 AudioUpdatePeriod = 125;
  519. // alxUpdate is somewhat expensive and does not need to be updated constantly,
  520. // though it does need to be updated in real time
  521. static U32 lastAudioUpdate = 0;
  522. U32 realTime = Platform::getRealMilliseconds();
  523. if((realTime - lastAudioUpdate) >= AudioUpdatePeriod)
  524. {
  525. alxUpdate();
  526. lastAudioUpdate = realTime;
  527. }
  528. #ifdef TORQUE_OS_IOS_PROFILE
  529. iPhoneProfilerEnd("CLIENT_PROC");
  530. #endif
  531. #ifdef TORQUE_OS_ANDROID_PROFILE
  532. AndroidProfilerEnd("CLIENT_PROC");
  533. #endif
  534. PROFILE_END();
  535. PROFILE_START(ClientNetProcess);
  536. GNet->processClient();
  537. PROFILE_END();
  538. if(Canvas && TextureManager::mDGLRender)
  539. {
  540. #ifdef TORQUE_OS_IOS_PROFILE
  541. iPhoneProfilerStart("GL_RENDER");
  542. #endif
  543. #ifdef TORQUE_OS_ANDROID_PROFILE
  544. AndroidProfilerStart("GL_RENDER");
  545. #endif
  546. bool preRenderOnly = false;
  547. if(gFrameSkip && gFrameCount % gFrameSkip)
  548. preRenderOnly = true;
  549. PROFILE_START(RenderFrame);
  550. Canvas->renderFrame(preRenderOnly);
  551. PROFILE_END();
  552. gFrameCount++;
  553. #ifdef TORQUE_OS_IOS_PROFILE
  554. iPhoneProfilerEnd("GL_RENDER");
  555. #endif
  556. #ifdef TORQUE_OS_ANDROID_PROFILE
  557. AndroidProfilerEnd("GL_RENDER");
  558. #endif
  559. }
  560. GNet->checkTimeouts();
  561. #ifdef TORQUE_ALLOW_MUSICPLAYER
  562. updateVolume();
  563. #endif
  564. PROFILE_END();
  565. }
  566. //--------------------------------------------------------------------------
  567. void DefaultGame::processInputEvent(InputEvent *event)
  568. {
  569. PROFILE_START(ProcessInputEvent);
  570. // [neo, 5/24/2007 - #2986]
  571. // Swapped around the order of call for global action map and canvas input
  572. // handling to give canvas first go as GlobalActionMap will eat any input
  573. // events meant for firstResponders only and as a "general" trap should really
  574. // should only be called if any "local" traps did not take it, e.g. left/right
  575. // in a text edit control should not be forwarded if the text edit has focus, etc.
  576. // Any new issues regarding input should most probably start looking here first!
  577. if(!(Canvas && Canvas->processInputEvent(event)))
  578. {
  579. if(!ActionMap::handleEventGlobal(event))
  580. {
  581. // Other input consumers here...
  582. ActionMap::handleEvent(event);
  583. }
  584. }
  585. PROFILE_END();
  586. }
  587. //-----------------------------------------------------------------------------
  588. void DefaultGame::processMouseMoveEvent(MouseMoveEvent * mEvent)
  589. {
  590. if (Canvas)
  591. Canvas->processMouseMoveEvent(mEvent);
  592. }
  593. //--------------------------------------------------------------------------
  594. void DefaultGame::processScreenTouchEvent(ScreenTouchEvent * mEvent)
  595. {
  596. if (Canvas)
  597. Canvas->processScreenTouchEvent(mEvent);
  598. }
  599. //--------------------------------------------------------------------------
  600. void DefaultGame::processConsoleEvent(ConsoleEvent *event)
  601. {
  602. char *argv[2];
  603. argv[0] = (char*)"eval";
  604. argv[1] = event->data;
  605. Sim::postCurrentEvent(Sim::getRootGroup(), new SimConsoleEvent(2, const_cast<const char**>(argv), false));
  606. }
  607. //--------------------------------------------------------------------------
  608. void DefaultGame::processPacketReceiveEvent(PacketReceiveEvent * prEvent)
  609. {
  610. GNet->processPacketReceiveEvent(prEvent);
  611. }