20 #include <condition_variable>
47 SDL_Window * hwnd{
nullptr};
48 SDL_GLContext gl_ctx{};
52 Handle(
const std::string& title,
int x,
int y,
int w,
int h,
57 Handle(Handle&& other)
58 : hwnd(other.hwnd), gl_ctx(other.gl_ctx)
64 Handle& operator= (Handle&& other)
66 std::swap(hwnd, other.hwnd);
67 std::swap(gl_ctx, other.gl_ctx);
71 bool isInitialized()
const
73 return (hwnd !=
nullptr && gl_ctx != 0);
79 std::unique_ptr<gl3::MeshRenderer> renderer;
80 static const int high_dpi_threshold = 144;
85 bool high_dpi =
false;
92 float pixel_scale_x = 1.0f, pixel_scale_y = 1.0f;
99 std::map<int, KeyDelegate> onKeyDown;
100 std::map<int, MouseDelegate> onMouseDown;
101 std::map<int, MouseDelegate> onMouseUp;
102 std::map<int, MouseDelegate> onMouseMove;
106 bool ctrlDown{
false};
108 #ifdef __EMSCRIPTEN__
109 std::string canvas_id_;
112 enum class RenderState
124 bool update_before_expose{
false};
127 bool takeScreenshot{
false};
128 std::string screenshot_file;
129 bool screenshot_convert;
132 void windowEvent(SDL_WindowEvent& ew);
133 void motionEvent(SDL_MouseMotionEvent& em);
134 void mouseEventDown(SDL_MouseButtonEvent& eb);
135 void mouseEventUp(SDL_MouseButtonEvent& eb);
136 void keyEvent(SDL_Keysym& ks);
137 void keyEvent(
char c);
138 void multiGestureEvent(SDL_MultiGestureEvent & e);
140 bool is_multithreaded{
true};
142 bool call_idle_func{
false};
146 void queueEvents(std::vector<SDL_Event> events)
149 std::lock_guard<std::mutex> evt_guard{event_mutex};
150 waiting_events.insert(waiting_events.end(), events.begin(), events.end());
152 if (is_multithreaded)
154 events_available.notify_all();
158 std::string saved_keys;
160 std::condition_variable events_available;
161 std::mutex event_mutex;
163 std::deque<SDL_Event> waiting_events;
170 static void StartSDL(
bool server_mode);
174 bool createWindow(
const char * title,
int x,
int y,
int w,
int h,
190 onKeyDown[key] = [func](GLenum) { func(); };
222 void getDpi(
int& wdpi,
int& hdpi);
243 takeScreenshot =
true;
244 screenshot_file = filename;
245 screenshot_convert = convert;
253 operator bool() {
return handle.isInitialized(); }
261 #ifdef __EMSCRIPTEN__
263 void setCanvasId(std::string canvas_id) { canvas_id_ = canvas_id; }
void signalKeyDown(SDL_Keycode k, SDL_Keymod m=KMOD_NONE)
void(* WindowDelegate)(int, int)
void getGLDrawSize(int &w, int &h)
bool isGlInitialized()
Returns true if the OpenGL context was successfully initialized.
void mainLoop()
Runs the window loop.
gl3::MeshRenderer & getRenderer()
void setTouchRotateCallback(TouchDelegate cb)
void setWindowTitle(std::string &title)
void setOnKeyDown(int key, KeyDelegate func)
void setWindowPos(int x, int y)
void setOnReshape(WindowDelegate func)
bool isWindowInitialized()
void screenshot(std::string filename, bool convert=false)
Queues a screenshot to be taken.
void getWindowSize(int &w, int &h)
void setOnMouseDown(int btn, MouseDelegate func)
void setCanvasId(std::string canvas_id)
void(* MouseDelegate)(EventInfo *)
void callKeyDown(SDL_Keycode k, Uint16 mod=0)
static void StartSDL(bool server_mode)
void setOnMouseMove(int btn, MouseDelegate func)
std::string getSavedKeys() const
Returns the keyboard events that have been logged by the window.
void setTouchPinchCallback(TouchDelegate cb)
bool isHighDpi() const
This property is set by createWindow().
void setOnExpose(Delegate func)
bool createWindow(const char *title, int x, int y, int w, int h, bool legacyGlOnly)
void(* TouchDelegate)(SDL_MultiGestureEvent &)
void setOnIdle(IdleDelegate func)
std::function< void(GLenum)> KeyDelegate
void setOnKeyDown(int key, Delegate func)
void getDpi(int &wdpi, int &hdpi)
std::string getCanvasId() const
void setOnMouseUp(int btn, MouseDelegate func)
void setWindowSize(int w, int h)