Window.h 1.8 KB

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