Main.cpp 2.7 KB

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