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

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