Platformer in OpenGL
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

153 rindas
3.3 KiB

pirms 5 gadiem
  1. /**
  2. * Application.hpp
  3. * Contributors:
  4. * * Arthur Sonzogni (author)
  5. * Licence:
  6. * * MIT
  7. */
  8. #include "Application.hpp"
  9. #include <GL/glew.h>
  10. #include <GLFW/glfw3.h>
  11. #include <iostream>
  12. #include <stdexcept>
  13. using namespace std;
  14. Application* currentApplication = NULL;
  15. Application& Application::getInstance() {
  16. if (currentApplication)
  17. return *currentApplication;
  18. else
  19. throw std::runtime_error("There is no current Application");
  20. }
  21. Application::Application()
  22. : state(stateReady), width(640), height(480), title("Application") {
  23. currentApplication = this;
  24. cout << "[Info] GLFW initialisation" << endl;
  25. // initialize the GLFW library
  26. if (!glfwInit()) {
  27. throw std::runtime_error("Couldn't init GLFW");
  28. }
  29. // setting the opengl version
  30. int major = 3;
  31. int minor = 2;
  32. glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, major);
  33. glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, minor);
  34. glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
  35. glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
  36. // create the window
  37. window = glfwCreateWindow(width, height, title.c_str(), NULL, NULL);
  38. if (!window) {
  39. glfwTerminate();
  40. throw std::runtime_error("Couldn't create a window");
  41. }
  42. glfwMakeContextCurrent(window);
  43. glewExperimental = GL_TRUE;
  44. GLenum err = glewInit();
  45. if (err != GLEW_OK) {
  46. glfwTerminate();
  47. throw std::runtime_error(string("Could initialize GLEW, error = ") +
  48. (const char*)glewGetErrorString(err));
  49. }
  50. // get version info
  51. const GLubyte* renderer = glGetString(GL_RENDERER);
  52. const GLubyte* version = glGetString(GL_VERSION);
  53. cout << "Renderer: " << renderer << endl;
  54. cout << "OpenGL version supported " << version << endl;
  55. // opengl configuration
  56. glEnable(GL_DEPTH_TEST); // enable depth-testing
  57. glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
  58. // vsync
  59. // glfwSwapInterval(false);
  60. }
  61. GLFWwindow* Application::getWindow() const {
  62. return window;
  63. }
  64. void Application::exit() {
  65. state = stateExit;
  66. }
  67. float Application::getFrameDeltaTime() const {
  68. return deltaTime;
  69. }
  70. float Application::getTime() const {
  71. return time;
  72. }
  73. void Application::run() {
  74. state = stateRun;
  75. // Make the window's context current
  76. glfwMakeContextCurrent(window);
  77. time = glfwGetTime();
  78. while (state == stateRun) {
  79. // compute new time and delta time
  80. float t = glfwGetTime();
  81. deltaTime = t - time;
  82. time = t;
  83. // detech window related changes
  84. detectWindowDimensionChange();
  85. // execute the frame code
  86. loop();
  87. // Swap Front and Back buffers (double buffering)
  88. glfwSwapBuffers(window);
  89. // Pool and process events
  90. glfwPollEvents();
  91. }
  92. glfwTerminate();
  93. }
  94. void Application::detectWindowDimensionChange() {
  95. int w, h;
  96. glfwGetWindowSize(getWindow(), &w, &h);
  97. dimensionChanged = (w != width or h != height);
  98. if (dimensionChanged) {
  99. width = w;
  100. height = h;
  101. glViewport(0, 0, width, height);
  102. }
  103. }
  104. void Application::loop() {
  105. cout << "[INFO] : loop" << endl;
  106. }
  107. int Application::getWidth() {
  108. return width;
  109. }
  110. int Application::getHeight() {
  111. return height;
  112. }
  113. float Application::getWindowRatio() {
  114. return float(width) / float(height);
  115. }
  116. bool Application::windowDimensionChanged() {
  117. return dimensionChanged;
  118. }