14
#include <SDL3/SDL_main.h>
15
#include <SDL3/SDL_test.h>
17
#ifdef SDL_PLATFORM_EMSCRIPTEN
18
#include <emscripten/emscripten.h>
23
#ifdef SDL_PLATFORM_3DS
27
#define SCREEN_WIDTH 320
28
#define SCREEN_HEIGHT 240
29
#elif defined(SDL_PLATFORM_IOS)
30
#define SCREEN_WIDTH 320
31
#define SCREEN_HEIGHT 480
33
#define SCREEN_WIDTH 640
34
#define SCREEN_HEIGHT 480
37
static SDL_Window *window;
49
static Object *active = NULL;
50
static Object *objects = NULL;
51
static int buttons = 0;
52
static SDL_bool isRect = SDL_FALSE;
54
static SDL_bool wheel_x_active = SDL_FALSE;
55
static SDL_bool wheel_y_active = SDL_FALSE;
56
static float wheel_x = SCREEN_WIDTH * 0.5f;
57
static float wheel_y = SCREEN_HEIGHT * 0.5f;
59
struct mouse_loop_data {
61
SDL_Renderer *renderer;
64
static void DrawObject(SDL_Renderer *renderer, Object *object)
66
SDL_SetRenderDrawColor(renderer, object->r, object->g, object->b, 255);
71
if (object->x1 > object->x2) {
73
rect.w = object->x1 - object->x2;
76
rect.w = object->x2 - object->x1;
79
if (object->y1 > object->y2) {
81
rect.h = object->y1 - object->y2;
84
rect.h = object->y2 - object->y1;
87
SDL_RenderFillRect(renderer, &rect);
89
SDL_RenderLine(renderer, object->x1, object->y1, object->x2, object->y2);
93
static void DrawObjects(SDL_Renderer *renderer)
95
Object *next = objects;
97
DrawObject(renderer, next);
102
static void AppendObject(Object *object)
105
Object *next = objects;
115
static void loop(void *arg)
117
struct mouse_loop_data *loop_data = (struct mouse_loop_data *)arg;
119
SDL_Renderer *renderer = loop_data->renderer;
122
while (SDL_PollEvent(&event)) {
123
switch (event.type) {
124
case SDL_EVENT_MOUSE_WHEEL:
125
if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
129
if (event.wheel.x != 0.0f) {
130
wheel_x_active = SDL_TRUE;
132
wheel_x += event.wheel.x * 10.0f;
134
if (event.wheel.y != 0.0f) {
135
wheel_y_active = SDL_TRUE;
137
wheel_y -= event.wheel.y * 10.0f;
141
case SDL_EVENT_MOUSE_MOTION:
146
active->x2 = event.motion.x;
147
active->y2 = event.motion.y;
150
case SDL_EVENT_MOUSE_BUTTON_DOWN:
152
active = SDL_calloc(1, sizeof(*active));
153
active->x1 = active->x2 = event.button.x;
154
active->y1 = active->y2 = event.button.y;
155
active->isRect = isRect;
158
switch (event.button.button) {
159
case SDL_BUTTON_LEFT:
161
buttons |= SDL_BUTTON_LMASK;
163
case SDL_BUTTON_MIDDLE:
165
buttons |= SDL_BUTTON_MMASK;
167
case SDL_BUTTON_RIGHT:
169
buttons |= SDL_BUTTON_RMASK;
174
buttons |= SDL_BUTTON_X1MASK;
179
buttons |= SDL_BUTTON_X2MASK;
184
case SDL_EVENT_MOUSE_BUTTON_UP:
189
switch (event.button.button) {
190
case SDL_BUTTON_LEFT:
191
buttons &= ~SDL_BUTTON_LMASK;
193
case SDL_BUTTON_MIDDLE:
194
buttons &= ~SDL_BUTTON_MMASK;
196
case SDL_BUTTON_RIGHT:
197
buttons &= ~SDL_BUTTON_RMASK;
200
buttons &= ~SDL_BUTTON_X1MASK;
203
buttons &= ~SDL_BUTTON_X2MASK;
208
AppendObject(active);
213
case SDL_EVENT_KEY_DOWN:
214
if (event.key.key == SDLK_C) {
216
SDL_GetWindowPosition(window, &x, &y);
217
SDL_GetWindowSize(window, &w, &h);
221
if (event.key.mod & SDL_KMOD_ALT) {
222
SDL_WarpMouseGlobal((float)(x + w), (float)(y + h));
224
SDL_WarpMouseInWindow(window, (float)w, (float)h);
228
case SDL_EVENT_KEY_UP:
229
switch (event.key.key) {
231
isRect = (event.key.state == SDL_PRESSED);
233
active->isRect = isRect;
242
loop_data->done = SDL_TRUE;
250
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
251
SDL_RenderClear(renderer);
254
SDL_SetRenderDrawColor(renderer, 0, 255, 128, 255);
255
if (wheel_x_active) {
256
SDL_RenderLine(renderer, wheel_x, 0.0f, wheel_x, (float)SCREEN_HEIGHT);
258
if (wheel_y_active) {
259
SDL_RenderLine(renderer, 0.0f, wheel_y, (float)SCREEN_WIDTH, wheel_y);
263
DrawObjects(renderer);
265
DrawObject(renderer, active);
268
SDL_RenderPresent(renderer);
270
#ifdef SDL_PLATFORM_EMSCRIPTEN
271
if (loop_data->done) {
272
emscripten_cancel_main_loop();
277
int main(int argc, char *argv[])
279
struct mouse_loop_data loop_data;
280
SDLTest_CommonState *state;
281
#ifdef SDL_PLATFORM_3DS
282
SDL_PropertiesID props;
286
state = SDLTest_CommonCreateState(argv, 0);
292
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
295
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
300
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
301
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
306
#ifdef SDL_PLATFORM_3DS
307
props = SDL_CreateProperties();
308
SDL_SetStringProperty(props, SDL_PROP_WINDOW_CREATE_TITLE_STRING, "Mouse Test");
309
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_X_NUMBER, SCREEN_X);
310
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_Y_NUMBER, SCREEN_Y);
311
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_WIDTH_NUMBER, SCREEN_WIDTH);
312
SDL_SetNumberProperty(props, SDL_PROP_WINDOW_CREATE_HEIGHT_NUMBER, SCREEN_HEIGHT);
313
SDL_SetNumberProperty(props, "flags", 0);
314
window = SDL_CreateWindowWithProperties(props);
316
window = SDL_CreateWindow("Mouse Test", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
319
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
323
loop_data.done = SDL_FALSE;
325
loop_data.renderer = SDL_CreateRenderer(window, NULL);
326
if (!loop_data.renderer) {
327
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
328
SDL_DestroyWindow(window);
333
#ifdef SDL_PLATFORM_EMSCRIPTEN
334
emscripten_set_main_loop_arg(loop, &loop_data, 0, 1);
336
while (loop_data.done == SDL_FALSE) {
341
SDL_DestroyRenderer(loop_data.renderer);
342
SDL_DestroyWindow(window);
345
SDLTest_CommonDestroyState(state);