1 | /* |
2 | Copyright (C) 1997-2021 Sam Lantinga <slouken@libsdl.org> |
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(){ |
39 | /* Check for events */ |
40 | while (SDL_PollEvent(&event)) { |
41 | SDLTest_CommonEvent(state, &event, &done); |
42 | switch(event.type) { |
43 | case SDL_MOUSEMOTION: |
44 | { |
45 | rect.x += event.motion.xrel; |
46 | rect.y += event.motion.yrel; |
47 | } |
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 | SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF); |
57 | SDL_RenderClear(renderer); |
58 | |
59 | /* Wrap the cursor rectangle at the screen edges to keep it visible */ |
60 | SDL_RenderGetViewport(renderer, &viewport); |
61 | if (rect.x < viewport.x) rect.x += viewport.w; |
62 | if (rect.y < viewport.y) rect.y += viewport.h; |
63 | if (rect.x > viewport.x + viewport.w) rect.x -= viewport.w; |
64 | if (rect.y > viewport.y + viewport.h) rect.y -= viewport.h; |
65 | |
66 | DrawRects(renderer); |
67 | |
68 | SDL_RenderPresent(renderer); |
69 | } |
70 | #ifdef __EMSCRIPTEN__ |
71 | if (done) { |
72 | emscripten_cancel_main_loop(); |
73 | } |
74 | #endif |
75 | } |
76 | |
77 | int |
78 | main(int argc, char *argv[]) |
79 | { |
80 | |
81 | /* Enable standard application logging */ |
82 | SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
83 | |
84 | /* Initialize test framework */ |
85 | state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
86 | if (!state) { |
87 | return 1; |
88 | } |
89 | for (i = 1; i < argc; ++i) { |
90 | SDLTest_CommonArg(state, i); |
91 | } |
92 | if (!SDLTest_CommonInit(state)) { |
93 | return 2; |
94 | } |
95 | |
96 | /* Create the windows and initialize the renderers */ |
97 | for (i = 0; i < state->num_windows; ++i) { |
98 | SDL_Renderer *renderer = state->renderers[i]; |
99 | SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE); |
100 | SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
101 | SDL_RenderClear(renderer); |
102 | } |
103 | |
104 | srand((unsigned int)time(NULL)); |
105 | if(SDL_SetRelativeMouseMode(SDL_TRUE) < 0) { |
106 | return 3; |
107 | }; |
108 | |
109 | rect.x = DEFAULT_WINDOW_WIDTH / 2; |
110 | rect.y = DEFAULT_WINDOW_HEIGHT / 2; |
111 | rect.w = 10; |
112 | rect.h = 10; |
113 | /* Main render loop */ |
114 | done = 0; |
115 | #ifdef __EMSCRIPTEN__ |
116 | emscripten_set_main_loop(loop, 0, 1); |
117 | #else |
118 | while (!done) { |
119 | loop(); |
120 | } |
121 | #endif |
122 | SDLTest_CommonQuit(state); |
123 | return 0; |
124 | } |
125 | |
126 | /* vi: set ts=4 sw=4 expandtab: */ |
127 | |