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 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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("OpenGL");
  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. //depth buffer stuff
  58. glGenTextures(1, &mColor);
  59. glBindTexture(GL_TEXTURE_2D, mColor);
  60. glTexStorage2D(GL_TEXTURE_2D, 1, GL_SRGB8_ALPHA8, conf->getValue<int>("graphics.res.x", 800), conf->getValue<int>("graphics.res.y", 600));
  61. glBindTexture(GL_TEXTURE_2D, 0);
  62. glGenTextures(1, &mDepth);
  63. glBindTexture(GL_TEXTURE_2D, mDepth);
  64. glTexStorage2D(GL_TEXTURE_2D, 1, GL_DEPTH_COMPONENT32F, conf->getValue<int>("graphics.res.x", 800), conf->getValue<int>("graphics.res.y", 600));
  65. glBindTexture(GL_TEXTURE_2D, 0);
  66. glGenFramebuffers(1, &mFBO);
  67. glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
  68. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, mColor, 0);
  69. glFramebufferTexture2D(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, mDepth, 0);
  70. GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
  71. if (status != GL_FRAMEBUFFER_COMPLETE) {
  72. fprintf(stderr, "glCheckFramebufferStatus: %x\n", status);
  73. }
  74. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  75. running = false;
  76. gls::setTracking(true); // track OpenGL state changes
  77. }
  78. void Game::start() {
  79. if (activeScene == nullptr) return;
  80. running = true;
  81. loop();
  82. }
  83. void Game::loop() {
  84. std::chrono::steady_clock::time_point lastFrameTime = std::chrono::steady_clock::now();
  85. while (running) {
  86. //time
  87. std::chrono::steady_clock::time_point curTime = std::chrono::steady_clock::now();
  88. auto timeSpan = std::chrono::duration_cast<std::chrono::duration<float>>(curTime - lastFrameTime);
  89. lastFrameTime = curTime;
  90. float dTime = timeSpan.count();
  91. activeScene->update(dTime);
  92. glBindFramebuffer(GL_FRAMEBUFFER, mFBO);
  93. glDepthFunc(GL_GREATER);
  94. glClearDepth(0.0f);
  95. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  96. activeScene->draw(); //draw the scene
  97. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  98. glBindFramebuffer(GL_READ_FRAMEBUFFER, mFBO);
  99. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // default FBO
  100. glBlitFramebuffer(
  101. 0, 0, conf->getValue<int>("graphics.res.x", 800), conf->getValue<int>("graphics.res.y", 600),
  102. 0, 0, conf->getValue<int>("graphics.res.x", 800), conf->getValue<int>("graphics.res.y", 600),
  103. GL_COLOR_BUFFER_BIT, GL_LINEAR);
  104. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  105. SDL_GL_SwapWindow(window.getWindow());
  106. }
  107. cleanup();
  108. }
  109. void Game::stop() {
  110. running = false;
  111. }
  112. void Game::cleanup() {
  113. SDL_Quit();
  114. }
  115. void Game::setScene(Scene* scene) {
  116. activeScene = scene;
  117. scene->link(this);
  118. scene->init();
  119. }
  120. void Game::setConfig(Config* conf) {
  121. this->conf = conf;
  122. }
  123. Config* Game::getConfig() {
  124. return conf;
  125. }
  126. };