Engine.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. //
  2. // Copyright (c) 2008-2013 the Urho3D project.
  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 deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // 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 FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Audio.h"
  24. #include "Console.h"
  25. #include "Context.h"
  26. #include "CoreEvents.h"
  27. #include "DebugHud.h"
  28. #include "Engine.h"
  29. #include "FileSystem.h"
  30. #include "Graphics.h"
  31. #include "Input.h"
  32. #include "InputEvents.h"
  33. #include "Log.h"
  34. #include "Navigation.h"
  35. #include "Network.h"
  36. #include "PackageFile.h"
  37. #include "PhysicsWorld.h"
  38. #include "ProcessUtils.h"
  39. #include "Profiler.h"
  40. #include "Renderer.h"
  41. #include "ResourceCache.h"
  42. #include "Scene.h"
  43. #include "SceneEvents.h"
  44. #include "StringUtils.h"
  45. #include "UI.h"
  46. #include "WorkQueue.h"
  47. #include "XMLFile.h"
  48. #include "DebugNew.h"
  49. #if defined(_MSC_VER) && defined(_DEBUG)
  50. // From dbgint.h
  51. #define nNoMansLandSize 4
  52. typedef struct _CrtMemBlockHeader
  53. {
  54. struct _CrtMemBlockHeader* pBlockHeaderNext;
  55. struct _CrtMemBlockHeader* pBlockHeaderPrev;
  56. char* szFileName;
  57. int nLine;
  58. size_t nDataSize;
  59. int nBlockUse;
  60. long lRequest;
  61. unsigned char gap[nNoMansLandSize];
  62. } _CrtMemBlockHeader;
  63. #endif
  64. #ifdef ANDROID
  65. extern "C"
  66. {
  67. void Android_JNI_FinishActivity();
  68. }
  69. #endif
  70. namespace Urho3D
  71. {
  72. extern const char* logLevelPrefixes[];
  73. Engine::Engine(Context* context) :
  74. Object(context),
  75. timeStep_(0.0f),
  76. timeStepSmoothing_(2),
  77. minFps_(10),
  78. #if defined(ANDROID) || defined(IOS) || defined(RASPI)
  79. maxFps_(60),
  80. maxInactiveFps_(10),
  81. pauseMinimized_(true),
  82. #else
  83. maxFps_(200),
  84. maxInactiveFps_(60),
  85. pauseMinimized_(false),
  86. #endif
  87. autoExit_(true),
  88. initialized_(false),
  89. #ifdef ANDROID
  90. exitRequested_(false),
  91. #endif
  92. exiting_(false),
  93. headless_(false),
  94. audioPaused_(false)
  95. {
  96. // Register self as a subsystem
  97. context_->RegisterSubsystem(this);
  98. // Create subsystems which do not depend on engine initialization or startup parameters
  99. context_->RegisterSubsystem(new Time(context_));
  100. context_->RegisterSubsystem(new WorkQueue(context_));
  101. #ifdef ENABLE_PROFILING
  102. context_->RegisterSubsystem(new Profiler(context_));
  103. #endif
  104. context_->RegisterSubsystem(new FileSystem(context_));
  105. #ifdef ENABLE_LOGGING
  106. context_->RegisterSubsystem(new Log(context_));
  107. #endif
  108. context_->RegisterSubsystem(new ResourceCache(context_));
  109. context_->RegisterSubsystem(new Network(context_));
  110. context_->RegisterSubsystem(new Input(context_));
  111. context_->RegisterSubsystem(new Audio(context_));
  112. context_->RegisterSubsystem(new UI(context_));
  113. // Register object factories for libraries which are not automatically registered along with subsystem creation
  114. RegisterSceneLibrary(context_);
  115. RegisterPhysicsLibrary(context_);
  116. RegisterNavigationLibrary(context_);
  117. SubscribeToEvent(E_EXITREQUESTED, HANDLER(Engine, HandleExitRequested));
  118. }
  119. Engine::~Engine()
  120. {
  121. }
  122. bool Engine::Initialize(const VariantMap& parameters)
  123. {
  124. if (initialized_)
  125. return true;
  126. PROFILE(InitEngine);
  127. // Set headless mode
  128. headless_ = GetParameter(parameters, "Headless", false).GetBool();
  129. // Register the rest of the subsystems
  130. if (!headless_)
  131. {
  132. context_->RegisterSubsystem(new Graphics(context_));
  133. context_->RegisterSubsystem(new Renderer(context_));
  134. }
  135. else
  136. {
  137. // Register graphics library objects explicitly in headless mode to allow them to work without using actual GPU resources
  138. RegisterGraphicsLibrary(context_);
  139. }
  140. // In debug mode, check now that all factory created objects can be created without crashing
  141. #ifdef _DEBUG
  142. const HashMap<ShortStringHash, SharedPtr<ObjectFactory> >& factories = context_->GetObjectFactories();
  143. for (HashMap<ShortStringHash, SharedPtr<ObjectFactory> >::ConstIterator i = factories.Begin(); i != factories.End(); ++i)
  144. SharedPtr<Object> object = i->second_->CreateObject();
  145. #endif
  146. // Start logging
  147. Log* log = GetSubsystem<Log>();
  148. if (log)
  149. {
  150. if (HasParameter(parameters, "LogLevel"))
  151. log->SetLevel(GetParameter(parameters, "LogLevel").GetInt());
  152. log->SetQuiet(GetParameter(parameters, "LogQuiet", false).GetBool());
  153. log->Open(GetParameter(parameters, "LogName", "Urho3D.log").GetString());
  154. }
  155. // Set maximally accurate low res timer
  156. GetSubsystem<Time>()->SetTimerPeriod(1);
  157. // Configure max FPS
  158. if (GetParameter(parameters, "FrameLimiter", true) == false)
  159. SetMaxFps(0);
  160. // Set amount of worker threads according to the available physical CPU cores. Using also hyperthreaded cores results in
  161. // unpredictable extra synchronization overhead. Also reserve one core for the main thread
  162. unsigned numThreads = GetParameter(parameters, "WorkerThreads", true).GetBool() ? GetNumPhysicalCPUs() - 1 : 0;
  163. if (numThreads)
  164. {
  165. GetSubsystem<WorkQueue>()->CreateThreads(numThreads);
  166. LOGINFO(ToString("Created %u worker thread%s", numThreads, numThreads > 1 ? "s" : ""));
  167. }
  168. // Add resource paths
  169. ResourceCache* cache = GetSubsystem<ResourceCache>();
  170. FileSystem* fileSystem = GetSubsystem<FileSystem>();
  171. String exePath = fileSystem->GetProgramDir();
  172. Vector<String> resourcePaths = GetParameter(parameters, "ResourcePaths", "CoreData;Data").GetString().Split(';');
  173. Vector<String> resourcePackages = GetParameter(parameters, "ResourcePackages").GetString().Split(';');
  174. for (unsigned i = 0; i < resourcePaths.Size(); ++i)
  175. {
  176. bool success = false;
  177. // If path is not absolute, prefer to add it as a package if possible
  178. if (!IsAbsolutePath(resourcePaths[i]))
  179. {
  180. String packageName = exePath + resourcePaths[i] + ".pak";
  181. if (fileSystem->FileExists(packageName))
  182. {
  183. SharedPtr<PackageFile> package(new PackageFile(context_));
  184. if (package->Open(packageName))
  185. {
  186. cache->AddPackageFile(package);
  187. success = true;
  188. }
  189. }
  190. if (!success)
  191. {
  192. String pathName = exePath + resourcePaths[i];
  193. if (fileSystem->DirExists(pathName))
  194. success = cache->AddResourceDir(pathName);
  195. }
  196. }
  197. else
  198. {
  199. String pathName = resourcePaths[i];
  200. if (fileSystem->DirExists(pathName))
  201. success = cache->AddResourceDir(pathName);
  202. }
  203. if (!success)
  204. {
  205. LOGERROR("Failed to add resource path " + resourcePaths[i]);
  206. return false;
  207. }
  208. }
  209. // Then add specified packages
  210. for (unsigned i = 0; i < resourcePackages.Size(); ++i)
  211. {
  212. bool success = false;
  213. String packageName = exePath + resourcePackages[i];
  214. if (fileSystem->FileExists(packageName))
  215. {
  216. SharedPtr<PackageFile> package(new PackageFile(context_));
  217. if (package->Open(packageName))
  218. {
  219. cache->AddPackageFile(package);
  220. success = true;
  221. }
  222. }
  223. if (!success)
  224. {
  225. LOGERROR("Failed to add resource package " + resourcePackages[i]);
  226. return false;
  227. }
  228. }
  229. // Initialize graphics & audio output
  230. if (!headless_)
  231. {
  232. Graphics* graphics = GetSubsystem<Graphics>();
  233. Renderer* renderer = GetSubsystem<Renderer>();
  234. if (HasParameter(parameters, "ExternalWindow"))
  235. graphics->SetExternalWindow(GetParameter(parameters, "ExternalWindow").GetPtr());
  236. graphics->SetForceSM2(GetParameter(parameters, "ForceSM2", false).GetBool());
  237. graphics->SetWindowTitle(GetParameter(parameters, "WindowTitle", "Urho3D").GetString());
  238. graphics->SetWindowIcon(cache->GetResource<Image>(GetParameter(parameters, "WindowIcon", "Textures/Icon.png").GetString()));
  239. if (!graphics->SetMode(
  240. GetParameter(parameters, "WindowWidth", 0).GetInt(),
  241. GetParameter(parameters, "WindowHeight", 0).GetInt(),
  242. GetParameter(parameters, "FullScreen", true).GetBool(),
  243. GetParameter(parameters, "WindowResizable", false).GetBool(),
  244. GetParameter(parameters, "VSync", false).GetBool(),
  245. GetParameter(parameters, "TripleBuffer", false).GetBool(),
  246. GetParameter(parameters, "MultiSample", 1).GetInt()
  247. ))
  248. return false;
  249. if (HasParameter(parameters, "RenderPath"))
  250. renderer->SetDefaultRenderPath(cache->GetResource<XMLFile>(GetParameter(parameters, "RenderPath").GetString()));
  251. renderer->SetDrawShadows(GetParameter(parameters, "Shadows", true).GetBool());
  252. if (renderer->GetDrawShadows() && GetParameter(parameters, "LowQualityShadows", false).GetBool())
  253. renderer->SetShadowQuality(SHADOWQUALITY_LOW_16BIT);
  254. if (GetParameter(parameters, "Sound", true).GetBool())
  255. {
  256. GetSubsystem<Audio>()->SetMode(
  257. GetParameter(parameters, "SoundBuffer", 100).GetInt(),
  258. GetParameter(parameters, "SoundMixRate", 44100).GetInt(),
  259. GetParameter(parameters, "SoundStereo", true).GetBool(),
  260. GetParameter(parameters, "SoundInterpolation", true).GetBool()
  261. );
  262. }
  263. }
  264. // Init FPU state of main thread
  265. InitFPU();
  266. frameTimer_.Reset();
  267. initialized_ = true;
  268. return true;
  269. }
  270. void Engine::RunFrame()
  271. {
  272. assert(initialized_);
  273. // If not headless, and the graphics subsystem no longer has a window open, assume we should exit
  274. if (!headless_ && !GetSubsystem<Graphics>()->IsInitialized())
  275. exiting_ = true;
  276. if (exiting_)
  277. return;
  278. // Note: there is a minimal performance cost to looking up subsystems (uses a hashmap); if they would be looked up several
  279. // times per frame it would be better to cache the pointers
  280. Time* time = GetSubsystem<Time>();
  281. Input* input = GetSubsystem<Input>();
  282. Audio* audio = GetSubsystem<Audio>();
  283. time->BeginFrame(timeStep_);
  284. // If pause when minimized -mode is in use, stop updates and audio as necessary
  285. if (pauseMinimized_ && input->IsMinimized())
  286. {
  287. if (audio->IsPlaying())
  288. {
  289. audio->Stop();
  290. audioPaused_ = true;
  291. }
  292. }
  293. else
  294. {
  295. // Only unpause when it was paused by the engine
  296. if (audioPaused_)
  297. {
  298. audio->Play();
  299. audioPaused_ = false;
  300. }
  301. Update();
  302. }
  303. Render();
  304. ApplyFrameLimit();
  305. time->EndFrame();
  306. }
  307. Console* Engine::CreateConsole()
  308. {
  309. if (headless_ || !initialized_)
  310. return 0;
  311. // Return existing console if possible
  312. Console* console = GetSubsystem<Console>();
  313. if (!console)
  314. {
  315. console = new Console(context_);
  316. context_->RegisterSubsystem(console);
  317. }
  318. return console;
  319. }
  320. DebugHud* Engine::CreateDebugHud()
  321. {
  322. if (headless_ || !initialized_)
  323. return 0;
  324. // Return existing debug HUD if possible
  325. DebugHud* debugHud = GetSubsystem<DebugHud>();
  326. if (!debugHud)
  327. {
  328. debugHud = new DebugHud(context_);
  329. context_->RegisterSubsystem(debugHud);
  330. }
  331. return debugHud;
  332. }
  333. void Engine::SetTimeStepSmoothing(int frames)
  334. {
  335. timeStepSmoothing_ = Clamp(frames, 1, 20);
  336. }
  337. void Engine::SetMinFps(int fps)
  338. {
  339. minFps_ = Max(fps, 0);
  340. }
  341. void Engine::SetMaxFps(int fps)
  342. {
  343. maxFps_ = Max(fps, 0);
  344. }
  345. void Engine::SetMaxInactiveFps(int fps)
  346. {
  347. maxInactiveFps_ = Max(fps, 0);
  348. }
  349. void Engine::SetPauseMinimized(bool enable)
  350. {
  351. pauseMinimized_ = enable;
  352. }
  353. void Engine::SetAutoExit(bool enable)
  354. {
  355. // On mobile platforms exit is mandatory if requested by the platform itself and should not be attempted to be disabled
  356. #if defined(ANDROID) || defined(IOS)
  357. enable = true;
  358. #endif
  359. autoExit_ = enable;
  360. }
  361. void Engine::Exit()
  362. {
  363. #if defined(IOS)
  364. // On iOS it's not legal for the application to exit on its own, instead it will be minimized with the home key
  365. #elif defined(ANDROID)
  366. // On Android we request the Java activity to finish itself
  367. if (!exitRequested_)
  368. {
  369. Android_JNI_FinishActivity();
  370. exitRequested_ = true;
  371. }
  372. #else
  373. DoExit();
  374. #endif
  375. }
  376. void Engine::DumpProfiler()
  377. {
  378. Profiler* profiler = GetSubsystem<Profiler>();
  379. if (profiler)
  380. LOGRAW(profiler->GetData(true, true) + "\n");
  381. }
  382. void Engine::DumpResources()
  383. {
  384. #ifdef ENABLE_LOGGING
  385. ResourceCache* cache = GetSubsystem<ResourceCache>();
  386. const HashMap<ShortStringHash, ResourceGroup>& resourceGroups = cache->GetAllResources();
  387. LOGRAW("\n");
  388. for (HashMap<ShortStringHash, ResourceGroup>::ConstIterator i = resourceGroups.Begin();
  389. i != resourceGroups.End(); ++i)
  390. {
  391. unsigned num = i->second_.resources_.Size();
  392. unsigned memoryUse = i->second_.memoryUse_;
  393. if (num)
  394. {
  395. LOGRAW("Resource type " + i->second_.resources_.Begin()->second_->GetTypeName() +
  396. ": count " + String(num) + " memory use " + String(memoryUse) + "\n");
  397. }
  398. }
  399. LOGRAW("Total memory use of all resources " + String(cache->GetTotalMemoryUse()) + "\n\n");
  400. #endif
  401. }
  402. void Engine::DumpMemory()
  403. {
  404. #ifdef ENABLE_LOGGING
  405. #if defined(_MSC_VER) && defined(_DEBUG)
  406. _CrtMemState state;
  407. _CrtMemCheckpoint(&state);
  408. _CrtMemBlockHeader* block = state.pBlockHeader;
  409. unsigned total = 0;
  410. unsigned blocks = 0;
  411. for (;;)
  412. {
  413. if (block && block->pBlockHeaderNext)
  414. block = block->pBlockHeaderNext;
  415. else
  416. break;
  417. }
  418. while (block)
  419. {
  420. if (block->nBlockUse > 0)
  421. {
  422. if (block->szFileName)
  423. LOGRAW("Block " + String((int)block->lRequest) + ": " + String(block->nDataSize) + " bytes, file " + String(block->szFileName) + " line " + String(block->nLine) + "\n");
  424. else
  425. LOGRAW("Block " + String((int)block->lRequest) + ": " + String(block->nDataSize) + " bytes\n");
  426. total += block->nDataSize;
  427. ++blocks;
  428. }
  429. block = block->pBlockHeaderPrev;
  430. }
  431. LOGRAW("Total allocated memory " + String(total) + " bytes in " + String(blocks) + " blocks\n\n");
  432. #else
  433. LOGRAW("DumpMemory() supported on MSVC debug mode only\n\n");
  434. #endif
  435. #endif
  436. }
  437. void Engine::Update()
  438. {
  439. PROFILE(Update);
  440. // Logic update event
  441. using namespace Update;
  442. VariantMap eventData;
  443. eventData[P_TIMESTEP] = timeStep_;
  444. SendEvent(E_UPDATE, eventData);
  445. // Logic post-update event
  446. SendEvent(E_POSTUPDATE, eventData);
  447. // Rendering update event
  448. SendEvent(E_RENDERUPDATE, eventData);
  449. // Post-render update event
  450. SendEvent(E_POSTRENDERUPDATE, eventData);
  451. }
  452. void Engine::Render()
  453. {
  454. if (headless_)
  455. return;
  456. PROFILE(Render);
  457. // If device is lost, BeginFrame will fail and we skip rendering
  458. Graphics* graphics = GetSubsystem<Graphics>();
  459. if (!graphics->BeginFrame())
  460. return;
  461. GetSubsystem<Renderer>()->Render();
  462. GetSubsystem<UI>()->Render();
  463. graphics->EndFrame();
  464. }
  465. void Engine::ApplyFrameLimit()
  466. {
  467. if (!initialized_)
  468. return;
  469. int maxFps = maxFps_;
  470. Input* input = GetSubsystem<Input>();
  471. if (input && !input->HasFocus())
  472. maxFps = Min(maxInactiveFps_, maxFps);
  473. long long elapsed = 0;
  474. // Perform waiting loop if maximum FPS set
  475. if (maxFps)
  476. {
  477. PROFILE(ApplyFrameLimit);
  478. long long targetMax = 1000000LL / maxFps;
  479. for (;;)
  480. {
  481. elapsed = frameTimer_.GetUSec(false);
  482. if (elapsed >= targetMax)
  483. break;
  484. // Sleep if 1 ms or more off the frame limiting goal
  485. if (targetMax - elapsed >= 1000LL)
  486. {
  487. unsigned sleepTime = (unsigned)((targetMax - elapsed) / 1000LL);
  488. Time::Sleep(sleepTime);
  489. }
  490. }
  491. }
  492. elapsed = frameTimer_.GetUSec(true);
  493. // If FPS lower than minimum, clamp elapsed time
  494. if (minFps_)
  495. {
  496. long long targetMin = 1000000LL / minFps_;
  497. if (elapsed > targetMin)
  498. elapsed = targetMin;
  499. }
  500. // Perform timestep smoothing
  501. timeStep_ = 0.0f;
  502. lastTimeSteps_.Push(elapsed / 1000000.0f);
  503. if (lastTimeSteps_.Size() > timeStepSmoothing_)
  504. {
  505. // If the smoothing configuration was changed, ensure correct amount of samples
  506. lastTimeSteps_.Erase(0, lastTimeSteps_.Size() - timeStepSmoothing_);
  507. for (unsigned i = 0; i < lastTimeSteps_.Size(); ++i)
  508. timeStep_ += lastTimeSteps_[i];
  509. timeStep_ /= lastTimeSteps_.Size();
  510. }
  511. else
  512. timeStep_ = lastTimeSteps_.Back();
  513. }
  514. VariantMap Engine::ParseParameters(const Vector<String>& arguments)
  515. {
  516. VariantMap ret;
  517. for (unsigned i = 0; i < arguments.Size(); ++i)
  518. {
  519. if (arguments[i].Length() > 1 && arguments[i][0] == '-')
  520. {
  521. String argument = arguments[i].Substring(1).ToLower();
  522. String value = i + 1 < arguments.Size() ? arguments[i + 1] : String::EMPTY;
  523. if (argument == "headless")
  524. ret["Headless"] = true;
  525. else if (argument == "nolimit")
  526. ret["FrameLimiter"] = false;
  527. else if (argument == "nosound")
  528. ret["Sound"] = false;
  529. else if (argument == "noip")
  530. ret["SoundInterpolation"] = false;
  531. else if (argument == "mono")
  532. ret["SoundStereo"] = false;
  533. else if (argument == "prepass")
  534. ret["RenderPath"] = "RenderPaths/Prepass.xml";
  535. else if (argument == "deferred")
  536. ret["RenderPath"] = "RenderPaths/Deferred.xml";
  537. else if (argument == "noshadows")
  538. ret["Shadows"] = false;
  539. else if (argument == "lqshadows")
  540. ret["LowQualityShadows"] = true;
  541. else if (argument == "nothreads")
  542. ret["WorkerThreads"] = false;
  543. else if (argument == "sm2")
  544. ret["ForceSM2"] = true;
  545. else if (argument == "v")
  546. ret["VSync"] = true;
  547. else if (argument == "t")
  548. ret["TripleBuffer"] = true;
  549. else if (argument == "w")
  550. ret["FullScreen"] = false;
  551. else if (argument == "s")
  552. ret["WindowResizable"] = true;
  553. else if (argument == "q")
  554. ret["LogQuiet"] = true;
  555. else if (argument == "log" && !value.Empty())
  556. {
  557. int logLevel = GetStringListIndex(value.CString(), logLevelPrefixes, -1);
  558. if (logLevel != -1)
  559. {
  560. ret["LogLevel"] = logLevel;
  561. ++i;
  562. }
  563. }
  564. else if (argument == "x" && !value.Empty())
  565. {
  566. ret["WindowWidth"] = ToInt(value);
  567. ++i;
  568. }
  569. else if (argument == "y" && !value.Empty())
  570. {
  571. ret["WindowHeight"] = ToInt(value);
  572. ++i;
  573. }
  574. else if (argument == "m" && !value.Empty())
  575. {
  576. ret["MultiSample"] = ToInt(value);
  577. ++i;
  578. }
  579. else if (argument == "b" && !value.Empty())
  580. {
  581. ret["SoundBuffer"] = ToInt(value);
  582. ++i;
  583. }
  584. else if (argument == "r" && !value.Empty())
  585. {
  586. ret["SoundMixRate"] = ToInt(value);
  587. ++i;
  588. }
  589. else if (argument == "p" && !value.Empty())
  590. {
  591. ret["ResourcePaths"] = value;
  592. ++i;
  593. }
  594. }
  595. }
  596. return ret;
  597. }
  598. bool Engine::HasParameter(const VariantMap& parameters, const String& parameter)
  599. {
  600. ShortStringHash nameHash(parameter);
  601. return parameters.Find(nameHash) != parameters.End();
  602. }
  603. const Variant& Engine::GetParameter(const VariantMap& parameters, const String& parameter, const Variant& defaultValue)
  604. {
  605. ShortStringHash nameHash(parameter);
  606. VariantMap::ConstIterator i = parameters.Find(nameHash);
  607. return i != parameters.End() ? i->second_ : defaultValue;
  608. }
  609. void Engine::HandleExitRequested(StringHash eventType, VariantMap& eventData)
  610. {
  611. if (autoExit_)
  612. {
  613. // Do not call Exit() here, as it contains mobile platform -specific tests to not exit.
  614. // If we do receive an exit request from the system on those platforms, we must comply
  615. DoExit();
  616. }
  617. }
  618. void Engine::DoExit()
  619. {
  620. Graphics* graphics = GetSubsystem<Graphics>();
  621. if (graphics)
  622. graphics->Close();
  623. exiting_ = true;
  624. }
  625. }