Main.cpp 2.8 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/Keys.h"
  8. #include "client/input/MouseButtons.h"
  9. #include "client/Engine.h"
  10. #include "client/utils/Clock.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, Keys& keys, MouseButtons& buttons) {
  21. static WindowSize& cSize = size;
  22. static Framebuffers& cFramebuffers = framebuffers;
  23. static Keys& cKeys = keys;
  24. static MouseButtons& cButtons = buttons;
  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. cKeys.press(key);
  34. } else if(action == GLFW_RELEASE) {
  35. cKeys.release(key);
  36. }
  37. });
  38. w.setMouseButtonCallback([](GLFWwindow*, int button, int action, int) {
  39. if(action == GLFW_PRESS) {
  40. cButtons.press(button);
  41. } else if(action == GLFW_RELEASE) {
  42. cButtons.release(button);
  43. }
  44. });
  45. w.setCursorPosCallback([](GLFWwindow*, double x, double y) {
  46. cButtons.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. Keys keys;
  67. MouseButtons mButtons;
  68. initCallbacks(window, size, framebuffers, keys, mButtons);
  69. Ray ray;
  70. Camera camera(ray);
  71. Clock fps;
  72. Engine engine(shaders, framebuffers, keys, mButtons, camera, ray, fps, size);
  73. window.show();
  74. const u64 nanosPerTick = 50000000;
  75. u64 lag = 0;
  76. while(!window.shouldClose() && engine.isRunning()) {
  77. engine.renderTick((float) lag / nanosPerTick);
  78. window.swapBuffers();
  79. lag += fps.update();
  80. while(lag >= nanosPerTick) {
  81. lag -= nanosPerTick;
  82. keys.tick();
  83. mButtons.tick();
  84. engine.tick();
  85. mButtons.postTick();
  86. }
  87. glfwPollEvents();
  88. }
  89. return 0;
  90. }