From a0e055a7f9b2c99951bbd5972fa7ddc4cd1e5d41 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 7 Nov 2021 02:48:29 -0700 Subject: [PATCH] testwm2: add mode menu, click on a mode to call SDL_SetWindowDisplayMode --- include/SDL_test_common.h | 3 +- src/test/SDL_test_common.c | 53 +++++++++++++++-- test/testwm2.c | 116 ++++++++++++++++++++++++++++++++++++- 3 files changed, 165 insertions(+), 7 deletions(-) diff --git a/include/SDL_test_common.h b/include/SDL_test_common.h index 97f036d23..21b412cd9 100644 --- a/include/SDL_test_common.h +++ b/include/SDL_test_common.h @@ -215,9 +215,10 @@ void SDLTest_CommonQuit(SDLTest_CommonState * state); * * \param renderer The renderer to draw to. * \param window The window whose information should be displayed. + * \param usedHeight Returns the height used, so the caller can draw more below. * */ -void SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window); +void SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window, int * usedHeight); /* Ends C function definitions when using C++ */ #ifdef __cplusplus diff --git a/src/test/SDL_test_common.c b/src/test/SDL_test_common.c index 69eaa71cd..12ada05d2 100644 --- a/src/test/SDL_test_common.c +++ b/src/test/SDL_test_common.c @@ -2192,7 +2192,7 @@ SDLTest_CommonQuit(SDLTest_CommonState * state) } void -SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window) +SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window, int * usedHeight) { char text[1024]; int textY = 0; @@ -2203,9 +2203,34 @@ SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window) float ddpi, hdpi, vdpi; Uint32 flags; const int windowDisplayIndex = SDL_GetWindowDisplayIndex(window); + SDL_RendererInfo info; + + /* Video */ + + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDLTest_DrawString(renderer, 0, textY, "-- Video --"); + textY += lineHeight; + + SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); + + SDL_snprintf(text, sizeof(text), "SDL_GetCurrentVideoDriver: %s", SDL_GetCurrentVideoDriver()); + SDLTest_DrawString(renderer, 0, textY, text); + textY += lineHeight; /* Renderer */ + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDLTest_DrawString(renderer, 0, textY, "-- Renderer --"); + textY += lineHeight; + + SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); + + if (0 == SDL_GetRendererInfo(renderer, &info)) { + SDL_snprintf(text, sizeof(text), "SDL_GetRendererInfo: name: %s", info.name); + SDLTest_DrawString(renderer, 0, textY, text); + textY += lineHeight; + } + if (0 == SDL_GetRendererOutputSize(renderer, &w, &h)) { SDL_snprintf(text, sizeof(text), "SDL_GetRendererOutputSize: %dx%d", w, h); SDLTest_DrawString(renderer, 0, textY, text); @@ -2220,9 +2245,12 @@ SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window) /* Window */ - SDLTest_DrawString(renderer, 0, textY, "----"); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDLTest_DrawString(renderer, 0, textY, "-- Window --"); textY += lineHeight; + SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); + SDL_GetWindowPosition(window, &x, &y); SDL_snprintf(text, sizeof(text), "SDL_GetWindowPosition: %d,%d", x, y); SDLTest_DrawString(renderer, 0, textY, text); @@ -2238,11 +2266,21 @@ SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window) SDLTest_DrawString(renderer, 0, textY, text); textY += lineHeight; + if (0 == SDL_GetWindowDisplayMode(window, &mode)) { + SDL_snprintf(text, sizeof(text), "SDL_GetWindowDisplayMode: %dx%d@%dHz (%s)", + mode.w, mode.h, mode.refresh_rate, SDL_GetPixelFormatName(mode.format)); + SDLTest_DrawString(renderer, 0, textY, text); + textY += lineHeight; + } + /* Display */ - SDLTest_DrawString(renderer, 0, textY, "----"); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDLTest_DrawString(renderer, 0, textY, "-- Display --"); textY += lineHeight; + SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); + SDL_snprintf(text, sizeof(text), "SDL_GetWindowDisplayIndex: %d", windowDisplayIndex); SDLTest_DrawString(renderer, 0, textY, text); textY += lineHeight; @@ -2286,9 +2324,12 @@ SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window) /* Mouse */ - SDLTest_DrawString(renderer, 0, textY, "----"); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDLTest_DrawString(renderer, 0, textY, "-- Mouse --"); textY += lineHeight; + SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); + flags = SDL_GetMouseState(&x, &y); SDL_snprintf(text, sizeof(text), "SDL_GetMouseState: %d,%d ", x, y); SDLTest_PrintButtonMask(text, sizeof(text), flags); @@ -2300,6 +2341,10 @@ SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window) SDLTest_PrintButtonMask(text, sizeof(text), flags); SDLTest_DrawString(renderer, 0, textY, text); textY += lineHeight; + + if (usedHeight) { + *usedHeight = textY; + } } /* vi: set ts=4 sw=4 expandtab: */ diff --git a/test/testwm2.c b/test/testwm2.c index 30b93bd23..03530e586 100644 --- a/test/testwm2.c +++ b/test/testwm2.c @@ -18,6 +18,7 @@ #endif #include "SDL_test_common.h" +#include "SDL_test_font.h" static SDLTest_CommonState *state; int done; @@ -39,6 +40,7 @@ static const char *cursorNames[] = { int system_cursor = -1; SDL_Cursor *cursor = NULL; SDL_bool relative_mode = SDL_FALSE; +int highlighted_mode = -1; /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ static void @@ -48,6 +50,92 @@ quit(int rc) exit(rc); } +/* Draws the modes menu, and stores the mode index under the mouse in highlighted_mode */ +static void +draw_modes_menu(SDL_Window* window, SDL_Renderer* renderer, SDL_Rect viewport) +{ + SDL_DisplayMode mode; + char text[1024]; + const int lineHeight = 10; + const int display_index = SDL_GetWindowDisplayIndex(window); + const int num_modes = SDL_GetNumDisplayModes(display_index); + int i; + int column_chars = 0; + int text_length; + int x, y; + int table_top; + SDL_Point mouse_pos = { -1, -1 }; + + /* Get mouse position */ + if (SDL_GetMouseFocus() == window) { + SDL_GetMouseState(&mouse_pos.x, &mouse_pos.y); + } + + x = 0; + y = viewport.y; + + y += lineHeight; + + SDL_snprintf(text, sizeof(text), "Click on a mode to set it with SDL_SetWindowDisplayMode"); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDLTest_DrawString(renderer, x, y, text); + y += lineHeight; + + SDL_snprintf(text, sizeof(text), "Press Ctrl+Enter to toggle SDL_WINDOW_FULLSCREEN"); + 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()) { + highlighted_mode = -1; + } + + for (i = 0; i < num_modes; ++i) { + SDL_Rect cell_rect; + + if (0 != SDL_GetDisplayMode(display_index, i, &mode)) { + return; + } + + SDL_snprintf(text, sizeof(text), "%d: %dx%d@%dHz", + i, mode.w, mode.h, mode.refresh_rate); + + /* 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 = text_length * FONT_CHARACTER_SIZE; + cell_rect.h = lineHeight; + + if (SDL_PointInRect(&mouse_pos, &cell_rect)) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + + /* Update cached mode under the mouse */ + if (window == SDL_GetMouseFocus()) { + highlighted_mode = i; + } + } else { + SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); + } + + SDLTest_DrawString(renderer, x, y, text); + y += lineHeight; + + if (y + lineHeight > (viewport.y + viewport.h)) { + /* Advance to next column */ + x += (column_chars + 1) * FONT_CHARACTER_SIZE; + y = table_top; + column_chars = 0; + } + } +} + void loop() { @@ -112,16 +200,40 @@ loop() SDL_SetCursor(cursor); } } + if (event.type == SDL_MOUSEBUTTONUP) { + SDL_Window* window = SDL_GetMouseFocus(); + if (highlighted_mode != -1 && window != NULL) { + const int display_index = SDL_GetWindowDisplayIndex(window); + SDL_DisplayMode mode; + if (0 != SDL_GetDisplayMode(display_index, highlighted_mode, &mode)) { + SDL_Log("Couldn't get display mode"); + } else { + SDL_SetWindowDisplayMode(window, &mode); + } + } + } } for (i = 0; i < state->num_windows; ++i) { + SDL_Window* window = state->windows[i]; SDL_Renderer *renderer = state->renderers[i]; - if (renderer != NULL) { + if (window != NULL && renderer != NULL) { + int y = 0; + SDL_Rect viewport, menurect; + + SDL_RenderGetViewport(renderer, &viewport); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDLTest_CommonDrawWindowInfo(renderer, state->windows[i]); + SDLTest_CommonDrawWindowInfo(renderer, state->windows[i], &y); + + menurect.x = 0; + menurect.y = y; + menurect.w = viewport.w; + menurect.h = viewport.h - y; + draw_modes_menu(window, renderer, menurect); SDL_RenderPresent(renderer); }