Game.cpp 5.3 KB

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