Window.h 1.7 KB

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