Added Size class
authorIngo Ruhnke <grumbel@gmx.de>
Fri, 20 Nov 2009 18:54:37 +0000 (18:54 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Fri, 20 Nov 2009 18:54:37 +0000 (18:54 +0000)
SVN-Revision: 6069

src/math/size.hpp [new file with mode: 0644]
src/supertux/gameconfig.cpp
src/supertux/gameconfig.hpp
src/supertux/main.cpp
src/supertux/menu/options_menu.cpp
src/video/gl/gl_renderer.cpp

diff --git a/src/math/size.hpp b/src/math/size.hpp
new file mode 100644 (file)
index 0000000..e7d4167
--- /dev/null
@@ -0,0 +1,179 @@
+/*
+**  ClanLib SDK
+**  Copyright (c) 1997-2005 The ClanLib Team
+**
+**  This software is provided 'as-is', without any express or implied
+**  warranty.  In no event will the authors be held liable for any damages
+**  arising from the use of this software.
+**
+**  Permission is granted to anyone to use this software for any purpose,
+**  including commercial applications, and to alter it and redistribute it
+**  freely, subject to the following restrictions:
+**
+**  1. The origin of this software must not be misrepresented; you must not
+**     claim that you wrote the original software. If you use this software
+**     in a product, an acknowledgment in the product documentation would be
+**     appreciated but is not required.
+**  2. Altered source versions must be plainly marked as such, and must not be
+**     misrepresented as being the original software.
+**  3. This notice may not be removed or altered from any source distribution.
+**
+**  Note: Some of the libraries ClanLib may link to may have additional
+**  requirements or restrictions.
+**
+**  File Author(s):
+**
+**    Magnus Norddahl
+**    (if your name is missing here, please add it)
+*/
+
+//! clanCore="Math"
+//! header=core.h
+
+#ifndef HEADER_WINDSTILLE_MATH_SIZE_HPP
+#define HEADER_WINDSTILLE_MATH_SIZE_HPP
+
+#if _MSC_VER > 1000
+#pragma once
+#endif
+
+#include <iostream>
+
+class Sizef;
+
+//: 2D (width,height) size structure.
+//- !group=Core/Math!
+//- !header=core.h!
+class Size
+{
+  //! Construction:
+public:
+  //: Constructs a size structure.
+  //param width: Initial width of size structure.
+  //param height: Initial height of size structure.
+  //param size: Size structure to construct this one from.
+  Size() 
+    : width(0), height(0)
+  {}
+
+  Size(int width_, int height_)
+    : width(width_), height(height_) 
+  {}
+
+  Size(const Size &s)
+    : width(s.width),
+      height(s.height)
+  {}
+
+  explicit Size(const Sizef& s);
+
+  //! Attributes:
+public:
+  //: Size width.
+  int width;
+
+  //: Size height.
+  int height;
+
+  //! Operations:
+public:
+  //: Size += Size operator.
+  Size &operator+=(const Size &s)
+  { width += s.width; height += s.height; return *this; }
+
+  //: Size -= Size operator.
+  Size &operator-=(const Size &s)
+  { width -= s.width; height -= s.height; return *this; }
+       
+  //: Size + Size operator.
+  Size operator+(const Size &s) const
+  { return Size(width + s.width, height + s.height); }
+
+  //: Size - Size operator.
+  Size operator-(const Size &s) const
+  { return Size(width - s.width, height - s.height); }
+
+  //: Size == Size operator (deep compare).
+  bool operator==(const Size &s) const
+  { return (width == s.width) && (height == s.height); }
+
+  //: Size != Size operator (deep compare).
+  bool operator!=(const Size &s) const
+  { return (width != s.width) || (height != s.height); }
+};
+
+//: 2D (width,height) floating point size structure.
+class Sizef
+{
+  //! Construction:
+public:
+  //: Constructs a size structure.
+  //param width: Initial width of size structure.
+  //param height: Initial height of size structure.
+  //param size: Size structure to construct this one from.
+  Sizef() 
+    : width(0.0f),
+      height(0.0f)
+  {}
+
+  Sizef(const Size& s) 
+    : width(static_cast<float>(s.width)),
+      height(static_cast<float>(s.height))
+  {}
+
+  Sizef(float width_, float height_)
+    : width(width_), 
+      height(height_) 
+  {}
+
+  Sizef(const Sizef &s)
+    : width(s.width),
+      height(s.height)
+  {}
+
+  //! Attributes:
+public:
+  //: Size width.
+  float width;
+
+  //: Size height.
+  float height;
+
+  //! Operations:
+public:
+  //: Size += Size operator.
+  Sizef &operator+=(const Sizef &s)
+  { width += s.width; height += s.height; return *this; }
+
+  //: Size -= Size operator.
+  Sizef &operator-=(const Sizef &s)
+  { width -= s.width; height -= s.height; return *this; }
+       
+  //: Size + Size operator.
+  Sizef operator+(const Sizef &s) const
+  { return Sizef(width + s.width, height + s.height); }
+
+  //: Size - Size operator.
+  Sizef operator-(const Sizef &s) const
+  { return Sizef(width - s.width, height - s.height); }
+
+  //: Size == Size operator (deep compare).
+  bool operator==(const Sizef &s) const
+  { return (width == s.width) && (height == s.height); }
+
+  //: Size != Size operator (deep compare).
+  bool operator!=(const Size &s) const
+  { return (width != s.width) || (height != s.height); }
+};
+
+inline Size::Size(const Sizef& s)
+  : width(static_cast<int>(s.width)),
+    height(static_cast<int>(s.height))
+{}
+
+inline std::ostream& operator<<(std::ostream& s, const Size& size) 
+{
+  return s << "Size(" << size.width << ", " << size.height << ")";
+}
+
+#endif
index 96dbdf7..d90ccc0 100644 (file)
 
 Config::Config() :
   profile(1),
-  fullscreen_width (800),
-  fullscreen_height(600),
-  window_width (800),
-  window_height(600),
-  aspect_width (0), // auto detect
-  aspect_height(0),
+  fullscreen_size(800, 600),
+  window_size(800, 600),
+  aspect_size(0, 0), // auto detect
   magnification(1.0f),
   use_fullscreen(false),
   video(AUTO_VIDEO),
@@ -76,14 +73,14 @@ Config::load()
     video = get_video_system(video_string);
     config_video_lisp->get("vsync", try_vsync);
 
-    config_video_lisp->get("fullscreen_width",  fullscreen_width);
-    config_video_lisp->get("fullscreen_height", fullscreen_height);
+    config_video_lisp->get("fullscreen_width",  fullscreen_size.width);
+    config_video_lisp->get("fullscreen_height", fullscreen_size.height);
 
-    config_video_lisp->get("window_width",  window_width);
-    config_video_lisp->get("window_height", window_height);
+    config_video_lisp->get("window_width",  window_size.width);
+    config_video_lisp->get("window_height", window_size.height);
 
-    config_video_lisp->get("aspect_width",  aspect_width);
-    config_video_lisp->get("aspect_height", aspect_height);
+    config_video_lisp->get("aspect_width",  aspect_size.width);
+    config_video_lisp->get("aspect_height", aspect_size.height);
   }
 
   const lisp::Lisp* config_audio_lisp = config_lisp->get_lisp("audio");
