2015-06-21 18:33:46 +03:00
|
|
|
/*
|
2024-01-02 00:15:26 +03:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Simple program: Test relative mouse motion */
|
|
|
|
|
2024-07-25 00:43:02 +03:00
|
|
|
#include <SDL3/SDL_test.h>
|
2022-11-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL_test_common.h>
|
2022-12-15 07:58:20 +03:00
|
|
|
#include <SDL3/SDL_main.h>
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 18:33:46 +03:00
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static SDLTest_CommonState *state;
|
2022-12-30 06:31:12 +03:00
|
|
|
static int i, done;
|
2022-12-31 22:19:32 +03:00
|
|
|
static SDL_FRect rect;
|
2022-12-30 06:31:12 +03:00
|
|
|
static SDL_Event event;
|
2024-07-25 00:43:02 +03:00
|
|
|
static SDL_bool warp;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2023-03-08 18:12:45 +03:00
|
|
|
static void DrawRects(SDL_Renderer *renderer)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
2021-03-27 01:52:30 +03:00
|
|
|
SDL_RenderFillRect(renderer, &rect);
|
2024-07-25 00:43:02 +03:00
|
|
|
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
2024-08-06 17:20:33 +03:00
|
|
|
SDLTest_DrawString(renderer, 0.f, 0.f, "Relative Mode: Enabled");
|
2024-07-25 00:43:02 +03:00
|
|
|
}
|
|
|
|
|
2024-09-12 04:31:09 +03:00
|
|
|
static void CenterMouse(void)
|
2024-07-25 00:43:02 +03:00
|
|
|
{
|
|
|
|
/* Warp the mouse back to the center of the window with input focus to use the
|
|
|
|
* center point for calculating future motion deltas.
|
|
|
|
*
|
|
|
|
* NOTE: DO NOT DO THIS IN REAL APPS/GAMES!
|
|
|
|
*
|
|
|
|
* This is an outdated method of handling relative pointer motion, and
|
|
|
|
* may not work properly, if at all, on some platforms. It is here *only*
|
|
|
|
* for testing the warp emulation code path internal to SDL.
|
|
|
|
*
|
|
|
|
* Relative mouse mode should be used instead!
|
|
|
|
*/
|
|
|
|
SDL_Window *window = SDL_GetKeyboardFocus();
|
|
|
|
if (window) {
|
|
|
|
int w, h;
|
|
|
|
float cx, cy;
|
|
|
|
|
|
|
|
SDL_GetWindowSize(window, &w, &h);
|
|
|
|
cx = (float)w / 2.f;
|
|
|
|
cy = (float)h / 2.f;
|
|
|
|
|
|
|
|
SDL_WarpMouseInWindow(window, cx, cy);
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2023-03-08 18:12:45 +03:00
|
|
|
static void loop(void)
|
2022-11-30 23:51:59 +03:00
|
|
|
{
|
2015-06-21 18:33:46 +03:00
|
|
|
/* Check for events */
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
2022-11-30 23:51:59 +03:00
|
|
|
switch (event.type) {
|
2024-07-25 00:43:02 +03:00
|
|
|
case SDL_EVENT_WINDOW_FOCUS_GAINED:
|
|
|
|
if (warp) {
|
|
|
|
/* This should activate relative mode for warp emulation, unless disabled via a hint. */
|
|
|
|
CenterMouse();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SDL_EVENT_KEY_DOWN:
|
|
|
|
if (event.key.key == SDLK_C) {
|
|
|
|
/* If warp emulation is active, showing the cursor should turn
|
|
|
|
* relative mode off, and it should re-activate after a warp
|
|
|
|
* when hidden again.
|
|
|
|
*/
|
|
|
|
if (SDL_CursorVisible()) {
|
|
|
|
SDL_HideCursor();
|
|
|
|
} else {
|
|
|
|
SDL_ShowCursor();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2023-01-24 04:54:09 +03:00
|
|
|
case SDL_EVENT_MOUSE_MOTION:
|
2022-11-30 23:51:59 +03:00
|
|
|
{
|
2024-07-25 00:43:02 +03:00
|
|
|
rect.x += event.motion.xrel;
|
|
|
|
rect.y += event.motion.yrel;
|
|
|
|
|
|
|
|
if (warp) {
|
|
|
|
CenterMouse();
|
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
} break;
|
2024-03-06 22:37:39 +03:00
|
|
|
default:
|
|
|
|
break;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
}
|
2024-07-25 00:43:02 +03:00
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
2024-06-13 05:08:06 +03:00
|
|
|
SDL_Rect viewport;
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
2022-11-27 19:38:43 +03:00
|
|
|
if (state->windows[i] == NULL) {
|
2015-06-21 18:33:46 +03:00
|
|
|
continue;
|
2022-11-27 19:38:43 +03:00
|
|
|
}
|
2024-07-25 00:43:02 +03:00
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
|
|
|
|
/* Wrap the cursor rectangle at the screen edges to keep it visible */
|
2022-12-27 17:21:13 +03:00
|
|
|
SDL_GetRenderViewport(renderer, &viewport);
|
2022-11-27 19:38:43 +03:00
|
|
|
if (rect.x < viewport.x) {
|
|
|
|
rect.x += viewport.w;
|
|
|
|
}
|
|
|
|
if (rect.y < viewport.y) {
|
|
|
|
rect.y += viewport.h;
|
|
|
|
}
|
|
|
|
if (rect.x > viewport.x + viewport.w) {
|
|
|
|
rect.x -= viewport.w;
|
|
|
|
}
|
|
|
|
if (rect.y > viewport.y + viewport.h) {
|
|
|
|
rect.y -= viewport.h;
|
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2021-03-27 01:52:30 +03:00
|
|
|
DrawRects(renderer);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
}
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 18:33:46 +03:00
|
|
|
if (done) {
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
|
|
|
/* Enable standard application logging */
|
2024-05-16 20:00:50 +03:00
|
|
|
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
|
2023-11-10 00:29:15 +03:00
|
|
|
if (!state) {
|
2015-06-21 18:33:46 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2023-03-17 02:25:39 +03:00
|
|
|
|
|
|
|
/* Parse commandline */
|
2024-07-25 00:43:02 +03:00
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (consumed == 0) {
|
|
|
|
consumed = -1;
|
|
|
|
if (SDL_strcasecmp(argv[i], "--warp") == 0) {
|
|
|
|
warp = SDL_TRUE;
|
|
|
|
consumed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consumed < 0) {
|
|
|
|
static const char *options[] = {
|
|
|
|
"[--warp]",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
i += consumed;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2023-03-17 02:25:39 +03:00
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
if (!SDLTest_CommonInit(state)) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Create the windows and initialize the renderers */
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
|
|
|
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
}
|
|
|
|
|
2024-07-25 00:43:02 +03:00
|
|
|
/* If warp mode is activated, the cursor will be repeatedly warped back to
|
|
|
|
* the center of the window to simulate the behavior of older games. The cursor
|
|
|
|
* is initially hidden in this case to trigger the warp emulation unless it has
|
|
|
|
* been explicitly disabled via a hint.
|
|
|
|
*
|
|
|
|
* Otherwise, try to activate relative mode.
|
|
|
|
*/
|
|
|
|
if (warp) {
|
|
|
|
SDL_HideCursor();
|
2024-08-06 17:20:33 +03:00
|
|
|
} else {
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
SDL_SetWindowRelativeMouseMode(state->windows[i], SDL_TRUE);
|
|
|
|
}
|
2022-11-17 12:43:45 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
rect.x = DEFAULT_WINDOW_WIDTH / 2;
|
|
|
|
rect.y = DEFAULT_WINDOW_HEIGHT / 2;
|
|
|
|
rect.w = 10;
|
|
|
|
rect.h = 10;
|
|
|
|
/* Main render loop */
|
|
|
|
done = 0;
|
2024-01-24 04:40:51 +03:00
|
|
|
#ifdef SDL_PLATFORM_EMSCRIPTEN
|
2015-06-21 18:33:46 +03:00
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
|
|
|
while (!done) {
|
|
|
|
loop();
|
2022-11-30 23:51:59 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
#endif
|
|
|
|
SDLTest_CommonQuit(state);
|
|
|
|
return 0;
|
|
|
|
}
|