Added support for desktop-fullscreen mode
authorIngo Ruhnke <grumbel@gmail.com>
Mon, 4 Aug 2014 02:28:25 +0000 (04:28 +0200)
committerIngo Ruhnke <grumbel@gmail.com>
Mon, 4 Aug 2014 02:31:19 +0000 (04:31 +0200)
src/supertux/gameconfig.cpp
src/supertux/main.cpp
src/supertux/menu/options_menu.cpp
src/video/gl/gl_renderer.cpp
src/video/gl/gl_renderer.hpp
src/video/sdl/sdl_renderer.cpp

index 0322a9d..23c0d4f 100644 (file)
@@ -27,7 +27,7 @@
 
 Config::Config() :
   profile(1),
-  fullscreen_size(800, 600),
+  fullscreen_size(0, 0),
   fullscreen_refresh_rate(0),
   window_size(800, 600),
   aspect_size(0, 0), // auto detect
index 604d91f..5dc4c88 100644 (file)
@@ -438,7 +438,7 @@ Main::init_video()
     SDL_SetWindowIcon(Renderer::instance()->get_window(), icon);
     SDL_FreeSurface(icon);
   }
-  //SDL_ShowCursor(0);
+  SDL_ShowCursor(0);
 
   log_info << (g_config->use_fullscreen?"fullscreen ":"window ")
            << " Window: "     << g_config->window_size
index d65aa7d..a8d37f6 100644 (file)
@@ -122,25 +122,30 @@ OptionsMenu::OptionsMenu() :
       fullscreen_res->list.push_back(out.str());
     }
   }
+  fullscreen_res->list.push_back("Desktop");
 
   std::ostringstream out;
-  out << g_config->fullscreen_size.width << "x" << g_config->fullscreen_size.height << "@" << g_config->fullscreen_refresh_rate;
-  std::string fllscrn_sz = out.str();
+  std::string fullscreen_size_str = "Desktop";
+  if (g_config->fullscreen_size != Size(0, 0))
+  {
+    out << g_config->fullscreen_size.width << "x" << g_config->fullscreen_size.height << "@" << g_config->fullscreen_refresh_rate;
+    fullscreen_size_str = out.str();
+  }
   size_t cnt = 0;
   for (std::vector<std::string>::iterator i = fullscreen_res->list.begin(); i != fullscreen_res->list.end(); ++i) 
   {
-    if (*i == fllscrn_sz)
+    if (*i == fullscreen_size_str)
     {
-      fllscrn_sz.clear();
+      fullscreen_size_str.clear();
       fullscreen_res->selected = cnt;
       break;
     }
     ++cnt;
   }
-  if (!fllscrn_sz.empty())
+  if (!fullscreen_size_str.empty())
   {
     fullscreen_res->selected = fullscreen_res->list.size();
-    fullscreen_res->list.push_back(fllscrn_sz);
+    fullscreen_res->list.push_back(fullscreen_size_str);
   }
 
   MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
@@ -243,7 +248,13 @@ OptionsMenu::menu_action(MenuItem* item)
         int width;
         int height;
         int refresh_rate;
