summaryrefslogtreecommitdiff
path: root/src/gfx.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2020-12-06 20:18:19 +0100
committerPatric Stout <github@truebrain.nl>2020-12-15 15:46:39 +0100
commitd5b9f7ac37c7d27d1ffe50e55aa73361da64189b (patch)
tree6c7612829daacd062b2d343e62ffbf5c034b60a2 /src/gfx.cpp
parentd15dc9f40f5a20bff452547a2dcb15231f9f969d (diff)
downloadopenttd-d5b9f7ac37c7d27d1ffe50e55aa73361da64189b.tar.xz
Add: [Emscripten] use "relative mouse mode" with SDL2
This mode doesn't wrap the mouse constantly, but requests SDL to lock the mouse pointer. This is needed, as with Emscripten you are not allowed to change the mouse poisition (only to lock it into place).
Diffstat (limited to 'src/gfx.cpp')
-rw-r--r--src/gfx.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/gfx.cpp b/src/gfx.cpp
index 6039946fc..8027dad26 100644
--- a/src/gfx.cpp
+++ b/src/gfx.cpp
@@ -1785,6 +1785,30 @@ void SetAnimatedMouseCursor(const AnimCursor *table)
}
/**
+ * Update cursor position on mouse movement for relative modes.
+ * @param delta_x How much change in the X position.
+ * @param delta_y How much change in the Y position.
+ */
+void CursorVars::UpdateCursorPositionRelative(int delta_x, int delta_y)
+{
+ if (this->fix_at) {
+ this->delta.x = delta_x;
+ this->delta.y = delta_y;
+ } else {
+ int last_position_x = this->pos.x;
+ int last_position_y = this->pos.y;
+
+ this->pos.x = Clamp(this->pos.x + delta_x, 0, _cur_resolution.width - 1);
+ this->pos.y = Clamp(this->pos.y + delta_y, 0, _cur_resolution.height - 1);
+
+ this->delta.x = last_position_x - this->pos.x;
+ this->delta.y = last_position_y - this->pos.y;
+
+ this->dirty = true;
+ }
+}
+
+/**
* Update cursor position on mouse movement.
* @param x New X position.
* @param y New Y position.