@@ -119,14 +116,14 @@ Config::save()
   writer.write("video", get_video_string(video));
   writer.write("vsync", try_vsync);
 
-  writer.write("fullscreen_width",  fullscreen_width);
-  writer.write("fullscreen_height", fullscreen_height);
+  writer.write("fullscreen_width",  fullscreen_size.width);
+  writer.write("fullscreen_height", fullscreen_size.height);
 
-  writer.write("window_width",  window_width);
-  writer.write("window_height", window_height);
+  writer.write("window_width",  window_size.width);
+  writer.write("window_height", window_size.height);
 
-  writer.write("aspect_width",  aspect_width);
-  writer.write("aspect_height", aspect_height);
+  writer.write("aspect_width",  aspect_size.width);
+  writer.write("aspect_height", aspect_size.height);
 
   writer.end_list("video");
 
index 6833c05..2d708c4 100644 (file)
@@ -18,6 +18,7 @@
 #define HEADER_SUPERTUX_SUPERTUX_GAMECONFIG_HPP
 
 #include "video/video_systems.hpp"
+#include "math/size.hpp"
 
 class Config
 {
@@ -31,16 +32,13 @@ public:
   int profile;
 
   // the width/height to be used to display the game in fullscreen
-  int fullscreen_width;
-  int fullscreen_height;
+  Size fullscreen_size;
 
-  // the width/height of the window managers window 
-  int window_width;
-  int window_height;
+  /** the width/height of the window managers window */
+  Size window_size;
 
-  // the aspect ratio
-  int aspect_width;
-  int aspect_height;
+  /** the aspect ratio */
+  Size aspect_size;
   
   float magnification;
 
index 37dd8f7..0913dae 100644 (file)
@@ -264,14 +264,9 @@ Main::parse_commandline(int argc, char** argv)
     } else if(arg == "--default" || arg == "-d") {
       g_config->use_fullscreen = false;
       
-      g_config->window_width  = 800;
-      g_config->window_height = 600;
-
-      g_config->fullscreen_width  = 800;
-      g_config->fullscreen_height = 600;
-
-      g_config->aspect_width  = 0;  // auto detect
-      g_config->aspect_height = 0;
+      g_config->window_size     = Size(800, 600);
+      g_config->fullscreen_size = Size(800, 600);
+      g_config->aspect_size     = Size(0, 0);  // auto detect
       
     } else if(arg == "--window" || arg == "-w") {
       g_config->use_fullscreen = false;
@@ -292,11 +287,8 @@ Main::parse_commandline(int argc, char** argv)
         }
         else
         {
-          g_config->window_width  = width;
-          g_config->window_height = height;
-
-          g_config->fullscreen_width  = width;
-          g_config->fullscreen_height = height;
+          g_config->window_size     = Size(width, height);
+          g_config->fullscreen_size = Size(width, height);
         }
       }
     } else if(arg == "--aspect" || arg == "-a") {
@@ -322,16 +314,15 @@ Main::parse_commandline(int argc, char** argv)
         }
         else 
         {
-          float aspect_ratio = static_cast<double>(g_config->aspect_width) /
-            static_cast<double>(g_config->aspect_height);
+          float aspect_ratio = static_cast<float>(aspect_width) / static_cast<float>(aspect_height);
 
           // use aspect ratio to calculate logical resolution
           if (aspect_ratio > 1) {
-            g_config->aspect_width  = static_cast<int> (600 * aspect_ratio + 0.5);
-            g_config->aspect_height = 600;
+            g_config->aspect_size = Size(static_cast<int>(600 * aspect_ratio + 0.5),
+                                         600);
           } else {
-            g_config->aspect_width  = 600;
-            g_config->aspect_height = static_cast<int> (600 * 1/aspect_ratio + 0.5);
+            g_config->aspect_size = Size(600, 
+                                         static_cast<int>(600 * 1/aspect_ratio + 0.5));
           }
         }
       }
