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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. namespace vtk {
  29. Game::Game() {
  30. activeScene = nullptr;
  31. }
  32. void Game::init() {
  33. SDL_Init(SDL_INIT_VIDEO);
  34. SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
  35. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
  36. SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
  37. SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
  38. SDL_GL_SetSwapInterval(conf->getValue<int>("graphics.vsync", 0));
  39. window.setTitle("OpenGL");
  40. window.setResolution(conf->getValue<int>("graphics.res.x", 800),
  41. conf->getValue<int>("graphics.res.y", 600));
  42. window.setFOV(conf->getValue<float>("graphics.fov", 45.0f));
  43. window.create();
  44. glewExperimental = GL_TRUE;
  45. glewInit();
  46. //OpenGL settings
  47. glEnable(GL_DEPTH_TEST);
  48. glDepthFunc(GL_LESS);
  49. glEnable(GL_CULL_FACE);
  50. glCullFace(GL_BACK);
  51. glFrontFace(GL_CW);
  52. running = false;
  53. gls::setTracking(true);
  54. }
  55. void Game::start() {
  56. if (activeScene == nullptr) return;
  57. running = true;
  58. loop();
  59. }
  60. void Game::loop() {
  61. int lastFrameTime = SDL_GetTicks();
  62. while (running) {
  63. //time
  64. int dTimeMS = SDL_GetTicks() - lastFrameTime;
  65. lastFrameTime = SDL_GetTicks();
  66. float dTime = (float)dTimeMS / 1000.0f;
  67. activeScene->update(dTime);
  68. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  69. activeScene->draw();
  70. SDL_GL_SwapWindow(window.getWindow());
  71. }
  72. cleanup();
  73. }
  74. void Game::stop() {
  75. running = false;
  76. }
  77. void Game::cleanup() {
  78. SDL_Quit();
  79. }
  80. void Game::setScene(Scene* scene) {
  81. activeScene = scene;
  82. scene->link(this);
  83. scene->init();
  84. }
  85. void Game::setConfig(Config* conf) {
  86. this->conf = conf;
  87. }
  88. Config* Game::getConfig() {
  89. return conf;
  90. }
  91. };