Allow the render context to do necessary work when the video mode changes.
authorSam Lantinga
Sun, 06 Aug 2006 04:39:13 +0000
changeset 1970db3ba6c0d0df
parent 1969 5d3724f64f2b
child 1971 5432b63cc919
Allow the render context to do necessary work when the video mode changes.
src/events/SDL_windowevents.c
src/video/SDL_renderer_gl.c
src/video/SDL_renderer_sw.c
src/video/SDL_sysvideo.h
src/video/SDL_video.c
src/video/cocoa/SDL_cocoaopengl.m
test/common.c
     1.1 --- a/src/events/SDL_windowevents.c	Sun Aug 06 00:09:04 2006 +0000
     1.2 +++ b/src/events/SDL_windowevents.c	Sun Aug 06 04:39:13 2006 +0000
     1.3 @@ -72,6 +72,7 @@
     1.4          }
     1.5          window->w = data1;
     1.6          window->h = data2;
     1.7 +        SDL_OnWindowResized(window);
     1.8          break;
     1.9      case SDL_WINDOWEVENT_MINIMIZED:
    1.10          if (window->flags & SDL_WINDOW_MINIMIZED) {
     2.1 --- a/src/video/SDL_renderer_gl.c	Sun Aug 06 00:09:04 2006 +0000
     2.2 +++ b/src/video/SDL_renderer_gl.c	Sun Aug 06 04:39:13 2006 +0000
     2.3 @@ -34,6 +34,7 @@
     2.4  
     2.5  static SDL_Renderer *GL_CreateRenderer(SDL_Window * window, Uint32 flags);
     2.6  static int GL_ActivateRenderer(SDL_Renderer * renderer);
     2.7 +static int GL_DisplayModeChanged(SDL_Renderer * renderer);
     2.8  static int GL_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
     2.9  static int GL_SetTexturePalette(SDL_Renderer * renderer,
    2.10                                  SDL_Texture * texture,
    2.11 @@ -97,6 +98,7 @@
    2.12  typedef struct
    2.13  {
    2.14      SDL_GLContext context;
    2.15 +    SDL_bool updateSize;
    2.16      SDL_bool GL_ARB_texture_rectangle_supported;
    2.17      int blendMode;
    2.18      int scaleMode;
    2.19 @@ -219,6 +221,7 @@
    2.20      }
    2.21  
    2.22      renderer->ActivateRenderer = GL_ActivateRenderer;
    2.23 +    renderer->DisplayModeChanged = GL_DisplayModeChanged;
    2.24      renderer->CreateTexture = GL_CreateTexture;
    2.25      renderer->SetTexturePalette = GL_SetTexturePalette;
    2.26      renderer->GetTexturePalette = GL_GetTexturePalette;
    2.27 @@ -282,13 +285,7 @@
    2.28      } else {
    2.29          data->glEnable(GL_TEXTURE_2D);
    2.30      }
    2.31 -    data->glMatrixMode(GL_PROJECTION);
    2.32 -    data->glLoadIdentity();
    2.33 -    data->glMatrixMode(GL_MODELVIEW);
    2.34 -    data->glLoadIdentity();
    2.35 -    data->glViewport(0, 0, window->w, window->h);
    2.36 -    data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0, 0.0,
    2.37 -                  1.0);
    2.38 +    data->updateSize = SDL_TRUE;
    2.39  
    2.40      return renderer;
    2.41  }
    2.42 @@ -299,7 +296,29 @@
    2.43      GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
    2.44      SDL_Window *window = SDL_GetWindowFromID(renderer->window);
    2.45  
    2.46 -    return SDL_GL_MakeCurrent(window->id, data->context);
    2.47 +    if (SDL_GL_MakeCurrent(window->id, data->context) < 0) {
    2.48 +        return -1;
    2.49 +    }
    2.50 +    if (data->updateSize) {
    2.51 +        data->glMatrixMode(GL_PROJECTION);
    2.52 +        data->glLoadIdentity();
    2.53 +        data->glMatrixMode(GL_MODELVIEW);
    2.54 +        data->glLoadIdentity();
    2.55 +        data->glViewport(0, 0, window->w, window->h);
    2.56 +        data->glOrtho(0.0, (GLdouble) window->w, (GLdouble) window->h, 0.0,
    2.57 +                      0.0, 1.0);
    2.58 +        data->updateSize = SDL_FALSE;
    2.59 +    }
    2.60 +    return 0;
    2.61 +}
    2.62 +
    2.63 +static int
    2.64 +GL_DisplayModeChanged(SDL_Renderer * renderer)
    2.65 +{
    2.66 +    GL_RenderData *data = (GL_RenderData *) renderer->driverdata;
    2.67 +
    2.68 +    data->updateSize = SDL_TRUE;
    2.69 +    return 0;
    2.70  }
    2.71  
    2.72  static __inline__ int
     3.1 --- a/src/video/SDL_renderer_sw.c	Sun Aug 06 00:09:04 2006 +0000
     3.2 +++ b/src/video/SDL_renderer_sw.c	Sun Aug 06 04:39:13 2006 +0000
     3.3 @@ -31,6 +31,8 @@
     3.4  /* SDL surface based renderer implementation */
     3.5  
     3.6  static SDL_Renderer *SW_CreateRenderer(SDL_Window * window, Uint32 flags);
     3.7 +static int SW_ActivateRenderer(SDL_Renderer * renderer);
     3.8 +static int SW_DisplayModeChanged(SDL_Renderer * renderer);
     3.9  static int SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture);
    3.10  static int SW_QueryTexturePixels(SDL_Renderer * renderer,
    3.11                                   SDL_Texture * texture, void **pixels,
    3.12 @@ -179,7 +181,8 @@
    3.13          SDL_OutOfMemory();
    3.14          return NULL;
    3.15      }
    3.16 -
    3.17 +    renderer->ActivateRenderer = SW_ActivateRenderer;
    3.18 +    renderer->DisplayModeChanged = SW_DisplayModeChanged;
    3.19      renderer->CreateTexture = SW_CreateTexture;
    3.20      renderer->QueryTexturePixels = SW_QueryTexturePixels;
    3.21      renderer->SetTexturePalette = SW_SetTexturePalette;
    3.22 @@ -270,6 +273,32 @@
    3.23  }
    3.24  
    3.25  static int
    3.26 +SW_ActivateRenderer(SDL_Renderer * renderer)
    3.27 +{
    3.28 +    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
    3.29 +
    3.30 +    if (data->renderer && data->renderer->ActivateRenderer) {
    3.31 +        if (data->renderer->ActivateRenderer(data->renderer) < 0) {
    3.32 +            return -1;
    3.33 +        }
    3.34 +    }
    3.35 +    return 0;
    3.36 +}
    3.37 +
    3.38 +static int
    3.39 +SW_DisplayModeChanged(SDL_Renderer * renderer)
    3.40 +{
    3.41 +    SW_RenderData *data = (SW_RenderData *) renderer->driverdata;
    3.42 +
    3.43 +    if (data->renderer && data->renderer->DisplayModeChanged) {
    3.44 +        if (data->renderer->DisplayModeChanged(data->renderer) < 0) {
    3.45 +            return -1;
    3.46 +        }
    3.47 +    }
    3.48 +    return 0;
    3.49 +}
    3.50 +
    3.51 +static int
    3.52  SW_CreateTexture(SDL_Renderer * renderer, SDL_Texture * texture)
    3.53  {
    3.54      if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
     4.1 --- a/src/video/SDL_sysvideo.h	Sun Aug 06 00:09:04 2006 +0000
     4.2 +++ b/src/video/SDL_sysvideo.h	Sun Aug 06 04:39:13 2006 +0000
     4.3 @@ -56,6 +56,7 @@
     4.4  struct SDL_Renderer
     4.5  {
     4.6      int (*ActivateRenderer) (SDL_Renderer * renderer);
     4.7 +    int (*DisplayModeChanged) (SDL_Renderer * renderer);
     4.8      int (*CreateTexture) (SDL_Renderer * renderer, SDL_Texture * texture);
     4.9      int (*QueryTexturePixels) (SDL_Renderer * renderer, SDL_Texture * texture,
    4.10                                 void **pixels, int *pitch);
    4.11 @@ -133,8 +134,7 @@
    4.12      SDL_DisplayMode *display_modes;
    4.13      SDL_DisplayMode desktop_mode;
    4.14      SDL_DisplayMode current_mode;
    4.15 -    SDL_DisplayMode desired_mode;
    4.16 -    SDL_DisplayMode *fullscreen_mode;
    4.17 +    SDL_DisplayMode fullscreen_mode;
    4.18      SDL_Palette *palette;
    4.19  
    4.20      Uint16 *gamma;
    4.21 @@ -389,6 +389,7 @@
    4.22  
    4.23  extern void SDL_OnWindowShown(SDL_Window * window);
    4.24  extern void SDL_OnWindowHidden(SDL_Window * window);
    4.25 +extern void SDL_OnWindowResized(SDL_Window * window);
    4.26  extern void SDL_OnWindowFocusGained(SDL_Window * window);
    4.27  extern void SDL_OnWindowFocusLost(SDL_Window * window);
    4.28  extern SDL_WindowID SDL_GetFocusWindow(void);
     5.1 --- a/src/video/SDL_video.c	Sun Aug 06 00:09:04 2006 +0000
     5.2 +++ b/src/video/SDL_video.c	Sun Aug 06 04:39:13 2006 +0000
     5.3 @@ -645,6 +645,7 @@
     5.4  SDL_SetFullscreenDisplayMode(const SDL_DisplayMode * mode)
     5.5  {
     5.6      SDL_VideoDisplay *display;
     5.7 +    SDL_DisplayMode fullscreen_mode;
     5.8      int i;
     5.9  
    5.10      if (!_this) {
    5.11 @@ -653,18 +654,29 @@
    5.12      }
    5.13  
    5.14      display = &SDL_CurrentDisplay;
    5.15 -    if (mode) {
    5.16 -        SDL_GetClosestDisplayMode(mode, &display->desired_mode);
    5.17 -        display->fullscreen_mode = &display->desired_mode;
    5.18 -    } else {
    5.19 -        display->fullscreen_mode = NULL;
    5.20 +    if (!mode) {
    5.21 +        mode = &display->desktop_mode;
    5.22      }
    5.23  
    5.24 +    SDL_GetClosestDisplayMode(mode, &fullscreen_mode);
    5.25 +    if (SDL_memcmp
    5.26 +        (&fullscreen_mode, &display->fullscreen_mode,
    5.27 +         sizeof(fullscreen_mode)) == 0) {
    5.28 +        /* Nothing to do... */
    5.29 +        return 0;
    5.30 +    }
    5.31 +    display->fullscreen_mode = fullscreen_mode;
    5.32 +
    5.33      /* Actually set the mode if we have a fullscreen window visible */
    5.34      for (i = 0; i < display->num_windows; ++i) {
    5.35          SDL_Window *window = &display->windows[i];
    5.36          if (FULLSCREEN_VISIBLE(window)) {
    5.37 -            return SDL_SetDisplayMode(display->fullscreen_mode);
    5.38 +            if (SDL_SetDisplayMode(&display->fullscreen_mode) < 0) {
    5.39 +                return -1;
    5.40 +            }
    5.41 +        }
    5.42 +        if (window->flags & SDL_WINDOW_FULLSCREEN) {
    5.43 +            SDL_OnWindowResized(window);
    5.44          }
    5.45      }
    5.46      return 0;
    5.47 @@ -678,7 +690,7 @@
    5.48          return -1;
    5.49      }
    5.50      if (mode) {
    5.51 -        *mode = *SDL_CurrentDisplay.fullscreen_mode;
    5.52 +        *mode = SDL_CurrentDisplay.fullscreen_mode;
    5.53      }
    5.54      return 0;
    5.55  }
    5.56 @@ -1197,7 +1209,7 @@
    5.57                  }
    5.58              }
    5.59  
    5.60 -            SDL_SetDisplayMode(display->fullscreen_mode);
    5.61 +            SDL_SetDisplayMode(&display->fullscreen_mode);
    5.62          }
    5.63      } else {
    5.64          window->flags &= ~SDL_WINDOW_FULLSCREEN;
    5.65 @@ -1252,12 +1264,22 @@
    5.66  }
    5.67  
    5.68  void
    5.69 +SDL_OnWindowResized(SDL_Window * window)
    5.70 +{
    5.71 +    SDL_Renderer *renderer = window->renderer;
    5.72 +
    5.73 +    if (renderer && renderer->DisplayModeChanged) {
    5.74 +        renderer->DisplayModeChanged(renderer);
    5.75 +    }
    5.76 +}
    5.77 +
    5.78 +void
    5.79  SDL_OnWindowFocusGained(SDL_Window * window)
    5.80  {
    5.81      SDL_VideoDisplay *display = SDL_GetDisplayFromWindow(window);
    5.82  
    5.83      if (window->flags & SDL_WINDOW_FULLSCREEN) {
    5.84 -        SDL_SetDisplayMode(display->fullscreen_mode);
    5.85 +        SDL_SetDisplayMode(&display->fullscreen_mode);
    5.86      }
    5.87      if (display->gamma && _this->SetDisplayGammaRamp) {
    5.88          _this->SetDisplayGammaRamp(_this, display->gamma);
     6.1 --- a/src/video/cocoa/SDL_cocoaopengl.m	Sun Aug 06 00:09:04 2006 +0000
     6.2 +++ b/src/video/cocoa/SDL_cocoaopengl.m	Sun Aug 06 04:39:13 2006 +0000
     6.3 @@ -268,6 +268,7 @@
     6.4          NSOpenGLContext *nscontext = (NSOpenGLContext *)context;
     6.5  
     6.6          [nscontext setView:[windowdata->window contentView]];
     6.7 +        [nscontext update];
     6.8          [nscontext makeCurrentContext];
     6.9      } else {
    6.10          [NSOpenGLContext clearCurrentContext];
     7.1 --- a/test/common.c	Sun Aug 06 00:09:04 2006 +0000
     7.2 +++ b/test/common.c	Sun Aug 06 04:39:13 2006 +0000
     7.3 @@ -749,15 +749,15 @@
     7.4              fprintf(stderr, "Window %d hidden", event->window.windowID);
     7.5              break;
     7.6          case SDL_WINDOWEVENT_EXPOSED:
     7.7 -            fprintf(stderr, "Window %d exposed: %d", event->window.windowID);
     7.8 +            fprintf(stderr, "Window %d exposed", event->window.windowID);
     7.9              break;
    7.10          case SDL_WINDOWEVENT_MOVED:
    7.11 -            fprintf(stderr, "Window %d moved to %d,%d: %d",
    7.12 +            fprintf(stderr, "Window %d moved to %d,%d",
    7.13                      event->window.windowID, event->window.data1,
    7.14                      event->window.data2);
    7.15              break;
    7.16          case SDL_WINDOWEVENT_RESIZED:
    7.17 -            fprintf(stderr, "Window %d resized to %dx%d: %d",
    7.18 +            fprintf(stderr, "Window %d resized to %dx%d",
    7.19                      event->window.windowID, event->window.data1,
    7.20                      event->window.data2);
    7.21              break;