Skip to content

Commit

Permalink
rcore.c : mouse offset and scaling consistency - part 1
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
SuperUserNameMan committed Jul 23, 2024
1 parent 79316b2 commit 02ae6e9
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions src/rcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 )

This comment has been minimized.

Copy link
@SuperUserNameMan

SuperUserNameMan Jul 23, 2024

Author Owner

woops, sorry, it was merged in the process despite raysan5#4164 was still pending

{
TRACELOG(LOG_ERROR, "Platform backend: Window initialization failed.");
CORE.Window.ready = false;
return;
}
//--------------------------------------------------------------

// Initialize rlgl default data (buffers and shaders)
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)
{
Expand Down

0 comments on commit 02ae6e9

Please sign in to comment.