Main.cpp 2.9 KB

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