123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312 |
- /*
- Copyright (C) 1997-2023 Sam Lantinga <[email protected]>
- 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.
- */
- #include <SDL3/SDL.h>
- #include <SDL3/SDL_main.h>
- #include <SDL3/SDL_test.h>
- #ifdef __EMSCRIPTEN__
- #include <emscripten/emscripten.h>
- #endif
- #include <stdlib.h> /* exit() */
- #ifdef __IOS__
- #define SCREEN_WIDTH 320
- #define SCREEN_HEIGHT 480
- #else
- #define SCREEN_WIDTH 640
- #define SCREEN_HEIGHT 480
- #endif
- static SDL_Window *window;
- typedef struct _Object
- {
- struct _Object *next;
- float x1, y1, x2, y2;
- Uint8 r, g, b;
- SDL_bool isRect;
- } Object;
- static Object *active = NULL;
- static Object *objects = NULL;
- static int buttons = 0;
- static SDL_bool isRect = SDL_FALSE;
- static SDL_bool wheel_x_active = SDL_FALSE;
- static SDL_bool wheel_y_active = SDL_FALSE;
- static float wheel_x = SCREEN_WIDTH * 0.5f;
- static float wheel_y = SCREEN_HEIGHT * 0.5f;
- struct mouse_loop_data {
- SDL_bool done;
- SDL_Renderer *renderer;
- };
- static void DrawObject(SDL_Renderer *renderer, Object *object)
- {
- SDL_SetRenderDrawColor(renderer, object->r, object->g, object->b, 255);
- if (object->isRect) {
- SDL_FRect rect;
- 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;
- }
- SDL_RenderFillRect(renderer, &rect);
- } else {
- SDL_RenderLine(renderer, object->x1, object->y1, object->x2, object->y2);
- }
- }
- static void DrawObjects(SDL_Renderer *renderer)
- {
- Object *next = objects;
- while (next != NULL) {
- DrawObject(renderer, next);
- next = next->next;
- }
- }
- static void AppendObject(Object *object)
- {
- if (objects) {
- Object *next = objects;
- while (next->next != NULL) {
- next = next->next;
- }
- next->next = object;
- } else {
- objects = object;
- }
- }
- static void loop(void *arg)
- {
- struct mouse_loop_data *loop_data = (struct mouse_loop_data *)arg;
- SDL_Event event;
- SDL_Renderer *renderer = loop_data->renderer;
- /* Check for events */
- while (SDL_PollEvent(&event)) {
- switch (event.type) {
- case SDL_EVENT_MOUSE_WHEEL:
- if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) {
- event.wheel.x *= -1;
- event.wheel.y *= -1;
- }
- if (event.wheel.x != 0.0f) {
- wheel_x_active = SDL_TRUE;
- /* "positive to the right and negative to the left" */
- wheel_x += event.wheel.x * 10.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.x * 10.0f;
- }
- break;
- case SDL_EVENT_MOUSE_MOTION:
- if (active == NULL) {
- break;
- }
- active->x2 = event.motion.x;
- active->y2 = event.motion.y;
- break;
- case SDL_EVENT_MOUSE_BUTTON_DOWN:
- if (active == NULL) {
- active = SDL_calloc(1, sizeof(*active));
- active->x1 = active->x2 = event.button.x;
- active->y1 = active->y2 = event.button.y;
- active->isRect = isRect;
- }
- switch (event.button.button) {
- 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;
- }
- break;
- case SDL_EVENT_MOUSE_BUTTON_UP:
- if (active == NULL) {
- break;
- }
- switch (event.button.button) {
- 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;
- }
- if (buttons == 0) {
- AppendObject(active);
- active = NULL;
- }
- break;
- case SDL_EVENT_KEY_DOWN:
- case SDL_EVENT_KEY_UP:
- switch (event.key.keysym.sym) {
- case SDLK_LSHIFT:
- isRect = (event.key.state == SDL_PRESSED);
- if (active) {
- active->isRect = isRect;
- }
- break;
- }
- break;
- case SDL_EVENT_QUIT:
- loop_data->done = SDL_TRUE;
- break;
- default:
- break;
- }
- }
- SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
- SDL_RenderClear(renderer);
- /* Mouse wheel */
- SDL_SetRenderDrawColor(renderer, 0, 255, 128, 255);
- if (wheel_x_active) {
- SDL_RenderLine(renderer, wheel_x, 0.0f, wheel_x, (float)SCREEN_HEIGHT);
- }
- if (wheel_y_active) {
- SDL_RenderLine(renderer, 0.0f, wheel_y, (float)SCREEN_WIDTH, wheel_y);
- }
- /* Objects from mouse clicks */
- DrawObjects(renderer);
- if (active) {
- DrawObject(renderer, active);
- }
- SDL_RenderPresent(renderer);
- #ifdef __EMSCRIPTEN__
- if (loop_data->done) {
- emscripten_cancel_main_loop();
- }
- #endif
- }
- int main(int argc, char *argv[])
- {
- struct mouse_loop_data loop_data;
- SDLTest_CommonState *state;
- /* Initialize test framework */
- state = SDLTest_CommonCreateState(argv, 0);
- if (state == NULL) {
- return 1;
- }
- /* Enable standard application logging */
- SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
- /* Parse commandline */
- if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
- return 1;
- }
- /* Initialize SDL (Note: video is required to start event loop) */
- if (SDL_Init(SDL_INIT_VIDEO) < 0) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
- exit(1);
- }
- /* Create a window to display joystick axis position */
- window = SDL_CreateWindow("Mouse Test", SCREEN_WIDTH, SCREEN_HEIGHT, 0);
- if (!window) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError());
- return 0;
- }
- loop_data.done = SDL_FALSE;
- loop_data.renderer = SDL_CreateRenderer(window, NULL, 0);
- if (!loop_data.renderer) {
- SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError());
- SDL_DestroyWindow(window);
- return 0;
- }
- /* Main render loop */
- #ifdef __EMSCRIPTEN__
- emscripten_set_main_loop_arg(loop, &loop_data, 0, 1);
- #else
- while (loop_data.done == SDL_FALSE) {
- loop(&loop_data);
- }
- #endif
- SDL_DestroyRenderer(loop_data.renderer);
- SDL_DestroyWindow(window);
- SDL_Quit();
- SDLTest_CommonDestroyState(state);
- return 0;
- }
|