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.
|
|
|
|
*/
|
|
|
|
|
2022-11-27 07:43:38 +03:00
|
|
|
#include <SDL3/SDL_test_common.h>
|
|
|
|
#include <SDL3/SDL_test_font.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
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
static SDLTest_CommonState *state;
|
2023-03-08 13:40:07 +03:00
|
|
|
static int done;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
|
|
|
static const char *cursorNames[] = {
|
2022-11-30 23:51:59 +03:00
|
|
|
"arrow",
|
|
|
|
"ibeam",
|
|
|
|
"wait",
|
|
|
|
"crosshair",
|
|
|
|
"waitarrow",
|
|
|
|
"sizeNWSE",
|
|
|
|
"sizeNESW",
|
|
|
|
"sizeWE",
|
|
|
|
"sizeNS",
|
|
|
|
"sizeALL",
|
|
|
|
"NO",
|
|
|
|
"hand",
|
2023-12-26 01:02:02 +03:00
|
|
|
"window top left",
|
|
|
|
"window top",
|
|
|
|
"window top right",
|
|
|
|
"window right",
|
|
|
|
"window bottom right",
|
|
|
|
"window bottom",
|
|
|
|
"window bottom left",
|
|
|
|
"window left"
|
2015-06-21 18:33:46 +03:00
|
|
|
};
|
2023-12-26 20:57:11 +03:00
|
|
|
SDL_COMPILE_TIME_ASSERT(cursorNames, SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
|
|
|
|
|
2023-03-08 13:40:07 +03:00
|
|
|
static int system_cursor = -1;
|
|
|
|
static SDL_Cursor *cursor = NULL;
|
|
|
|
static const SDL_DisplayMode *highlighted_mode = NULL;
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2021-11-07 12:48:29 +03:00
|
|
|
/* Draws the modes menu, and stores the mode index under the mouse in highlighted_mode */
|
|
|
|
static void
|
2023-01-05 03:22:40 +03:00
|
|
|
draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
|
2021-11-07 12:48:29 +03:00
|
|
|
{
|
Windows default to fullscreen desktop mode if they don't pick an explicit video mode
Rather than iterating over display modes using an index, there is a new function SDL_GetFullscreenDisplayModes() to get the list of available fullscreen modes on a display.
{
SDL_DisplayID display = SDL_GetPrimaryDisplay();
int num_modes = 0;
SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(display, &num_modes);
if (modes) {
for (i = 0; i < num_modes; ++i) {
SDL_DisplayMode *mode = modes[i];
SDL_Log("Display %" SDL_PRIu32 " mode %d: %dx%d@%gHz, %d%% scale\n",
display, i, mode->pixel_w, mode->pixel_h, mode->refresh_rate, (int)(mode->display_scale * 100.0f));
}
SDL_free(modes);
}
}
SDL_GetDesktopDisplayMode() and SDL_GetCurrentDisplayMode() return pointers to display modes rather than filling in application memory.
Windows now have an explicit fullscreen mode that is set, using SDL_SetWindowFullscreenMode(). The fullscreen mode for a window can be queried with SDL_GetWindowFullscreenMode(), which returns a pointer to the mode, or NULL if the window will be fullscreen desktop. SDL_SetWindowFullscreen() just takes a boolean value, setting the correct fullscreen state based on the selected mode.
2023-02-01 08:23:14 +03:00
|
|
|
const SDL_DisplayMode **modes;
|
2021-11-07 12:48:29 +03:00
|
|
|
char text[1024];
|
|
|
|
const int lineHeight = 10;
|
2023-02-15 08:05:00 +03:00
|
|
|
int i, j;
|
2021-11-07 12:48:29 +03:00
|
|
|
int column_chars = 0;
|
|
|
|
int text_length;
|
2023-01-05 03:22:40 +03:00
|
|
|
float x, y;
|
|
|
|
float table_top;
|
2022-12-30 06:31:12 +03:00
|
|
|
SDL_FPoint mouse_pos = { -1.0f, -1.0f };
|
2023-02-15 08:05:00 +03:00
|
|
|
SDL_DisplayID *display_ids;
|
2021-11-07 12:48:29 +03:00
|
|
|
|
|
|
|
/* Get mouse position */
|
|
|
|
if (SDL_GetMouseFocus() == window) {
|
2022-12-30 06:31:12 +03:00
|
|
|
float window_x, window_y;
|
2021-11-10 08:03:42 +03:00
|
|
|
float logical_x, logical_y;
|
|
|
|
|
|
|
|
SDL_GetMouseState(&window_x, &window_y);
|
2023-02-03 23:25:46 +03:00
|
|
|
SDL_RenderCoordinatesFromWindow(renderer, window_x, window_y, &logical_x, &logical_y);
|
2021-11-10 08:03:42 +03:00
|
|
|
|
2022-12-30 06:31:12 +03:00
|
|
|
mouse_pos.x = logical_x;
|
|
|
|
mouse_pos.y = logical_y;
|
2021-11-07 12:48:29 +03:00
|
|
|
}
|
|
|
|
|
2023-01-05 03:22:40 +03:00
|
|
|
x = 0.0f;
|
2021-11-07 12:48:29 +03:00
|
|
|
y = viewport.y;
|
|
|
|
|
|
|
|
y += lineHeight;
|
|
|
|
|
2023-03-10 02:10:00 +03:00
|
|
|
SDL_strlcpy(text, "Click on a mode to set it with SDL_SetWindowFullscreenMode", sizeof(text));
|
2021-11-07 12:48:29 +03:00
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
|
|
|
SDLTest_DrawString(renderer, x, y, text);
|
|
|
|
y += lineHeight;
|
|
|
|
|
2023-03-10 02:10:00 +03:00
|
|
|
SDL_strlcpy(text, "Press Ctrl+Enter to toggle SDL_WINDOW_FULLSCREEN", sizeof(text));
|
2021-11-07 12:48:29 +03:00
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
|
|
|
SDLTest_DrawString(renderer, x, y, text);
|
|
|
|
y += lineHeight;
|
|
|
|
|
|
|
|
table_top = y;
|
|
|
|
|
|
|
|
/* Clear the cached mode under the mouse */
|
|
|
|
if (window == SDL_GetMouseFocus()) {
|
2023-02-15 08:05:00 +03:00
|
|
|
highlighted_mode = NULL;
|
2021-11-07 12:48:29 +03:00
|
|
|
}
|
|
|
|
|
2023-02-15 08:05:00 +03:00
|
|
|
display_ids = SDL_GetDisplays(NULL);
|
|
|
|
|
|
|
|
if (display_ids) {
|
|
|
|
for (i = 0; display_ids[i]; ++i) {
|
|
|
|
const SDL_DisplayID display_id = display_ids[i];
|
|
|
|
modes = SDL_GetFullscreenDisplayModes(display_id, NULL);
|
|
|
|
for (j = 0; modes[j]; ++j) {
|
|
|
|
SDL_FRect cell_rect;
|
|
|
|
const SDL_DisplayMode *mode = modes[j];
|
|
|
|
|
2023-05-18 17:34:16 +03:00
|
|
|
(void)SDL_snprintf(text, sizeof(text), "%s mode %d: %dx%d@%gx %gHz",
|
2023-02-15 08:05:00 +03:00
|
|
|
SDL_GetDisplayName(display_id),
|
2023-05-18 17:34:16 +03:00
|
|
|
j, mode->w, mode->h, mode->pixel_density, mode->refresh_rate);
|
2023-02-15 08:05:00 +03:00
|
|
|
|
|
|
|
/* Update column width */
|
|
|
|
text_length = (int)SDL_strlen(text);
|
|
|
|
column_chars = SDL_max(column_chars, text_length);
|
|
|
|
|
|
|
|
/* Check if under mouse */
|
|
|
|
cell_rect.x = x;
|
|
|
|
cell_rect.y = y;
|
|
|
|
cell_rect.w = (float)(text_length * FONT_CHARACTER_SIZE);
|
|
|
|
cell_rect.h = (float)lineHeight;
|
|
|
|
|
|
|
|
if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) {
|
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
|
|
|
|
|
|
|
/* Update cached mode under the mouse */
|
|
|
|
if (window == SDL_GetMouseFocus()) {
|
|
|
|
highlighted_mode = mode;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
|
|
|
|
}
|
2021-11-07 12:48:29 +03:00
|
|
|
|
2023-02-15 08:05:00 +03:00
|
|
|
SDLTest_DrawString(renderer, x, y, text);
|
|
|
|
y += lineHeight;
|
2021-11-07 12:48:29 +03:00
|
|
|
|
2023-02-15 08:05:00 +03:00
|
|
|
if ((y + lineHeight) > (viewport.y + viewport.h)) {
|
|
|
|
/* Advance to next column */
|
|
|
|
x += (column_chars + 1) * FONT_CHARACTER_SIZE;
|
|
|
|
y = table_top;
|
|
|
|
column_chars = 0;
|
|
|
|
}
|
2021-11-07 12:48:29 +03:00
|
|
|
}
|
2023-02-15 08:05:00 +03:00
|
|
|
SDL_free((void *)modes);
|
2021-11-07 12:48:29 +03:00
|
|
|
}
|
2023-02-15 08:05:00 +03:00
|
|
|
SDL_free(display_ids);
|
2021-11-07 12:48:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 13:40:07 +03:00
|
|
|
static void loop(void)
|
2015-06-21 18:33:46 +03:00
|
|
|
{
|
2016-07-13 17:07:46 +03:00
|
|
|
int i;
|
2015-06-21 18:33:46 +03:00
|
|
|
SDL_Event event;
|
2024-01-25 17:29:15 +03:00
|
|
|
|
|
|
|
#ifdef TEST_WAITEVENTTIMEOUT
|
|
|
|
/* Wait up to 20 ms for input, as a test */
|
|
|
|
Uint64 then = SDL_GetTicks();
|
|
|
|
if (SDL_WaitEventTimeout(NULL, 20)) {
|
|
|
|
SDL_Log("Got an event!\n");
|
|
|
|
}
|
|
|
|
Uint64 now = SDL_GetTicks();
|
|
|
|
SDL_Log("Waited %d ms for events\n", (int)(now - then));
|
|
|
|
#endif
|
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
|
|
|
|
2023-01-24 04:54:09 +03:00
|
|
|
if (event.type == SDL_EVENT_WINDOW_RESIZED) {
|
2022-12-22 18:20:48 +03:00
|
|
|
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
|
|
|
|
if (window) {
|
|
|
|
SDL_Log("Window %" SDL_PRIu32 " resized to %" SDL_PRIs32 "x%" SDL_PRIs32 "\n",
|
|
|
|
event.window.windowID,
|
|
|
|
event.window.data1,
|
|
|
|
event.window.data2);
|
2022-11-30 23:51:59 +03:00
|
|
|
}
|
2022-12-22 18:20:48 +03:00
|
|
|
}
|
2023-01-24 04:54:09 +03:00
|
|
|
if (event.type == SDL_EVENT_WINDOW_MOVED) {
|
2022-12-22 18:20:48 +03:00
|
|
|
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
|
|
|
|
if (window) {
|
|
|
|
SDL_Log("Window %" SDL_PRIu32 " moved to %" SDL_PRIs32 ",%" SDL_PRIs32 " (display %s)\n",
|
|
|
|
event.window.windowID,
|
|
|
|
event.window.data1,
|
|
|
|
event.window.data2,
|
2023-01-30 00:30:55 +03:00
|
|
|
SDL_GetDisplayName(SDL_GetDisplayForWindow(window)));
|
2022-11-30 23:51:59 +03:00
|
|
|
}
|
2022-12-22 18:20:48 +03:00
|
|
|
}
|
2023-01-24 04:54:09 +03:00
|
|
|
if (event.type == SDL_EVENT_KEY_UP) {
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_bool updateCursor = SDL_FALSE;
|
|
|
|
|
2024-07-01 06:05:22 +03:00
|
|
|
if (event.key.key == SDLK_A) {
|
2023-02-01 22:45:31 +03:00
|
|
|
SDL_assert(!"Keyboard generated assert");
|
2024-06-22 05:50:10 +03:00
|
|
|
} else if (event.key.key == SDLK_LEFT) {
|
2022-11-30 23:51:59 +03:00
|
|
|
--system_cursor;
|
|
|
|
if (system_cursor < 0) {
|
|
|
|
system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
updateCursor = SDL_TRUE;
|
2024-06-22 05:50:10 +03:00
|
|
|
} else if (event.key.key == SDLK_RIGHT) {
|
2022-11-30 23:51:59 +03:00
|
|
|
++system_cursor;
|
|
|
|
if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
|
|
|
|
system_cursor = 0;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
updateCursor = SDL_TRUE;
|
|
|
|
}
|
|
|
|
if (updateCursor) {
|
|
|
|
SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
|
2022-12-30 02:07:59 +03:00
|
|
|
SDL_DestroyCursor(cursor);
|
2022-11-30 23:51:59 +03:00
|
|
|
cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
|
|
|
|
SDL_SetCursor(cursor);
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
}
|
2023-01-30 06:06:08 +03:00
|
|
|
if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_Window *window = SDL_GetMouseFocus();
|
2023-11-10 00:29:15 +03:00
|
|
|
if (highlighted_mode && window) {
|
2023-02-15 08:05:00 +03:00
|
|
|
SDL_memcpy(&state->fullscreen_mode, highlighted_mode, sizeof(state->fullscreen_mode));
|
|
|
|
SDL_SetWindowFullscreenMode(window, highlighted_mode);
|
2021-11-07 12:48:29 +03:00
|
|
|
}
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
}
|
2016-07-13 17:07:46 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
SDL_Window *window = state->windows[i];
|
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
2023-11-10 00:29:15 +03:00
|
|
|
if (window && renderer) {
|
2023-01-05 03:22:40 +03:00
|
|
|
float y = 0.0f;
|
2024-06-13 05:08:06 +03:00
|
|
|
SDL_Rect viewport;
|
|
|
|
SDL_FRect menurect;
|
2021-11-07 12:48:29 +03:00
|
|
|
|
2022-12-27 17:21:13 +03:00
|
|
|
SDL_GetRenderViewport(renderer, &viewport);
|
2021-11-07 12:48:29 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
2021-06-08 03:24:57 +03:00
|
|
|
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
|
|
|
SDLTest_CommonDrawWindowInfo(renderer, state->windows[i], &y);
|
2021-11-07 12:48:29 +03:00
|
|
|
|
2023-01-05 03:22:40 +03:00
|
|
|
menurect.x = 0.0f;
|
2022-11-30 23:51:59 +03:00
|
|
|
menurect.y = y;
|
2024-06-13 05:08:06 +03:00
|
|
|
menurect.w = (float)viewport.w;
|
|
|
|
menurect.h = (float)viewport.h - y;
|
2022-11-30 23:51:59 +03:00
|
|
|
draw_modes_menu(window, renderer, menurect);
|
2021-06-08 03:24:57 +03:00
|
|
|
|
2023-01-03 03:04:08 +03:00
|
|
|
SDL_Delay(16);
|
2022-11-30 23:51:59 +03:00
|
|
|
SDL_RenderPresent(renderer);
|
2016-07-13 17:07:46 +03:00
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
}
|
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
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* 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
|
|
|
/* Enable standard application logging */
|
2024-05-16 20:00:50 +03:00
|
|
|
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
2023-03-17 02:25:39 +03:00
|
|
|
|
2019-05-19 08:45:15 +03:00
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
|
|
|
|
SDLTest_CommonQuit(state);
|
|
|
|
return 1;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|
|
|
|
|
2016-07-13 17:07:46 +03:00
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
|
|
|
|
SDL_RenderClear(renderer);
|
|
|
|
}
|
2022-11-30 23:51:59 +03:00
|
|
|
|
2015-06-21 18:33:46 +03:00
|
|
|
/* 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();
|
|
|
|
}
|
|
|
|
#endif
|
2022-12-30 02:07:59 +03:00
|
|
|
SDL_DestroyCursor(cursor);
|
2015-06-21 18:33:46 +03:00
|
|
|
|
2023-08-22 19:44:28 +03:00
|
|
|
SDLTest_CleanupTextDrawing();
|
|
|
|
SDLTest_CommonQuit(state);
|
2022-11-27 19:38:43 +03:00
|
|
|
return 0;
|
2015-06-21 18:33:46 +03:00
|
|
|
}
|