| 1 |
/* |
| 2 |
Copyright (C) 1997-2026 Sam Lantinga <[email protected]> |
| 3 |
|
| 4 |
This software is provided 'as-is', without any express or implied |
| 5 |
warranty. In no event will the authors be held liable for any damages |
| 6 |
arising from the use of this software. |
| 7 |
|
| 8 |
Permission is granted to anyone to use this software for any purpose, |
| 9 |
including commercial applications, and to alter it and redistribute it |
| 10 |
freely. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "SDL.h" |
| 14 |
|
| 15 |
#ifdef __EMSCRIPTEN__ |
| 16 |
#include <emscripten/emscripten.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
#include <stdlib.h> /* exit() */ |
| 20 |
|
| 21 |
#ifdef __3DS__ |
| 22 |
/* For mouse-based tests, we want to have the window on the touch screen */ |
| 23 |
#define SCREEN_X 40 |
| 24 |
#define SCREEN_Y 240 |
| 25 |
#define SCREEN_WIDTH 320 |
| 26 |
#define SCREEN_HEIGHT 240 |
| 27 |
#elif defined(__IPHONEOS__) |
| 28 |
#define SCREEN_WIDTH 320 |
| 29 |
#define SCREEN_HEIGHT 480 |
| 30 |
#else |
| 31 |
#define SCREEN_WIDTH 640 |
| 32 |
#define SCREEN_HEIGHT 480 |
| 33 |
#endif |
| 34 |
|
| 35 |
#ifndef SCREEN_X |
| 36 |
#define SCREEN_X SDL_WINDOWPOS_CENTERED |
| 37 |
#endif |
| 38 |
#ifndef SCREEN_Y |
| 39 |
#define SCREEN_Y SDL_WINDOWPOS_CENTERED |
| 40 |
#endif |
| 41 |
|
| 42 |
static SDL_Window *window; |
| 43 |
|
| 44 |
typedef struct _Object |
| 45 |
{ |
| 46 |
struct _Object *next; |
| 47 |
|
| 48 |
int x1, y1, x2, y2; |
| 49 |
Uint8 r, g, b; |
| 50 |
|
| 51 |
SDL_bool isRect; |
| 52 |
} Object; |
| 53 |
|
| 54 |
static Object *active = NULL; |
| 55 |
static Object *objects = NULL; |
| 56 |
static int buttons = 0; |
| 57 |
static SDL_bool isRect = SDL_FALSE; |
| 58 |
|
| 59 |
static SDL_bool wheel_x_active = SDL_FALSE; |
| 60 |
static SDL_bool wheel_y_active = SDL_FALSE; |
| 61 |
static float wheel_x = SCREEN_WIDTH * 0.5f; |
| 62 |
static float wheel_y = SCREEN_HEIGHT * 0.5f; |
| 63 |
|
| 64 |
static SDL_bool done = SDL_FALSE; |
| 65 |
|
| 66 |
void DrawObject(SDL_Renderer *renderer, Object *object) |
| 67 |
{ |
| 68 |
SDL_SetRenderDrawColor(renderer, object->r, object->g, object->b, 255); |
| 69 |
|
| 70 |
if (object->isRect) { |
| 71 |
SDL_Rect rect; |
| 72 |
|
| 73 |
if (object->x1 > object->x2) { |
| 74 |
rect.x = object->x2; |
| 75 |
rect.w = object->x1 - object->x2; |
| 76 |
} else { |
| 77 |
rect.x = object->x1; |
| 78 |
rect.w = object->x2 - object->x1; |
| 79 |
} |
| 80 |
|
| 81 |
if (object->y1 > object->y2) { |
| 82 |
rect.y = object->y2; |
| 83 |
rect.h = object->y1 - object->y2; |
| 84 |
} else { |
| 85 |
rect.y = object->y1; |
| 86 |
rect.h = object->y2 - object->y1; |
| 87 |
} |
| 88 |
|
| 89 |
/* SDL_RenderDrawRect(renderer, &rect); */ |
| 90 |
SDL_RenderFillRect(renderer, &rect); |
| 91 |
} else { |
| 92 |
SDL_RenderDrawLine(renderer, object->x1, object->y1, object->x2, object->y2); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
void DrawObjects(SDL_Renderer *renderer) |
| 97 |
{ |
| 98 |
Object *next = objects; |
| 99 |
while (next) { |
| 100 |
DrawObject(renderer, next); |
| 101 |
next = next->next; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void AppendObject(Object *object) |
| 106 |
{ |
| 107 |
if (objects) { |
| 108 |
Object *next = objects; |
| 109 |
while (next->next) { |
| 110 |
next = next->next; |
| 111 |
} |
| 112 |
next->next = object; |
| 113 |
} else { |
| 114 |
objects = object; |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
void loop(void *arg) |
| 119 |
{ |
| 120 |
SDL_Renderer *renderer = (SDL_Renderer *)arg; |
| 121 |
SDL_Event event; |
| 122 |
|
| 123 |
/* Check for events */ |
| 124 |
while (SDL_PollEvent(&event)) { |
| 125 |
switch (event.type) { |
| 126 |
case SDL_MOUSEWHEEL: |
| 127 |
if (event.wheel.direction == SDL_MOUSEWHEEL_FLIPPED) { |
| 128 |
event.wheel.preciseX *= -1.0f; |
| 129 |
event.wheel.preciseY *= -1.0f; |
| 130 |
event.wheel.x *= -1; |
| 131 |
event.wheel.y *= -1; |
| 132 |
} |
| 133 |
if (event.wheel.preciseX != 0.0f) { |
| 134 |
wheel_x_active = SDL_TRUE; |
| 135 |
/* "positive to the right and negative to the left" */ |
| 136 |
wheel_x += event.wheel.preciseX * 10.0f; |
| 137 |
} |
| 138 |
if (event.wheel.preciseY != 0.0f) { |
| 139 |
wheel_y_active = SDL_TRUE; |
| 140 |
/* "positive away from the user and negative towards the user" */ |
| 141 |
wheel_y -= event.wheel.preciseY * 10.0f; |
| 142 |
} |
| 143 |
break; |
| 144 |
|
| 145 |
case SDL_MOUSEMOTION: |
| 146 |
if (!active) { |
| 147 |
break; |
| 148 |
} |
| 149 |
|
| 150 |
active->x2 = event.motion.x; |
| 151 |
active->y2 = event.motion.y; |
| 152 |
break; |
| 153 |
|
| 154 |
case SDL_MOUSEBUTTONDOWN: |
| 155 |
if (!active) { |
| 156 |
active = SDL_calloc(1, sizeof(*active)); |
| 157 |
active->x1 = active->x2 = event.button.x; |
| 158 |
active->y1 = active->y2 = event.button.y; |
| 159 |
active->isRect = isRect; |
| 160 |
} |
| 161 |
|
| 162 |
switch (event.button.button) { |
| 163 |
case SDL_BUTTON_LEFT: |
| 164 |
active->r = 255; |
| 165 |
buttons |= SDL_BUTTON_LMASK; |
| 166 |
break; |
| 167 |
case SDL_BUTTON_MIDDLE: |
| 168 |
active->g = 255; |
| 169 |
buttons |= SDL_BUTTON_MMASK; |
| 170 |
break; |
| 171 |
case SDL_BUTTON_RIGHT: |
| 172 |
active->b = 255; |
| 173 |
buttons |= SDL_BUTTON_RMASK; |
| 174 |
break; |
| 175 |
case SDL_BUTTON_X1: |
| 176 |
active->r = 255; |
| 177 |
active->b = 255; |
| 178 |
buttons |= SDL_BUTTON_X1MASK; |
| 179 |
break; |
| 180 |
case SDL_BUTTON_X2: |
| 181 |
active->g = 255; |
| 182 |
active->b = 255; |
| 183 |
buttons |= SDL_BUTTON_X2MASK; |
| 184 |
break; |
| 185 |
} |
| 186 |
break; |
| 187 |
|
| 188 |
case SDL_MOUSEBUTTONUP: |
| 189 |
if (!active) { |
| 190 |
break; |
| 191 |
} |
| 192 |
|
| 193 |
switch (event.button.button) { |
| 194 |
case SDL_BUTTON_LEFT: |
| 195 |
buttons &= ~SDL_BUTTON_LMASK; |
| 196 |
break; |
| 197 |
case SDL_BUTTON_MIDDLE: |
| 198 |
buttons &= ~SDL_BUTTON_MMASK; |
| 199 |
break; |
| 200 |
case SDL_BUTTON_RIGHT: |
| 201 |
buttons &= ~SDL_BUTTON_RMASK; |
| 202 |
break; |
| 203 |
case SDL_BUTTON_X1: |
| 204 |
buttons &= ~SDL_BUTTON_X1MASK; |
| 205 |
break; |
| 206 |
case SDL_BUTTON_X2: |
| 207 |
buttons &= ~SDL_BUTTON_X2MASK; |
| 208 |
break; |
| 209 |
} |
| 210 |
|
| 211 |
if (buttons == 0) { |
| 212 |
AppendObject(active); |
| 213 |
active = NULL; |
| 214 |
} |
| 215 |
break; |
| 216 |
|
| 217 |
case SDL_KEYDOWN: |
| 218 |
case SDL_KEYUP: |
| 219 |
switch (event.key.keysym.sym) { |
| 220 |
case SDLK_LSHIFT: |
| 221 |
isRect = (event.key.state == SDL_PRESSED); |
| 222 |
if (active) { |
| 223 |
active->isRect = isRect; |
| 224 |
} |
| 225 |
break; |
| 226 |
} |
| 227 |
break; |
| 228 |
|
| 229 |
case SDL_QUIT: |
| 230 |
done = SDL_TRUE; |
| 231 |
break; |
| 232 |
|
| 233 |
default: |
| 234 |
break; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
| 239 |
SDL_RenderClear(renderer); |
| 240 |
|
| 241 |
/* Mouse wheel */ |
| 242 |
SDL_SetRenderDrawColor(renderer, 0, 255, 128, 255); |
| 243 |
if (wheel_x_active) { |
| 244 |
SDL_RenderDrawLine(renderer, (int)wheel_x, 0, (int)wheel_x, SCREEN_HEIGHT); |
| 245 |
} |
| 246 |
if (wheel_y_active) { |
| 247 |
SDL_RenderDrawLine(renderer, 0, (int)wheel_y, SCREEN_WIDTH, (int)wheel_y); |
| 248 |
} |
| 249 |
|
| 250 |
/* Objects from mouse clicks */ |
| 251 |
DrawObjects(renderer); |
| 252 |
if (active) { |
| 253 |
DrawObject(renderer, active); |
| 254 |
} |
| 255 |
|
| 256 |
SDL_RenderPresent(renderer); |
| 257 |
|
| 258 |
#ifdef __EMSCRIPTEN__ |
| 259 |
if (done) { |
| 260 |
emscripten_cancel_main_loop(); |
| 261 |
} |
| 262 |
#endif |
| 263 |
} |
| 264 |
|
| 265 |
int main(int argc, char *argv[]) |
| 266 |
{ |
| 267 |
SDL_Renderer *renderer; |
| 268 |
|
| 269 |
/* Enable standard application logging */ |
| 270 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 271 |
|
| 272 |
/* Initialize SDL (Note: video is required to start event loop) */ |
| 273 |
if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
| 274 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError()); |
| 275 |
exit(1); |
| 276 |
} |
| 277 |
|
| 278 |
/* Create a window to display joystick axis position */ |
| 279 |
window = SDL_CreateWindow("Mouse Test", SCREEN_X, SCREEN_Y, SCREEN_WIDTH, SCREEN_HEIGHT, 0); |
| 280 |
if (!window) { |
| 281 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s\n", SDL_GetError()); |
| 282 |
return SDL_FALSE; |
| 283 |
} |
| 284 |
|
| 285 |
renderer = SDL_CreateRenderer(window, -1, 0); |
| 286 |
if (!renderer) { |
| 287 |
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n", SDL_GetError()); |
| 288 |
SDL_DestroyWindow(window); |
| 289 |
return SDL_FALSE; |
| 290 |
} |
| 291 |
|
| 292 |
/* Main render loop */ |
| 293 |
#ifdef __EMSCRIPTEN__ |
| 294 |
emscripten_set_main_loop_arg(loop, renderer, 0, 1); |
| 295 |
#else |
| 296 |
while (!done) { |
| 297 |
loop(renderer); |
| 298 |
} |
| 299 |
#endif |
| 300 |
|
| 301 |
SDL_DestroyRenderer(renderer); |
| 302 |
SDL_DestroyWindow(window); |
| 303 |
|
| 304 |
SDL_Quit(); |
| 305 |
|
| 306 |
return 0; |
| 307 |
} |
| 308 |
|
| 309 |
/* vi: set ts=4 sw=4 expandtab: */ |