2
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
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.
8
Permission is granted to anyone to use this software for any purpose,
9
including commercial applications, and to alter it and redistribute it
13
/* Simple program: Test relative mouse motion */
15
#include <SDL3/SDL_test.h>
16
#include <SDL3/SDL_test_common.h>
17
#include <SDL3/SDL_main.h>
19
#ifdef SDL_PLATFORM_EMSCRIPTEN
20
#include <emscripten/emscripten.h>
23
static SDLTest_CommonState *state;
26
static SDL_Event event;
29
static void DrawRects(SDL_Renderer *renderer)
31
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
32
SDL_RenderFillRect(renderer, &rect);
34
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
35
SDLTest_DrawString(renderer, 0.f, 0.f, "Relative Mode: Enabled");
38
static void CenterMouse()
40
/* Warp the mouse back to the center of the window with input focus to use the
41
* center point for calculating future motion deltas.
43
* NOTE: DO NOT DO THIS IN REAL APPS/GAMES!
45
* This is an outdated method of handling relative pointer motion, and
46
* may not work properly, if at all, on some platforms. It is here *only*
47
* for testing the warp emulation code path internal to SDL.
49
* Relative mouse mode should be used instead!
51
SDL_Window *window = SDL_GetKeyboardFocus();
56
SDL_GetWindowSize(window, &w, &h);
60
SDL_WarpMouseInWindow(window, cx, cy);
66
/* Check for events */
67
while (SDL_PollEvent(&event)) {
68
SDLTest_CommonEvent(state, &event, &done);
70
case SDL_EVENT_WINDOW_FOCUS_GAINED:
72
/* This should activate relative mode for warp emulation, unless disabled via a hint. */
76
case SDL_EVENT_KEY_DOWN:
77
if (event.key.key == SDLK_C) {
78
/* If warp emulation is active, showing the cursor should turn
79
* relative mode off, and it should re-activate after a warp
82
if (SDL_CursorVisible()) {
89
case SDL_EVENT_MOUSE_MOTION:
91
rect.x += event.motion.xrel;
92
rect.y += event.motion.yrel;
103
for (i = 0; i < state->num_windows; ++i) {
105
SDL_Renderer *renderer = state->renderers[i];
106
if (state->windows[i] == NULL) {
110
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
111
SDL_RenderClear(renderer);
113
/* Wrap the cursor rectangle at the screen edges to keep it visible */
114
SDL_GetRenderViewport(renderer, &viewport);
115
if (rect.x < viewport.x) {
116
rect.x += viewport.w;
118
if (rect.y < viewport.y) {
119
rect.y += viewport.h;
121
if (rect.x > viewport.x + viewport.w) {
122
rect.x -= viewport.w;
124
if (rect.y > viewport.y + viewport.h) {
125
rect.y -= viewport.h;
130
SDL_RenderPresent(renderer);
132
#ifdef SDL_PLATFORM_EMSCRIPTEN
134
emscripten_cancel_main_loop();
139
int main(int argc, char *argv[])
141
/* Enable standard application logging */
142
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
144
/* Initialize test framework */
145
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
150
/* Parse commandline */
151
for (i = 1; i < argc;) {
154
consumed = SDLTest_CommonArg(state, i);
157
if (SDL_strcasecmp(argv[i], "--warp") == 0) {
164
static const char *options[] = {
168
SDLTest_CommonLogUsage(state, argv[0], options);
174
if (!SDLTest_CommonInit(state)) {
178
/* Create the windows and initialize the renderers */
179
for (i = 0; i < state->num_windows; ++i) {
180
SDL_Renderer *renderer = state->renderers[i];
181
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
182
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
183
SDL_RenderClear(renderer);
186
/* If warp mode is activated, the cursor will be repeatedly warped back to
187
* the center of the window to simulate the behavior of older games. The cursor
188
* is initially hidden in this case to trigger the warp emulation unless it has
189
* been explicitly disabled via a hint.
191
* Otherwise, try to activate relative mode.
196
for (i = 0; i < state->num_windows; ++i) {
197
SDL_SetWindowRelativeMouseMode(state->windows[i], SDL_TRUE);
201
rect.x = DEFAULT_WINDOW_WIDTH / 2;
202
rect.y = DEFAULT_WINDOW_HEIGHT / 2;
205
/* Main render loop */
207
#ifdef SDL_PLATFORM_EMSCRIPTEN
208
emscripten_set_main_loop(loop, 0, 1);
214
SDLTest_CommonQuit(state);