testwm.c: show all modes of all displays in the on-screen list (#7323)

* testwm.c: show all modes of all displays in the on-screen list

To allow testing https://github.com/libsdl-org/SDL/pull/7317
This commit is contained in:
Eric Wasylishen 2023-02-14 22:05:00 -07:00 committed by GitHub
parent beb6a2afdc
commit 1f46986d33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 48 additions and 45 deletions

View File

@ -40,7 +40,7 @@ static const char *cursorNames[] = {
int system_cursor = -1;
SDL_Cursor *cursor = NULL;
SDL_bool relative_mode = SDL_FALSE;
int highlighted_mode = -1;
const SDL_DisplayMode *highlighted_mode = NULL;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
@ -57,13 +57,13 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
const SDL_DisplayMode **modes;
char text[1024];
const int lineHeight = 10;
const SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
int i;
int i, j;
int column_chars = 0;
int text_length;
float x, y;
float table_top;
SDL_FPoint mouse_pos = { -1.0f, -1.0f };
SDL_DisplayID *display_ids;
/* Get mouse position */
if (SDL_GetMouseFocus() == window) {
@ -96,49 +96,58 @@ draw_modes_menu(SDL_Window *window, SDL_Renderer *renderer, SDL_FRect viewport)
/* Clear the cached mode under the mouse */
if (window == SDL_GetMouseFocus()) {
highlighted_mode = -1;
highlighted_mode = NULL;
}
modes = SDL_GetFullscreenDisplayModes(displayID, NULL);
for (i = 0; modes[i]; ++i) {
SDL_FRect cell_rect;
const SDL_DisplayMode *mode = modes[i];
display_ids = SDL_GetDisplays(NULL);
(void)SDL_snprintf(text, sizeof text, "%d: %dx%d@%gHz",
i, mode->pixel_w, mode->pixel_h, mode->refresh_rate);
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];
/* Update column width */
text_length = (int)SDL_strlen(text);
column_chars = SDL_max(column_chars, text_length);
(void)SDL_snprintf(text, sizeof text, "%s mode %d: %dx%d@%gHz",
SDL_GetDisplayName(display_id),
j, mode->pixel_w, mode->pixel_h, mode->refresh_rate);
/* 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;
/* Update column width */
text_length = (int)SDL_strlen(text);
column_chars = SDL_max(column_chars, text_length);
if (SDL_PointInRectFloat(&mouse_pos, &cell_rect)) {
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
/* 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;
/* Update cached mode under the mouse */
if (window == SDL_GetMouseFocus()) {
highlighted_mode = i;
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);
}
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;
}
}
} 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;
SDL_free((void *)modes);
}
SDL_free(display_ids);
}
SDL_free((void *)modes);
}
void loop()
@ -206,15 +215,9 @@ void loop()
}
if (event.type == SDL_EVENT_MOUSE_BUTTON_UP) {
SDL_Window *window = SDL_GetMouseFocus();
if (highlighted_mode != -1 && window != NULL) {
SDL_DisplayID displayID = SDL_GetDisplayForWindow(window);
int num_modes;
const SDL_DisplayMode **modes = SDL_GetFullscreenDisplayModes(displayID, &num_modes);
if (highlighted_mode < num_modes) {
SDL_memcpy(&state->fullscreen_mode, modes[highlighted_mode], sizeof(state->fullscreen_mode));
SDL_SetWindowFullscreenMode(window, modes[highlighted_mode]);
}
SDL_free((void *)modes);
if (highlighted_mode != NULL && window != NULL) {
SDL_memcpy(&state->fullscreen_mode, highlighted_mode, sizeof(state->fullscreen_mode));
SDL_SetWindowFullscreenMode(window, highlighted_mode);
}
}
}