+static float current_display_scale = 1.0f;
+
+
+// export this for other parts of the code to use.
+float get_display_scale(void)
+{
+ return current_display_scale;
+}
+
+static
+void recalculate_display_scale(SDL_Window* win, SDL_Renderer* rend)
+{
+ int w0 = 0;
+ SDL_GetWindowSize(win, &w0, NULL);
+
+ int w1 = 0;
+ SDL_GetRendererOutputSize(rend, &w1, NULL);
+
+ current_display_scale = (float) w1 / (float) w0;
+}
+
+static
+void maybe_fixup_input_for_display_scale(SDL_Window* win, SDL_Renderer* rend, SDL_Event* e)
+{
+ // note: we check for window move as well, because you may move the window to
+ // another monitor with a different display scale.
+ switch (e->type) {
+ case SDL_WINDOWEVENT: {
+ switch (e->window.event) {
+ case SDL_WINDOWEVENT_MOVED:
+ case SDL_WINDOWEVENT_SIZE_CHANGED:
+ recalculate_display_scale(win, rend);
+ break;
+ }
+ } break;
+
+ // this is the fixup.
+ case SDL_MOUSEMOTION: {
+ // note: do it this way *just in case* there are non-integer display scales out there.
+ e->motion.x = (int) ((float) e->motion.x * current_display_scale);
+ e->motion.y = (int) ((float) e->motion.y * current_display_scale);
+ } break;
+
+ case SDL_MOUSEBUTTONUP:
+ case SDL_MOUSEBUTTONDOWN: {
+ e->button.x = (int) ((float) e->button.x * current_display_scale);
+ e->button.y = (int) ((float) e->button.y * current_display_scale);
+ } break;
+ }
+}
+
+