Game.cpp 5.2 KB

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