Application.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Application.h"
  24. #include "Engine.h"
  25. #include "Exception.h"
  26. #include "Log.h"
  27. #include "PackageFile.h"
  28. #include "ProcessUtils.h"
  29. #include "ResourceCache.h"
  30. #include "Scene.h"
  31. #include "ScriptEngine.h"
  32. #include "ScriptFile.h"
  33. #include "StringUtils.h"
  34. #include "DebugNew.h"
  35. Application::Application()
  36. {
  37. }
  38. Application::~Application()
  39. {
  40. // The scripts possibly hold references to engine subsystems, not releasing them shows up as numerous memory leaks
  41. if (mEngine)
  42. {
  43. mScriptFile.reset();
  44. mCache->releaseResources(ShortStringHash("ScriptFile"), true);
  45. }
  46. }
  47. void Application::run()
  48. {
  49. // Create application directory
  50. std::string applicationDir = getUserDocumentsDirectory() + "Urho3D";
  51. createDirectory(applicationDir);
  52. // Parse scene or script file name from the command line
  53. const std::vector<std::string>& arguments = getArguments();
  54. std::string fileName;
  55. if ((arguments.size()) && (arguments[0][0] != '-'))
  56. fileName = replace(arguments[0], '\\', '/');
  57. if (fileName.empty())
  58. EXCEPTION("Usage: Urho3D <scriptfile | scenefile> [options]\n\n"
  59. "Either a script file or a scene file can be specified. The script file should implement the function void start(), "
  60. "which should in turn subscribe to all necessary events, such as the application update. If a scene is loaded, it "
  61. "should contain script objects to implement the application logic. Refer to the readme for the command line options.");
  62. // Instantiate the engine
  63. mEngine = new Engine();
  64. // Try to open the file before setting screen mode
  65. // Check first the resource cache
  66. SharedPtr<File> file;
  67. mCache = mEngine->getResourceCache();
  68. if (mCache->exists(fileName))
  69. file = mCache->getFile(fileName);
  70. // If not found, open using the full absolute filename, and add the path as a resource directory
  71. else
  72. {
  73. fileName = getAbsoluteFileName(fileName);
  74. file = new File(fileName);
  75. mCache->addResourcePath(getPreferredResourcePath(getPath(fileName)));
  76. }
  77. // Initialize engine & scripting. Render once first to avoid a white screen in case init takes a long time
  78. mEngine->createScriptEngine();
  79. mEngine->init(arguments);
  80. mEngine->render();
  81. // Script mode: execute the rest of initialization, including scene creation, in script
  82. std::string extension = getExtension(fileName);
  83. if ((extension != ".xml") && (extension != ".bin") && (extension != ".dat"))
  84. {
  85. mScriptFile = new ScriptFile(mEngine->getScriptEngine(), fileName);
  86. mScriptFile->load(*file, mCache);
  87. if (!mScriptFile->execute("void start()"))
  88. EXCEPTION("Failed to execute the start() function");
  89. }
  90. // Scene mode: create and load a scene, then let it run
  91. else
  92. {
  93. mScene = mEngine->createScene("Urho3D");
  94. if (extension == ".xml")
  95. mScene->loadXML(*file);
  96. else
  97. mScene->load(*file);
  98. }
  99. // Run until exited
  100. while (!mEngine->isExiting())
  101. mEngine->runFrame();
  102. }