2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2023-01-09 10:41:41 -07:00
|
|
|
Copyright (C) 1997-2023 Sam Lantinga <slouken@libsdl.org>
|
2015-06-21 09:33:46 -06: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 */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL_test_common.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static SDLTest_CommonState *state;
|
2022-12-29 20:31:12 -07:00
|
|
|
static int i, done;
|
|
|
|
static float mouseX, mouseY;
|
2022-12-31 12:19:32 -07:00
|
|
|
static SDL_FRect rect;
|
2022-12-29 20:31:12 -07:00
|
|
|
static SDL_Event event;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
static void
|
2022-11-30 13:51:59 -07:00
|
|
|
DrawRects(SDL_Renderer *renderer)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
|
2022-12-31 12:19:32 -07:00
|
|
|
rect.x = mouseX;
|
|
|
|
rect.y = mouseY;
|
2021-03-26 16:52:30 -06:00
|
|
|
SDL_RenderFillRect(renderer, &rect);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2022-11-30 13:51:59 -07:00
|
|
|
loop()
|
|
|
|
{
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Check for events */
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
2022-11-30 13:51:59 -07:00
|
|
|
switch (event.type) {
|
2023-01-23 18:54:09 -07:00
|
|
|
case SDL_EVENT_MOUSE_MOTION:
|
2022-11-30 13:51:59 -07:00
|
|
|
{
|
2022-12-29 20:31:12 -07:00
|
|
|
mouseX += event.motion.xrel;
|
|
|
|
mouseY += event.motion.yrel;
|
2022-11-30 13:51:59 -07:00
|
|
|
} break;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
SDL_Rect viewport;
|
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
2022-11-27 09:38:43 -07:00
|
|
|
if (state->windows[i] == NULL) {
|
2015-06-21 09:33:46 -06:00
|
|
|
continue;
|
2022-11-27 09:38:43 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06: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 07:21:13 -07:00
|
|
|
SDL_GetRenderViewport(renderer, &viewport);
|
2022-11-27 09:38:43 -07: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 09:33:46 -06:00
|
|
|
|
2021-03-26 16:52:30 -06:00
|
|
|
DrawRects(renderer);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
SDL_RenderPresent(renderer);
|
|
|
|
}
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
if (done) {
|
|
|
|
emscripten_cancel_main_loop();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
int main(int argc, char *argv[])
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
/* Enable standard application logging */
|
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
|
2022-11-27 09:38:43 -07:00
|
|
|
if (state == NULL) {
|
2015-06-21 09:33:46 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
for (i = 1; i < argc; ++i) {
|
|
|
|
SDLTest_CommonArg(state, i);
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
srand((unsigned int)time(NULL));
|
2022-11-27 09:38:43 -07:00
|
|
|
if (SDL_SetRelativeMouseMode(SDL_TRUE) < 0) {
|
2015-06-21 09:33:46 -06:00
|
|
|
return 3;
|
2022-11-17 02:43:45 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
rect.x = DEFAULT_WINDOW_WIDTH / 2;
|
|
|
|
rect.y = DEFAULT_WINDOW_HEIGHT / 2;
|
|
|
|
rect.w = 10;
|
|
|
|
rect.h = 10;
|
|
|
|
/* Main render loop */
|
|
|
|
done = 0;
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
|
|
|
while (!done) {
|
|
|
|
loop();
|
2022-11-30 13:51:59 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
#endif
|
|
|
|
SDLTest_CommonQuit(state);
|
|
|
|
return 0;
|
|
|
|
}
|