testwm2: add mode menu, click on a mode to call SDL_SetWindowDisplayMode
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
diff --git a/include/SDL_test_common.h b/include/SDL_test_common.h
index 97f036d..21b412c 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 69eaa71..12ada05 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 30b93bd..03530e5 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);
}