When the window is resized, the viewport is automatically reset.
authorSam Lantinga <slouken@libsdl.org>
Wed, 29 May 2013 03:22:19 -0700
changeset 724039eb4958950a
parent 7239 04dda95ba22c
child 7241 28602f5ca96d
When the window is resized, the viewport is automatically reset.
This resolves lots of confusion around resizable windows. Most people don't expect a viewport to be implicitly set when the renderer is created and then not to be reset to the window size if the window is resized.

Added common test command line parameters --logical WxH and --scale N to test the render logical size and scaling APIs.
include/SDL_render.h
include/SDL_test_common.h
src/render/SDL_render.c
src/render/SDL_sysrender.h
src/test/SDL_test_common.c
     1.1 --- a/include/SDL_render.h	Wed May 29 03:07:55 2013 -0700
     1.2 +++ b/include/SDL_render.h	Wed May 29 03:22:19 2013 -0700
     1.3 @@ -481,8 +481,7 @@
     1.4   *
     1.5   *  \return 0 on success, or -1 on error
     1.6   *
     1.7 - *  \note When the window is resized, the current viewport is automatically
     1.8 - *        centered within the new window size.
     1.9 + *  \note If the window associated with the renderer is resized, the viewport is automatically reset.
    1.10   *
    1.11   *  \sa SDL_RenderGetViewport()
    1.12   *  \sa SDL_RenderSetLogicalSize()
     2.1 --- a/include/SDL_test_common.h	Wed May 29 03:07:55 2013 -0700
     2.2 +++ b/include/SDL_test_common.h	Wed May 29 03:22:19 2013 -0700
     2.3 @@ -69,6 +69,9 @@
     2.4      int window_minH;
     2.5      int window_maxW;
     2.6      int window_maxH;
     2.7 +    int logical_w;
     2.8 +    int logical_h;
     2.9 +    float scale;
    2.10      int depth;
    2.11      int refresh_rate;
    2.12      int num_windows;
     3.1 --- a/src/render/SDL_render.c	Wed May 29 03:07:55 2013 -0700
     3.2 +++ b/src/render/SDL_render.c	Wed May 29 03:22:19 2013 -0700
     3.3 @@ -110,29 +110,11 @@
     3.4                  renderer->WindowEvent(renderer, &event->window);
     3.5              }
     3.6  
     3.7 -            if (event->window.event == SDL_WINDOWEVENT_RESIZED) {
     3.8 -                if (renderer->logical_w) {
     3.9 -                    /* We'll update the renderer in the SIZE_CHANGED event */
    3.10 -                } else {
    3.11 -                    /* Try to keep the previous viewport centered */
    3.12 -                    int w, h;
    3.13 -
    3.14 -                    SDL_GetWindowSize(window, &w, &h);
    3.15 -                    if (renderer->target) {
    3.16 -                        renderer->viewport_backup.x = (w - renderer->viewport_backup.w) / 2;
    3.17 -                        renderer->viewport_backup.y = (h - renderer->viewport_backup.h) / 2;
    3.18 -                    } else {
    3.19 -                        renderer->viewport.x = (w - renderer->viewport.w) / 2;
    3.20 -                        renderer->viewport.y = (h - renderer->viewport.h) / 2;
    3.21 -                        renderer->UpdateViewport(renderer);
    3.22 -                    }
    3.23 -                }
    3.24 -                renderer->resized = SDL_TRUE;
    3.25 -            } else if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
    3.26 +            if (event->window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
    3.27                  if (renderer->logical_w) {
    3.28                      UpdateLogicalSize(renderer);
    3.29 -                } else if (!renderer->resized) {
    3.30 -                    /* Window was programmatically resized, reset viewport */
    3.31 +                } else {
    3.32 +                    /* Window was resized, reset viewport */
    3.33                      int w, h;
    3.34  
    3.35                      SDL_GetWindowSize(window, &w, &h);
    3.36 @@ -149,7 +131,6 @@
    3.37                          renderer->UpdateViewport(renderer);
    3.38                      }
    3.39                  }
    3.40 -                renderer->resized = SDL_FALSE;
    3.41              } else if (event->window.event == SDL_WINDOWEVENT_HIDDEN) {
    3.42                  renderer->hidden = SDL_TRUE;
    3.43              } else if (event->window.event == SDL_WINDOWEVENT_SHOWN) {
     4.1 --- a/src/render/SDL_sysrender.h	Wed May 29 03:07:55 2013 -0700
     4.2 +++ b/src/render/SDL_sysrender.h	Wed May 29 03:22:19 2013 -0700
     4.3 @@ -123,7 +123,6 @@
     4.4      /* The window associated with the renderer */
     4.5      SDL_Window *window;
     4.6      SDL_bool hidden;
     4.7 -    SDL_bool resized;
     4.8  
     4.9      /* The logical resolution for rendering */
    4.10      int logical_w;
     5.1 --- a/src/test/SDL_test_common.c	Wed May 29 03:07:55 2013 -0700
     5.2 +++ b/src/test/SDL_test_common.c	Wed May 29 03:22:19 2013 -0700
     5.3 @@ -27,7 +27,7 @@
     5.4  #include <stdio.h>
     5.5  
     5.6  #define VIDEO_USAGE \
     5.7 -"[--video driver] [--renderer driver] [--gldebug] [--info all|video|modes|render|event] [--log all|error|system|audio|video|render|input] [--display N] [--fullscreen | --fullscreen-desktop | --windows N] [--title title] [--icon icon.bmp] [--center | --position X,Y] [--geometry WxH] [--min-geometry WxH] [--max-geometry WxH] [--depth N] [--refresh R] [--vsync] [--noframe] [--resize] [--minimize] [--maximize] [--grab]"
     5.8 +"[--video driver] [--renderer driver] [--gldebug] [--info all|video|modes|render|event] [--log all|error|system|audio|video|render|input] [--display N] [--fullscreen | --fullscreen-desktop | --windows N] [--title title] [--icon icon.bmp] [--center | --position X,Y] [--geometry WxH] [--min-geometry WxH] [--max-geometry WxH] [--logical WxH] [--scale N] [--depth N] [--refresh R] [--vsync] [--noframe] [--resize] [--minimize] [--maximize] [--grab]"
     5.9  
    5.10  #define AUDIO_USAGE \
    5.11  "[--rate N] [--format U8|S8|U16|U16LE|U16BE|S16|S16LE|S16BE] [--channels N] [--samples N]"
    5.12 @@ -301,6 +301,33 @@
    5.13          state->window_maxH = SDL_atoi(h);
    5.14          return 2;
    5.15      }
    5.16 +    if (SDL_strcasecmp(argv[index], "--logical") == 0) {
    5.17 +        char *w, *h;
    5.18 +        ++index;
    5.19 +        if (!argv[index]) {
    5.20 +            return -1;
    5.21 +        }
    5.22 +        w = argv[index];
    5.23 +        h = argv[index];
    5.24 +        while (*h && *h != 'x') {
    5.25 +            ++h;
    5.26 +        }
    5.27 +        if (!*h) {
    5.28 +            return -1;
    5.29 +        }
    5.30 +        *h++ = '\0';
    5.31 +        state->logical_w = SDL_atoi(w);
    5.32 +        state->logical_h = SDL_atoi(h);
    5.33 +        return 2;
    5.34 +    }
    5.35 +    if (SDL_strcasecmp(argv[index], "--scale") == 0) {
    5.36 +        ++index;
    5.37 +        if (!argv[index]) {
    5.38 +            return -1;
    5.39 +        }
    5.40 +        state->scale = SDL_atof(argv[index]);
    5.41 +        return 2;
    5.42 +    }
    5.43      if (SDL_strcasecmp(argv[index], "--depth") == 0) {
    5.44          ++index;
    5.45          if (!argv[index]) {
    5.46 @@ -849,6 +876,11 @@
    5.47                              SDL_GetError());
    5.48                      return SDL_FALSE;
    5.49                  }
    5.50 +                if (state->logical_w && state->logical_h) {
    5.51 +                    SDL_RenderSetLogicalSize(state->renderers[i], state->logical_w, state->logical_h);
    5.52 +                } else if (state->scale) {
    5.53 +                    SDL_RenderSetScale(state->renderers[i], state->scale, state->scale);
    5.54 +                }
    5.55                  if (state->verbose & VERBOSE_RENDER) {
    5.56                      SDL_RendererInfo info;
    5.57  
    5.58 @@ -1140,24 +1172,6 @@
    5.59      switch (event->type) {
    5.60      case SDL_WINDOWEVENT:
    5.61          switch (event->window.event) {
    5.62 -        case SDL_WINDOWEVENT_SIZE_CHANGED:
    5.63 -            {
    5.64 -                SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);
    5.65 -                if (window) {
    5.66 -                    for (i = 0; i < state->num_windows; ++i) {
    5.67 -                        if (window == state->windows[i] &&
    5.68 -                            (state->window_flags & SDL_WINDOW_RESIZABLE)) {
    5.69 -                            SDL_Rect viewport;
    5.70 -
    5.71 -                            viewport.x = 0;
    5.72 -                            viewport.y = 0;
    5.73 -                            SDL_GetWindowSize(window, &viewport.w, &viewport.h);
    5.74 -                            SDL_RenderSetViewport(state->renderers[i], &viewport);
    5.75 -                        }
    5.76 -                    }
    5.77 -                }
    5.78 -            }
    5.79 -            break;
    5.80          case SDL_WINDOWEVENT_CLOSE:
    5.81              {
    5.82                  SDL_Window *window = SDL_GetWindowFromID(event->window.windowID);