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`.
This commit is contained in:
SuperUserNameMan 2024-07-23 11:05:27 +02:00 committed by GitHub
parent 79316b2da9
commit 02ae6e9365
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -310,11 +310,14 @@ typedef struct CoreData {
} Keyboard; } Keyboard;
struct { struct {
Vector2 offset; // Mouse offset Vector2 offset; // Mouse offset in CORE.Window.screen coordinate (set by the user)
Vector2 scale; // Mouse scaling Vector2 scale; // Mouse scaling in CORE.Window.screen coordinate (set by the user)
Vector2 currentPosition; // Mouse position on screen Vector2 currentPosition; // Mouse position on screen
Vector2 previousPosition; // Previous mouse position 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 int cursor; // Tracks current mouse cursor
bool cursorHidden; // Track if cursor is hidden bool cursorHidden; // Track if cursor is hidden
bool cursorOnScreen; // Tracks if cursor is inside client area 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 memset(&CORE.Input, 0, sizeof(CORE.Input)); // Reset CORE.Input structure to 0
CORE.Input.Keyboard.exitKey = KEY_ESCAPE; CORE.Input.Keyboard.exitKey = KEY_ESCAPE;
CORE.Input.Mouse.scale = (Vector2){ 1.0f, 1.0f }; 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.Mouse.cursor = MOUSE_CURSOR_ARROW;
CORE.Input.Gamepad.lastButtonPressed = GAMEPAD_BUTTON_UNKNOWN; CORE.Input.Gamepad.lastButtonPressed = GAMEPAD_BUTTON_UNKNOWN;
// Initialize platform // 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) // Initialize rlgl default data (buffers and shaders)
@ -3028,27 +3039,43 @@ bool IsMouseButtonUp(int button)
return up; return up;
} }
// Get mouse position X // Get mouse position X in screen coordinate
int GetMouseX(void) 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; return mouseX;
} }
// Get mouse position Y // Get mouse position Y in screen coordinate
int GetMouseY(void) 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; return mouseY;
} }
// Get mouse position XY // Get mouse position XY in screen coordinate
Vector2 GetMousePosition(void) Vector2 GetMousePosition(void)
{ {
Vector2 position = { 0 }; Vector2 position = { 0 };
position.x = (CORE.Input.Mouse.currentPosition.x + CORE.Input.Mouse.offset.x)*CORE.Input.Mouse.scale.x; // First : apply the platform's offset and scaling :
position.y = (CORE.Input.Mouse.currentPosition.y + CORE.Input.Mouse.offset.y)*CORE.Input.Mouse.scale.y; 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; return position;
} }
@ -3064,14 +3091,14 @@ Vector2 GetMouseDelta(void)
return delta; return delta;
} }
// Set mouse offset // Set user's mouse offset
// NOTE: Useful when rendering to different size targets // NOTE: Useful when rendering to different size targets
void SetMouseOffset(int offsetX, int offsetY) void SetMouseOffset(int offsetX, int offsetY)
{ {
CORE.Input.Mouse.offset = (Vector2){ (float)offsetX, (float)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 // NOTE: Useful when rendering to different size targets
void SetMouseScale(float scaleX, float scaleY) void SetMouseScale(float scaleX, float scaleY)
{ {