Main.cpp 2.8 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/Keys.h"
  8. #include "client/input/MouseButtons.h"
  9. #include "client/Engine.h"
  10. bool initGLEW() {
  11. GLenum err = glewInit();
  12. if(err != GLEW_OK) {
  13. std::cout << "could not initialize GLEW: " << glewGetErrorString(err) << "\n";
  14. return true;
  15. }
  16. std::cout << "using GLEW " << glewGetString(GLEW_VERSION) << "\n";
  17. return false;
  18. }
  19. void initCallbacks(Window& w, WindowSize& size, Framebuffers& framebuffers, Keys& keys, MouseButtons& buttons) {
  20. static WindowSize& cSize = size;
  21. static Framebuffers& cFramebuffers = framebuffers;
  22. static Keys& cKeys = keys;
  23. static MouseButtons& cButtons = buttons;
  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. cKeys.press(key);
  33. } else if(action == GLFW_RELEASE) {
  34. cKeys.release(key);
  35. }
  36. });
  37. w.setMouseButtonCallback([](GLFWwindow*, int button, int action, int) {
  38. if(action == GLFW_PRESS) {
  39. cButtons.press(button);
  40. } else if(action == GLFW_RELEASE) {
  41. cButtons.release(button);
  42. }
  43. });
  44. w.setCursorPosCallback([](GLFWwindow*, double x, double y) {
  45. cButtons.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. Keys keys;
  66. MouseButtons mButtons;
  67. initCallbacks(window, size, framebuffers, keys, mButtons);
  68. Engine engine(shaders, framebuffers, keys, mButtons, size);
  69. window.show();
  70. const u64 nanosPerTick = 50000000;
  71. u64 lastTime = GLFWWrapper::getTimeNanos();
  72. u64 lag = 0;
  73. while(!window.shouldClose() && engine.isRunning()) {
  74. engine.renderTick((float) lag / nanosPerTick);
  75. window.swapBuffers();
  76. u64 newTime = GLFWWrapper::getTimeNanos();
  77. lag += newTime - lastTime;
  78. lastTime = newTime;
  79. while(lag >= nanosPerTick) {
  80. lag -= nanosPerTick;
  81. keys.tick();
  82. mButtons.tick();
  83. engine.tick();
  84. mButtons.postTick();
  85. }
  86. glfwPollEvents();
  87. }
  88. return 0;
  89. }