@@ -451,9 +442,9 @@ Main::init_video()
   SDL_ShowCursor(0);
 
   log_info << (g_config->use_fullscreen?"fullscreen ":"window ")
-           << " Window: "     << g_config->window_width     << "x" << g_config->window_height
-           << " Fullscreen: " << g_config->fullscreen_width << "x" << g_config->fullscreen_height
-           << " Area: "       << g_config->aspect_width     << "x" << g_config->aspect_height << std::endl;
+           << " Window: "     << g_config->window_size
+           << " Fullscreen: " << g_config->fullscreen_size
+           << " Area: "       << g_config->aspect_size << std::endl;
 }
 
 void
index b43e5ce..8948545 100644 (file)
@@ -118,10 +118,10 @@ OptionsMenu::OptionsMenu() :
   aspect->list.push_back("16:9");
   aspect->list.push_back("1368:768");
 
-  if (g_config->aspect_width != 0 && g_config->aspect_height != 0)
+  if (g_config->aspect_size != Size(0, 0))
   {
     std::ostringstream out;
-    out << g_config->aspect_width << ":" << g_config->aspect_height;
+    out << g_config->aspect_size.width << ":" << g_config->aspect_size.height;
     std::string aspect_ratio = out.str();
     for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
     {
@@ -170,12 +170,12 @@ OptionsMenu::menu_action(MenuItem* item)
     { 
       if (item->list[item->selected] == "auto")
       {
-        g_config->aspect_width  = 0; // Magic values
-        g_config->aspect_height = 0;
+        g_config->aspect_size = Size(0, 0); // Magic values
         Renderer::instance()->apply_config();
         MenuManager::recalc_pos();
       }
-      else if(sscanf(item->list[item->selected].c_str(), "%d:%d", &g_config->aspect_width, &g_config->aspect_height) == 2)
+      else if (sscanf(item->list[item->selected].c_str(), "%d:%d", 
+                      &g_config->aspect_size.width, &g_config->aspect_size.height) == 2)
       {
         Renderer::instance()->apply_config();
         MenuManager::recalc_pos();
@@ -201,7 +201,8 @@ OptionsMenu::menu_action(MenuItem* item)
       break;
 
     case MNID_FULLSCREEN_RESOLUTION:
-      if(sscanf(item->list[item->selected].c_str(), "%dx%d", &g_config->fullscreen_width, &g_config->fullscreen_height) == 2)
+      if(sscanf(item->list[item->selected].c_str(), "%dx%d", 
+                &g_config->fullscreen_size.width, &g_config->fullscreen_size.height) == 2)
       {
         // do nothing, changes are only applied when toggling fullscreen mode
       }      
index bffa2cd..ecba7df 100644 (file)
@@ -153,14 +153,14 @@ GLRenderer::GLRenderer()
   if(g_config->use_fullscreen)
   {
     flags |= SDL_FULLSCREEN;
-    width  = g_config->fullscreen_width;
-    height = g_config->fullscreen_height;
+    width  = g_config->fullscreen_size.width;
+    height = g_config->fullscreen_size.height;
   }
   else
   {
     //      flags |= SDL_RESIZABLE;
-    width  = g_config->window_width;
-    height = g_config->window_height;
+    width  = g_config->window_size.width;
+    height = g_config->window_size.height;
   }
 
   int bpp = 0;
@@ -529,8 +529,7 @@ GLRenderer::resize(int w, int h)
   // unavoidable with SDL at the moment
   SDL_SetVideoMode(w, h, 0, SDL_OPENGL /*| SDL_RESIZABLE*/);
 
-  g_config->window_width  = w;
-  g_config->window_height = h;
+  g_config->window_size = Size(w, h);
 
   apply_config();
 }
@@ -542,17 +541,19 @@ GLRenderer::apply_config()
   {
     std::cout << "Applying Config:" 
               << "\n  Desktop: " << desktop_width << "x" << desktop_height
-              << "\n  Window:  " << g_config->window_width << "x" << g_config->window_height
-              << "\n  FullRes: " << g_config->fullscreen_width << "x" << g_config->fullscreen_height
-              << "\n  Aspect:  " << g_config->aspect_width << ":" << g_config->aspect_height
+              << "\n  Window:  " << g_config->window_size
+              << "\n  FullRes: " << g_config->fullscreen_size
+              << "\n  Aspect:  " << g_config->aspect_size
               << "\n  Magnif:  " << g_config->magnification
               << std::endl;
   }
 
   float target_aspect = static_cast<float>(desktop_width) / static_cast<float>(desktop_height);
   
-  if (g_config->aspect_width != 0 && g_config->aspect_height != 0)
-    target_aspect = float(g_config->aspect_width) / float(g_config->aspect_height);
+  if (g_config->aspect_size != Size(0, 0))
+  {
+    target_aspect = float(g_config->aspect_size.width) / float(g_config->aspect_size.height);
+  }
 
   float desktop_aspect = 4.0f / 3.0f; // random default fallback guess
   
@@ -566,14 +567,14 @@ GLRenderer::apply_config()
   // Get the screen width
   if (g_config->use_fullscreen)
   {
-    w = g_config->fullscreen_width;
-    h = g_config->fullscreen_height;
+    w = g_config->fullscreen_size.width;
+    h = g_config->fullscreen_size.height;
     desktop_aspect = float(w) / float(h);
   }
   else
   {
-    w = g_config->window_width;        
-    h = g_config->window_height;
+    w = g_config->window_size.width;        
+    h = g_config->window_size.height;
   }
 
   if (target_aspect > 1.0f)