Window.h 2.2 KB

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