Application.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 "Font.h"
  27. #include "Input.h"
  28. #include "Log.h"
  29. #include "PackageFile.h"
  30. #include "ProcessUtils.h"
  31. #include "ResourceCache.h"
  32. #include "Scene.h"
  33. #include "ScriptEngine.h"
  34. #include "ScriptFile.h"
  35. #include "StringUtils.h"
  36. #include "DebugNew.h"
  37. Application::Application()
  38. {
  39. }
  40. Application::~Application()
  41. {
  42. // The scripts possibly hold references to engine subsystems, not releasing them shows up as numerous memory leaks
  43. if (mEngine)
  44. {
  45. mScriptFile.reset();
  46. mCache->releaseResources(ShortStringHash("ScriptFile"), true);
  47. }
  48. }
  49. void Application::run()
  50. {
  51. // Create application directory
  52. std::string applicationDir = getUserDocumentsDirectory() + "Urho3D";
  53. createDirectory(applicationDir);
  54. // Parse scene or script file name from the command line
  55. const std::vector<std::string>& arguments = getArguments();
  56. std::string fileName;
  57. if ((arguments.size()) && (arguments[0][0] != '-'))
  58. fileName = replace(arguments[0], '\\', '/');
  59. if (fileName.empty())
  60. EXCEPTION("Usage: Urho3D <scriptfile | scenefile> [options]\n\n"
  61. "Either a script file or a scene file can be specified. The script file should implement the function void start(), "
  62. "which should in turn subscribe to all necessary events, such as the application update. If a scene is loaded, it "
  63. "should contain script objects to implement the application logic. Refer to the readme for the command line options.");
  64. // Instantiate the engine
  65. mEngine = new Engine();
  66. // Try to open the file before setting screen mode
  67. // Check first the resource cache
  68. SharedPtr<File> file;
  69. mCache = mEngine->getResourceCache();
  70. if (mCache->exists(fileName))
  71. file = mCache->getFile(fileName);
  72. // If not found, open using the full absolute filename, and add the path as a resource directory
  73. else
  74. {
  75. fileName = getAbsoluteFileName(fileName);
  76. file = new File(fileName);
  77. mCache->addResourcePath(getPath(fileName));
  78. }
  79. // Initialize engine & scripting
  80. mEngine->init(arguments);
  81. mEngine->createScriptEngine();
  82. // Script mode: execute the rest of initialization, including scene creation, in script
  83. std::string extension = getExtension(fileName);
  84. if ((extension != ".xml") && (extension != ".scn") && (extension != ".bin"))
  85. {
  86. mScriptFile = new ScriptFile(mEngine->getScriptEngine(), fileName);
  87. mScriptFile->load(*file, mCache);
  88. if (!mScriptFile->execute("void start()"))
  89. EXCEPTION("Failed to execute the start() function");
  90. }
  91. // Scene mode: create and load a scene, then let it run
  92. else
  93. {
  94. mScene = mEngine->createScene("Urho3D");
  95. if (extension == ".xml")
  96. mScene->loadXML(*file);
  97. else
  98. mScene->load(*file);
  99. }
  100. // Run until exited
  101. while (!mEngine->isExiting())
  102. mEngine->runFrame();
  103. }