When custom aspect ratio is set, add it to the list in the option menu, also save...
authorIngo Ruhnke <grumbel@gmx.de>
Wed, 7 May 2008 11:15:36 +0000 (11:15 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Wed, 7 May 2008 11:15:36 +0000 (11:15 +0000)
SVN-Revision: 5426

src/gameconfig.cpp
src/gameconfig.hpp
src/gui/menu.cpp
src/main.cpp
src/options_menu.cpp

index 3825011..5236ba6 100644 (file)
@@ -47,7 +47,9 @@ Config::Config()
 
   screenwidth = 800;
   screenheight = 600;
-  aspect_ratio = -1;       // autodetect
+  
+  aspect_width  = 4;       // autodetect
+  aspect_height = 3;       // autodetect
 
   enable_script_debugger = false;
 
@@ -81,7 +83,8 @@ Config::load()
     config_video_lisp->get("vsync", try_vsync);
     config_video_lisp->get("width", screenwidth);
     config_video_lisp->get("height", screenheight);
-    config_video_lisp->get("aspect_ratio", aspect_ratio);
+    config_video_lisp->get("aspect_width",  aspect_width);
+    config_video_lisp->get("aspect_height", aspect_height);
   }
 
   const lisp::Lisp* config_audio_lisp = config_lisp->get_lisp("audio");
@@ -113,7 +116,8 @@ Config::save()
   writer.write_bool("vsync", try_vsync);
   writer.write_int("width", screenwidth);
   writer.write_int("height", screenheight);
-  writer.write_float("aspect_ratio", aspect_ratio);
+  writer.write_float("aspect_width",  aspect_width);
+  writer.write_float("aspect_height", aspect_height);
   writer.end_list("video");
 
   writer.start_list("audio");
index bfd5ab9..775e451 100644 (file)
@@ -42,7 +42,8 @@ public:
    */
   int screenwidth;
   int screenheight;
-  float aspect_ratio;
+  int aspect_width;
+  int aspect_height;
 
   bool use_fullscreen;
   VideoSystem video;
index c8ee876..b0394ac 100644 (file)
@@ -251,7 +251,7 @@ Menu::Menu()
 void
 Menu::set_pos(float x, float y, float rw, float rh)
 {
-  pos_x = x + get_width() * rw;
+  pos_x = x + get_width()  * rw;
   pos_y = y + get_height() * rh;
 }
 
