Simple game engine with complete export to scripting language
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

247 lignes
6.2KB

  1. #include "Application.h"
  2. #include "Utilities/CfgParser.h"
  3. #include "Utilities/Log.h"
  4. #include "ScriptSystem_Application.h"
  5. #include "ScriptSystem_Camera.h"
  6. #include "ScriptSystem_Font.h"
  7. #include "ScriptSystem_Image.h"
  8. #include "ScriptSystem_Math.h"
  9. #include "ScriptSystem_RigidBody.h"
  10. #include "ScriptSystem_SceneGraph.h"
  11. #include "ScriptSystem_SceneNode.h"
  12. #include "ScriptSystem_Model.h"
  13. namespace BlueCore
  14. {
  15. void Application::KeySlot(int key, int action)
  16. {
  17. if (key == GLFW_KEY_ESC && action == GLFW_RELEASE)
  18. quit();
  19. }
  20. void Application::MouseMoveSlot(int x, int y)
  21. {
  22. _CameraPhi += (x - _LastMouseX) / 200.0;
  23. _LastMouseX = x;
  24. _CameraTheta += (y - _LastMouseY) / 200.0;
  25. _LastMouseY = y;
  26. }
  27. void Application::MouseWheelSlot(int z)
  28. {
  29. if ((z - _LastWheel) < 0)
  30. _CameraRadius *= 2.0;
  31. else
  32. _CameraRadius *= 0.5;
  33. if (_CameraRadius < 1.0)
  34. _CameraRadius = 1.0;
  35. _LastWheel = z;
  36. }
  37. bool Application::initialize()
  38. {
  39. CfgParser cfg;
  40. cfg.parseFile("options.cfg");
  41. int width = cfg.get("width", 640);
  42. int height = cfg.get("height", 480);
  43. bool fullscreen = cfg.get("fullscreen", false);
  44. _Window = new RenderWindow();
  45. if (_Window->create(width, height, 0, 0, 0, fullscreen) == false)
  46. return false;
  47. _Device = new RenderDevice(_Window);
  48. _FontManager = new FontManager(_Device);
  49. _MeshManager = new MeshManager(_Device);
  50. _TextureManager = new TextureManager();
  51. _ScriptSystem = new ScriptSystem();
  52. _ShaderManager = new ShaderManager(_Window);
  53. _Simulation = new RigidBodySimulation(_ScriptSystem);
  54. _ModelManager = new ModelManager (_TextureManager, _ShaderManager, _MeshManager);
  55. _RenderQueue = new RenderQueue();
  56. setupScriptSystem_Application(_ScriptSystem, this);
  57. setupScriptSystem_Camera(_ScriptSystem);
  58. setupScriptSystem_Font(_ScriptSystem, _FontManager);
  59. setupScriptSystem_Image(_ScriptSystem, _TextureManager, _Device);
  60. setupScriptSystem_Math(_ScriptSystem);
  61. setupScriptSystem_RigidBody(_ScriptSystem, _Simulation);
  62. setupScriptSystem_SceneGraph(_ScriptSystem);
  63. setupScriptSystem_SceneNode(_ScriptSystem);
  64. setupScriptSystem_Model(_ScriptSystem, _ModelManager);
  65. if (_ScriptSystem->loadScript("main") == false)
  66. return false;
  67. _ScriptSystem->callFunction("Initialize");
  68. _Window->KeySignal.connect(this, &Application::KeySlot);
  69. _Window->MouseMoveSignal.connect(this, &Application::MouseMoveSlot);
  70. _Window->MouseWheelSignal.connect(this, &Application::MouseWheelSlot);
  71. _Running = true;
  72. return true;
  73. }
  74. void Application::shutdown()
  75. {
  76. _ScriptSystem->callFunction("Shutdown");
  77. }
  78. void Application::quit()
  79. {
  80. _Running = false;
  81. }
  82. void Application::run()
  83. {
  84. clog << "--- starting main loop..."<< endlog;
  85. _SimulationTime = 0.0;
  86. _StartTime = glfwGetTime();
  87. _LastTime = _StartTime;
  88. double delta = 0.0;
  89. _Paused = false;
  90. _Running = true;
  91. ref_ptr<Mesh> star_mesh = _MeshManager->loadMesh ("stars.3ds");
  92. ref_ptr<Texture> star_texture = _TextureManager->loadTexture ("stars.png", 0);
  93. GLfloat lightAmbient[] = {0.0f, 0.0f, 0.0f, 1.0f} ;
  94. GLfloat lightDiffuse[] = {1.0f, 1.0f, 1.0f, 1.0f} ;
  95. GLfloat lightSpecular[] = {1.0f, 1.0f, 1.0f, 1.0f} ;
  96. GLfloat lightPosition[] = { 1.0, 0.0, 0.0, 0.0};
  97. glLightfv ( GL_LIGHT0, GL_AMBIENT, lightAmbient ) ;
  98. glLightfv ( GL_LIGHT0, GL_DIFFUSE, lightDiffuse ) ;
  99. glLightfv ( GL_LIGHT0, GL_SPECULAR, lightSpecular ) ;
  100. glLightfv ( GL_LIGHT0, GL_POSITION, lightPosition );
  101. glEnable ( GL_LIGHT0 ) ;
  102. while (_Window->isOpen() && _Running)
  103. {
  104. double now = glfwGetTime();
  105. delta = now - _LastTime;
  106. _LastTime = now;
  107. _ScriptSystem->callFunction("OnFrame", delta, now - _StartTime);
  108. _Device->clear();
  109. //Quaternion q(-_CameraPhi, 0.0, -_CameraTheta);
  110. //camera->setPosition(q.apply(Vector3(0.0, 0.0, _CameraRadius)));
  111. //camera->setRotation(q);
  112. //pos += 5 * _DeltaTime;
  113. _Device->setAmbientLight(1.0, 1.0, 1.0);
  114. if (_SceneGraph.valid())
  115. {
  116. _RenderQueue->clear();
  117. _SceneGraph->update(delta);
  118. _SceneGraph->queue(_RenderQueue);
  119. _Device->begin3D(_SceneGraph->getCamera());
  120. _RenderQueue->render(_Device);
  121. _Device->end3D();
  122. }
  123. /*
  124. {
  125. _Device->begin3D(_Camera);
  126. _RenderQueue->clear();
  127. _RenderQueue->addOpaqueItem(model, Vector3(0.0, 0.0, 0.0),
  128. Quaternion());
  129. _RenderQueue->render(_Device);
  130. _Device->end3D();
  131. }
  132. */
  133. /*
  134. */
  135. // device->useShader (program);
  136. // device->setTexture (stage, name, texture)
  137. // device->clearTextures (stage+1);
  138. //glEnable (GL_TEXTURE_2D);
  139. //glEnable (GL_LIGHTING);
  140. //glBindTexture (GL_TEXTURE_2D, texture->getId() );
  141. // device->
  142. //device->
  143. //model->render();
  144. if (_Paused == false)
  145. {
  146. _Simulation->saveStates();
  147. _Simulation->updateSteps(delta);
  148. while (_Simulation->getSteps() > 0)
  149. {
  150. _ScriptSystem->callFunction("OnStep",
  151. _Simulation->getStepSize(), _SimulationTime);
  152. _SimulationTime += _Simulation->getStepSize();
  153. _Simulation->step();
  154. }
  155. }
  156. _Device->pushAbsoluteTransformation(Vector3(), _SceneGraph->getCamera()->getRotation());
  157. /*
  158. _Device->setShaderProgram (0);
  159. glEnable(GL_DEPTH_TEST);
  160. glDepthMask(GL_FALSE);
  161. */
  162. glDisable(GL_BLEND);
  163. glEnable(GL_DEPTH_TEST);
  164. _Device->setTexture(0, star_texture, true);
  165. glDisable(GL_LIGHTING);
  166. glColor4d( 1.0f, 1.0f, 1.0f, 1.0f);
  167. star_mesh->render();
  168. _Device->popTransformation();
  169. /*
  170. class RenderState
  171. {
  172. bool _Blending;
  173. GLint _BlendFuncSrc;
  174. GLint _BlendFuncDest;
  175. bool _DepthTest;
  176. bool _DepthMask;
  177. bool _Lighting;
  178. };
  179. GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
  180. GLfloat mat_shininess[] = { 2.0 };
  181. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular);
  182. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular);
  183. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  184. glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
  185. */
  186. _Device->begin2D();
  187. _ScriptSystem->callFunction("OnOverlay", delta, now - _StartTime);
  188. _Device->end2D();
  189. _Device->swap();
  190. }
  191. clog << "--- main loop finished..."<< endlog;
  192. }
  193. void Application::setSceneGraph(SceneGraph *graph)
  194. {
  195. _SceneGraph = graph;
  196. }
  197. void Application::togglePause()
  198. {
  199. _Paused = !_Paused;
  200. }
  201. }