Fixed window resize in OpenGL and added window resize to SDL renderer
authorIngo Ruhnke <grumbel@gmail.com>
Thu, 31 Jul 2014 04:02:55 +0000 (06:02 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Thu, 31 Jul 2014 04:02:55 +0000 (06:02 +0200)
src/supertux/main.cpp
src/supertux/screen_manager.cpp
src/video/gl/gl_renderer.cpp
src/video/sdl/sdl_renderer.cpp

index 3749a5c..15ac35b 100644 (file)
@@ -464,12 +464,6 @@ Main::init_rand()
 void
 Main::init_video()
 {
-  SCREEN_WIDTH  = 800;
-  SCREEN_HEIGHT = 600;
-
-  PHYSICAL_SCREEN_WIDTH = SCREEN_WIDTH;
-  PHYSICAL_SCREEN_HEIGHT = SCREEN_HEIGHT;
-
   SDL_SetWindowTitle(Renderer::instance()->get_window(), PACKAGE_NAME " " PACKAGE_VERSION);
 
   const char* icon_fname = "images/engine/icons/supertux-256x256.png";
index add990f..e1e7f12 100644 (file)
@@ -206,7 +206,7 @@ ScreenManager::process_events()
         break;
               
       case SDL_WINDOWEVENT:
-        switch(event.window.type)
+        switch(event.window.event)
         {
           case SDL_WINDOWEVENT_RESIZED:
             Renderer::instance()->resize(event.window.data1,
index ba3a601..ca7a477 100644 (file)
@@ -21,8 +21,6 @@
 #include <iostream>
 #include <physfs.h>
 #include "SDL.h"
-//#include "SDL/SDL.h"
-//#include "SDL/SDL_opengl.h"
 
 #include "supertux/gameconfig.hpp"
 #include "supertux/globals.hpp"
@@ -36,6 +34,7 @@
 #endif
 
 GLRenderer::GLRenderer() :
+  window(),
   desktop_size(-1, -1),
   screen_size(-1, -1),
   fullscreen_active(false),
@@ -472,12 +471,11 @@ GLRenderer::flip()
 void
 GLRenderer::resize(int w, int h)
 {
-#ifdef OLD_SDL1
-  SDL_CreateWindow(SDL_GL_CreateContext(w, h, 0, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE));
-#endif
-
   g_config->window_size = Size(w, h);
 
+  PHYSICAL_SCREEN_WIDTH = w;
+  PHYSICAL_SCREEN_HEIGHT = h;
+
   apply_config();
 }
 
@@ -607,35 +605,60 @@ GLRenderer::apply_config()
 void
 GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
 {
-  // Only change video mode when its different from the current one
-  if (screen_size != size || fullscreen_active != fullscreen)
+  if (window)
   {
-    int flags = SDL_WINDOW_OPENGL;
+    SDL_SetWindowSize(window, size.width, size.height);
 
     if (fullscreen)
     {
-      flags |= SDL_WINDOW_FULLSCREEN;
+      int fullscreen_flags = SDL_WINDOW_FULLSCREEN; // SDL_WINDOW_FULLSCREEN_DESKTOP or 0
+      SDL_SetWindowDisplayMode(window, NULL);
+      SDL_SetWindowFullscreen(window, fullscreen_flags);
     }
     else
     {
-      flags |= SDL_WINDOW_RESIZABLE;
+      SDL_SetWindowFullscreen(window, 0);
     }
-
-    window = SDL_CreateWindow("SuperTux",
-                              SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
-                              size.width, size.height,
-                              flags);
-    if (!window)
-    {
-      std::ostringstream msg;
-      msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
-      throw std::runtime_error(msg.str());
-    }
-    else
+  }
+  else
+  {
+    // Only change video mode when its different from the current one
+    if (screen_size != size || fullscreen_active != fullscreen)
     {
-      glcontext = SDL_GL_CreateContext(window);
-      screen_size = size;
-      fullscreen_active = fullscreen;
+      int flags = SDL_WINDOW_OPENGL;
+
+      if (fullscreen)
+      {
+        flags |= SDL_WINDOW_FULLSCREEN;
+      }
+      else
+      {
+        flags |= SDL_WINDOW_RESIZABLE;
+      }
+
+      window = SDL_CreateWindow("SuperTux",
+                                SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
+                                size.width, size.height,
+                                flags);
+      if (!window)
+      {
+        std::ostringstream msg;
+        msg << "Couldn't set video mode " << size.width << "x" << size.height << ": " << SDL_GetError();
+        throw std::runtime_error(msg.str());
+      }
+      else
+      {
+        glcontext = SDL_GL_CreateContext(window);
+        screen_size = size;
+        
+        PHYSICAL_SCREEN_WIDTH = size.width;
+        PHYSICAL_SCREEN_HEIGHT = size.height;
+
+        SCREEN_WIDTH = size.width;
+        SCREEN_HEIGHT = size.height;
+        
+        fullscreen_active = fullscreen;
+      }
     }
   }
 }
index ec15d30..eb8a031 100644 (file)
@@ -39,7 +39,7 @@ SDLRenderer::SDLRenderer() :
   int width  = g_config->window_size.width;
   int height = g_config->window_size.height;
 
-  int flags = 0;
+  int flags = SDL_WINDOW_RESIZABLE;
   if(g_config->use_fullscreen)
   {
     flags |= SDL_WINDOW_FULLSCREEN;
@@ -47,6 +47,12 @@ SDLRenderer::SDLRenderer() :
     height = g_config->fullscreen_size.height;
   }
 
+  SCREEN_WIDTH = width;
+  SCREEN_HEIGHT = height;
+
+  PHYSICAL_SCREEN_WIDTH = width;
+  PHYSICAL_SCREEN_HEIGHT = height;
+
   int ret = SDL_CreateWindowAndRenderer(width, height, flags,
                                         &window, &renderer);
 
@@ -432,9 +438,13 @@ SDLRenderer::flip()
 }
 
 void
-SDLRenderer::resize(int, int)
+SDLRenderer::resize(int w , int h)
 {
-    
+  SCREEN_WIDTH  = w;
+  SCREEN_HEIGHT = h;
+
+  PHYSICAL_SCREEN_WIDTH = w;
+  PHYSICAL_SCREEN_HEIGHT = h;
 }
 
 void