Main.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. 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. RenderSettings renderSettings;
  66. Engine engine(shaders, framebuffers, size, renderSettings);
  67. Control control;
  68. Clock fps;
  69. Clock tps;
  70. static Game game(control, fps, tps, renderSettings);
  71. initCallbacks(window, size, framebuffers, control);
  72. window.show();
  73. const u64 nanosPerTick = 50000000;
  74. u64 lag = 0;
  75. while(!window.shouldClose() && game.isRunning()) {
  76. engine.renderTick((float) lag / nanosPerTick, game);
  77. window.swapBuffers();
  78. lag += fps.update();
  79. while(lag >= nanosPerTick) {
  80. lag -= nanosPerTick;
  81. tps.update();
  82. control.tick();
  83. game.tick();
  84. }
  85. glfwPollEvents();
  86. }
  87. return 0;
  88. }