2021-09-10 04:33:35 +08:00
|
|
|
/*
|
2024-01-02 05:15:26 +08:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2021-09-10 04:33:35 +08:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
2022-11-27 12:43:38 +08:00
|
|
|
#include <SDL3/SDL.h>
|
2022-12-15 12:58:20 +08:00
|
|
|
#include <SDL3/SDL_main.h>
|
2023-03-17 07:25:39 +08:00
|
|
|
#include <SDL3/SDL_test.h>
|
2021-09-10 04:33:35 +08:00
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2021-09-10 04:33:35 +08:00
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2021-11-14 02:29:50 +08:00
|
|
|
#include <stdlib.h> /* exit() */
|
|
|
|
|
2024-04-14 06:23:18 +08:00
|
|
|
#ifdef SDL_PLATFORM_3DS
|
|
|
|
/* For mouse-based tests, we want to have the window on the touch screen */
|
|
|
|
#define SCREEN_X 40
|
|
|
|
#define SCREEN_Y 240
|
|
|
|
#define SCREEN_WIDTH 320
|
|
|
|
#define SCREEN_HEIGHT 240
|
|
|
|
#elif defined(SDL_PLATFORM_IOS)
|
|
|
|
#define SCREEN_WIDTH 320
|
|
|
|
#define SCREEN_HEIGHT 480
|
2021-09-10 04:33:35 +08:00
|
|
|
#else
|
2022-12-01 04:51:59 +08:00
|
|
|
#define SCREEN_WIDTH 640
|
|
|
|
#define SCREEN_HEIGHT 480
|
2021-09-10 04:33:35 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static SDL_Window *window;
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
typedef struct _Object
|
|
|
|
{
|
2022-09-05 23:27:25 +08:00
|
|
|
struct _Object *next;
|
2021-09-10 04:33:35 +08:00
|
|
|
|
2022-12-30 11:31:12 +08:00
|
|
|
float x1, y1, x2, y2;
|
2021-09-10 04:33:35 +08:00
|
|
|
Uint8 r, g, b;
|
|
|
|
|
2024-09-18 22:52:28 +08:00
|
|
|
bool isRect;
|
2022-09-05 23:27:25 +08:00
|
|
|
} Object;
|
|
|
|
|
|
|
|
static Object *active = NULL;
|
|
|
|
static Object *objects = NULL;
|
2021-09-10 04:33:35 +08:00
|
|
|
static int buttons = 0;
|
2024-09-18 22:52:28 +08:00
|
|
|
static bool isRect = false;
|
2021-09-10 04:33:35 +08:00
|
|
|
|
2024-09-18 22:52:28 +08:00
|
|
|
static bool wheel_x_active = false;
|
|
|
|
static bool wheel_y_active = false;
|
2022-04-03 05:28:56 +08:00
|
|
|
static float wheel_x = SCREEN_WIDTH * 0.5f;
|
|
|
|
static float wheel_y = SCREEN_HEIGHT * 0.5f;
|
|
|
|
|
2023-03-17 07:25:39 +08:00
|
|
|
struct mouse_loop_data {
|
2024-09-18 22:52:28 +08:00
|
|
|
bool done;
|
2023-03-17 07:25:39 +08:00
|
|
|
SDL_Renderer *renderer;
|
|
|
|
};
|
2021-09-10 04:33:35 +08:00
|
|
|
|
2023-03-08 18:40:07 +08:00
|
|
|
static void DrawObject(SDL_Renderer *renderer, Object *object)
|
2021-09-10 04:33:35 +08:00
|
|
|
{
|
2022-09-05 23:27:25 +08:00
|
|
|
SDL_SetRenderDrawColor(renderer, object->r, object->g, object->b, 255);
|
|
|
|
|
|
|
|
if (object->isRect) {
|
2022-12-30 11:31:12 +08:00
|
|
|
SDL_FRect rect;
|
2022-09-05 23:27:25 +08:00
|
|
|
|
|
|
|
if (object->x1 > object->x2) {
|
|
|
|
rect.x = object->x2;
|
|
|
|
rect.w = object->x1 - object->x2;
|
|
|
|
} else {
|
|
|
|
rect.x = object->x1;
|
|
|
|
rect.w = object->x2 - object->x1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object->y1 > object->y2) {
|
|
|
|
rect.y = object->y2;
|
|
|
|
rect.h = object->y1 - object->y2;
|
|
|
|
} else {
|
|
|
|
rect.y = object->y1;
|
|
|
|
rect.h = object->y2 - object->y1;
|
|
|
|
}
|
|
|
|
|
2023-01-01 03:19:32 +08:00
|
|
|
SDL_RenderFillRect(renderer, &rect);
|
2022-09-05 23:27:25 +08:00
|
|
|
} else {
|
2023-01-01 03:19:32 +08:00
|
|
|
SDL_RenderLine(renderer, object->x1, object->y1, object->x2, object->y2);
|
2022-09-05 23:27:25 +08:00
|
|
|
}
|
2021-09-10 04:33:35 +08:00
|
|
|
}
|
|
|
|
|
2023-03-08 18:40:07 +08:00
|
|
|
static void DrawObjects(SDL_Renderer *renderer)
|
2021-09-10 04:33:35 +08:00
|
|
|
{
|
2022-09-05 23:27:25 +08:00
|
|
|
Object *next = objects;
|
2023-11-10 05:29:15 +08:00
|
|
|
while (next) {
|
2022-09-05 23:27:25 +08:00
|
|
|
DrawObject(renderer, next);
|
2021-09-10 04:33:35 +08:00
|
|
|
next = next->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 18:40:07 +08:00
|
|
|
static void AppendObject(Object *object)
|
2021-09-10 04:33:35 +08:00
|
|
|
{
|
2022-09-05 23:27:25 +08:00
|
|
|
if (objects) {
|
|
|
|
Object *next = objects;
|
2023-11-10 05:29:15 +08:00
|
|
|
while (next->next) {
|
2021-09-10 04:33:35 +08:00
|
|
|
next = next->next;
|
|
|
|
}
|
2022-09-05 23:27:25 +08:00
|
|
|
next->next = object;
|
2021-09-10 04:33:35 +08:00
|
|
|
} else {
|
2022-09-05 23:27:25 +08:00
|
|
|
objects = object;
|
2021-09-10 04:33:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 18:40:07 +08:00
|
|
|
static void loop(void *arg)
|
2021-09-10 04:33:35 +08:00
|
|
|
{
|
2023-03-17 07:25:39 +08:00
|
|
|
struct mouse_loop_data *loop_data = (struct mouse_loop_data *)arg;
|
2021-09-10 04:33:35 +08:00
|
|
|
SDL_Event event;
|
2023-03-17 07:25:39 +08:00
|
|
|
SDL_Renderer *renderer = loop_data->renderer;
|
2021-09-10 04:33:35 +08:00
|
|
|
|
|
|
|
/* Check for events */
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
switch (event.type) {
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_MOUSE_WHEEL:
|
2022-12-01 04:51:59 +08:00
|
|
|
if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
|
|
|
|
event.wheel.x *= -1;
|
|
|
|
event.wheel.y *= -1;
|
|
|
|
}
|
2022-12-30 11:31:12 +08:00
|
|
|
if (event.wheel.x != 0.0f) {
|
2024-09-18 22:52:28 +08:00
|
|
|
wheel_x_active = true;
|
2022-12-01 04:51:59 +08:00
|
|
|
/* "positive to the right and negative to the left" */
|
2022-12-30 11:31:12 +08:00
|
|
|
wheel_x += event.wheel.x * 10.0f;
|
2022-12-01 04:51:59 +08:00
|
|
|
}
|
2023-11-29 06:15:18 +08:00
|
|
|
if (event.wheel.y != 0.0f) {
|
2024-09-18 22:52:28 +08:00
|
|
|
wheel_y_active = true;
|
2022-12-01 04:51:59 +08:00
|
|
|
/* "positive away from the user and negative towards the user" */
|
2023-11-29 06:15:18 +08:00
|
|
|
wheel_y -= event.wheel.y * 10.0f;
|
2022-12-01 04:51:59 +08:00
|
|
|
}
|
|
|
|
break;
|
2022-04-03 05:28:56 +08:00
|
|
|
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_MOUSE_MOTION:
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!active) {
|
2021-09-10 04:33:35 +08:00
|
|
|
break;
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2021-09-10 04:33:35 +08:00
|
|
|
|
|
|
|
active->x2 = event.motion.x;
|
|
|
|
active->y2 = event.motion.y;
|
|
|
|
break;
|
|
|
|
|
2023-01-30 11:06:08 +08:00
|
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!active) {
|
2021-09-10 04:33:35 +08:00
|
|
|
active = SDL_calloc(1, sizeof(*active));
|
|
|
|
active->x1 = active->x2 = event.button.x;
|
|
|
|
active->y1 = active->y2 = event.button.y;
|
2022-09-05 23:27:25 +08:00
|
|
|
active->isRect = isRect;
|
2021-09-10 04:33:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (event.button.button) {
|
2022-12-01 04:51:59 +08:00
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
active->r = 255;
|
|
|
|
buttons |= SDL_BUTTON_LMASK;
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_MIDDLE:
|
|
|
|
active->g = 255;
|
|
|
|
buttons |= SDL_BUTTON_MMASK;
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_RIGHT:
|
|
|
|
active->b = 255;
|
|
|
|
buttons |= SDL_BUTTON_RMASK;
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_X1:
|
|
|
|
active->r = 255;
|
|
|
|
active->b = 255;
|
|
|
|
buttons |= SDL_BUTTON_X1MASK;
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_X2:
|
|
|
|
active->g = 255;
|
|
|
|
active->b = 255;
|
|
|
|
buttons |= SDL_BUTTON_X2MASK;
|
|
|
|
break;
|
2021-09-10 04:33:35 +08:00
|
|
|
}
|
|
|
|
break;
|
2022-09-05 23:27:25 +08:00
|
|
|
|
2023-01-30 11:06:08 +08:00
|
|
|
case SDL_EVENT_MOUSE_BUTTON_UP:
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!active) {
|
2021-09-10 04:33:35 +08:00
|
|
|
break;
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2021-09-10 04:33:35 +08:00
|
|
|
|
|
|
|
switch (event.button.button) {
|
2022-12-01 04:51:59 +08:00
|
|
|
case SDL_BUTTON_LEFT:
|
|
|
|
buttons &= ~SDL_BUTTON_LMASK;
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_MIDDLE:
|
|
|
|
buttons &= ~SDL_BUTTON_MMASK;
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_RIGHT:
|
|
|
|
buttons &= ~SDL_BUTTON_RMASK;
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_X1:
|
|
|
|
buttons &= ~SDL_BUTTON_X1MASK;
|
|
|
|
break;
|
|
|
|
case SDL_BUTTON_X2:
|
|
|
|
buttons &= ~SDL_BUTTON_X2MASK;
|
|
|
|
break;
|
2021-09-10 04:33:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buttons == 0) {
|
2022-09-05 23:27:25 +08:00
|
|
|
AppendObject(active);
|
2021-09-10 04:33:35 +08:00
|
|
|
active = NULL;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_KEY_DOWN:
|
2024-07-01 11:05:22 +08:00
|
|
|
if (event.key.key == SDLK_C) {
|
2024-05-02 22:16:36 +08:00
|
|
|
int x, y, w, h;
|
|
|
|
SDL_GetWindowPosition(window, &x, &y);
|
|
|
|
SDL_GetWindowSize(window, &w, &h);
|
|
|
|
w /= 2;
|
|
|
|
h /= 2;
|
|
|
|
|
2024-06-22 10:50:10 +08:00
|
|
|
if (event.key.mod & SDL_KMOD_ALT) {
|
2024-05-02 22:16:36 +08:00
|
|
|
SDL_WarpMouseGlobal((float)(x + w), (float)(y + h));
|
|
|
|
} else {
|
|
|
|
SDL_WarpMouseInWindow(window, (float)w, (float)h);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
SDL_FALLTHROUGH;
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_KEY_UP:
|
2024-06-22 10:50:10 +08:00
|
|
|
switch (event.key.key) {
|
2022-09-05 23:27:25 +08:00
|
|
|
case SDLK_LSHIFT:
|
2024-09-10 00:18:02 +08:00
|
|
|
isRect = event.key.down;
|
2022-09-05 23:27:25 +08:00
|
|
|
if (active) {
|
|
|
|
active->isRect = isRect;
|
|
|
|
}
|
|
|
|
break;
|
2024-03-07 02:35:18 +08:00
|
|
|
default:
|
|
|
|
break;
|
2022-09-05 23:27:25 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2023-01-24 09:54:09 +08:00
|
|
|
case SDL_EVENT_QUIT:
|
2024-09-18 22:52:28 +08:00
|
|
|
loop_data->done = true;
|
2021-09-10 04:33:35 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
|
2022-04-03 05:28:56 +08:00
|
|
|
/* Mouse wheel */
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 255, 128, 255);
|
|
|
|
if (wheel_x_active) {
|
2023-01-01 03:19:32 +08:00
|
|
|
SDL_RenderLine(renderer, wheel_x, 0.0f, wheel_x, (float)SCREEN_HEIGHT);
|
2022-04-03 05:28:56 +08:00
|
|
|
}
|
|
|
|
if (wheel_y_active) {
|
2023-01-01 03:19:32 +08:00
|
|
|
SDL_RenderLine(renderer, 0.0f, wheel_y, (float)SCREEN_WIDTH, wheel_y);
|
2022-04-03 05:28:56 +08:00
|
|
|
}
|
|
|
|
|
2022-09-05 23:27:25 +08:00
|
|
|
/* Objects from mouse clicks */
|
|
|
|
DrawObjects(renderer);
|
2022-11-28 00:38:43 +08:00
|
|
|
if (active) {
|
2022-09-05 23:27:25 +08:00
|
|
|
DrawObject(renderer, active);
|
2022-11-28 00:38:43 +08:00
|
|
|
}
|
2021-09-10 04:33:35 +08:00
|
|
|
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2023-03-17 07:25:39 +08:00
|
|
|
if (loop_data->done) {
|
2021-09-10 04:33:35 +08:00
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-12-01 04:51:59 +08:00
|
|
|
int main(int argc, char *argv[])
|
2021-09-10 04:33:35 +08:00
|
|
|
{
|
2023-03-17 07:25:39 +08:00
|
|
|
struct mouse_loop_data loop_data;
|
|
|
|
SDLTest_CommonState *state;
|
2024-04-14 06:23:18 +08:00
|
|
|
#ifdef SDL_PLATFORM_3DS
|
|
|
|
SDL_PropertiesID props;
|
|
|
|
#endif
|
2023-03-17 07:25:39 +08:00
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, 0);
|
2023-11-10 05:29:15 +08:00
|
|
|
if (!state) {
|
2023-03-17 07:25:39 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2021-11-23 19:01:10 +08:00
|
|
|
|
2023-03-17 07:25:39 +08:00
|
|
|
/* Parse commandline */
|
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-09-10 04:33:35 +08:00
|
|
|
/* Initialize SDL (Note: video is required to start event loop) */
|
2024-08-23 08:33:49 +08:00
|
|
|
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
2021-09-10 04:33:35 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create a window to display joystick axis position */
|
2024-04-14 06:23:18 +08:00
|
|
|
#ifdef SDL_PLATFORM_3DS
|
|
|
|
props = SDL_CreateProperties();
|
|
|
|
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, "Mouse Test");
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SCREEN_X);
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SCREEN_Y);
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, SCREEN_WIDTH);
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, SCREEN_HEIGHT);
|
|
|
|
SDL_SetNumberProperty(props, "flags", 0);
|
|
|
|
window = SDL_CreateWindowWithProperties(props);
|
|
|
|
#else
|
2023-03-06 06:44:38 +08:00
|
|
|
window = SDL_CreateWindow("Mouse Test", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
|
2024-04-14 06:23:18 +08:00
|
|
|
#endif
|
2023-03-17 07:25:39 +08:00
|
|
|
if (!window) {
|
2021-09-10 04:33:35 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
|
2023-03-17 07:25:39 +08:00
|
|
|
return 0;
|
2021-09-10 04:33:35 +08:00
|
|
|
}
|
|
|
|
|
2024-09-18 22:52:28 +08:00
|
|
|
loop_data.done = false;
|
2023-03-17 07:25:39 +08:00
|
|
|
|
2024-05-14 01:31:55 +08:00
|
|
|
loop_data.renderer = SDL_CreateRenderer(window, NULL);
|
2023-03-17 07:25:39 +08:00
|
|
|
if (!loop_data.renderer) {
|
2021-09-10 04:33:35 +08:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
|
|
|
|
SDL_DestroyWindow(window);
|
2023-03-17 07:25:39 +08:00
|
|
|
return 0;
|
2021-09-10 04:33:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Main render loop */
|
2024-01-24 09:40:51 +08:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2023-03-17 07:25:39 +08:00
|
|
|
emscripten_set_main_loop_arg(loop, &loop_data, 0, 1);
|
2021-09-10 04:33:35 +08:00
|
|
|
#else
|
2024-09-18 22:52:28 +08:00
|
|
|
while (loop_data.done == false) {
|
2023-03-17 07:25:39 +08:00
|
|
|
loop(&loop_data);
|
2021-09-10 04:33:35 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2023-03-17 07:25:39 +08:00
|
|
|
SDL_DestroyRenderer(loop_data.renderer);
|
2021-09-10 04:33:35 +08:00
|
|
|
SDL_DestroyWindow(window);
|
|
|
|
|
|
|
|
SDL_Quit();
|
2023-03-17 07:25:39 +08:00
|
|
|
SDLTest_CommonDestroyState(state);
|
2021-09-10 04:33:35 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|