| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,166 @@ |
| 1 |
+/// Guards |
|
| 2 |
+ |
|
| 3 |
+#ifdef GLBACKEND_SDL |
|
| 4 |
+#ifndef GLBACKEND_SDL_HPP_ |
|
| 5 |
+#define GLBACKEND_SDL_HPP_ |
|
| 6 |
+ |
|
| 7 |
+ |
|
| 8 |
+/// Includes |
|
| 9 |
+ |
|
| 10 |
+#include <array> |
|
| 11 |
+#include <string> |
|
| 12 |
+ |
|
| 13 |
+#include <glbackend.hpp> |
|
| 14 |
+ |
|
| 15 |
+// #include <SDL2/SDL.h> |
|
| 16 |
+#include <SDL2/SDL_keyboard.h> |
|
| 17 |
+#include <SDL2/SDL_mouse.h> |
|
| 18 |
+#include <SDL2/SDL_scancode.h> |
|
| 19 |
+#include <SDL2/SDL_stdinc.h> |
|
| 20 |
+#include <SDL2/SDL_timer.h> |
|
| 21 |
+#include <SDL2/SDL_video.h> |
|
| 22 |
+ |
|
| 23 |
+ |
|
| 24 |
+/// Class definition |
|
| 25 |
+ |
|
| 26 |
+class GLBackendSDL final : public GLBackend |
|
| 27 |
+{
|
|
| 28 |
+ |
|
| 29 |
+public: |
|
| 30 |
+ |
|
| 31 |
+ //// Special member functions |
|
| 32 |
+ |
|
| 33 |
+ explicit GLBackendSDL( |
|
| 34 |
+ std::string const & title, |
|
| 35 |
+ std::array<int, 2> size = {0, 0},
|
|
| 36 |
+ std::array<int, 2> version = {0, 0},
|
|
| 37 |
+ int samples = 0, |
|
| 38 |
+ bool fullscreen = false, |
|
| 39 |
+ bool transparent = false |
|
| 40 |
+ ); |
|
| 41 |
+ ~GLBackendSDL(); |
|
| 42 |
+ GLBackendSDL(GLBackendSDL &&) noexcept; |
|
| 43 |
+ GLBackendSDL(GLBackendSDL const &) = delete; |
|
| 44 |
+ GLBackendSDL & operator=(GLBackendSDL &&) = delete; |
|
| 45 |
+ GLBackendSDL & operator=(GLBackendSDL const &) = delete; |
|
| 46 |
+ |
|
| 47 |
+ //// Context |
|
| 48 |
+ |
|
| 49 |
+ void current() override; |
|
| 50 |
+ |
|
| 51 |
+ //// Render loop |
|
| 52 |
+ |
|
| 53 |
+ void swap() override; |
|
| 54 |
+ |
|
| 55 |
+ void events() override; |
|
| 56 |
+ |
|
| 57 |
+ bool running() const override; |
|
| 58 |
+ bool running(bool running) override; |
|
| 59 |
+ |
|
| 60 |
+ float time() const override; |
|
| 61 |
+ float time(float time) override; |
|
| 62 |
+ |
|
| 63 |
+ //// Input and output |
|
| 64 |
+ |
|
| 65 |
+ void lock(bool lock) override; |
|
| 66 |
+ |
|
| 67 |
+ bool key (std::string const & key) const override; |
|
| 68 |
+ bool button(int button) const override; |
|
| 69 |
+ |
|
| 70 |
+ //// Debug |
|
| 71 |
+ |
|
| 72 |
+ std::string debug_info() const override; |
|
| 73 |
+ |
|
| 74 |
+protected: |
|
| 75 |
+ |
|
| 76 |
+ //// Special member functions |
|
| 77 |
+ |
|
| 78 |
+ void destroy_(); |
|
| 79 |
+ |
|
| 80 |
+protected: |
|
| 81 |
+ |
|
| 82 |
+ //// Context |
|
| 83 |
+ |
|
| 84 |
+ SDL_Window * window_; |
|
| 85 |
+ SDL_GLContext context_; |
|
| 86 |
+ |
|
| 87 |
+ //// Render loop |
|
| 88 |
+ |
|
| 89 |
+ bool running_; |
|
| 90 |
+ float time_; |
|
| 91 |
+ |
|
| 92 |
+}; |
|
| 93 |
+ |
|
| 94 |
+ |
|
| 95 |
+/// Inline definitions |
|
| 96 |
+ |
|
| 97 |
+inline void GLBackendSDL::current() |
|
| 98 |
+{
|
|
| 99 |
+ SDL_GL_MakeCurrent(window_, context_); |
|
| 100 |
+} |
|
| 101 |
+ |
|
| 102 |
+inline void GLBackendSDL::swap() |
|
| 103 |
+{
|
|
| 104 |
+ SDL_GL_SwapWindow(window_); |
|
| 105 |
+} |
|
| 106 |
+ |
|
| 107 |
+inline bool GLBackendSDL::running(bool running) |
|
| 108 |
+{
|
|
| 109 |
+ running_ = running; |
|
| 110 |
+ return running; |
|
| 111 |
+} |
|
| 112 |
+ |
|
| 113 |
+inline bool GLBackendSDL::running() const |
|
| 114 |
+{
|
|
| 115 |
+ return running_; |
|
| 116 |
+} |
|
| 117 |
+ |
|
| 118 |
+inline float GLBackendSDL::time() const |
|
| 119 |
+{
|
|
| 120 |
+ // return (float)SDL_GetTicks() / 1000.0F - time_; |
|
| 121 |
+ return |
|
| 122 |
+ (float)SDL_GetPerformanceCounter() / |
|
| 123 |
+ (float)SDL_GetPerformanceFrequency() |
|
| 124 |
+ - time_; |
|
| 125 |
+} |
|
| 126 |
+ |
|
| 127 |
+inline float GLBackendSDL::time(float time) |
|
| 128 |
+{
|
|
| 129 |
+ // time_ = (float)SDL_GetTicks() / 1000.0F - time; |
|
| 130 |
+ time_ = |
|
| 131 |
+ (float)SDL_GetPerformanceCounter() / |
|
| 132 |
+ (float)SDL_GetPerformanceFrequency() |
|
| 133 |
+ - time; |
|
| 134 |
+ return time; |
|
| 135 |
+} |
|
| 136 |
+ |
|
| 137 |
+inline void GLBackendSDL::lock(bool lock) |
|
| 138 |
+{
|
|
| 139 |
+ SDL_SetRelativeMouseMode(lock ? SDL_TRUE : SDL_FALSE); |
|
| 140 |
+} |
|
| 141 |
+ |
|
| 142 |
+inline bool GLBackendSDL::key(std::string const & key) const |
|
| 143 |
+{
|
|
| 144 |
+ auto * keys = SDL_GetKeyboardState(nullptr); |
|
| 145 |
+ if (key == "Enter") |
|
| 146 |
+ return keys[SDL_SCANCODE_RETURN]; |
|
| 147 |
+ else if (key == "Control") |
|
| 148 |
+ return keys[SDL_SCANCODE_RCTRL] || keys[SDL_SCANCODE_LCTRL]; |
|
| 149 |
+ else if (key == "Shift") |
|
| 150 |
+ return keys[SDL_SCANCODE_RSHIFT] || keys[SDL_SCANCODE_LSHIFT]; |
|
| 151 |
+ else if (key == "Alt") |
|
| 152 |
+ return keys[SDL_SCANCODE_RALT] || keys[SDL_SCANCODE_LALT]; |
|
| 153 |
+ else |
|
| 154 |
+ return keys[SDL_GetScancodeFromName(key.c_str())]; |
|
| 155 |
+} |
|
| 156 |
+ |
|
| 157 |
+inline bool GLBackendSDL::button(int button) const |
|
| 158 |
+{
|
|
| 159 |
+ return SDL_GetMouseState(nullptr, nullptr) & (Uint32)SDL_BUTTON(button); |
|
| 160 |
+} |
|
| 161 |
+ |
|
| 162 |
+ |
|
| 163 |
+/// Guards |
|
| 164 |
+ |
|
| 165 |
+#endif |
|
| 166 |
+#endif |