-        if(sscanf(item->list[item->selected].c_str(), "%dx%d@%d",
+        if (item->list[item->selected] == "Desktop")
+        {
+          g_config->fullscreen_size.width = 0;
+          g_config->fullscreen_size.height = 0;
+          g_config->fullscreen_refresh_rate = 0;
+        }
+        else if(sscanf(item->list[item->selected].c_str(), "%dx%d@%d",
                   &width, &height, &refresh_rate) == 3)
         {
           // do nothing, changes are only applied when toggling fullscreen mode
index d24127c..25603c7 100644 (file)
@@ -38,7 +38,6 @@
 GLRenderer::GLRenderer() :
   window(),
   desktop_size(0, 0),
-  screen_size(0, 0),
   fullscreen_active(false),
   last_texture(static_cast<GLuint> (-1))
 {
@@ -69,14 +68,7 @@ GLRenderer::GLRenderer() :
   SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
   SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE,  5);
 
-  if(g_config->use_fullscreen)
-  {
-    apply_video_mode(g_config->fullscreen_size, true);
-  }
-  else
-  {
-    apply_video_mode(g_config->window_size, false);
-  }
+  apply_video_mode();
 
   // setup opengl state and transform
   glDisable(GL_DEPTH_TEST);
@@ -247,9 +239,9 @@ GLRenderer::draw_filled_rect(const DrawingRequest& request)
 
     // inner rectangle
     Rectf irect(request.pos.x    + radius,
-               request.pos.y    + radius,
-               request.pos.x + fillrectrequest->size.x - radius,
-               request.pos.y + fillrectrequest->size.y - radius);
+                request.pos.y    + radius,
+                request.pos.x + fillrectrequest->size.x - radius,
+                request.pos.y + fillrectrequest->size.y - radius);
 
     int n = 8;
     int p = 0;
@@ -472,10 +464,10 @@ GLRenderer::resize(int w, int h)
 void
 GLRenderer::apply_config()
 {
-  apply_video_mode(screen_size, g_config->use_fullscreen);
+  apply_video_mode();
 
   Size target_size = g_config->use_fullscreen ?
-    g_config->fullscreen_size :
+    ((g_config->fullscreen_size == Size(0, 0)) ? desktop_size : g_config->fullscreen_size) :
     g_config->window_size;
 
   float pixel_aspect_ratio = 1.0f;
@@ -495,7 +487,7 @@ GLRenderer::apply_config()
 
   Vector scale;
   Size logical_size;
-  calculate_viewport(min_size, max_size, screen_size,
+  calculate_viewport(min_size, max_size, target_size,
                      pixel_aspect_ratio,
                      g_config->magnification,
                      scale,
@@ -528,48 +520,84 @@ GLRenderer::apply_config()
 }
 
 void
-GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
+GLRenderer::apply_video_mode()
 {
   if (window)
   {
-    SDL_SetWindowSize(window, size.width, size.height);
-
-    if (fullscreen)
+    if (!g_config->use_fullscreen)
+    {
+      SDL_SetWindowFullscreen(window, 0);
+    }
+    else
     {
-      SDL_DisplayMode mode;
-      mode.format = SDL_PIXELFORMAT_RGB888;
-      mode.w = g_config->fullscreen_size.width;
-      mode.h = g_config->fullscreen_size.height;
-      mode.refresh_rate = g_config->fullscreen_refresh_rate;
-      mode.driverdata = 0;
-
-      if (SDL_SetWindowDisplayMode(window, &mode) != 0)
+      if (g_config->fullscreen_size.width == 0 &&
+          g_config->fullscreen_size.height == 0)
       {
-        log_warning << "failed to set display mode: "
-                    << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
-                    << SDL_GetError() << std::endl;
+        if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP) != 0)
+        {
+          log_warning << "failed to switch to desktop fullscreen mode: "
+                      << SDL_GetError() << std::endl;
+        }
+        else
+        {
+          log_info << "switched to desktop fullscreen mode" << std::endl;
+        }
       }
       else
       {
-        SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
+        SDL_DisplayMode mode;
+        mode.format = SDL_PIXELFORMAT_RGB888;
+        mode.w = g_config->fullscreen_size.width;
+        mode.h = g_config->fullscreen_size.height;
+        mode.refresh_rate = g_config->fullscreen_refresh_rate;
+        mode.driverdata = 0;
+
+        if (SDL_SetWindowDisplayMode(window, &mode) != 0)
+        {
+          log_warning << "failed to set display mode: "
+                      << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
+                      << SDL_GetError() << std::endl;
+        }
+        else
+        {
+          if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN) != 0)
+          {
+            log_warning << "failed to switch to fullscreen mode: "
+                        << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
+                        << SDL_GetError() << std::endl;
+          }
+          else
+          {
+            log_info << "switched to fullscreen mode: "
+                     << mode.w << "x" << mode.h << "@" << mode.refresh_rate << std::endl;
+          }
+        }
       }
     }
-    else
-    {
-      SDL_SetWindowFullscreen(window, 0);
-    }
   }
   else
   {
     int flags = SDL_WINDOW_OPENGL;
 
-    if (fullscreen)
+    Size size;
+    if (g_config->use_fullscreen)
     {
-      flags |= SDL_WINDOW_FULLSCREEN;
+      if (g_config->fullscreen_size == Size(0, 0))
+      {
+        flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
+        size = desktop_size;
+      }
+      else
+      {
+        flags |= SDL_WINDOW_FULLSCREEN;
+        size.width  = g_config->fullscreen_size.width;
+        size.height = g_config->fullscreen_size.height;
+      }
     }
     else
     {
       flags |= SDL_WINDOW_RESIZABLE;
+      size = g_config->window_size;
     }
 
     window = SDL_CreateWindow("SuperTux",
@@ -585,12 +613,11 @@ GLRenderer::apply_video_mode(const Size& size, bool fullscreen)
     else
     {
       glcontext = SDL_GL_CreateContext(window);
-      screen_size = size;
 
       SCREEN_WIDTH = size.width;
       SCREEN_HEIGHT = size.height;
 
-      fullscreen_active = fullscreen;
+      fullscreen_active = g_config->use_fullscreen;
     }
   }
 }
index d1b7026..dd07e82 100644 (file)
@@ -111,7 +111,6 @@ private:
   SDL_GLContext glcontext;
   SDL_Rect viewport;
   Size desktop_size;
-  Size screen_size;
   bool fullscreen_active;
 
   GLuint last_texture;
@@ -129,7 +128,7 @@ public:
   void flip();
   void resize(int w, int h);
   void apply_config();
-  void apply_video_mode(const Size& size, bool fullscreen);
+  void apply_video_mode();
   Vector to_logical(int physical_x, int physical_y);
   void set_gamma(float gamma);
   SDL_Window* get_window() const { return window; }
index 3ba6f80..6c7baa7 100644 (file)
@@ -57,9 +57,18 @@ SDLRenderer::SDLRenderer() :
   int flags = SDL_WINDOW_RESIZABLE;
   if(g_config->use_fullscreen)
   {
-    flags |= SDL_WINDOW_FULLSCREEN;
-    width  = g_config->fullscreen_size.width;
-    height = g_config->fullscreen_size.height;
+    if (g_config->fullscreen_size == Size(0, 0))
+    {
+      flags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
+      width = g_config->window_size.width;
+      height = g_config->window_size.height;
+    }
+    else
+    {
+      flags |= SDL_WINDOW_FULLSCREEN;
+      width  = g_config->fullscreen_size.width;
+      height = g_config->fullscreen_size.height;
+    }
   }
 
   SCREEN_WIDTH = width;
@@ -236,41 +245,56 @@ SDLRenderer::apply_video_mode()
   }
   else
   {
-    SDL_DisplayMode mode;
-    mode.format = SDL_PIXELFORMAT_RGB888;
-    mode.w = g_config->fullscreen_size.width;
-    mode.h = g_config->fullscreen_size.height;
-    mode.refresh_rate = g_config->fullscreen_refresh_rate;
-    mode.driverdata = 0;
-
-    if (SDL_SetWindowDisplayMode(window, &mode) != 0)
+    if (g_config->fullscreen_size.width == 0 &&
+        g_config->fullscreen_size.height == 0)
     {
-      log_warning << "failed to set display mode: "
-                  << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
-                  << SDL_GetError() << std::endl;
+        if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN_DESKTOP) != 0)
+        {
+          log_warning << "failed to switch to desktop fullscreen mode: "
+                      << SDL_GetError() << std::endl;
+        }
+        else
+        {
+          log_info << "switched to desktop fullscreen mode" << std::endl;
+        }
     }
     else
     {
-      if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN) != 0)
+      SDL_DisplayMode mode;
+      mode.format = SDL_PIXELFORMAT_RGB888;
+      mode.w = g_config->fullscreen_size.width;
+      mode.h = g_config->fullscreen_size.height;
+      mode.refresh_rate = g_config->fullscreen_refresh_rate;
+      mode.driverdata = 0;
+
+      if (SDL_SetWindowDisplayMode(window, &mode) != 0)
       {
-        log_warning << "failed to switch to fullscreen mode: "
+        log_warning << "failed to set display mode: "
                     << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
                     << SDL_GetError() << std::endl;
       }
       else
       {
-        log_info << "switched to fullscreen mode: "
-                 << mode.w << "x" << mode.h << "@" << mode.refresh_rate << std::endl;
+        if (SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN) != 0)
+        {
+          log_warning << "failed to switch to fullscreen mode: "
+                      << mode.w << "x" << mode.h << "@" << mode.refresh_rate << ": "
+                      << SDL_GetError() << std::endl;
+        }
+        else
+        {
+          log_info << "switched to fullscreen mode: "
+                   << mode.w << "x" << mode.h << "@" << mode.refresh_rate << std::endl;
+        }
       }
     }
   }
-
 }
 
 void
 SDLRenderer::apply_viewport()
 {
-  Size target_size = g_config->use_fullscreen ?
+  Size target_size = (g_config->use_fullscreen && g_config->fullscreen_size != Size(0, 0)) ?
     g_config->fullscreen_size :
     g_config->window_size;