Window.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #ifndef WINDOW_H
  2. #define WINDOW_H
  3. #include "data/List.h"
  4. #include "math/Vector.h"
  5. #include "utils/Clock.h"
  6. #include "utils/Error.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. bool isCursorTrapped();
  30. const IntVector2& getSize();
  31. bool hasSizeChanged();
  32. void show();
  33. bool shouldClose();
  34. Error startFrame(Clock::Nanos& n);
  35. void endFrame();
  36. Error tick();
  37. void postTick();
  38. template<ShouldRun SR, Tick T, Render R>
  39. Error run(Clock::Nanos nanosPerTick) {
  40. Clock::Nanos lag = 0;
  41. while(SR()) {
  42. Clock::Nanos passedTime = 0;
  43. Error e = startFrame(passedTime);
  44. if(e.has()) {
  45. return e;
  46. }
  47. lag += passedTime;
  48. while(lag >= nanosPerTick) {
  49. lag -= nanosPerTick;
  50. e = tick();
  51. if(e.has()) {
  52. return e;
  53. }
  54. T();
  55. postTick();
  56. }
  57. R(static_cast<float>(lag) / static_cast<float>(nanosPerTick));
  58. endFrame();
  59. }
  60. return Error();
  61. }
  62. namespace Input {
  63. void setLimit(int limit);
  64. void reset();
  65. void enable();
  66. void disable();
  67. bool isEnabled();
  68. void fill(const char* s);
  69. int getCursor();
  70. void setCursor(int index);
  71. const List<uint32>& getUnicode();
  72. template<int N>
  73. void toString(StringBuffer<N>& s) {
  74. for(uint32 c : getUnicode()) {
  75. s.appendUnicode(c);
  76. }
  77. }
  78. }
  79. namespace Controls {
  80. typedef StringBuffer<32> ButtonName;
  81. typedef int ButtonId;
  82. ButtonId add(const ButtonName& name);
  83. void bindKey(ButtonId id, int key);
  84. void bindGamepad(ButtonId id, int gamepadButton);
  85. void bindMouse(ButtonId id, int mouseButton);
  86. Vector2 getLastMousePosition();
  87. Vector2 getMousePosition();
  88. Vector2 getLeftGamepadAxis();
  89. Vector2 getRightGamepadAxis();
  90. float getLeftGamepadTrigger();
  91. float getRightGamepadTrigger();
  92. bool isDown(ButtonId id);
  93. int getDownTime(ButtonId id);
  94. bool wasReleased(ButtonId id);
  95. const ButtonName& getName(ButtonId id);
  96. }
  97. }
  98. #endif