pingus-cvs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Pingus-CVS] r3945 - in trunk/pingus/src: . display


From: jsalmon3
Subject: [Pingus-CVS] r3945 - in trunk/pingus/src: . display
Date: Sun, 14 Dec 2008 00:35:38 +0100

Author: jsalmon3
Date: 2008-12-14 00:35:17 +0100 (Sun, 14 Dec 2008)
New Revision: 3945

Modified:
   trunk/pingus/src/config_manager.cpp
   trunk/pingus/src/config_manager.hpp
   trunk/pingus/src/display/display.cpp
   trunk/pingus/src/option_menu.cpp
   trunk/pingus/src/option_menu.hpp
   trunk/pingus/src/pingus_main.cpp
Log:
Added loading and saving config options



Modified: trunk/pingus/src/config_manager.cpp
===================================================================
--- trunk/pingus/src/config_manager.cpp 2008-12-13 21:46:16 UTC (rev 3944)
+++ trunk/pingus/src/config_manager.cpp 2008-12-13 23:35:17 UTC (rev 3945)
@@ -15,12 +15,18 @@
 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
 #include <iostream>
+#include <fstream>
 #include "SDL.h"
 #include "globals.hpp"
+#include "system.hpp"
 #include "fps_counter.hpp"
 #include "display/display.hpp"
 #include "screen/screen_manager.hpp"
+#include "sexpr_file_reader.hpp"
+#include "sexpr_file_writer.hpp"
 #include "config_manager.hpp"
+#include "lisp/lisp.hpp"
+#include "lisp/parser.hpp"
 
 ConfigManager config_manager;
 
@@ -33,6 +39,162 @@
 }
 
 void
+ConfigManager::load(const std::string& file)
+{
+  filename = file;
+
+  boost::shared_ptr<lisp::Lisp> sexpr;
+
+  try 
+    {
+      sexpr = lisp::Parser::parse(filename);
+    }
+  catch (const std::runtime_error& e) 
+    {
+      std::cerr << "ConfigManager: " << e.what() << std::endl;
+      return;
+    }
+
+  if (!sexpr)
+    {
+      std::cerr << "ConfigManager: Couldn't find config file '" <<
+        filename << "'." << std::endl;
+      return;
+    }
+
+  SExprFileReader reader(sexpr->get_list_elem(0));
+  if (reader.get_name() != "pingus-config")
+    {
+      std::cerr << "Error: " << filename << ": not a (pingus-config) file" << 
std::endl;
+      return;
+    }
+
+  const std::vector<FileReader>& sections = reader.get_sections();
+  for (std::vector<FileReader>::const_iterator i = sections.begin();
+       i != sections.end(); ++i)
+    {
+      int int_value;
+      bool bool_value;
+      std::string string_value;
+      Size size_value;
+
+      if (i->get_name() == "master_volume")
+        {
+          i->read_int("value", int_value);
+          set_master_volume(int_value);
+        }
+      else if (i->get_name() == "sound_volume")
+        {
+          i->read_int("value", int_value);
+          set_sound_volume(int_value);
+        }
+      else if (i->get_name() == "music_volume")
+        {
+          i->read_int("value", int_value);
+          set_music_volume(int_value);
+        }
+      else if (i->get_name() == "resolution")
+        {
+          i->read_size("value", size_value);
+          set_resolution(size_value);
+        }
+      else if (i->get_name() == "fullscreen")
+        {
+          i->read_bool("value", bool_value);
+          set_fullscreen(bool_value);
+        }
+      else if (i->get_name() == "allow_resize")
+        {
+          i->read_bool("value", bool_value);
+          set_allow_resize(bool_value);
+        }
+      else if (i->get_name() == "mouse_grab")
+        {
+          i->read_bool("value", bool_value);
+          set_mouse_grab(bool_value);
+        }
+      else if (i->get_name() == "print_fps")
+        {
+          i->read_bool("value", bool_value);
+          set_print_fps(bool_value);
+        }
+      else if (i->get_name() == "language")
+        {
+          i->read_string("value", string_value);
+          set_language(string_value);
+        }
+      else if (i->get_name() == "swcursor")
+        {
+          i->read_bool("value", bool_value);
+          set_swcursor(bool_value);
+        }
+      else if (i->get_name() == "autoscroll")
+        {
+          i->read_bool("value", bool_value);
+          set_autoscroll(bool_value);
+        }
+    }
+}
+
+void
+ConfigManager::save()
+{
+  if (filename.empty())
+    filename = System::get_userdir() + "config";
+
+  std::ofstream out(filename.c_str());
+  SExprFileWriter writer(out);
+
+  writer.begin_section("pingus-config");
+
+  writer.begin_section("master_volume");
+  writer.write_int("value", get_master_volume());
+  writer.end_section();
+
+  writer.begin_section("sound_volume");
+  writer.write_int("value", get_sound_volume());
+  writer.end_section();
+
+  writer.begin_section("music_volume");
+  writer.write_int("value", get_music_volume());
+  writer.end_section();
+
+  writer.begin_section("resolution");
+  writer.write_size("value", get_resolution());
+  writer.end_section();
+
+  writer.begin_section("fullscreen");
+  writer.write_bool("value", get_fullscreen());
+  writer.end_section();
+
+  writer.begin_section("allow_resize");
+  writer.write_bool("value", get_allow_resize());
+  writer.end_section();
+
+  writer.begin_section("mouse_grab");
+  writer.write_bool("value", get_mouse_grab());
+  writer.end_section();
+
+  writer.begin_section("print_fps");
+  writer.write_bool("value", get_print_fps());
+  writer.end_section();
+
+  writer.begin_section("language");
+  writer.write_string("value", get_language());
+  writer.end_section();
+
+  writer.begin_section("swcursor");
+  writer.write_bool("value", get_swcursor());
+  writer.end_section();
+
+  writer.begin_section("autoscroll");
+  writer.write_bool("value", get_autoscroll());
+  writer.end_section();
+
+  writer.end_section();        // pingus-config
+}
+
+void
 ConfigManager::set_master_volume(int v)
 {
   if (maintainer_mode)

Modified: trunk/pingus/src/config_manager.hpp
===================================================================
--- trunk/pingus/src/config_manager.hpp 2008-12-13 21:46:16 UTC (rev 3944)
+++ trunk/pingus/src/config_manager.hpp 2008-12-13 23:35:17 UTC (rev 3945)
@@ -29,6 +29,9 @@
   ConfigManager();
   ~ConfigManager();
 
+  /** Load the configuration file */
+  void load(const std::string& file);
+
   /** Save the configuration to file */
   void save();
 
@@ -79,6 +82,8 @@
 private:
   ConfigManager (const ConfigManager&);
   ConfigManager& operator= (const ConfigManager&);
+
+  std::string filename;
 };
 
 extern ConfigManager config_manager;

