| 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 |
/* Usage: |
| 14 |
* Spacebar to begin recording a gesture on all touches. |
| 15 |
* s to save all touches into "./gestureSave" |
| 16 |
* l to load all touches from "./gestureSave" |
| 17 |
*/ |
| 18 |
|
| 19 |
#include "SDL.h" |
| 20 |
#include <stdlib.h> /* for exit() */ |
| 21 |
|
| 22 |
#ifdef __EMSCRIPTEN__ |
| 23 |
#include <emscripten/emscripten.h> |
| 24 |
#endif |
| 25 |
|
| 26 |
#include "SDL_test.h" |
| 27 |
#include "SDL_test_common.h" |
| 28 |
|
| 29 |
#define WIDTH 640 |
| 30 |
#define HEIGHT 480 |
| 31 |
#define BPP 4 |
| 32 |
|
| 33 |
/* MUST BE A POWER OF 2! */ |
| 34 |
#define EVENT_BUF_SIZE 256 |
| 35 |
|
| 36 |
#define VERBOSE 0 |
| 37 |
|
| 38 |
static SDLTest_CommonState *state; |
| 39 |
static SDL_Event events[EVENT_BUF_SIZE]; |
| 40 |
static int eventWrite; |
| 41 |
static int colors[7] = { 0xFF, 0xFF00, 0xFF0000, 0xFFFF00, 0x00FFFF, 0xFF00FF, 0xFFFFFF }; |
| 42 |
static int quitting = 0; |
| 43 |
|
| 44 |
typedef struct |
| 45 |
{ |
| 46 |
float x, y; |
| 47 |
} Point; |
| 48 |
|
| 49 |
typedef struct |
| 50 |
{ |
| 51 |
float ang, r; |
| 52 |
Point p; |
| 53 |
} Knob; |
| 54 |
|
| 55 |
static Knob knob = { 0.0f, 0.1f, { 0.0f, 0.0f } }; |
| 56 |
|
| 57 |
static void |
| 58 |
setpix(SDL_Surface *screen, float _x, float _y, unsigned int col) |
| 59 |
{ |
| 60 |
Uint32 *pixmem32; |
| 61 |
Uint32 colour; |
| 62 |
Uint8 r, g, b; |
| 63 |
const int x = (int)_x; |
| 64 |
const int y = (int)_y; |
| 65 |
float a; |
| 66 |
|
| 67 |
if ((x < 0) || (x >= screen->w) || (y < 0) || (y >= screen->h)) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
pixmem32 = (Uint32 *)screen->pixels + y * screen->pitch / BPP + x; |
| 72 |
|
| 73 |
SDL_memcpy(&colour, pixmem32, screen->format->BytesPerPixel); |
| 74 |
|
| 75 |
SDL_GetRGB(colour, screen->format, &r, &g, &b); |
| 76 |
|
| 77 |
/* r = 0;g = 0; b = 0; */ |
| 78 |
a = (float)((col >> 24) & 0xFF); |
| 79 |
if (a == 0) { |
| 80 |
a = 0xFF; /* Hack, to make things easier. */ |
| 81 |
} |
| 82 |
|
| 83 |
a = (a == 0.0f) ? 1 : (a / 255.0f); |
| 84 |
r = (Uint8)(r * (1 - a) + ((col >> 16) & 0xFF) * a); |
| 85 |
g = (Uint8)(g * (1 - a) + ((col >> 8) & 0xFF) * a); |
| 86 |
b = (Uint8)(b * (1 - a) + ((col >> 0) & 0xFF) * a); |
| 87 |
colour = SDL_MapRGB(screen->format, r, g, b); |
| 88 |
|
| 89 |
*pixmem32 = colour; |
| 90 |
} |
| 91 |
|
| 92 |
#if 0 /* unused */ |
| 93 |
static void |
| 94 |
drawLine(SDL_Surface *screen, float x0, float y0, float x1, float y1, unsigned int col) |
| 95 |
{ |
| 96 |
float t; |
| 97 |
for (t = 0; t < 1; t += (float) (1.0f / SDL_max(SDL_fabs(x0 - x1), SDL_fabs(y0 - y1)))) { |
| 98 |
setpix(screen, x1 + t * (x0 - x1), y1 + t * (y0 - y1), col); |
| 99 |
} |
| 100 |
} |
| 101 |
#endif |
| 102 |
|
| 103 |
static void |
| 104 |
drawCircle(SDL_Surface *screen, float x, float y, float r, unsigned int c) |
| 105 |
{ |
| 106 |
float tx, ty, xr; |
| 107 |
for (ty = (float)-SDL_fabs(r); ty <= (float)SDL_fabs((int)r); ty++) { |
| 108 |
xr = (float)SDL_sqrt(r * r - ty * ty); |
| 109 |
if (r > 0) { /* r > 0 ==> filled circle */ |
| 110 |
for (tx = -xr + 0.5f; tx <= xr - 0.5f; tx++) { |
| 111 |
setpix(screen, x + tx, y + ty, c); |
| 112 |
} |
| 113 |
} else { |
| 114 |
setpix(screen, x - xr + 0.5f, y + ty, c); |
| 115 |
setpix(screen, x + xr - 0.5f, y + ty, c); |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
static void |
| 121 |
drawKnob(SDL_Surface *screen, const Knob *k) |
| 122 |
{ |
| 123 |
drawCircle(screen, k->p.x * screen->w, k->p.y * screen->h, k->r * screen->w, 0xFFFFFF); |
| 124 |
drawCircle(screen, (k->p.x + k->r / 2 * SDL_cosf(k->ang)) * screen->w, |
| 125 |
(k->p.y + k->r / 2 * SDL_sinf(k->ang)) * screen->h, k->r / 4 * screen->w, 0); |
| 126 |
} |
| 127 |
|
| 128 |
static void |
| 129 |
DrawScreen(SDL_Window *window) |
| 130 |
{ |
| 131 |
SDL_Surface *screen = SDL_GetWindowSurface(window); |
| 132 |
int i; |
| 133 |
|
| 134 |
if (!screen) { |
| 135 |
return; |
| 136 |
} |
| 137 |
|
| 138 |
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 75, 75, 75)); |
| 139 |
|
| 140 |
/* draw Touch History */ |
| 141 |
for (i = eventWrite; i < eventWrite + EVENT_BUF_SIZE; ++i) { |
| 142 |
const SDL_Event *event = &events[i & (EVENT_BUF_SIZE - 1)]; |
| 143 |
const float age = (float)(i - eventWrite) / EVENT_BUF_SIZE; |
| 144 |
float x, y; |
| 145 |
unsigned int c, col; |
| 146 |
|
| 147 |
if ((event->type == SDL_FINGERMOTION) || |
| 148 |
(event->type == SDL_FINGERDOWN) || |
| 149 |
(event->type == SDL_FINGERUP)) { |
| 150 |
x = event->tfinger.x; |
| 151 |
y = event->tfinger.y; |
| 152 |
|
| 153 |
/* draw the touch: */ |
| 154 |
c = colors[event->tfinger.fingerId % 7]; |
| 155 |
col = ((unsigned int)(c * (0.1f + 0.85f))) | (unsigned int)(0xFF * age) << 24; |
| 156 |
|
| 157 |
if (event->type == SDL_FINGERMOTION) { |
| 158 |
drawCircle(screen, x * screen->w, y * screen->h, 5, col); |
| 159 |
} else if (event->type == SDL_FINGERDOWN) { |
| 160 |
drawCircle(screen, x * screen->w, y * screen->h, -10, col); |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
if (knob.p.x > 0) { |
| 166 |
drawKnob(screen, &knob); |
| 167 |
} |
| 168 |
|
| 169 |
SDL_UpdateWindowSurface(window); |
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
loop(void) |
| 174 |
{ |
| 175 |
SDL_Event event; |
| 176 |
SDL_RWops *stream; |
| 177 |
int i; |
| 178 |
|
| 179 |
while (SDL_PollEvent(&event)) { |
| 180 |
SDLTest_CommonEvent(state, &event, &quitting); |
| 181 |
|
| 182 |
/* Record _all_ events */ |
| 183 |
events[eventWrite & (EVENT_BUF_SIZE - 1)] = event; |
| 184 |
eventWrite++; |
| 185 |
|
| 186 |
switch (event.type) { |
| 187 |
case SDL_KEYDOWN: |
| 188 |
switch (event.key.keysym.sym) { |
| 189 |
case SDLK_i: |
| 190 |
{ |
| 191 |
for (i = 0; i < SDL_GetNumTouchDevices(); ++i) { |
| 192 |
const SDL_TouchID id = SDL_GetTouchDevice(i); |
| 193 |
const char *name = SDL_GetTouchName(i); |
| 194 |
SDL_Log("Fingers Down on device %" SDL_PRIs64 " (%s): %d", id, name, SDL_GetNumTouchFingers(id)); |
| 195 |
} |
| 196 |
break; |
| 197 |
} |
| 198 |
|
| 199 |
case SDLK_SPACE: |
| 200 |
SDL_RecordGesture(-1); |
| 201 |
break; |
| 202 |
|
| 203 |
case SDLK_s: |
| 204 |
stream = SDL_RWFromFile("gestureSave", "w"); |
| 205 |
SDL_Log("Wrote %i templates", SDL_SaveAllDollarTemplates(stream)); |
| 206 |
SDL_RWclose(stream); |
| 207 |
break; |
| 208 |
|
| 209 |
case SDLK_l: |
| 210 |
stream = SDL_RWFromFile("gestureSave", "r"); |
| 211 |
SDL_Log("Loaded: %i", SDL_LoadDollarTemplates(-1, stream)); |
| 212 |
SDL_RWclose(stream); |
| 213 |
break; |
| 214 |
} |
| 215 |
break; |
| 216 |
|
| 217 |
#if VERBOSE |
| 218 |
case SDL_FINGERMOTION: |
| 219 |
SDL_Log("Finger: %" SDL_PRIs64 ", x: %f, y: %f", event.tfinger.fingerId, |
| 220 |
event.tfinger.x, event.tfinger.y); |
| 221 |
break; |
| 222 |
|
| 223 |
case SDL_FINGERDOWN: |
| 224 |
SDL_Log("Finger: %" SDL_PRIs64 " down - x: %f, y: %f", |
| 225 |
event.tfinger.fingerId, event.tfinger.x, event.tfinger.y); |
| 226 |
break; |
| 227 |
|
| 228 |
case SDL_FINGERUP: |
| 229 |
SDL_Log("Finger: %" SDL_PRIs64 " up - x: %f, y: %f", |
| 230 |
event.tfinger.fingerId, event.tfinger.x, event.tfinger.y); |
| 231 |
break; |
| 232 |
#endif |
| 233 |
|
| 234 |
case SDL_MULTIGESTURE: |
| 235 |
#if VERBOSE |
| 236 |
SDL_Log("Multi Gesture: x = %f, y = %f, dAng = %f, dR = %f", |
| 237 |
event.mgesture.x, event.mgesture.y, |
| 238 |
event.mgesture.dTheta, event.mgesture.dDist); |
| 239 |
SDL_Log("MG: numDownTouch = %i", event.mgesture.numFingers); |
| 240 |
#endif |
| 241 |
|
| 242 |
knob.p.x = event.mgesture.x; |
| 243 |
knob.p.y = event.mgesture.y; |
| 244 |
knob.ang += event.mgesture.dTheta; |
| 245 |
knob.r += event.mgesture.dDist; |
| 246 |
break; |
| 247 |
|
| 248 |
case SDL_DOLLARGESTURE: |
| 249 |
SDL_Log("Gesture %" SDL_PRIs64 " performed, error: %f", |
| 250 |
event.dgesture.gestureId, event.dgesture.error); |
| 251 |
break; |
| 252 |
|
| 253 |
case SDL_DOLLARRECORD: |
| 254 |
SDL_Log("Recorded gesture: %" SDL_PRIs64, event.dgesture.gestureId); |
| 255 |
break; |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
for (i = 0; i < state->num_windows; ++i) { |
| 260 |
if (state->windows[i]) { |
| 261 |
DrawScreen(state->windows[i]); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
#ifdef __EMSCRIPTEN__ |
| 266 |
if (quitting) { |
| 267 |
emscripten_cancel_main_loop(); |
| 268 |
} |
| 269 |
#endif |
| 270 |
} |
| 271 |
|
| 272 |
int main(int argc, char *argv[]) |
| 273 |
{ |
| 274 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
| 275 |
if (!state) { |
| 276 |
return 1; |
| 277 |
} |
| 278 |
|
| 279 |
state->window_title = "Gesture Test"; |
| 280 |
state->window_w = WIDTH; |
| 281 |
state->window_h = HEIGHT; |
| 282 |
state->skip_renderer = SDL_TRUE; |
| 283 |
|
| 284 |
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) { |
| 285 |
SDLTest_CommonQuit(state); |
| 286 |
return 1; |
| 287 |
} |
| 288 |
|
| 289 |
#ifdef __EMSCRIPTEN__ |
| 290 |
emscripten_set_main_loop(loop, 0, 1); |
| 291 |
#else |
| 292 |
while (!quitting) { |
| 293 |
loop(); |
| 294 |
} |
| 295 |
#endif |
| 296 |
|
| 297 |
SDLTest_CommonQuit(state); |
| 298 |
return 0; |
| 299 |
} |
| 300 |
|