index 20ea613..c78e7fb 100644 (file)
@@ -289,7 +289,8 @@ static bool parse_commandline(int argc, char** argv)
       config->use_fullscreen = true;
     } else if(arg == "--default" || arg == "-d") {
       config->use_fullscreen = false;
-      config->aspect_ratio = -1;
+      config->aspect_width  = -1;
+      config->aspect_height = -1;
       config->screenwidth = 800;
       config->screenheight = 600;
     } else if(arg == "--window" || arg == "-w") {
@@ -308,18 +309,11 @@ static bool parse_commandline(int argc, char** argv)
       if(i+1 >= argc) {
         print_usage(argv[0]);
         throw std::runtime_error("Need to specify a parameter for aspect switch");
-      }
-      if(strcasecmp(argv[i+1], "auto") == 0) {
-        i++;
-        config->aspect_ratio = -1;
       } else {
-        int aspect_width, aspect_height;
-        if(sscanf(argv[++i], "%d:%d", &aspect_width, &aspect_height) != 2) {
+        if(sscanf(argv[++i], "%d:%d", &config->aspect_width, &config->aspect_height) != 2) {
           print_usage(argv[0]);
           throw std::runtime_error("Invalid aspect spec, should be WIDTH:HEIGHT");
         }
-        config->aspect_ratio = static_cast<double>(aspect_width) /
-                               static_cast<double>(aspect_height);
       }
     } else if(arg == "--show-fps") {
       config->show_fps = true;
@@ -400,24 +394,9 @@ void init_video()
     desktop_height = info->current_h;
   }
 #endif
-
-  double aspect_ratio = config->aspect_ratio;
-
-  // try to guess aspect ratio of monitor if needed
-  if (aspect_ratio <= 0) {
-// TODO: commented out because 
-// 1) it tends to guess wrong if widescreen-monitors don't stretch 800x600 to fit, but just display black borders
-// 2) aspect ratios other than 4:3 are largely untested
-/*
-    if(config->use_fullscreen && desktop_width > 0) {
-      aspect_ratio = static_cast<double>(desktop_width) / static_cast<double>(desktop_height);
-    } else {
-*/
-      aspect_ratio = 4.0 / 3.0;
-/*
-    }
-*/
-  }
+  
+  double aspect_ratio = static_cast<double>(config->aspect_width) /
+                               static_cast<double>(config->aspect_height);
 
   // use aspect ratio to calculate logical resolution
   if (aspect_ratio > 1) {
index 1bd8e70..f1234f8 100644 (file)
@@ -18,6 +18,7 @@
 //  along with this program; if not, write to the Free Software
 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 #include <config.h>
+#include <math.h> //for log10()
 
 #include "profile_menu.hpp"
 #include "options_menu.hpp"
@@ -123,13 +124,32 @@ OptionsMenu::OptionsMenu()
   add_toggle(MNID_FULLSCREEN,_("Fullscreen"), config->use_fullscreen)
     ->set_help(_("Let the game cover the whole screen"));
 
-  MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ration"));
+  MenuItem* aspect = add_string_select(MNID_ASPECTRATIO, _("Aspect Ratio"));
   aspect->set_help(_("Adjust the aspect ratio"));
+  
   aspect->list.push_back("16:9");
   aspect->list.push_back("16:10");
   aspect->list.push_back("4:3");
   aspect->list.push_back("5:4");
 
+  std::ostringstream out;
+  out << config->aspect_width << ":" << config->aspect_height;
+  std::string aspect_ratio = out.str();
+  for(std::vector<std::string>::iterator i = aspect->list.begin(); i != aspect->list.end(); ++i)
+    {
+      if(*i == aspect_ratio)
+        {
+          aspect_ratio.clear();
+          break;
+        }
+    }
+
+  if (!aspect_ratio.empty())
+    {
+      aspect->selected = aspect->list.size();
+      aspect->list.push_back(aspect_ratio);
+    }
+  
   if (sound_manager->is_audio_enabled()) {
     add_toggle(MNID_SOUND, _("Sound"), config->sound_enabled)
       ->set_help(_("Disable all sound effects in the game"));
@@ -159,26 +179,26 @@ OptionsMenu::menu_action(MenuItem* item)
   switch (item->id) {
     case MNID_ASPECTRATIO:
       { // FIXME: Really crude and ugly here, move to video or so
-        int   aspect_width;
-        int   aspect_height;
-
-        if(sscanf(item->list[item->selected].c_str(), "%d:%d", &aspect_width, &aspect_height) == 2) 
+        if(sscanf(item->list[item->selected].c_str(), "%d:%d", &config->aspect_width, &config->aspect_height) == 2) 
           {
-            config->aspect_ratio = static_cast<double>(aspect_width) /
-              static_cast<double>(aspect_height);
+            float aspect_ratio = static_cast<double>(config->aspect_width) /
+              static_cast<double>(config->aspect_height);
 
-            if (config->aspect_ratio > 1) {
-              SCREEN_WIDTH  = static_cast<int> (600 * config->aspect_ratio + 0.5);
+            if (aspect_ratio > 1) {
+              SCREEN_WIDTH  = static_cast<int> (600 * aspect_ratio + 0.5);
               SCREEN_HEIGHT = 600;
             } else {
               SCREEN_WIDTH  = 600;
-              SCREEN_HEIGHT = static_cast<int> (600 * 1/config->aspect_ratio + 0.5);
+              SCREEN_HEIGHT = static_cast<int> (600 * 1/aspect_ratio + 0.5);
             }
 
             glMatrixMode(GL_PROJECTION);
             glLoadIdentity();
             glOrtho(0, SCREEN_WIDTH, SCREEN_HEIGHT, 0, -1.0, 1.0);
             std::cout << __FILE__ << ":" << __LINE__ << ": change aspect ratio to " << item->list[item->selected] << std::endl;
+
+            // Reposition the menu to be in the center of the screen again
+            set_pos(SCREEN_WIDTH/2, SCREEN_HEIGHT/2);
           }
       }
       break;