From 02ae6e93658e934f3494d712f152bc403381b735 Mon Sep 17 00:00:00 2001 From: SuperUserNameMan <9801802+SuperUserNameMan@users.noreply.github.com> Date: Tue, 23 Jul 2024 11:05:27 +0200 Subject: [PATCH] `rcore.c` : mouse offset and scaling consistency - part 1 I did not want to touch `rcore.c`, but it was required for cross-paltform consistency, so that `SetMouseScale()` and `SetMouseOffset()` have same effect on every platform, and don't interfere with mouse offset and scaling required and preset by a platform which should, instead, use the new `CORE.Input.Mouse.platformOffset` and `CORE.Input.Mouse.platformScale`. --- src/rcore.c | 51 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/src/rcore.c b/src/rcore.c index 045e63a4e..0dafd820f 100644 --- a/src/rcore.c +++ b/src/rcore.c @@ -310,11 +310,14 @@ typedef struct CoreData { } Keyboard; struct { - Vector2 offset; // Mouse offset - Vector2 scale; // Mouse scaling + Vector2 offset; // Mouse offset in CORE.Window.screen coordinate (set by the user) + Vector2 scale; // Mouse scaling in CORE.Window.screen coordinate (set by the user) Vector2 currentPosition; // Mouse position on screen Vector2 previousPosition; // Previous mouse position + Vector2 platformOffset; // Extra mouse offset set by the platform (for cross-platform consistency) + Vector2 platformScale; // Extra mouse scaling set by the platform (for cross-platform consistency) + int cursor; // Tracks current mouse cursor bool cursorHidden; // Track if cursor is hidden bool cursorOnScreen; // Tracks if cursor is inside client area @@ -628,12 +631,20 @@ void InitWindow(int width, int height, const char *title) memset(&CORE.Input, 0, sizeof(CORE.Input)); // Reset CORE.Input structure to 0 CORE.Input.Keyboard.exitKey = KEY_ESCAPE; CORE.Input.Mouse.scale = (Vector2){ 1.0f, 1.0f }; + CORE.Input.Mouse.offset = (Vector2){ 0.0f, 0.0f }; + CORE.Input.Mouse.platformScale = (Vector2){ 1.0f, 1.0f }; + CORE.Input.Mouse.platformOffset = (Vector2){ 0.0f, 0.0f }; CORE.Input.Mouse.cursor = MOUSE_CURSOR_ARROW; CORE.Input.Gamepad.lastButtonPressed = GAMEPAD_BUTTON_UNKNOWN; // Initialize platform //-------------------------------------------------------------- - InitPlatform(); + if ( InitPlatform() != 0 ) + { + TRACELOG(LOG_ERROR, "Platform backend: Window initialization failed."); + CORE.Window.ready = false; + return; + } //-------------------------------------------------------------- // Initialize rlgl default data (buffers and shaders) @@ -3028,27 +3039,43 @@ bool IsMouseButtonUp(int button) return up; } -// Get mouse position X +// Get mouse position X in screen coordinate int GetMouseX(void) { - int mouseX = (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x); + // First : apply the platform's offset and scaling : + int mouseX = (int)((CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.platformOffset.x)*CORE.Input.Mouse.platformScale.x); + + // Second : apply user's offset and scaling : + mouseX = (int)(((float)mouseX + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x); + return mouseX; } -// Get mouse position Y +// Get mouse position Y in screen coordinate int GetMouseY(void) { - int mouseY = (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y); + // First : apply the platform's offset and scaling : + int mouseY = (int)((CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.platformOffset.y)*CORE.Input.Mouse.platformScale.y); + + // Second : apply user's offset and scaling : + mouseY = (int)(((float)mouseY + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y); + return mouseY; } -// Get mouse position XY +// Get mouse position XY in screen coordinate Vector2 GetMousePosition(void) { Vector2 position = { 0 }; - position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x; - position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y; + // First : apply the platform's offset and scaling : + position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.platformOffset.x)*CORE.Input.Mouse.platformScale.x; + position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.platformOffset.y)*CORE.Input.Mouse.platformScale.y; + + // Second : apply the user's offset and scaling : + + position.x = (position.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x; + position.y = (position.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y; return position; } @@ -3064,14 +3091,14 @@ Vector2 GetMouseDelta(void) return delta; } -// Set mouse offset +// Set user's mouse offset // NOTE: Useful when rendering to different size targets void SetMouseOffset(int offsetX, int offsetY) { CORE.Input.Mouse.offset = (Vector2){ (float)offsetX, (float)offsetY }; } -// Set mouse scaling +// Set user's mouse scaling // NOTE: Useful when rendering to different size targets void SetMouseScale(float scaleX, float scaleY) {