Window.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #ifndef WINDOW_H
  2. #define WINDOW_H
  3. #include "math/Vector.h"
  4. #include "utils/Clock.h"
  5. #include "utils/Error.h"
  6. #include "utils/List.h"
  7. #include "utils/StringBuffer.h"
  8. namespace Window {
  9. typedef bool (*ShouldRun)();
  10. typedef void (*Tick)();
  11. typedef void (*Render)(float);
  12. struct Options final {
  13. int majorVersion;
  14. int minorVersion;
  15. const IntVector2& size;
  16. bool fullscreen;
  17. bool es;
  18. bool vsync;
  19. const char* name;
  20. Options(int majorVersion, int minorVersion, const IntVector2& size,
  21. bool es, const char* name);
  22. };
  23. Error open(const Options& options);
  24. void close();
  25. float getTicksPerSecond();
  26. float getFramesPerSecond();
  27. void trapCursor();
  28. void freeCursor();
  29. const IntVector2& getSize();
  30. bool hasSizeChanged();
  31. void show();
  32. bool shouldClose();
  33. Clock::Nanos startFrame();
  34. void endFrame();
  35. void tick();
  36. template<ShouldRun SR, Tick T, Render R>
  37. void run(Clock::Nanos nanosPerTick) {
  38. Clock::Nanos lag = 0;
  39. while(SR()) {
  40. lag += startFrame();
  41. while(lag >= nanosPerTick) {
  42. lag -= nanosPerTick;
  43. tick();
  44. T();
  45. }
  46. R(static_cast<float>(lag) / nanosPerTick);
  47. endFrame();
  48. }
  49. }
  50. namespace Input {
  51. void setLimit(int limit);
  52. void reset();
  53. void enable();
  54. void disable();
  55. void fill(const char* s);
  56. int getCursor();
  57. void setCursor(int index);
  58. const List<uint32>& getUnicode();
  59. template<int N>
  60. void toString(StringBuffer<N>& s) {
  61. for(uint32 c : getUnicode()) {
  62. s.appendUnicode(c);
  63. }
  64. }
  65. }
  66. namespace Controls {
  67. typedef StringBuffer<32> ButtonName;
  68. typedef int ButtonId;
  69. ButtonId add(const ButtonName& name);
  70. void bindKey(ButtonId id, int key);
  71. void bindGamepad(ButtonId id, int gamepadButton);
  72. void bindMouse(ButtonId id, int mouseButton);
  73. Vector2 getLastMousePosition();
  74. Vector2 getMousePosition();
  75. Vector2 getLeftGamepadAxis();
  76. Vector2 getRightGamepadAxis();
  77. float getLeftGamepadTrigger();
  78. float getRightGamepadTrigger();
  79. bool isDown(ButtonId id);
  80. int getDownTime(ButtonId id);
  81. bool wasReleased(ButtonId id);
  82. const ButtonName& getName(ButtonId id);
  83. }
  84. }
  85. #endif