You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

game.cpp 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * =====================================================================================
  3. *
  4. * Filename: game.cpp
  5. *
  6. * Description: Game class source
  7. *
  8. * Version: 1.0
  9. * Created: 03/23/2014 11:33:25 PM
  10. * Revision: none
  11. * Compiler: gcc
  12. *
  13. * Author: YOUR NAME (),
  14. * Organization:
  15. *
  16. * =====================================================================================
  17. */
  18. #include "game.h"
  19. #include "loadShader.h"
  20. #include "graphics/camera.h"
  21. #include "graphics/glstate.h"
  22. #include "scene.h"
  23. //SPAGHETTI INCLUDES
  24. #define GLM_FORCE_RADIANS
  25. #include <glm/glm.hpp>
  26. #include <glm/gtc/matrix_transform.hpp>
  27. #include <glm/gtc/type_ptr.hpp>
  28. #include <chrono>
  29. namespace vtk {
  30. Game::Game() {
  31. activeScene = nullptr;
  32. }
  33. void Game::init() {
  34. SDL_Init(SDL_INIT_VIDEO);
  35. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  36. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  37. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
  38. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  39. SDL_GL_SetSwapInterval(conf->getValue<int>("graphics.vsync", 0));
  40. window.setTitle("VTK");
  41. window.setResolution(conf->getValue<int>("graphics.res.x", 800),
  42. conf->getValue<int>("graphics.res.y", 600));
  43. window.setFOV(conf->getValue<float>("graphics.fov", 45.0f));
  44. window.create();
  45. glewExperimental = GL_TRUE;
  46. glewInit();
  47. //OpenGL settings
  48. glEnable(GL_DEPTH_TEST);
  49. glDepthFunc(GL_LESS);
  50. glEnable(GL_CULL_FACE);
  51. glCullFace(GL_BACK);
  52. glFrontFace(GL_CW);
  53. //needed for inverted depth buffer for long distance rendering
  54. glDepthFunc(GL_GREATER);
  55. glClearDepth(0.0f);
  56. glClipControl(GL_LOWER_LEFT, GL_ZERO_TO_ONE);
  57. running = false;
  58. gls::setTracking(true); // track OpenGL state changes
  59. }
  60. void Game::start() {
  61. if (activeScene == nullptr) return;
  62. running = true;
  63. loop();
  64. }
  65. void Game::loop() {
  66. std::chrono::steady_clock::time_point lastFrameTime = std::chrono::steady_clock::now();
  67. while (running) {
  68. //time
  69. std::chrono::steady_clock::time_point curTime = std::chrono::steady_clock::now();
  70. auto timeSpan = std::chrono::duration_cast<std::chrono::duration<float>>(curTime - lastFrameTime);
  71. lastFrameTime = curTime;
  72. float dTime = timeSpan.count();
  73. activeScene->update(dTime);
  74. //glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
  75. glDepthFunc(GL_GREATER);
  76. glClearDepth(0.0f);
  77. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  78. activeScene->draw(); //draw the scene
  79. SDL_GL_SwapWindow(window.getWindow());
  80. }
  81. cleanup();
  82. }
  83. void Game::stop() {
  84. running = false;
  85. }
  86. void Game::cleanup() {
  87. SDL_Quit();
  88. }
  89. void Game::setScene(Scene* scene) {
  90. activeScene = scene;
  91. scene->link(this);
  92. scene->init();
  93. }
  94. void Game::setConfig(Config* conf) {
  95. this->conf = conf;
  96. }
  97. Config* Game::getConfig() {
  98. return conf;
  99. }
  100. };