Atlas - testwm.c
Home / ext / SDL / test Lines: 1 | Size: 9431 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)][FILE BEGIN]1/* 2 Copyright (C) 1997-2026 Sam Lantinga <[email protected]> 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 <SDL3/SDL_test_common.h> 14#include <SDL3/SDL_test_font.h> 15#include <SDL3/SDL_main.h> 16 17#ifdef SDL_PLATFORM_EMSCRIPTEN 18#include <emscripten/emscripten.h> 19#endif 20 21static SDLTest_CommonState *state; 22static int done; 23 24static const char *cursorNames[] = { 25 "arrow", 26 "ibeam", 27 "wait", 28 "crosshair", 29 "waitarrow", 30 "sizeNWSE", 31 "sizeNESW", 32 "sizeWE", 33 "sizeNS", 34 "sizeALL", 35 "NO", 36 "hand", 37 "window top left", 38 "window top", 39 "window top right", 40 "window right", 41 "window bottom right", 42 "window bottom", 43 "window bottom left", 44 "window left" 45}; 46SDL_COMPILE_TIME_ASSERT(cursorNames, SDL_arraysize(cursorNames) == SDL_SYSTEM_CURSOR_COUNT); 47 48static int system_cursor = -1; 49static SDL_Cursor *cursor = NULL; 50static SDL_DisplayMode highlighted_mode; 51 52/* Draws the modes menu, and stores the mode index under the mouse in highlighted_mode */ 53static void 54draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport) 55{ 56 SDL_DisplayMode **modes; 57 char text[1024]; 58 const int lineHeight = 10; 59 int i, j; 60 int column_chars = 0; 61 int text_length; 62 float x, y; 63 float table_top; 64 SDL_FPoint mouse_pos = { -1.0f, -1.0f }; 65 SDL_DisplayID *displays; 66 67 /* Get mouse position */ 68 if (SDL_GetMouseFocus() == window) { 69 float window_x, window_y; 70 float logical_x, logical_y; 71 72 SDL_GetMouseState(&window_x, &window_y); 73 SDL_RenderCoordinatesFromWindow(renderer, window_x, window_y, &logical_x, &logical_y); 74 75 mouse_pos.x = logical_x; 76 mouse_pos.y = logical_y; 77 } 78 79 x = 0.0f; 80 y = viewport.y; 81 82 y += lineHeight; 83 84 SDL_strlcpy(text, "Click on a mode to set it with SDL_SetWindowFullscreenMode", sizeof(text)); 85 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 86 SDLTest_DrawString(renderer, x, y, text); 87 y += lineHeight; 88 89 SDL_strlcpy(text, "Press Ctrl+Enter to toggle SDL_WINDOW_FULLSCREEN", sizeof(text)); 90 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 91 SDLTest_DrawString(renderer, x, y, text); 92 y += lineHeight; 93 94 table_top = y; 95 96 /* Clear the cached mode under the mouse */ 97 if (window == SDL_GetMouseFocus()) { 98 SDL_zero(highlighted_mode); 99 } 100 101 displays = SDL_GetDisplays(NULL); 102 if (displays) { 103 for (i = 0; displays[i]; ++i) { 104 SDL_DisplayID display = displays[i]; 105 modes = SDL_GetFullscreenDisplayModes(display, NULL); 106 for (j = 0; modes[j]; ++j) { 107 SDL_FRect cell_rect; 108 const SDL_DisplayMode *mode = modes[j]; 109 if (mode->format == SDL_PIXELFORMAT_INDEX8) { 110 continue; 111 } 112 113 (void)SDL_snprintf(text, sizeof(text), "%s mode %d: %dx%d@%gx %gHz", 114 SDL_GetDisplayName(display), 115 j, mode->w, mode->h, mode->pixel_density, mode->refresh_rate); 116 117 /* Update column width */ 118 text_length = (int)SDL_strlen(text); 119 column_chars = SDL_max(column_chars, text_length); 120 121 /* Check if under mouse */ 122 cell_rect.x = x; 123 cell_rect.y = y; 124 cell_rect.w = (float)(text_length * FONT_CHARACTER_SIZE); 125 cell_rect.h = (float)lineHeight; 126 127 if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) { 128 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 129 130 /* Update cached mode under the mouse */ 131 if (window == SDL_GetMouseFocus()) { 132 SDL_copyp(&highlighted_mode, mode); 133 } 134 } else { 135 SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); 136 } 137 138 SDLTest_DrawString(renderer, x, y, text); 139 y += lineHeight; 140 141 if ((y + lineHeight) > (viewport.y + viewport.h)) { 142 /* Advance to next column */ 143 x += (column_chars + 1) * FONT_CHARACTER_SIZE; 144 y = table_top; 145 column_chars = 0; 146 } 147 } 148 SDL_free(modes); 149 } 150 SDL_free(displays); 151 } 152} 153 154static void loop(void) 155{ 156 int i; 157 SDL_Event event; 158 159#ifdef TEST_WAITEVENTTIMEOUT 160 /* Wait up to 20 ms for input, as a test */ 161 Uint64 then = SDL_GetTicks(); 162 if (SDL_WaitEventTimeout(NULL, 20)) { 163 SDL_Log("Got an event!"); 164 } 165 Uint64 now = SDL_GetTicks(); 166 SDL_Log("Waited %d ms for events", (int)(now - then)); 167#endif 168 169 while (SDL_PollEvent(&event)) { 170 SDLTest_CommonEvent(state, &event, &done); 171 SDL_ConvertEventToRenderCoordinates(SDL_GetRenderer(SDL_GetWindowFromEvent(&event)), &event); 172 173 if (event.type == SDL_EVENT_WINDOW_RESIZED) { 174 SDL_Window *window = SDL_GetWindowFromEvent(&event); 175 if (window) { 176 SDL_Log("Window %" SDL_PRIu32 " resized to %" SDL_PRIs32 "x%" SDL_PRIs32, 177 event.window.windowID, 178 event.window.data1, 179 event.window.data2); 180 } 181 } 182 if (event.type == SDL_EVENT_WINDOW_MOVED) { 183 SDL_Window *window = SDL_GetWindowFromEvent(&event); 184 if (window) { 185 SDL_Log("Window %" SDL_PRIu32 " moved to %" SDL_PRIs32 ",%" SDL_PRIs32 " (display %s)", 186 event.window.windowID, 187 event.window.data1, 188 event.window.data2, 189 SDL_GetDisplayName(SDL_GetDisplayForWindow(window))); 190 } 191 } 192 if (event.type == SDL_EVENT_KEY_UP) { 193 bool updateCursor = false; 194 195 if (event.key.key == SDLK_A) { 196 SDL_assert(!"Keyboard generated assert"); 197 } else if (event.key.key == SDLK_LEFT) { 198 --system_cursor; 199 if (system_cursor < 0) { 200 system_cursor = SDL_SYSTEM_CURSOR_COUNT - 1; 201 } 202 updateCursor = true; 203 } else if (event.key.key == SDLK_RIGHT) { 204 ++system_cursor; 205 if (system_cursor >= SDL_SYSTEM_CURSOR_COUNT) { 206 system_cursor = 0; 207 } 208 updateCursor = true; 209 } 210 if (updateCursor) { 211 SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]); 212 SDL_DestroyCursor(cursor); 213 cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor); 214 SDL_SetCursor(cursor); 215 } 216 } 217 if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) { 218 SDL_Window *window = SDL_GetMouseFocus(); 219 if (highlighted_mode.w && window) { 220 SDL_copyp(&state->fullscreen_mode, &highlighted_mode); 221 SDL_SetWindowFullscreenMode(window, &highlighted_mode); 222 } 223 } 224 } 225 226 for (i = 0; i < state->num_windows; ++i) { 227 SDL_Window *window = state->windows[i]; 228 SDL_Renderer *renderer = state->renderers[i]; 229 if (window && renderer) { 230 float y = 0.0f; 231 SDL_Rect viewport; 232 SDL_FRect menurect; 233 234 SDL_SetRenderViewport(renderer, NULL); 235 SDL_GetRenderSafeArea(renderer, &viewport); 236 SDL_SetRenderViewport(renderer, &viewport); 237 238 SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); 239 SDL_RenderClear(renderer); 240 241 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 242 SDLTest_CommonDrawWindowInfo(renderer, state->windows[i], &y); 243 244 menurect.x = 0.0f; 245 menurect.y = y; 246 menurect.w = (float)viewport.w; 247 menurect.h = (float)viewport.h - y; 248 draw_modes_menu(window, renderer, menurect); 249 250 SDL_Delay(16); 251 SDL_RenderPresent(renderer); 252 } 253 } 254#ifdef SDL_PLATFORM_EMSCRIPTEN 255 if (done) { 256 emscripten_cancel_main_loop(); 257 } 258#endif 259} 260 261int main(int argc, char *argv[]) 262{ 263 int i; 264 265 /* Initialize test framework */ 266 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO); 267 if (!state) { 268 return 1; 269 } 270 271 /* Parse commandline */ 272 if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) { 273 SDLTest_CommonQuit(state); 274 return 1; 275 } 276 277 for (i = 0; i < state->num_windows; ++i) { 278 SDL_Renderer *renderer = state->renderers[i]; 279 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF); 280 SDL_RenderClear(renderer); 281 } 282 283SDL_StopTextInput(state->windows[0]); 284SDL_StopTextInput(state->windows[0]); 285 /* Main render loop */ 286 done = 0; 287#ifdef SDL_PLATFORM_EMSCRIPTEN 288 emscripten_set_main_loop(loop, 0, 1); 289#else 290 while (!done) { 291 loop(); 292 } 293#endif 294 SDL_DestroyCursor(cursor); 295 296 SDLTest_CleanupTextDrawing(); 297 SDLTest_CommonQuit(state); 298 return 0; 299} 300[FILE END](C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.