Game.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 "Camera.h"
  24. #include "Engine.h"
  25. #include "Font.h"
  26. #include "Game.h"
  27. #include "Geometry.h"
  28. #include "IndexBuffer.h"
  29. #include "Input.h"
  30. #include "Log.h"
  31. #include "Model.h"
  32. #include "PackageFile.h"
  33. #include "Pipeline.h"
  34. #include "ProcessUtils.h"
  35. #include "Profiler.h"
  36. #include "Renderer.h"
  37. #include "ResourceCache.h"
  38. #include "Scene.h"
  39. #include "ScriptEngine.h"
  40. #include "ScriptFile.h"
  41. #include "StringUtils.h"
  42. #include "VertexBuffer.h"
  43. #include <angelscript.h>
  44. #include "DebugNew.h"
  45. Game::Game(const std::vector<std::string>& arguments) :
  46. mArguments(arguments)
  47. {
  48. std::string userDir = getUserDocumentsDirectory();
  49. std::string applicationDir = userDir + "ScriptTest";
  50. // Test the "allowed path" feature. Access outside the working directory, and these paths, should cause an exception
  51. registerDirectory("Data");
  52. registerDirectory(getSystemFontDirectory());
  53. registerDirectory(applicationDir);
  54. createDirectory(applicationDir);
  55. }
  56. Game::~Game()
  57. {
  58. // The scripts hold references to engine subsystems, not releasing them shows up as numerous memory leaks
  59. mScriptFile.reset();
  60. mCache->releaseResources(ShortStringHash("ScriptFile"), true);
  61. }
  62. void Game::run()
  63. {
  64. init();
  65. if (!mRunFrameFunction)
  66. return;
  67. while (!mEngine->isExiting())
  68. mScriptFile->execute(mRunFrameFunction);
  69. }
  70. void Game::init()
  71. {
  72. PROFILE(Game_Init);
  73. std::string logName = "ScriptTest.log";
  74. mEngine = new Engine(logName);
  75. // Add the resources as a package if available
  76. mCache = mEngine->getResourceCache();
  77. if (fileExists("Data.pak"))
  78. mCache->addPackageFile(new PackageFile("Data.pak"));
  79. // Force forward rendering
  80. mArguments.insert(mArguments.begin(), "-forward");
  81. mEngine->init("ScriptTest", mArguments);
  82. mEngine->createScriptEngine();
  83. mCache->addResourcePath(getSystemFontDirectory());
  84. Pipeline* pipeline = mEngine->getPipeline();
  85. for (unsigned i = 0; i < mArguments.size(); ++i)
  86. {
  87. if ((pipeline) && (toLower(mArguments[i]) == "-noshadows"))
  88. pipeline->setDrawShadows(false);
  89. if (toLower(mArguments[i]) == "-nolimit")
  90. mEngine->setMaxFps(0);
  91. }
  92. createSkyPlaneModel();
  93. // Execute the rest of initialization, including scene creation, in script
  94. mScriptFile = mCache->getResource<ScriptFile>("Scripts/NinjaSnowWar.as");
  95. mInitFunction = mScriptFile->getFunction("void init()");
  96. mRunFrameFunction = mScriptFile->getFunction("void runFrame()");
  97. mScriptFile->execute(mInitFunction);
  98. }
  99. void Game::createSkyPlaneModel()
  100. {
  101. const float skyplanevertices[] =
  102. {
  103. -1, 0, -1, 0, 0,
  104. 1, 0, -1, 2, 0,
  105. 1, 0, 1, 2, 2,
  106. -1, 0, 1, 0, 2
  107. };
  108. const unsigned short skyplaneindices[] =
  109. {
  110. 0, 1, 3,
  111. 1, 2, 3
  112. };
  113. Renderer* renderer = mEngine->getRenderer();
  114. SharedPtr<VertexBuffer> vb(new VertexBuffer(renderer));
  115. vb->setSize(4, MASK_POSITION | MASK_TEXCOORD1);
  116. vb->setData(skyplanevertices);
  117. SharedPtr<IndexBuffer> ib(new IndexBuffer(renderer));
  118. ib->setSize(6, false);
  119. ib->setData(skyplaneindices);
  120. SharedPtr<Geometry> geom(new Geometry());
  121. geom->setVertexBuffer(0, vb);
  122. geom->setIndexBuffer(ib);
  123. geom->setDrawRange(TRIANGLE_LIST, 0, ib->getIndexCount());
  124. SharedPtr<Model> model(new Model(renderer, "Models/CloudPlane.mdl"));
  125. model->setNumGeometries(1);
  126. model->setNumGeometryLodLevels(0, 1);
  127. model->setGeometry(0, 0, geom);
  128. model->setBoundingBox(BoundingBox(-1, 1));
  129. // Add the cloudplane model as a manual resource so it can be retrieved during savegame load
  130. mCache->addManualResource(model);
  131. }