Modified: trunk/pingus/src/display/display.cpp
===================================================================
--- trunk/pingus/src/display/display.cpp        2008-12-13 21:46:16 UTC (rev 
3944)
+++ trunk/pingus/src/display/display.cpp        2008-12-13 23:35:17 UTC (rev 
3945)
@@ -44,19 +44,19 @@
 int
 Display::get_width()
 {
-  return framebuffer->get_size().width;
+  return framebuffer.get() ? framebuffer->get_size().width : 0;
 }
 
 int
 Display::get_height()
 {
-  return framebuffer->get_size().height;
+  return framebuffer.get() ? framebuffer->get_size().height : 0;
 }
 
 Size
 Display::get_size()
 {
-  return framebuffer->get_size();
+  return framebuffer.get() ? framebuffer->get_size() : Size(0, 0);
 }
 
 void

Modified: trunk/pingus/src/option_menu.cpp
===================================================================
--- trunk/pingus/src/option_menu.cpp    2008-12-13 21:46:16 UTC (rev 3944)
+++ trunk/pingus/src/option_menu.cpp    2008-12-13 23:35:17 UTC (rev 3945)
@@ -61,6 +61,7 @@
   }
 
   void on_click() {
+    config_manager.save();
     parent->close_screen();
     Sound::PingusSound::play_sound("yipee");
   }
@@ -194,11 +195,6 @@
   autoscroll_box->set_state(config_manager.get_autoscroll(), false);
   
C(config_manager.on_autoscroll_change.connect(boost::bind(&CheckBox::set_state, 
autoscroll_box, _1, false)));
 
-  save_label = new Label(_("Save:"), Rect(Vector2i(Display::get_width()/2 - 
280, Display::get_height()/2 + 160), Size(60, 32)));
-  gui_manager->add(save_label);
-  save_box = new CheckBox(Rect(Vector2i(Display::get_width()/2 - 280 + 60, 
Display::get_height()/2 + 160), Size(32, 32)));
-  gui_manager->add(save_box);
-
   defaults_label = new Label(_("Reset to Defaults:"), 
Rect(Vector2i(Display::get_width()/2 - 100, Display::get_height()/2 + 160), 
Size(170, 32)));
   gui_manager->add(defaults_label);
   defaults_box = new CheckBox(Rect(Vector2i(Display::get_width()/2 - 100 + 
170, Display::get_height()/2 + 160), Size(32, 32)));
@@ -294,10 +290,6 @@
 
   if (ok_button)
     ok_button->set_pos(size.width/2 + 225, size.height/2 + 125);
-  if (save_label)
-    save_label->set_rect(Rect(Vector2i(Display::get_width()/2 - 280, 
Display::get_height()/2 + 160), Size(60, 32)));
-  if (save_box)
-    save_box->set_rect(Rect(Vector2i(Display::get_width()/2 - 280 + 60, 
Display::get_height()/2 + 160), Size(32, 32)));
   if (defaults_label)
     defaults_label->set_rect(Rect(Vector2i(Display::get_width()/2 - 100, 
Display::get_height()/2 + 160), Size(170, 32)));
   if (defaults_box)

Modified: trunk/pingus/src/option_menu.hpp
===================================================================
--- trunk/pingus/src/option_menu.hpp    2008-12-13 21:46:16 UTC (rev 3944)
+++ trunk/pingus/src/option_menu.hpp    2008-12-13 23:35:17 UTC (rev 3945)
@@ -59,9 +59,6 @@
   SliderBox* sound_volume_box;
   SliderBox* music_volume_box;
 
-  Label* save_label;
-  CheckBox* save_box;
-
   Label* defaults_label;
   CheckBox* defaults_box;
 

Modified: trunk/pingus/src/pingus_main.cpp
===================================================================
--- trunk/pingus/src/pingus_main.cpp    2008-12-13 21:46:16 UTC (rev 3944)
+++ trunk/pingus/src/pingus_main.cpp    2008-12-13 23:35:17 UTC (rev 3945)
@@ -142,7 +142,7 @@
 void
 PingusMain::read_rc_file (void)
 {
-  if (cmd_options.no_config_file.is_set() &&
+  if (!cmd_options.no_config_file.is_set() ||
       !cmd_options.no_config_file.get())
     {
       std::string rcfile;
@@ -152,8 +152,7 @@
       else
        rcfile = cmd_options.config_file.get();
 
-      //constructor of config must be run
-      //FIXME: Config config(rcfile);
+      config_manager.load(rcfile);
     }
 }
 





reply via email to

[Prev in Thread] Current Thread [Next in Thread]