Game.cpp 4.8 KB

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