| 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 |
/* Simple program: Test relative mouse motion */ |
| 14 |
|
| 15 |
#include <stdlib.h> |
| 16 |
#include <stdio.h> |
| 17 |
#include <time.h> |
| 18 |
|
| 19 |
#include "SDL_test_common.h" |
| 20 |
|
| 21 |
#ifdef __EMSCRIPTEN__ |
| 22 |
#include <emscripten/emscripten.h> |
| 23 |
#endif |
| 24 |
|
| 25 |
static SDLTest_CommonState *state; |
| 26 |
int i, done; |
| 27 |
SDL_Rect rect; |
| 28 |
SDL_Event event; |
| 29 |
|
| 30 |
static void |
| 31 |
DrawRects(SDL_Renderer *renderer) |
| 32 |
{ |
| 33 |
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); |
| 34 |
SDL_RenderFillRect(renderer, &rect); |
| 35 |
} |
| 36 |
|
| 37 |
static void |
| 38 |
loop(void) |
| 39 |
{ |
| 40 |
/* Check for events */ |
| 41 |
while (SDL_PollEvent(&event)) { |
| 42 |
SDLTest_CommonEvent(state, &event, &done); |
| 43 |
switch (event.type) { |
| 44 |
case SDL_MOUSEMOTION: |
| 45 |
{ |
| 46 |
rect.x += event.motion.xrel; |
| 47 |
rect.y += event.motion.yrel; |
| 48 |
} break; |
| 49 |
} |
| 50 |
} |
| 51 |
for (i = 0; i < state->num_windows; ++i) { |
| 52 |
SDL_Rect viewport; |
| 53 |
SDL_Renderer *renderer = state->renderers[i]; |
| 54 |
if (state->windows[i] == NULL) { |
| 55 |
continue; |
| 56 |
} |
| 57 |
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); |
| 58 |
SDL_RenderClear(renderer); |
| 59 |
|
| 60 |
/* Wrap the cursor rectangle at the screen edges to keep it visible */ |
| 61 |
SDL_RenderGetViewport(renderer, &viewport); |
| 62 |
if (rect.x < viewport.x) { |
| 63 |
rect.x += viewport.w; |
| 64 |
} |
| 65 |
if (rect.y < viewport.y) { |
| 66 |
rect.y += viewport.h; |
| 67 |
} |
| 68 |
if (rect.x > viewport.x + viewport.w) { |
| 69 |
rect.x -= viewport.w; |
| 70 |
} |
| 71 |
if (rect.y > viewport.y + viewport.h) { |
| 72 |
rect.y -= viewport.h; |
| 73 |
} |
| 74 |
|
| 75 |
DrawRects(renderer); |
| 76 |
|
| 77 |
SDL_RenderPresent(renderer); |
| 78 |
} |
| 79 |
#ifdef __EMSCRIPTEN__ |
| 80 |
if (done) { |
| 81 |
emscripten_cancel_main_loop(); |
| 82 |
} |
| 83 |
#endif |
| 84 |
} |
| 85 |
|
| 86 |
int main(int argc, char *argv[]) |
| 87 |
{ |
| 88 |
|
| 89 |
/* Enable standard application logging */ |
| 90 |
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
| 91 |
|
| 92 |
/* Initialize test framework */ |
| 93 |
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
| 94 |
if (!state) { |
| 95 |
return 1; |
| 96 |
} |
| 97 |
for (i = 1; i < argc; ++i) { |
| 98 |
SDLTest_CommonArg(state, i); |
| 99 |
} |
| 100 |
if (!SDLTest_CommonInit(state)) { |
| 101 |
return 2; |
| 102 |
} |
| 103 |
|
| 104 |
/* Create the windows and initialize the renderers */ |
| 105 |
for (i = 0; i < state->num_windows; ++i) { |
| 106 |
SDL_Renderer *renderer = state->renderers[i]; |
| 107 |
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); |
| 108 |
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
| 109 |
SDL_RenderClear(renderer); |
| 110 |
} |
| 111 |
|
| 112 |
srand((unsigned int)time(NULL)); |
| 113 |
if (SDL_SetRelativeMouseMode(SDL_TRUE) < 0) { |
| 114 |
return 3; |
| 115 |
} |
| 116 |
|
| 117 |
rect.x = DEFAULT_WINDOW_WIDTH / 2; |
| 118 |
rect.y = DEFAULT_WINDOW_HEIGHT / 2; |
| 119 |
rect.w = 10; |
| 120 |
rect.h = 10; |
| 121 |
/* Main render loop */ |
| 122 |
done = 0; |
| 123 |
#ifdef __EMSCRIPTEN__ |
| 124 |
emscripten_set_main_loop(loop, 0, 1); |
| 125 |
#else |
| 126 |
while (!done) { |
| 127 |
loop(); |
| 128 |
} |
| 129 |
#endif |
| 130 |
SDLTest_CommonQuit(state); |
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
/* vi: set ts=4 sw=4 expandtab: */ |