testwm2: add mode menu, click on a mode to call SDL_SetWindowDisplayMode

This commit is contained in:
Eric Wasylishen 2021-11-07 02:48:29 -07:00 committed by Sam Lantinga
parent 70f7ebe635
commit a0e055a7f9
3 changed files with 165 additions and 7 deletions

View File

@ -215,9 +215,10 @@ void SDLTest_CommonQuit(SDLTest_CommonState * state);
* *
* \param renderer The renderer to draw to. * \param renderer The renderer to draw to.
* \param window The window whose information should be displayed. * \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++ */ /* Ends C function definitions when using C++ */
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -2192,7 +2192,7 @@ SDLTest_CommonQuit(SDLTest_CommonState * state)
} }
void void
SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window) SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window, int * usedHeight)
{ {
char text[1024]; char text[1024];
int textY = 0; int textY = 0;
@ -2203,9 +2203,34 @@ SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window)
float ddpi, hdpi, vdpi; float ddpi, hdpi, vdpi;
Uint32 flags; Uint32 flags;
const int windowDisplayIndex = SDL_GetWindowDisplayIndex(window); 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 */ /* 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)) { if (0 == SDL_GetRendererOutputSize(renderer, &w, &h)) {
SDL_snprintf(text, sizeof(text), "SDL_GetRendererOutputSize: %dx%d", w, h); SDL_snprintf(text, sizeof(text), "SDL_GetRendererOutputSize: %dx%d", w, h);
SDLTest_DrawString(renderer, 0, textY, text); SDLTest_DrawString(renderer, 0, textY, text);
@ -2220,9 +2245,12 @@ SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window)
/* Window */ /* Window */
SDLTest_DrawString(renderer, 0, textY, "----"); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDLTest_DrawString(renderer, 0, textY, "-- Window --");
textY += lineHeight; textY += lineHeight;
SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
SDL_GetWindowPosition(window, &x, &y); SDL_GetWindowPosition(window, &x, &y);
SDL_snprintf(text, sizeof(text), "SDL_GetWindowPosition: %d,%d", x, y); SDL_snprintf(text, sizeof(text), "SDL_GetWindowPosition: %d,%d", x, y);
SDLTest_DrawString(renderer, 0, textY, text); SDLTest_DrawString(renderer, 0, textY, text);
@ -2238,11 +2266,21 @@ SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window)
SDLTest_DrawString(renderer, 0, textY, text); SDLTest_DrawString(renderer, 0, textY, text);
textY += lineHeight; 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 */ /* Display */
SDLTest_DrawString(renderer, 0, textY, "----"); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDLTest_DrawString(renderer, 0, textY, "-- Display --");
textY += lineHeight; textY += lineHeight;
SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
SDL_snprintf(text, sizeof(text), "SDL_GetWindowDisplayIndex: %d", windowDisplayIndex); SDL_snprintf(text, sizeof(text), "SDL_GetWindowDisplayIndex: %d", windowDisplayIndex);
SDLTest_DrawString(renderer, 0, textY, text); SDLTest_DrawString(renderer, 0, textY, text);
textY += lineHeight; textY += lineHeight;
@ -2286,9 +2324,12 @@ SDLTest_CommonDrawWindowInfo(SDL_Renderer * renderer, SDL_Window * window)
/* Mouse */ /* Mouse */
SDLTest_DrawString(renderer, 0, textY, "----"); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDLTest_DrawString(renderer, 0, textY, "-- Mouse --");
textY += lineHeight; textY += lineHeight;
SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255);
flags = SDL_GetMouseState(&x, &y); flags = SDL_GetMouseState(&x, &y);
SDL_snprintf(text, sizeof(text), "SDL_GetMouseState: %d,%d ", x, y); SDL_snprintf(text, sizeof(text), "SDL_GetMouseState: %d,%d ", x, y);
SDLTest_PrintButtonMask(text, sizeof(text), flags); 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_PrintButtonMask(text, sizeof(text), flags);
SDLTest_DrawString(renderer, 0, textY, text); SDLTest_DrawString(renderer, 0, textY, text);
textY += lineHeight; textY += lineHeight;
if (usedHeight) {
*usedHeight = textY;
}
} }
/* vi: set ts=4 sw=4 expandtab: */ /* vi: set ts=4 sw=4 expandtab: */

View File

@ -18,6 +18,7 @@
#endif #endif
#include "SDL_test_common.h" #include "SDL_test_common.h"
#include "SDL_test_font.h"
static SDLTest_CommonState *state; static SDLTest_CommonState *state;
int done; int done;
@ -39,6 +40,7 @@ static const char *cursorNames[] = {
int system_cursor = -1; int system_cursor = -1;
SDL_Cursor *cursor = NULL; SDL_Cursor *cursor = NULL;
SDL_bool relative_mode = SDL_FALSE; SDL_bool relative_mode = SDL_FALSE;
int highlighted_mode = -1;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */ /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void static void
@ -48,6 +50,92 @@ quit(int rc)
exit(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 void
loop() loop()
{ {
@ -112,16 +200,40 @@ loop()
SDL_SetCursor(cursor); 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) { for (i = 0; i < state->num_windows; ++i) {
SDL_Window* window = state->windows[i];
SDL_Renderer *renderer = state->renderers[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_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); 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); SDL_RenderPresent(renderer);
} }