2015-06-21 09:33:46 -06:00
|
|
|
/*
|
2024-01-01 14:15:26 -07:00
|
|
|
Copyright (C) 1997-2024 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
#include <emscripten/emscripten.h>
|
|
|
|
#endif
|
|
|
|
|
2022-11-26 21:43:38 -07:00
|
|
|
#include <SDL3/SDL_test_common.h>
|
|
|
|
#include <SDL3/SDL_test_font.h>
|
2022-12-14 21:58:20 -07:00
|
|
|
#include <SDL3/SDL_main.h>
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
static SDLTest_CommonState *state;
|
2023-03-08 03:40:07 -07:00
|
|
|
static int done;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
|
|
|
static const char *cursorNames[] = {
|
2022-11-30 13:51:59 -07:00
|
|
|
"arrow",
|
|
|
|
"ibeam",
|
|
|
|
"wait",
|
|
|
|
"crosshair",
|
|
|
|
"waitarrow",
|
|
|
|
"sizeNWSE",
|
|
|
|
"sizeNESW",
|
|
|
|
"sizeWE",
|
|
|
|
"sizeNS",
|
|
|
|
"sizeALL",
|
|
|
|
"NO",
|
|
|
|
"hand",
|
2023-12-25 15:02:02 -07:00
|
|
|
"window top left",
|
|
|
|
"window top",
|
|
|
|
"window top right",
|
|
|
|
"window right",
|
|
|
|
"window bottom right",
|
|
|
|
"window bottom",
|
|
|
|
"window bottom left",
|
|
|
|
"window left"
|
2015-06-21 09:33:46 -06:00
|
|
|
};
|
2023-12-26 10:57:11 -07:00
|
|
|
SDL_COMPILE_TIME_ASSERT(cursorNames, SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
|
|
|
|
|
2023-03-08 03:40:07 -07:00
|
|
|
static int system_cursor = -1;
|
|
|
|
static SDL_Cursor *cursor = NULL;
|
|
|
|
static SDL_bool relative_mode = SDL_FALSE;
|
|
|
|
static const SDL_DisplayMode *highlighted_mode = NULL;
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2021-11-07 02:48:29 -07:00
|
|
|
/* Draws the modes menu, and stores the mode index under the mouse in highlighted_mode */
|
|
|
|
static void
|
2023-01-04 17:22:40 -07:00
|
|
|
draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
|
2021-11-07 02:48:29 -07: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-01-31 22:23:14 -07:00
|
|
|
const SDL_DisplayMode **modes;
|
2021-11-07 02:48:29 -07:00
|
|
|
char text[1024];
|
|
|
|
const int lineHeight = 10;
|
2023-02-14 22:05:00 -07:00
|
|
|
int i, j;
|
2021-11-07 02:48:29 -07:00
|
|
|
int column_chars = 0;
|
|
|
|
int text_length;
|
2023-01-04 17:22:40 -07:00
|
|
|
float x, y;
|
|
|
|
float table_top;
|
2022-12-29 20:31:12 -07:00
|
|
|
SDL_FPoint mouse_pos = { -1.0f, -1.0f };
|
2023-02-14 22:05:00 -07:00
|
|
|
SDL_DisplayID *display_ids;
|
2021-11-07 02:48:29 -07:00
|
|
|
|
|
|
|
/* Get mouse position */
|
|
|
|
if (SDL_GetMouseFocus() == window) {
|
2022-12-29 20:31:12 -07:00
|
|
|
float window_x, window_y;
|
2021-11-09 22:03:42 -07:00
|
|
|
float logical_x, logical_y;
|
|
|
|
|
|
|
|
SDL_GetMouseState(&window_x, &window_y);
|
2023-02-03 13:25:46 -07:00
|
|
|
SDL_RenderCoordinatesFromWindow(renderer, window_x, window_y, &logical_x, &logical_y);
|
2021-11-09 22:03:42 -07:00
|
|
|
|
2022-12-29 20:31:12 -07:00
|
|
|
mouse_pos.x = logical_x;
|
|
|
|
mouse_pos.y = logical_y;
|
2021-11-07 02:48:29 -07:00
|
|
|
}
|
|
|
|
|
2023-01-04 17:22:40 -07:00
|
|
|
x = 0.0f;
|
2021-11-07 02:48:29 -07:00
|
|
|
y = viewport.y;
|
|
|
|
|
|
|
|
y += lineHeight;
|
|
|
|
|
2023-03-09 16:10:00 -07:00
|
|
|
SDL_strlcpy(text, "Click on a mode to set it with SDL_SetWindowFullscreenMode", sizeof(text));
|
2021-11-07 02:48:29 -07:00
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
|
|
|
SDLTest_DrawString(renderer, x, y, text);
|
|
|
|
y += lineHeight;
|
|
|
|
|
2023-03-09 16:10:00 -07:00
|
|
|
SDL_strlcpy(text, "Press Ctrl+Enter to toggle SDL_WINDOW_FULLSCREEN", sizeof(text));
|
2021-11-07 02:48:29 -07: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-14 22:05:00 -07:00
|
|
|
highlighted_mode = NULL;
|
2021-11-07 02:48:29 -07:00
|
|
|
}
|
|
|
|
|
2023-02-14 22:05:00 -07: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 08:34:16 -06:00
|
|
|
(void)SDL_snprintf(text, sizeof(text), "%s mode %d: %dx%d@%gx %gHz",
|
2023-02-14 22:05:00 -07:00
|
|
|
SDL_GetDisplayName(display_id),
|
2023-05-18 08:34:16 -06:00
|
|
|
j, mode->w, mode->h, mode->pixel_density, mode->refresh_rate);
|
2023-02-14 22:05:00 -07: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 02:48:29 -07:00
|
|
|
|
2023-02-14 22:05:00 -07:00
|
|
|
SDLTest_DrawString(renderer, x, y, text);
|
|
|
|
y += lineHeight;
|
2021-11-07 02:48:29 -07:00
|
|
|
|
2023-02-14 22:05:00 -07: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 02:48:29 -07:00
|
|
|
}
|
2023-02-14 22:05:00 -07:00
|
|
|
SDL_free((void *)modes);
|
2021-11-07 02:48:29 -07:00
|
|
|
}
|
2023-02-14 22:05:00 -07:00
|
|
|
SDL_free(display_ids);
|
2021-11-07 02:48:29 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 03:40:07 -07:00
|
|
|
static void loop(void)
|
2015-06-21 09:33:46 -06:00
|
|
|
{
|
2016-07-13 08:07:46 -06:00
|
|
|
int i;
|
2015-06-21 09:33:46 -06:00
|
|
|
SDL_Event event;
|
2022-11-30 13:51:59 -07:00
|
|
|
/* Check for events */
|
|
|
|
while (SDL_PollEvent(&event)) {
|
|
|
|
SDLTest_CommonEvent(state, &event, &done);
|
|
|
|
|
2023-01-23 18:54:09 -07:00
|
|
|
if (event.type == SDL_EVENT_WINDOW_RESIZED) {
|
2022-12-22 08:20:48 -07: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 13:51:59 -07:00
|
|
|
}
|
2022-12-22 08:20:48 -07:00
|
|
|
}
|
2023-01-23 18:54:09 -07:00
|
|
|
if (event.type == SDL_EVENT_WINDOW_MOVED) {
|
2022-12-22 08:20:48 -07: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-29 14:30:55 -07:00
|
|
|
SDL_GetDisplayName(SDL_GetDisplayForWindow(window)));
|
2022-11-30 13:51:59 -07:00
|
|
|
}
|
2022-12-22 08:20:48 -07:00
|
|
|
}
|
2023-01-23 18:54:09 -07:00
|
|
|
if (event.type == SDL_EVENT_WINDOW_FOCUS_LOST) {
|
2022-12-22 08:20:48 -07:00
|
|
|
relative_mode = SDL_GetRelativeMouseMode();
|
|
|
|
if (relative_mode) {
|
|
|
|
SDL_SetRelativeMouseMode(SDL_FALSE);
|
2022-11-30 13:51:59 -07:00
|
|
|
}
|
2022-12-22 08:20:48 -07:00
|
|
|
}
|
2023-01-23 18:54:09 -07:00
|
|
|
if (event.type == SDL_EVENT_WINDOW_FOCUS_GAINED) {
|
2022-12-22 08:20:48 -07:00
|
|
|
if (relative_mode) {
|
|
|
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
}
|
2023-01-23 18:54:09 -07:00
|
|
|
if (event.type == SDL_EVENT_KEY_UP) {
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_bool updateCursor = SDL_FALSE;
|
|
|
|
|
2023-02-01 12:45:31 -07:00
|
|
|
if (event.key.keysym.sym == SDLK_a) {
|
|
|
|
SDL_assert(!"Keyboard generated assert");
|
|
|
|
} else if (event.key.keysym.sym == SDLK_LEFT) {
|
2022-11-30 13:51:59 -07:00
|
|
|
--system_cursor;
|
|
|
|
if (system_cursor < 0) {
|
|
|
|
system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
updateCursor = SDL_TRUE;
|
|
|
|
} else if (event.key.keysym.sym == SDLK_RIGHT) {
|
|
|
|
++system_cursor;
|
|
|
|
if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
|
|
|
|
system_cursor = 0;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
updateCursor = SDL_TRUE;
|
|
|
|
}
|
|
|
|
if (updateCursor) {
|
|
|
|
SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
|
2022-12-29 16:07:59 -07:00
|
|
|
SDL_DestroyCursor(cursor);
|
2022-11-30 13:51:59 -07:00
|
|
|
cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
|
|
|
|
SDL_SetCursor(cursor);
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
}
|
2023-01-29 20:06:08 -07:00
|
|
|
if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_Window *window = SDL_GetMouseFocus();
|
2023-11-09 14:29:15 -07:00
|
|
|
if (highlighted_mode && window) {
|
2023-02-14 22:05:00 -07:00
|
|
|
SDL_memcpy(&state->fullscreen_mode, highlighted_mode, sizeof(state->fullscreen_mode));
|
|
|
|
SDL_SetWindowFullscreenMode(window, highlighted_mode);
|
2021-11-07 02:48:29 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
}
|
2016-07-13 08:07:46 -06:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
for (i = 0; i < state->num_windows; ++i) {
|
|
|
|
SDL_Window *window = state->windows[i];
|
|
|
|
SDL_Renderer *renderer = state->renderers[i];
|
2023-11-09 14:29:15 -07:00
|
|
|
if (window && renderer) {
|
2023-01-04 17:22:40 -07:00
|
|
|
float y = 0.0f;
|
|
|
|
SDL_Rect viewport;
|
|
|
|
SDL_FRect menurect;
|
2021-11-07 02:48:29 -07:00
|
|
|
|
2022-12-27 07:21:13 -07:00
|
|
|
SDL_GetRenderViewport(renderer, &viewport);
|
2021-11-07 02:48:29 -07:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
2021-06-07 18:24:57 -06:00
|
|
|
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
|
|
|
|
SDLTest_CommonDrawWindowInfo(renderer, state->windows[i], &y);
|
2021-11-07 02:48:29 -07:00
|
|
|
|
2023-01-04 17:22:40 -07:00
|
|
|
menurect.x = 0.0f;
|
2022-11-30 13:51:59 -07:00
|
|
|
menurect.y = y;
|
2023-01-04 17:22:40 -07:00
|
|
|
menurect.w = (float)viewport.w;
|
|
|
|
menurect.h = (float)viewport.h - y;
|
2022-11-30 13:51:59 -07:00
|
|
|
draw_modes_menu(window, renderer, menurect);
|
2021-06-07 18:24:57 -06:00
|
|
|
|
2023-01-02 17:04:08 -07:00
|
|
|
SDL_Delay(16);
|
2022-11-30 13:51:59 -07:00
|
|
|
SDL_RenderPresent(renderer);
|
2016-07-13 08:07:46 -06:00
|
|
|
}
|
2022-11-30 13:51:59 -07:00
|
|
|
}
|
2015-06-21 09:33:46 -06:00
|
|
|
#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
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Initialize test framework */
|
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
|
2023-11-09 14:29:15 -07:00
|
|
|
if (!state) {
|
2015-06-21 09:33:46 -06:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2023-03-16 17:25:39 -06:00
|
|
|
/* Enable standard application logging */
|
|
|
|
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
|
|
|
|
2019-05-18 23:45:15 -06:00
|
|
|
if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
|
|
|
|
SDLTest_CommonQuit(state);
|
|
|
|
return 1;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|
|
|
|
|
2016-07-13 08:07:46 -06: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 13:51:59 -07:00
|
|
|
|
2015-06-21 09:33:46 -06:00
|
|
|
/* Main render loop */
|
|
|
|
done = 0;
|
|
|
|
#ifdef __EMSCRIPTEN__
|
|
|
|
emscripten_set_main_loop(loop, 0, 1);
|
|
|
|
#else
|
|
|
|
while (!done) {
|
|
|
|
loop();
|
|
|
|
}
|
|
|
|
#endif
|
2022-12-29 16:07:59 -07:00
|
|
|
SDL_DestroyCursor(cursor);
|
2015-06-21 09:33:46 -06:00
|
|
|
|
2023-08-22 10:44:28 -06:00
|
|
|
SDLTest_CleanupTextDrawing();
|
|
|
|
SDLTest_CommonQuit(state);
|
2022-11-27 09:38:43 -07:00
|
|
|
return 0;
|
2015-06-21 09:33:46 -06:00
|
|
|
}
|