Window.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. Clock::Nanos startFrame();
  35. void endFrame();
  36. void tick();
  37. void postTick();
  38. template<ShouldRun SR, Tick T, Render R>
  39. void run(Clock::Nanos nanosPerTick) {
  40. Clock::Nanos lag = 0;
  41. while(SR()) {
  42. lag += startFrame();
  43. while(lag >= nanosPerTick) {
  44. lag -= nanosPerTick;
  45. tick();
  46. T();
  47. postTick();
  48. }
  49. R(static_cast<float>(lag) / nanosPerTick);
  50. endFrame();
  51. }
  52. }
  53. namespace Input {
  54. void setLimit(int limit);
  55. void reset();
  56. void enable();
  57. void disable();
  58. bool isEnabled();
  59. void fill(const char* s);
  60. int getCursor();
  61. void setCursor(int index);
  62. const List<uint32>& getUnicode();
  63. template<int N>
  64. void toString(StringBuffer<N>& s) {
  65. for(uint32 c : getUnicode()) {
  66. s.appendUnicode(c);
  67. }
  68. }
  69. }
  70. namespace Controls {
  71. typedef StringBuffer<32> ButtonName;
  72. typedef int ButtonId;
  73. ButtonId add(const ButtonName& name);
  74. void bindKey(ButtonId id, int key);
  75. void bindGamepad(ButtonId id, int gamepadButton);
  76. void bindMouse(ButtonId id, int mouseButton);
  77. Vector2 getLastMousePosition();
  78. Vector2 getMousePosition();
  79. Vector2 getLeftGamepadAxis();
  80. Vector2 getRightGamepadAxis();
  81. float getLeftGamepadTrigger();
  82. float getRightGamepadTrigger();
  83. bool isDown(ButtonId id);
  84. int getDownTime(ButtonId id);
  85. bool wasReleased(ButtonId id);
  86. const ButtonName& getName(ButtonId id);
  87. }
  88. }
  89. #endif