mirror of
https://github.com/libsdl-org/SDL.git
synced 2026-04-05 07:09:32 +00:00
Mouse coordinates are floating point
You can get sub-pixel mouse coordinates and motion depending on the platform and display scaling. Fixes https://github.com/libsdl-org/SDL/issues/2999
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
* Mouse test suite
|
||||
*/
|
||||
#include <limits.h>
|
||||
#include <float.h>
|
||||
|
||||
#include <SDL3/SDL.h>
|
||||
#include <SDL3/SDL_test.h>
|
||||
@@ -27,8 +28,8 @@ static int mouseStateCheck(Uint32 state)
|
||||
*/
|
||||
int mouse_getMouseState(void *arg)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
float x;
|
||||
float y;
|
||||
Uint32 state;
|
||||
|
||||
/* Pump some events to update mouse state */
|
||||
@@ -41,26 +42,26 @@ int mouse_getMouseState(void *arg)
|
||||
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
|
||||
|
||||
/* Case where x pointer is not NULL */
|
||||
x = INT_MIN;
|
||||
x = FLT_MIN;
|
||||
state = SDL_GetMouseState(&x, NULL);
|
||||
SDLTest_AssertPass("Call to SDL_GetMouseState(&x, NULL)");
|
||||
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
|
||||
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %g", x);
|
||||
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
|
||||
|
||||
/* Case where y pointer is not NULL */
|
||||
y = INT_MIN;
|
||||
y = FLT_MIN;
|
||||
state = SDL_GetMouseState(NULL, &y);
|
||||
SDLTest_AssertPass("Call to SDL_GetMouseState(NULL, &y)");
|
||||
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
|
||||
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %g", y);
|
||||
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
|
||||
|
||||
/* Case where x and y pointer is not NULL */
|
||||
x = INT_MIN;
|
||||
y = INT_MIN;
|
||||
x = FLT_MIN;
|
||||
y = FLT_MIN;
|
||||
state = SDL_GetMouseState(&x, &y);
|
||||
SDLTest_AssertPass("Call to SDL_GetMouseState(&x, &y)");
|
||||
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
|
||||
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
|
||||
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %g", x);
|
||||
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %g", y);
|
||||
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
|
||||
|
||||
return TEST_COMPLETED;
|
||||
@@ -72,8 +73,8 @@ int mouse_getMouseState(void *arg)
|
||||
*/
|
||||
int mouse_getRelativeMouseState(void *arg)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
float x;
|
||||
float y;
|
||||
Uint32 state;
|
||||
|
||||
/* Pump some events to update mouse state */
|
||||
@@ -86,26 +87,26 @@ int mouse_getRelativeMouseState(void *arg)
|
||||
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
|
||||
|
||||
/* Case where x pointer is not NULL */
|
||||
x = INT_MIN;
|
||||
x = FLT_MIN;
|
||||
state = SDL_GetRelativeMouseState(&x, NULL);
|
||||
SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, NULL)");
|
||||
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
|
||||
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %g", x);
|
||||
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
|
||||
|
||||
/* Case where y pointer is not NULL */
|
||||
y = INT_MIN;
|
||||
y = FLT_MIN;
|
||||
state = SDL_GetRelativeMouseState(NULL, &y);
|
||||
SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(NULL, &y)");
|
||||
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
|
||||
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %g", y);
|
||||
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
|
||||
|
||||
/* Case where x and y pointer is not NULL */
|
||||
x = INT_MIN;
|
||||
y = INT_MIN;
|
||||
x = FLT_MIN;
|
||||
y = FLT_MIN;
|
||||
state = SDL_GetRelativeMouseState(&x, &y);
|
||||
SDLTest_AssertPass("Call to SDL_GetRelativeMouseState(&x, &y)");
|
||||
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
|
||||
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
|
||||
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %g", x);
|
||||
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %g", y);
|
||||
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
|
||||
|
||||
return TEST_COMPLETED;
|
||||
@@ -431,23 +432,24 @@ int mouse_warpMouseInWindow(void *arg)
|
||||
{
|
||||
const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
|
||||
int numPositions = 6;
|
||||
int xPositions[6];
|
||||
int yPositions[6];
|
||||
int x, y, i, j;
|
||||
float xPositions[6];
|
||||
float yPositions[6];
|
||||
float x, y;
|
||||
int i, j;
|
||||
SDL_Window *window;
|
||||
|
||||
xPositions[0] = -1;
|
||||
xPositions[1] = 0;
|
||||
xPositions[2] = 1;
|
||||
xPositions[3] = w - 1;
|
||||
xPositions[4] = w;
|
||||
xPositions[5] = w + 1;
|
||||
xPositions[3] = (float)w - 1;
|
||||
xPositions[4] = (float)w;
|
||||
xPositions[5] = (float)w + 1;
|
||||
yPositions[0] = -1;
|
||||
yPositions[1] = 0;
|
||||
yPositions[2] = 1;
|
||||
yPositions[3] = h - 1;
|
||||
yPositions[4] = h;
|
||||
yPositions[5] = h + 1;
|
||||
yPositions[3] = (float)h - 1;
|
||||
yPositions[4] = (float)h;
|
||||
yPositions[5] = (float)h + 1;
|
||||
/* Create test window */
|
||||
window = createMouseSuiteTestWindow();
|
||||
if (window == NULL) {
|
||||
@@ -455,14 +457,14 @@ int mouse_warpMouseInWindow(void *arg)
|
||||
}
|
||||
|
||||
/* Mouse to random position inside window */
|
||||
x = SDLTest_RandomIntegerInRange(1, w - 1);
|
||||
y = SDLTest_RandomIntegerInRange(1, h - 1);
|
||||
x = (float)SDLTest_RandomIntegerInRange(1, w - 1);
|
||||
y = (float)SDLTest_RandomIntegerInRange(1, h - 1);
|
||||
SDL_WarpMouseInWindow(window, x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
|
||||
|
||||
/* Same position again */
|
||||
SDL_WarpMouseInWindow(window, x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
|
||||
|
||||
/* Mouse to various boundary positions */
|
||||
for (i = 0; i < numPositions; i++) {
|
||||
@@ -470,7 +472,7 @@ int mouse_warpMouseInWindow(void *arg)
|
||||
x = xPositions[i];
|
||||
y = yPositions[j];
|
||||
SDL_WarpMouseInWindow(window, x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
|
||||
|
||||
/* TODO: add tracking of events and check that each call generates a mouse motion event */
|
||||
SDL_PumpEvents();
|
||||
@@ -492,7 +494,7 @@ int mouse_warpMouseInWindow(void *arg)
|
||||
int mouse_getMouseFocus(void *arg)
|
||||
{
|
||||
const int w = MOUSE_TESTWINDOW_WIDTH, h = MOUSE_TESTWINDOW_HEIGHT;
|
||||
int x, y;
|
||||
float x, y;
|
||||
SDL_Window *window;
|
||||
SDL_Window *focusWindow;
|
||||
|
||||
@@ -507,10 +509,10 @@ int mouse_getMouseFocus(void *arg)
|
||||
}
|
||||
|
||||
/* Mouse to random position inside window */
|
||||
x = SDLTest_RandomIntegerInRange(1, w - 1);
|
||||
y = SDLTest_RandomIntegerInRange(1, h - 1);
|
||||
x = (float)SDLTest_RandomIntegerInRange(1, w - 1);
|
||||
y = (float)SDLTest_RandomIntegerInRange(1, h - 1);
|
||||
SDL_WarpMouseInWindow(window, x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
|
||||
|
||||
/* Pump events to update focus state */
|
||||
SDL_Delay(100);
|
||||
@@ -524,10 +526,10 @@ int mouse_getMouseFocus(void *arg)
|
||||
SDLTest_AssertCheck(focusWindow == window, "Check returned window value is test window");
|
||||
|
||||
/* Mouse to random position outside window */
|
||||
x = SDLTest_RandomIntegerInRange(-9, -1);
|
||||
y = SDLTest_RandomIntegerInRange(-9, -1);
|
||||
x = (float)SDLTest_RandomIntegerInRange(-9, -1);
|
||||
y = (float)SDLTest_RandomIntegerInRange(-9, -1);
|
||||
SDL_WarpMouseInWindow(window, x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%i,%i)", x, y);
|
||||
SDLTest_AssertPass("SDL_WarpMouseInWindow(...,%.f,%.f)", x, y);
|
||||
|
||||
/* Clean up test window */
|
||||
destroyMouseSuiteTestWindow(window);
|
||||
@@ -568,18 +570,18 @@ int mouse_getDefaultCursor(void *arg)
|
||||
*/
|
||||
int mouse_getGlobalMouseState(void *arg)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
float x;
|
||||
float y;
|
||||
Uint32 state;
|
||||
|
||||
x = INT_MIN;
|
||||
y = INT_MIN;
|
||||
x = FLT_MIN;
|
||||
y = FLT_MIN;
|
||||
|
||||
/* Get current cursor */
|
||||
state = SDL_GetGlobalMouseState(&x, &y);
|
||||
SDLTest_AssertPass("Call to SDL_GetGlobalMouseState()");
|
||||
SDLTest_AssertCheck(x > INT_MIN, "Validate that value of x is > INT_MIN, got: %i", x);
|
||||
SDLTest_AssertCheck(y > INT_MIN, "Validate that value of y is > INT_MIN, got: %i", y);
|
||||
SDLTest_AssertCheck(x > FLT_MIN, "Validate that value of x is > FLT_MIN, got: %.f", x);
|
||||
SDLTest_AssertCheck(y > FLT_MIN, "Validate that value of y is > FLT_MIN, got: %.f", y);
|
||||
SDLTest_AssertCheck(mouseStateCheck(state), "Validate state returned from function, got: %" SDL_PRIu32, state);
|
||||
|
||||
return TEST_COMPLETED;
|
||||
|
||||
@@ -95,8 +95,8 @@ static SDL_Gamepad **gamepads;
|
||||
static int num_gamepads = 0;
|
||||
static SDL_Joystick *virtual_joystick = NULL;
|
||||
static SDL_GamepadAxis virtual_axis_active = SDL_GAMEPAD_AXIS_INVALID;
|
||||
static int virtual_axis_start_x;
|
||||
static int virtual_axis_start_y;
|
||||
static float virtual_axis_start_x;
|
||||
static float virtual_axis_start_y;
|
||||
static SDL_GamepadButton virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
|
||||
static void UpdateWindowTitle()
|
||||
@@ -424,9 +424,9 @@ static void CloseVirtualGamepad()
|
||||
}
|
||||
}
|
||||
|
||||
static SDL_GamepadButton FindButtonAtPosition(int x, int y)
|
||||
static SDL_GamepadButton FindButtonAtPosition(float x, float y)
|
||||
{
|
||||
SDL_Point point;
|
||||
SDL_FPoint point;
|
||||
int i;
|
||||
SDL_bool showing_front = ShowingFront();
|
||||
|
||||
@@ -435,12 +435,12 @@ static SDL_GamepadButton FindButtonAtPosition(int x, int y)
|
||||
for (i = 0; i < SDL_GAMEPAD_BUTTON_TOUCHPAD; ++i) {
|
||||
SDL_bool on_front = (i < SDL_GAMEPAD_BUTTON_PADDLE1 || i > SDL_GAMEPAD_BUTTON_PADDLE4);
|
||||
if (on_front == showing_front) {
|
||||
SDL_Rect rect;
|
||||
rect.x = button_positions[i].x;
|
||||
rect.y = button_positions[i].y;
|
||||
rect.w = BUTTON_SIZE;
|
||||
rect.h = BUTTON_SIZE;
|
||||
if (SDL_PointInRect(&point, &rect)) {
|
||||
SDL_FRect rect;
|
||||
rect.x = (float)button_positions[i].x;
|
||||
rect.y = (float)button_positions[i].y;
|
||||
rect.w = (float)BUTTON_SIZE;
|
||||
rect.h = (float)BUTTON_SIZE;
|
||||
if (SDL_PointInRectFloat(&point, &rect)) {
|
||||
return (SDL_GamepadButton)i;
|
||||
}
|
||||
}
|
||||
@@ -448,9 +448,9 @@ static SDL_GamepadButton FindButtonAtPosition(int x, int y)
|
||||
return SDL_GAMEPAD_BUTTON_INVALID;
|
||||
}
|
||||
|
||||
static SDL_GamepadAxis FindAxisAtPosition(int x, int y)
|
||||
static SDL_GamepadAxis FindAxisAtPosition(float x, float y)
|
||||
{
|
||||
SDL_Point point;
|
||||
SDL_FPoint point;
|
||||
int i;
|
||||
SDL_bool showing_front = ShowingFront();
|
||||
|
||||
@@ -458,12 +458,12 @@ static SDL_GamepadAxis FindAxisAtPosition(int x, int y)
|
||||
point.y = y;
|
||||
for (i = 0; i < SDL_GAMEPAD_AXIS_MAX; ++i) {
|
||||
if (showing_front) {
|
||||
SDL_Rect rect;
|
||||
rect.x = axis_positions[i].x;
|
||||
rect.y = axis_positions[i].y;
|
||||
rect.w = AXIS_SIZE;
|
||||
rect.h = AXIS_SIZE;
|
||||
if (SDL_PointInRect(&point, &rect)) {
|
||||
SDL_FRect rect;
|
||||
rect.x = (float)axis_positions[i].x;
|
||||
rect.y = (float)axis_positions[i].y;
|
||||
rect.w = (float)AXIS_SIZE;
|
||||
rect.h = (float)AXIS_SIZE;
|
||||
if (SDL_PointInRectFloat(&point, &rect)) {
|
||||
return (SDL_GamepadAxis)i;
|
||||
}
|
||||
}
|
||||
@@ -471,13 +471,13 @@ static SDL_GamepadAxis FindAxisAtPosition(int x, int y)
|
||||
return SDL_GAMEPAD_AXIS_INVALID;
|
||||
}
|
||||
|
||||
static void VirtualGamepadMouseMotion(int x, int y)
|
||||
static void VirtualGamepadMouseMotion(float x, float y)
|
||||
{
|
||||
if (virtual_button_active != SDL_GAMEPAD_BUTTON_INVALID) {
|
||||
if (virtual_axis_active != SDL_GAMEPAD_AXIS_INVALID) {
|
||||
const int MOVING_DISTANCE = 2;
|
||||
if (SDL_abs(x - virtual_axis_start_x) >= MOVING_DISTANCE ||
|
||||
SDL_abs(y - virtual_axis_start_y) >= MOVING_DISTANCE) {
|
||||
const float MOVING_DISTANCE = 2.0f;
|
||||
if (SDL_fabs(x - virtual_axis_start_x) >= MOVING_DISTANCE ||
|
||||
SDL_fabs(y - virtual_axis_start_y) >= MOVING_DISTANCE) {
|
||||
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED);
|
||||
virtual_button_active = SDL_GAMEPAD_BUTTON_INVALID;
|
||||
}
|
||||
@@ -488,12 +488,12 @@ static void VirtualGamepadMouseMotion(int x, int y)
|
||||
if (virtual_axis_active == SDL_GAMEPAD_AXIS_LEFT_TRIGGER ||
|
||||
virtual_axis_active == SDL_GAMEPAD_AXIS_RIGHT_TRIGGER) {
|
||||
int range = (SDL_JOYSTICK_AXIS_MAX - SDL_JOYSTICK_AXIS_MIN);
|
||||
float distance = SDL_clamp(((float)y - virtual_axis_start_y) / AXIS_SIZE, 0.0f, 1.0f);
|
||||
float distance = SDL_clamp((y - virtual_axis_start_y) / AXIS_SIZE, 0.0f, 1.0f);
|
||||
Sint16 value = (Sint16)(SDL_JOYSTICK_AXIS_MIN + (distance * range));
|
||||
SDL_SetJoystickVirtualAxis(virtual_joystick, virtual_axis_active, value);
|
||||
} else {
|
||||
float distanceX = SDL_clamp(((float)x - virtual_axis_start_x) / AXIS_SIZE, -1.0f, 1.0f);
|
||||
float distanceY = SDL_clamp(((float)y - virtual_axis_start_y) / AXIS_SIZE, -1.0f, 1.0f);
|
||||
float distanceX = SDL_clamp((x - virtual_axis_start_x) / AXIS_SIZE, -1.0f, 1.0f);
|
||||
float distanceY = SDL_clamp((y - virtual_axis_start_y) / AXIS_SIZE, -1.0f, 1.0f);
|
||||
Sint16 valueX, valueY;
|
||||
|
||||
if (distanceX >= 0) {
|
||||
@@ -512,7 +512,7 @@ static void VirtualGamepadMouseMotion(int x, int y)
|
||||
}
|
||||
}
|
||||
|
||||
static void VirtualGamepadMouseDown(int x, int y)
|
||||
static void VirtualGamepadMouseDown(float x, float y)
|
||||
{
|
||||
SDL_GamepadButton button;
|
||||
SDL_GamepadAxis axis;
|
||||
@@ -531,7 +531,7 @@ static void VirtualGamepadMouseDown(int x, int y)
|
||||
}
|
||||
}
|
||||
|
||||
static void VirtualGamepadMouseUp(int x, int y)
|
||||
static void VirtualGamepadMouseUp(float x, float y)
|
||||
{
|
||||
if (virtual_button_active != SDL_GAMEPAD_BUTTON_INVALID) {
|
||||
SDL_SetJoystickVirtualButton(virtual_joystick, virtual_button_active, SDL_RELEASED);
|
||||
|
||||
@@ -72,18 +72,18 @@ void loop()
|
||||
|
||||
if (event.type == SDL_MOUSEMOTION) {
|
||||
if (event.motion.state) {
|
||||
int xrel, yrel;
|
||||
float xrel, yrel;
|
||||
int window_w, window_h;
|
||||
SDL_Window *window = SDL_GetWindowFromID(event.motion.windowID);
|
||||
SDL_GetWindowSize(window, &window_w, &window_h);
|
||||
xrel = event.motion.xrel;
|
||||
yrel = event.motion.yrel;
|
||||
if (event.motion.y < window_h / 2) {
|
||||
if (event.motion.y < (float)window_h / 2.0f) {
|
||||
angle += xrel;
|
||||
} else {
|
||||
angle -= xrel;
|
||||
}
|
||||
if (event.motion.x < window_w / 2) {
|
||||
if (event.motion.x < (float)window_w / 2.0f) {
|
||||
angle -= yrel;
|
||||
} else {
|
||||
angle += yrel;
|
||||
|
||||
@@ -39,7 +39,7 @@ static int current_alpha = 255;
|
||||
static int current_color = 255;
|
||||
static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
|
||||
|
||||
int mouse_begin_x = -1, mouse_begin_y = -1;
|
||||
float mouse_begin_x = -1.0f, mouse_begin_y = -1.0f;
|
||||
int done;
|
||||
|
||||
void DrawPoints(SDL_Renderer *renderer)
|
||||
@@ -86,9 +86,9 @@ void DrawPoints(SDL_Renderer *renderer)
|
||||
|
||||
#define MAX_LINES 16
|
||||
int num_lines = 0;
|
||||
SDL_Rect lines[MAX_LINES];
|
||||
SDL_FRect lines[MAX_LINES];
|
||||
static int
|
||||
add_line(int x1, int y1, int x2, int y2)
|
||||
add_line(float x1, float y1, float x2, float y2)
|
||||
{
|
||||
if (num_lines >= MAX_LINES) {
|
||||
return 0;
|
||||
@@ -97,7 +97,7 @@ add_line(int x1, int y1, int x2, int y2)
|
||||
return 0;
|
||||
}
|
||||
|
||||
SDL_Log("adding line (%d, %d), (%d, %d)\n", x1, y1, x2, y2);
|
||||
SDL_Log("adding line (%g, %g), (%g, %g)\n", x1, y1, x2, y2);
|
||||
lines[num_lines].x = x1;
|
||||
lines[num_lines].y = y1;
|
||||
lines[num_lines].w = x2;
|
||||
@@ -123,16 +123,16 @@ void DrawLines(SDL_Renderer *renderer)
|
||||
SDL_RenderLine(renderer, 0, viewport.h / 2, viewport.w - 1, viewport.h / 2);
|
||||
SDL_RenderLine(renderer, viewport.w / 2, 0, viewport.w / 2, viewport.h - 1);
|
||||
} else {
|
||||
SDL_RenderLine(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
|
||||
SDL_RenderLineFloat(renderer, lines[i].x, lines[i].y, lines[i].w, lines[i].h);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define MAX_RECTS 16
|
||||
int num_rects = 0;
|
||||
SDL_Rect rects[MAX_RECTS];
|
||||
SDL_FRect rects[MAX_RECTS];
|
||||
static int
|
||||
add_rect(int x1, int y1, int x2, int y2)
|
||||
add_rect(float x1, float y1, float x2, float y2)
|
||||
{
|
||||
if (num_rects >= MAX_RECTS) {
|
||||
return 0;
|
||||
@@ -142,13 +142,13 @@ add_rect(int x1, int y1, int x2, int y2)
|
||||
}
|
||||
|
||||
if (x1 > x2) {
|
||||
SWAP(int, x1, x2);
|
||||
SWAP(float, x1, x2);
|
||||
}
|
||||
if (y1 > y2) {
|
||||
SWAP(int, y1, y2);
|
||||
SWAP(float, y1, y2);
|
||||
}
|
||||
|
||||
SDL_Log("adding rect (%d, %d), (%d, %d) [%dx%d]\n", x1, y1, x2, y2,
|
||||
SDL_Log("adding rect (%g, %g), (%g, %g) [%gx%g]\n", x1, y1, x2, y2,
|
||||
x2 - x1, y2 - y1);
|
||||
|
||||
rects[num_rects].x = x1;
|
||||
@@ -163,7 +163,7 @@ static void
|
||||
DrawRects(SDL_Renderer *renderer)
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, 255, 127, 0, 255);
|
||||
SDL_RenderFillRects(renderer, rects, num_rects);
|
||||
SDL_RenderFillRectsFloat(renderer, rects, num_rects);
|
||||
}
|
||||
|
||||
static void
|
||||
@@ -175,8 +175,8 @@ DrawRectLineIntersections(SDL_Renderer *renderer)
|
||||
|
||||
for (i = 0; i < num_rects; i++) {
|
||||
for (j = 0; j < num_lines; j++) {
|
||||
int x1, y1, x2, y2;
|
||||
SDL_Rect r;
|
||||
float x1, y1, x2, y2;
|
||||
SDL_FRect r;
|
||||
|
||||
r = rects[i];
|
||||
x1 = lines[j].x;
|
||||
@@ -184,8 +184,8 @@ DrawRectLineIntersections(SDL_Renderer *renderer)
|
||||
x2 = lines[j].w;
|
||||
y2 = lines[j].h;
|
||||
|
||||
if (SDL_GetRectAndLineIntersection(&r, &x1, &y1, &x2, &y2)) {
|
||||
SDL_RenderLine(renderer, x1, y1, x2, y2);
|
||||
if (SDL_GetRectAndLineIntersectionFloat(&r, &x1, &y1, &x2, &y2)) {
|
||||
SDL_RenderLineFloat(renderer, x1, y1, x2, y2);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -200,9 +200,9 @@ DrawRectRectIntersections(SDL_Renderer *renderer)
|
||||
|
||||
for (i = 0; i < num_rects; i++) {
|
||||
for (j = i + 1; j < num_rects; j++) {
|
||||
SDL_Rect r;
|
||||
if (SDL_GetRectIntersection(&rects[i], &rects[j], &r)) {
|
||||
SDL_RenderFillRect(renderer, &r);
|
||||
SDL_FRect r;
|
||||
if (SDL_GetRectIntersectionFloat(&rects[i], &rects[j], &r)) {
|
||||
SDL_RenderFillRectFloat(renderer, &r);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -235,16 +235,22 @@ void loop()
|
||||
if (event.key.keysym.mod & SDL_KMOD_SHIFT) {
|
||||
num_lines = 0;
|
||||
} else {
|
||||
add_line(rand() % 640, rand() % 480, rand() % 640,
|
||||
rand() % 480);
|
||||
add_line(
|
||||
(float)(rand() % 640),
|
||||
(float)(rand() % 480),
|
||||
(float)(rand() % 640),
|
||||
(float)(rand() % 480));
|
||||
}
|
||||
break;
|
||||
case 'r':
|
||||
if (event.key.keysym.mod & SDL_KMOD_SHIFT) {
|
||||
num_rects = 0;
|
||||
} else {
|
||||
add_rect(rand() % 640, rand() % 480, rand() % 640,
|
||||
rand() % 480);
|
||||
add_rect(
|
||||
(float)(rand() % 640),
|
||||
(float)(rand() % 480),
|
||||
(float)(rand() % 640),
|
||||
(float)(rand() % 480));
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -33,7 +33,7 @@ typedef struct _Object
|
||||
{
|
||||
struct _Object *next;
|
||||
|
||||
int x1, y1, x2, y2;
|
||||
float x1, y1, x2, y2;
|
||||
Uint8 r, g, b;
|
||||
|
||||
SDL_bool isRect;
|
||||
@@ -56,7 +56,7 @@ void DrawObject(SDL_Renderer *renderer, Object *object)
|
||||
SDL_SetRenderDrawColor(renderer, object->r, object->g, object->b, 255);
|
||||
|
||||
if (object->isRect) {
|
||||
SDL_Rect rect;
|
||||
SDL_FRect rect;
|
||||
|
||||
if (object->x1 > object->x2) {
|
||||
rect.x = object->x2;
|
||||
@@ -74,10 +74,9 @@ void DrawObject(SDL_Renderer *renderer, Object *object)
|
||||
rect.h = object->y2 - object->y1;
|
||||
}
|
||||
|
||||
/* SDL_RenderRect(renderer, &rect); */
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
SDL_RenderFillRectFloat(renderer, &rect);
|
||||
} else {
|
||||
SDL_RenderLine(renderer, object->x1, object->y1, object->x2, object->y2);
|
||||
SDL_RenderLineFloat(renderer, object->x1, object->y1, object->x2, object->y2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,20 +112,18 @@ void loop(void *arg)
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEWHEEL:
|
||||
if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
|
||||
event.wheel.preciseX *= -1.0f;
|
||||
event.wheel.preciseY *= -1.0f;
|
||||
event.wheel.x *= -1;
|
||||
event.wheel.y *= -1;
|
||||
}
|
||||
if (event.wheel.preciseX != 0.0f) {
|
||||
if (event.wheel.x != 0.0f) {
|
||||
wheel_x_active = SDL_TRUE;
|
||||
/* "positive to the right and negative to the left" */
|
||||
wheel_x += event.wheel.preciseX * 10.0f;
|
||||
wheel_x += event.wheel.x * 10.0f;
|
||||
}
|
||||
if (event.wheel.preciseY != 0.0f) {
|
||||
if (event.wheel.x != 0.0f) {
|
||||
wheel_y_active = SDL_TRUE;
|
||||
/* "positive away from the user and negative towards the user" */
|
||||
wheel_y -= event.wheel.preciseY * 10.0f;
|
||||
wheel_y -= event.wheel.x * 10.0f;
|
||||
}
|
||||
break;
|
||||
|
||||
@@ -229,10 +226,10 @@ void loop(void *arg)
|
||||
/* Mouse wheel */
|
||||
SDL_SetRenderDrawColor(renderer, 0, 255, 128, 255);
|
||||
if (wheel_x_active) {
|
||||
SDL_RenderLine(renderer, (int)wheel_x, 0, (int)wheel_x, SCREEN_HEIGHT);
|
||||
SDL_RenderLineFloat(renderer, wheel_x, 0.0f, wheel_x, (float)SCREEN_HEIGHT);
|
||||
}
|
||||
if (wheel_y_active) {
|
||||
SDL_RenderLine(renderer, 0, (int)wheel_y, SCREEN_WIDTH, (int)wheel_y);
|
||||
SDL_RenderLineFloat(renderer, 0.0f, wheel_y, (float)SCREEN_WIDTH, wheel_y);
|
||||
}
|
||||
|
||||
/* Objects from mouse clicks */
|
||||
|
||||
@@ -149,7 +149,7 @@ static const Uint32 fps_check_delay = 5000;
|
||||
|
||||
SDL_Surface *MooseYUVSurfaces[MOOSEFRAMES_COUNT];
|
||||
SDL_Texture *MooseTexture = NULL;
|
||||
SDL_Rect displayrect;
|
||||
SDL_FRect displayrect;
|
||||
int window_w;
|
||||
int window_h;
|
||||
int paused = 0;
|
||||
@@ -192,7 +192,7 @@ void MoveSprites(SDL_Renderer *renderer)
|
||||
SDL_UpdateTexture(MooseTexture, NULL, MooseYUVSurfaces[i]->pixels, MooseYUVSurfaces[i]->pitch);
|
||||
}
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderTexture(renderer, MooseTexture, NULL, &displayrect);
|
||||
SDL_RenderTextureFloat(renderer, MooseTexture, NULL, &displayrect);
|
||||
SDL_RenderPresent(renderer);
|
||||
} else {
|
||||
SDL_Texture *tmp;
|
||||
@@ -209,7 +209,7 @@ void MoveSprites(SDL_Renderer *renderer)
|
||||
}
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderTexture(renderer, tmp, NULL, &displayrect);
|
||||
SDL_RenderTextureFloat(renderer, tmp, NULL, &displayrect);
|
||||
SDL_RenderPresent(renderer);
|
||||
SDL_DestroyTexture(tmp);
|
||||
}
|
||||
@@ -231,8 +231,10 @@ void loop()
|
||||
switch (event.type) {
|
||||
case SDL_WINDOWEVENT_RESIZED:
|
||||
SDL_SetRenderViewport(renderer, NULL);
|
||||
displayrect.w = window_w = event.window.data1;
|
||||
displayrect.h = window_h = event.window.data2;
|
||||
window_w = event.window.data1;
|
||||
window_h = event.window.data2;
|
||||
displayrect.w = (float)window_w;
|
||||
displayrect.h = (float)window_h;
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
displayrect.x = event.button.x - window_w / 2;
|
||||
@@ -488,8 +490,8 @@ int main(int argc, char **argv)
|
||||
|
||||
displayrect.x = 0;
|
||||
displayrect.y = 0;
|
||||
displayrect.w = window_w;
|
||||
displayrect.h = window_h;
|
||||
displayrect.w = (float)window_w;
|
||||
displayrect.h = (float)window_h;
|
||||
|
||||
/* Ignore key up events, they don't even get filtered */
|
||||
SDL_SetEventEnabled(SDL_KEYUP, SDL_FALSE);
|
||||
|
||||
@@ -23,14 +23,17 @@
|
||||
#endif
|
||||
|
||||
static SDLTest_CommonState *state;
|
||||
int i, done;
|
||||
SDL_Rect rect;
|
||||
SDL_Event event;
|
||||
static int i, done;
|
||||
static float mouseX, mouseY;
|
||||
static SDL_Rect rect;
|
||||
static SDL_Event event;
|
||||
|
||||
static void
|
||||
DrawRects(SDL_Renderer *renderer)
|
||||
{
|
||||
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
||||
rect.x = (int)mouseX;
|
||||
rect.y = (int)mouseY;
|
||||
SDL_RenderFillRect(renderer, &rect);
|
||||
}
|
||||
|
||||
@@ -43,8 +46,8 @@ loop()
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEMOTION:
|
||||
{
|
||||
rect.x += event.motion.xrel;
|
||||
rect.y += event.motion.yrel;
|
||||
mouseX += event.motion.xrel;
|
||||
mouseY += event.motion.yrel;
|
||||
} break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -64,18 +64,18 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_Rect viewport)
|
||||
int text_length;
|
||||
int x, y;
|
||||
int table_top;
|
||||
SDL_Point mouse_pos = { -1, -1 };
|
||||
SDL_FPoint mouse_pos = { -1.0f, -1.0f };
|
||||
|
||||
/* Get mouse position */
|
||||
if (SDL_GetMouseFocus() == window) {
|
||||
int window_x, window_y;
|
||||
float window_x, window_y;
|
||||
float logical_x, logical_y;
|
||||
|
||||
SDL_GetMouseState(&window_x, &window_y);
|
||||
SDL_RenderWindowToLogical(renderer, window_x, window_y, &logical_x, &logical_y);
|
||||
|
||||
mouse_pos.x = (int)logical_x;
|
||||
mouse_pos.y = (int)logical_y;
|
||||
mouse_pos.x = logical_x;
|
||||
mouse_pos.y = logical_y;
|
||||
}
|
||||
|
||||
x = 0;
|
||||
@@ -101,7 +101,7 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_Rect viewport)
|
||||
}
|
||||
|
||||
for (i = 0; i < num_modes; ++i) {
|
||||
SDL_Rect cell_rect;
|
||||
SDL_FRect cell_rect;
|
||||
|
||||
if (0 != SDL_GetDisplayMode(display_index, i, &mode)) {
|
||||
return;
|
||||
@@ -115,12 +115,12 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_Rect viewport)
|
||||
column_chars = SDL_max(column_chars, text_length);
|
||||
|
||||
/* Check if under mouse */
|
||||
cell_rect.x = x;
|
||||
cell_rect.y = y;
|
||||
cell_rect.w = text_length * FONT_CHARACTER_SIZE;
|
||||
cell_rect.h = lineHeight;
|
||||
cell_rect.x = (float)x;
|
||||
cell_rect.y = (float)y;
|
||||
cell_rect.w = (float)(text_length * FONT_CHARACTER_SIZE);
|
||||
cell_rect.h = (float)lineHeight;
|
||||
|
||||
if (SDL_PointInRect(&mouse_pos, &cell_rect)) {
|
||||
if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) {
|
||||
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
||||
|
||||
/* Update cached mode under the mouse */
|
||||
|
||||
Reference in New Issue
Block a user