Game.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. mPaused(false)
  48. {
  49. std::string userDir = getUserDocumentsDirectory();
  50. std::string applicationDir = userDir + "ScriptTest";
  51. // Test the "allowed path" feature. Access outside the working directory, and these paths, should cause an exception
  52. registerDirectory("Data");
  53. registerDirectory(getSystemFontDirectory());
  54. registerDirectory(applicationDir);
  55. createDirectory(applicationDir);
  56. }
  57. Game::~Game()
  58. {
  59. // The scripts hold references to engine subsystems, not releasing them shows up as numerous memory leaks
  60. mCache->releaseResources(ShortStringHash("ScriptFile"), true);
  61. }
  62. void Game::run()
  63. {
  64. init();
  65. while (!mEngine->isExiting())
  66. {
  67. Entity* cameraEntity = mScene->getEntity("Camera");
  68. Camera* camera = 0;
  69. if (cameraEntity)
  70. camera = cameraEntity->getComponent<Camera>();
  71. mEngine->runFrame(mScene, camera, !mPaused);
  72. Input* input = mEngine->getInput();
  73. if (input->getKeyPress(KEY_ESCAPE))
  74. mEngine->exit();
  75. }
  76. }
  77. void Game::init()
  78. {
  79. PROFILE(Game_Init);
  80. std::string logName = "ScriptTest.log";
  81. mEngine = new Engine(logName);
  82. // Add the resources as a package if available
  83. mCache = mEngine->getResourceCache();
  84. if (fileExists("Data.pak"))
  85. mCache->addPackageFile(new PackageFile("Data.pak"));
  86. // Force forward rendering
  87. mArguments.insert(mArguments.begin(), "-forward");
  88. mEngine->init("ScriptTest", mArguments);
  89. mEngine->createScriptEngine();
  90. // Register the pause variable for script access
  91. mEngine->getScriptEngine()->getAngelScriptEngine()->RegisterGlobalProperty("bool paused", &mPaused);
  92. mCache->addResourcePath(getSystemFontDirectory());
  93. Pipeline* pipeline = mEngine->getPipeline();
  94. for (unsigned i = 0; i < mArguments.size(); ++i)
  95. {
  96. if ((pipeline) && (toLower(mArguments[i]) == "-noshadows"))
  97. pipeline->setDrawShadows(false);
  98. if (toLower(mArguments[i]) == "-nolimit")
  99. mEngine->setMaxFps(0);
  100. }
  101. createSkyPlaneModel();
  102. mScene = mEngine->createScene("ScriptTest", BoundingBox(-100000.0f, 100000.f));
  103. // Execute the rest of initialization in script
  104. ScriptFile* script = mCache->getResource<ScriptFile>("Scripts/NinjaSnowWar.as");
  105. std::vector<Variant> arguments;
  106. arguments.push_back(Variant((void*)mScene.getPtr()));
  107. script->execute("void init(Scene@)", 0, arguments);
  108. }
  109. void Game::createSkyPlaneModel()
  110. {
  111. const float skyplanevertices[] =
  112. {
  113. -1, 0, -1, 0, 0,
  114. 1, 0, -1, 2, 0,
  115. 1, 0, 1, 2, 2,
  116. -1, 0, 1, 0, 2
  117. };
  118. const unsigned short skyplaneindices[] =
  119. {
  120. 0, 1, 3,
  121. 1, 2, 3
  122. };
  123. Renderer* renderer = mEngine->getRenderer();
  124. SharedPtr<VertexBuffer> vb(new VertexBuffer(renderer));
  125. vb->setSize(4, MASK_POSITION | MASK_TEXCOORD1);
  126. vb->setData(skyplanevertices);
  127. SharedPtr<IndexBuffer> ib(new IndexBuffer(renderer));
  128. ib->setSize(6, false);
  129. ib->setData(skyplaneindices);
  130. SharedPtr<Geometry> geom(new Geometry());
  131. geom->setVertexBuffer(0, vb);
  132. geom->setIndexBuffer(ib);
  133. geom->setDrawRange(TRIANGLE_LIST, 0, ib->getIndexCount());
  134. SharedPtr<Model> model(new Model(renderer, "Models/CloudPlane.mdl"));
  135. model->setNumGeometries(1);
  136. model->setNumGeometryLodLevels(0, 1);
  137. model->setGeometry(0, 0, geom);
  138. model->setBoundingBox(BoundingBox(-1, 1));
  139. // Add the cloudplane model as a manual resource so it can be retrieved during savegame load
  140. mCache->addManualResource(model);
  141. }