Window.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #ifndef WINDOW_H
  2. #define WINDOW_H
  3. #define GLFW_INCLUDE_NONE
  4. #include <GLFW/glfw3.h>
  5. #include "input/Buttons.h"
  6. #include "input/TextInput.h"
  7. #include "rendering/WindowOptions.h"
  8. #include "utils/Clock.h"
  9. #include "utils/Error.h"
  10. class Window final {
  11. GLFWwindow* window;
  12. Clock fps;
  13. Clock tps;
  14. Size size;
  15. public:
  16. TextInput* textInput;
  17. Buttons buttons;
  18. Window();
  19. ~Window();
  20. Window(const Window&) = delete;
  21. Window& operator=(const Window&) = delete;
  22. Window(Window&&) = delete;
  23. Window& operator=(Window&&) = delete;
  24. Error open(const WindowOptions& options);
  25. const Clock& getFrameClock() const;
  26. const Clock& getTickClock() const;
  27. const Size& getSize() const;
  28. void trapCursor(bool trap);
  29. template<typename T>
  30. void run(T& game, Clock::Nanos nanosPerTick) {
  31. glfwShowWindow(window);
  32. Clock::Nanos lag = 0;
  33. while(!glfwWindowShouldClose(window) && game.isRunning()) {
  34. lag += fps.update();
  35. while(lag >= nanosPerTick) {
  36. lag -= nanosPerTick;
  37. tps.update();
  38. buttons.tick();
  39. game.tick();
  40. }
  41. game.render(static_cast<float>(lag) / nanosPerTick);
  42. glfwSwapBuffers(window);
  43. glfwPollEvents();
  44. }
  45. }
  46. private:
  47. static void onKey(GLFWwindow* w, int key, int scancode, int action,
  48. int mods);
  49. static void onChar(GLFWwindow* w, unsigned int codepoint);
  50. static void onResize(GLFWwindow* w, int width, int height);
  51. static void onMouse(GLFWwindow* w, int button, int action, int mods);
  52. static void onMouseMove(GLFWwindow* w, double x, double y);
  53. };
  54. #endif