Main.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #include <iostream>
  2. #include "client/rendering/wrapper/GLFWWrapper.h"
  3. #include "client/rendering/WindowSize.h"
  4. #include "client/rendering/wrapper/Window.h"
  5. #include "client/rendering/Shaders.h"
  6. #include "client/rendering/Framebuffers.h"
  7. #include "client/input/Control.h"
  8. #include "client/rendering/Engine.h"
  9. #include "client/utils/Clock.h"
  10. #include "client/rendering/RenderSettings.h"
  11. #include "rendering/wrapper/GLWrapper.h"
  12. bool initGLEW() {
  13. GLenum err = glewInit();
  14. if(err != GLEW_OK) {
  15. std::cout << "could not initialize GLEW: " << glewGetErrorString(err) << "\n";
  16. return true;
  17. }
  18. std::cout << "using GLEW " << glewGetString(GLEW_VERSION) << "\n";
  19. return false;
  20. }
  21. void initCallbacks(Window& w, WindowSize& size, Framebuffers& framebuffers, Control& control) {
  22. static WindowSize& cSize = size;
  23. static Framebuffers& cFramebuffers = framebuffers;
  24. static Control& cControl = control;
  25. w.setFramebufferSizeCallback([](GLFWwindow*, int newWidth, int newHeight) {
  26. glViewport(0, 0, newWidth, newHeight);
  27. cSize.width = newWidth;
  28. cSize.height = newHeight;
  29. cFramebuffers.resize(newWidth, newHeight);
  30. });
  31. w.setKeyCallback([](GLFWwindow*, int key, int, int action, int) {
  32. if(action == GLFW_PRESS) {
  33. cControl.keys.press(key);
  34. } else if(action == GLFW_RELEASE) {
  35. cControl.keys.release(key);
  36. }
  37. });
  38. w.setMouseButtonCallback([](GLFWwindow*, int button, int action, int) {
  39. if(action == GLFW_PRESS) {
  40. cControl.buttons.press(button);
  41. } else if(action == GLFW_RELEASE) {
  42. cControl.buttons.release(button);
  43. }
  44. });
  45. w.setCursorPosCallback([](GLFWwindow*, double x, double y) {
  46. cControl.buttons.move(x, y);
  47. });
  48. }
  49. int main() {
  50. if(GLFWWrapper::hasError()) {
  51. return 0;
  52. }
  53. WindowSize size(1024, 620);
  54. Window window(size, "Test");
  55. if(window.hasError() || initGLEW()) {
  56. return 0;
  57. }
  58. Shaders shaders;
  59. if(shaders.hasError()) {
  60. return 0;
  61. }
  62. Framebuffers framebuffers(size);
  63. if(framebuffers.hasError()) {
  64. return 0;
  65. }
  66. RenderSettings renderSettings;
  67. Engine engine(shaders, framebuffers, size, renderSettings);
  68. Control control;
  69. Clock fps;
  70. Clock tps;
  71. static Game game(control, fps, tps, renderSettings);
  72. initCallbacks(window, size, framebuffers, control);
  73. window.show();
  74. GLWrapper::checkAndPrintError("setup error");
  75. const u64 nanosPerTick = 50000000;
  76. u64 lag = 0;
  77. while(!window.shouldClose() && game.isRunning()) {
  78. GLWrapper::checkAndPrintError("loop error");
  79. engine.renderTick(static_cast<float> (lag) / nanosPerTick, game);
  80. window.swapBuffers();
  81. lag += fps.update();
  82. while(lag >= nanosPerTick) {
  83. lag -= nanosPerTick;
  84. tps.update();
  85. control.tick();
  86. game.tick();
  87. }
  88. glfwPollEvents();
  89. }
  90. return 0;
  91. }