Window.h 2.5 KB

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