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 | #include <stdlib.h> |
14 | #include <stdio.h> |
15 | |
16 | #ifdef __EMSCRIPTEN__ |
17 | #include <emscripten/emscripten.h> |
18 | #endif |
19 | |
20 | #include "SDL_test_common.h" |
21 | |
22 | static SDLTest_CommonState *state; |
23 | int done; |
24 | |
25 | static const char *cursorNames[] = { |
26 | "arrow" , |
27 | "ibeam" , |
28 | "wait" , |
29 | "crosshair" , |
30 | "waitarrow" , |
31 | "sizeNWSE" , |
32 | "sizeNESW" , |
33 | "sizeWE" , |
34 | "sizeNS" , |
35 | "sizeALL" , |
36 | "NO" , |
37 | "hand" , |
38 | }; |
39 | int system_cursor = -1; |
40 | SDL_Cursor *cursor = NULL; |
41 | |
42 | /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ |
43 | static void |
44 | quit(int rc) |
45 | { |
46 | SDLTest_CommonQuit(state); |
47 | exit(rc); |
48 | } |
49 | |
50 | void |
51 | loop() |
52 | { |
53 | int i; |
54 | SDL_Event event; |
55 | /* Check for events */ |
56 | while (SDL_PollEvent(&event)) { |
57 | SDLTest_CommonEvent(state, &event, &done); |
58 | |
59 | if (event.type == SDL_WINDOWEVENT) { |
60 | if (event.window.event == SDL_WINDOWEVENT_RESIZED) { |
61 | SDL_Window *window = SDL_GetWindowFromID(event.window.windowID); |
62 | if (window) { |
63 | SDL_Log("Window %d resized to %dx%d\n" , |
64 | event.window.windowID, |
65 | event.window.data1, |
66 | event.window.data2); |
67 | } |
68 | } |
69 | if (event.window.event == SDL_WINDOWEVENT_MOVED) { |
70 | SDL_Window *window = SDL_GetWindowFromID(event.window.windowID); |
71 | if (window) { |
72 | SDL_Log("Window %d moved to %d,%d (display %s)\n" , |
73 | event.window.windowID, |
74 | event.window.data1, |
75 | event.window.data2, |
76 | SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window))); |
77 | } |
78 | } |
79 | } |
80 | if (event.type == SDL_KEYUP) { |
81 | SDL_bool updateCursor = SDL_FALSE; |
82 | |
83 | if (event.key.keysym.sym == SDLK_LEFT) { |
84 | --system_cursor; |
85 | if (system_cursor < 0) { |
86 | system_cursor = SDL_NUM_SYSTEM_CURSORS - 1; |
87 | } |
88 | updateCursor = SDL_TRUE; |
89 | } else if (event.key.keysym.sym == SDLK_RIGHT) { |
90 | ++system_cursor; |
91 | if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) { |
92 | system_cursor = 0; |
93 | } |
94 | updateCursor = SDL_TRUE; |
95 | } |
96 | if (updateCursor) { |
97 | SDL_Log("Changing cursor to \"%s\"" , cursorNames[system_cursor]); |
98 | SDL_FreeCursor(cursor); |
99 | cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor); |
100 | SDL_SetCursor(cursor); |
101 | } |
102 | } |
103 | } |
104 | |
105 | for (i = 0; i < state->num_windows; ++i) { |
106 | SDL_Renderer *renderer = state->renderers[i]; |
107 | if (renderer != NULL) { |
108 | SDL_RenderClear(renderer); |
109 | SDL_RenderPresent(renderer); |
110 | } |
111 | } |
112 | #ifdef __EMSCRIPTEN__ |
113 | if (done) { |
114 | emscripten_cancel_main_loop(); |
115 | } |
116 | #endif |
117 | } |
118 | |
119 | int |
120 | main(int argc, char *argv[]) |
121 | { |
122 | int i; |
123 | |
124 | /* Enable standard application logging */ |
125 | SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO); |
126 | |
127 | SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS); |
128 | |
129 | /* Initialize test framework */ |
130 | state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); |
131 | if (!state) { |
132 | return 1; |
133 | } |
134 | |
135 | if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) { |
136 | SDLTest_CommonQuit(state); |
137 | return 1; |
138 | } |
139 | |
140 | SDL_EventState(SDL_DROPFILE, SDL_ENABLE); |
141 | SDL_EventState(SDL_DROPTEXT, SDL_ENABLE); |
142 | |
143 | for (i = 0; i < state->num_windows; ++i) { |
144 | SDL_Renderer *renderer = state->renderers[i]; |
145 | SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); |
146 | SDL_RenderClear(renderer); |
147 | } |
148 | |
149 | /* Main render loop */ |
150 | done = 0; |
151 | #ifdef __EMSCRIPTEN__ |
152 | emscripten_set_main_loop(loop, 0, 1); |
153 | #else |
154 | while (!done) { |
155 | loop(); |
156 | } |
157 | #endif |
158 | SDL_FreeCursor(cursor); |
159 | |
160 | quit(0); |
161 | /* keep the compiler happy ... */ |
162 | return(0); |
163 | } |
164 | |
165 | /* vi: set ts=4 sw=4 expandtab: */ |
166 | |