1 /******************************************************************************
2  *  Warmux is a convivial mass murder game.
3  *  Copyright (C) 2001-2011 Warmux Team.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; either version 2 of the License, or
8  *  (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program; if not, write to the Free Software
17  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18  ******************************************************************************
19  * Configuration of Warmux : store game config of every tunable variable of Warmux.
20  * Vars have a default value and can be change with the file configuration.
21  * This object aims to become the only place where the XML is read and/or
22  * written. It is handled as a singleton pattern whereas one doesn't really
23  * care when the XML config file is loaded, but when it was actually read, this
24  * is useless to load it again.
25  * On the other hand, this objet is not designed to handle any other
26  * information whereas it is nothing but a shell around the XML config file
27  * created to be able to handle config data easily.
28  *****************************************************************************/
29 
30 #ifndef CONFIG_H
31 #define CONFIG_H
32 //-----------------------------------------------------------------------------
33 #include <list>
34 #include <string>
35 #include <map>
36 #include <WARMUX_base.h>
37 #include <WARMUX_singleton.h>
38 #include <WARMUX_point.h>
39 #include <WARMUX_team_config.h>
40 
41 // Forward declarations
42 class ObjectConfig;
43 typedef struct _xmlNode xmlNode;
44 
45 //-----------------------------------------------------------------------------
46 
47 typedef enum
48 {
49   QUALITY_16BPP = 0,
50   QUALITY_MIX   = 1,
51 #ifndef HAVE_HANDHELD
52   QUALITY_32BPP = 2,
53 #endif
54   QUALITY_MAX
55 } Quality;
56 
57 class Config : public Singleton<Config>
58 {
59 public:
60   const ObjectConfig &GetObjectConfig(const std::string &name,
61                                       const std::string &xml_config) const;
62   void RemoveAllObjectConfigs();
63 
64 #ifdef ENABLE_NLS
65   void SetLanguage(const std::string& language);
GetLanguage()66   std::string GetLanguage() const { return default_language; };
67 #endif
68 
GetDisplayEnergyCharacter()69   bool GetDisplayEnergyCharacter() const { return display_energy_character; };
SetDisplayEnergyCharacter(const bool dec)70   void SetDisplayEnergyCharacter(const bool dec) { display_energy_character = dec; };
71 
GetDisplayNameCharacter()72   bool GetDisplayNameCharacter() const { return display_name_character; };
SetDisplayNameCharacter(const bool dnc)73   void SetDisplayNameCharacter(const bool dnc) { display_name_character = dnc; };
74 
GetWindParticlesPercentage()75   uint GetWindParticlesPercentage() const { return wind_particles_percentage; };
SetWindParticlesPercentage(uint dwp)76   void SetWindParticlesPercentage(uint dwp) { wind_particles_percentage = dwp; };
77 
GetDisplayMultiLayerSky()78   bool GetDisplayMultiLayerSky() const { return display_multi_layer_sky; };
SetDisplayMultiLayerSky(bool dmsl)79   void SetDisplayMultiLayerSky(bool dmsl) { display_multi_layer_sky = dmsl; };
80 
GetDefaultMouseCursor()81   bool GetDefaultMouseCursor() const { return default_mouse_cursor; };
SetDefaultMouseCursor(const bool dmc)82   void SetDefaultMouseCursor(const bool dmc) { default_mouse_cursor = dmc; };
83 
GetScrollOnBorder()84   bool GetScrollOnBorder() const { return scroll_on_border; };
SetScrollOnBorder(const bool sob)85   void SetScrollOnBorder(const bool sob) { scroll_on_border = sob; };
86 
GetScrollBorderSize()87   uint GetScrollBorderSize() const { return scroll_border_size; };
SetScrollBorderSize(const uint size)88   void SetScrollBorderSize(const uint size) { scroll_border_size = size; };
89 
IsVideoFullScreen()90   bool IsVideoFullScreen() const { return video_fullscreen; };
SetVideoFullScreen(const bool set_fullscreen)91   void SetVideoFullScreen(const bool set_fullscreen) { video_fullscreen = set_fullscreen; };
92 
GetVideoWidth()93   uint GetVideoWidth() const { return video_width; };
SetVideoWidth(const uint width)94   void SetVideoWidth(const uint width) { video_width = width; };
95 
GetVideoHeight()96   uint GetVideoHeight() const { return video_height; };
SetVideoHeight(const uint height)97   void SetVideoHeight(const uint height) { video_height = height; };
98 
GetQuality()99   Quality GetQuality() const { return quality; }
SetQuality(Quality qual)100   void SetQuality(Quality qual) { quality = qual; }
101 
GetResolutionAvailable()102   std::list<Point2i> & GetResolutionAvailable() { return resolution_available; };
GetMaxFps()103   uint GetMaxFps() const { return max_fps; };
104 
IsBlingBlingInterface()105   bool IsBlingBlingInterface() const { return bling_bling_interface; };
SetBlingBlingInterface(bool bling_bling)106   void SetBlingBlingInterface(bool bling_bling) { bling_bling_interface = bling_bling; };
107 
GetSoundMusic()108   bool GetSoundMusic() const { return sound_music; };
SetSoundMusic(const bool music)109   void SetSoundMusic(const bool music) { sound_music = music; };
110 
GetSoundEffects()111   bool GetSoundEffects() const { return sound_effects; };
SetSoundEffects(const bool effects)112   void SetSoundEffects(const bool effects) { sound_effects = effects; };
113 
GetSoundFrequency()114   uint GetSoundFrequency() const { return sound_frequency; };
SetSoundFrequency(const uint freq)115   void SetSoundFrequency(const uint freq) { sound_frequency = freq; };
116 
GetVolumeMusic()117   uint GetVolumeMusic() const { return volume_music; }
118   void SetVolumeMusic(uint vol);
GetVolumeEffects()119   uint GetVolumeEffects() const { return volume_effects; }
SetVolumeEffects(uint vol)120   void SetVolumeEffects(uint vol) { volume_effects = vol; }
121   static uint GetMaxVolume();
GetWarnOnNewPlayer()122   bool GetWarnOnNewPlayer() const { return warn_on_new_player; }
SetWarnOnNewPlayer(bool w)123   void SetWarnOnNewPlayer(bool w) { warn_on_new_player = w; }
124 
GetCheckUpdates()125   bool GetCheckUpdates() const { return check_updates; }
SetCheckUpdates(const bool check)126   void SetCheckUpdates(const bool check) { check_updates = check; }
127 
GetLeftHandedMouse()128   bool GetLeftHandedMouse() const { return lefthanded_mouse; }
SetLeftHandedMouse(const bool left)129   void SetLeftHandedMouse(const bool left) { lefthanded_mouse = left; }
130 
AccessTeamList()131   std::list<ConfigTeam> & AccessTeamList() { return teams; };
GetMapName()132   const std::string & GetMapName() const { return map_name; };
SetMapName(const std::string & new_name)133   void SetMapName(const std::string& new_name) { map_name = new_name; }
134 
135   const std::string& GetTtfFilename() const;
136 
GetDataDir()137   std::string GetDataDir() const { return data_dir + "/"; };
138 #ifdef ENABLE_NLS
GetLocaleDir()139   std::string GetLocaleDir() const { return locale_dir; };
140 #endif
GetPersonalDataDir()141   std::string GetPersonalDataDir() const { return personal_data_dir; };
GetPersonalConfigDir()142   std::string GetPersonalConfigDir() const { return personal_config_dir; };
GetChatLogDir()143   std::string GetChatLogDir() const { return chat_log_dir; };
144 
145   bool Save(bool save_current_teams = false);
GetGameMode()146   const std::string &GetGameMode() const { return m_game_mode; }
SetGameMode(std::string s)147   void SetGameMode(std::string s) { m_game_mode = s; }
148 
GetNetworkClientHost()149   const std::string &GetNetworkClientHost() const { return m_network_client_host; }
SetNetworkClientHost(std::string s)150   void SetNetworkClientHost(std::string s) { m_network_client_host = s; }
GetNetworkClientPort()151   const std::string &GetNetworkClientPort() const { return m_network_client_port; }
SetNetworkClientPort(std::string s)152   void SetNetworkClientPort(std::string s) { m_network_client_port = s; }
153 
GetNetworkServerPort()154   const std::string &GetNetworkServerPort() const { return m_network_server_port; }
SetNetworkServerPort(std::string s)155   void SetNetworkServerPort(std::string s) { m_network_server_port = s; }
GetNetworkServerGameName()156   const std::string &GetNetworkServerGameName() const { return m_network_server_game_name; }
SetNetworkServerGameName(std::string s)157   void SetNetworkServerGameName(std::string s) { m_network_server_game_name = s; }
GetNetworkServerPublic()158   bool GetNetworkServerPublic() const { return m_network_server_public; }
SetNetworkServerPublic(bool b)159   void SetNetworkServerPublic(bool b) { m_network_server_public = b; }
160 
161   void SetNetworkLocalTeams();
AccessNetworkTeamsList()162   const std::list<ConfigTeam>& AccessNetworkTeamsList() const { return network_local_teams; };
163 
164   // return true if the directory is created
165   bool MkdirPersonalConfigDir() const;
166   bool MkdirPersonalDataDir() const;
167   bool MkdirChatLogDir() const;
168 
169   bool RemovePersonalConfigFile() const;
170 
171 protected:
172   bool SaveXml(bool save_current_teams);
173   std::string GetEnv(const std::string & name, const std::string &default_value) const;
174 
175   std::string default_language;
176   std::string m_game_mode;
177   std::string m_filename;
178 
179   // Code setting it must make sure it ends with the path separator
180 #ifdef ENABLE_NLS
181   std::string locale_dir;
182 #endif
183   std::string data_dir, personal_data_dir, personal_config_dir, chat_log_dir;
184 
185   std::list<ConfigTeam> teams;
186   std::string map_name;
187 
188   std::string m_default_config;
189 
190   // Game settings
191   bool display_energy_character;
192   bool display_name_character;
193   uint wind_particles_percentage;
194   bool display_multi_layer_sky;
195   bool default_mouse_cursor;
196 
197   // Video settings
198   uint video_width;
199   uint video_height;
200   bool video_fullscreen;
201   uint max_fps;
202   bool bling_bling_interface;
203   std::list<Point2i> resolution_available;
204   bool scroll_on_border;
205   uint scroll_border_size;
206 
207   // Sound settings
208   bool sound_music;
209   bool sound_effects;
210   uint sound_frequency;
211   uint volume_music;
212   uint volume_effects;
213   bool warn_on_new_player;
214 
215   // various settings
216   bool check_updates;
217   bool lefthanded_mouse;
218 
219   // network previous connection as client
220   std::string m_network_client_host;
221   std::string m_network_client_port;
222 
223   // network previous connection as server
224   std::string m_network_server_game_name;
225   std::string m_network_server_port;
226   bool m_network_server_public;
227 
228   // personal teams used durint last network game
229   std::list<ConfigTeam> network_local_teams;
230 
231   // Font setting
232 #ifdef ENABLE_NLS
233   std::map<std::string, std::string>  fonts;
234 #endif
235   std::string font_dir;
236   std::string ttf_filename;
237 
238   Quality quality;
239 
240   friend class Singleton<Config>;
241   Config();
~Config()242   ~Config() { RemoveAllObjectConfigs(); singleton = NULL; }
243 
244 private:
245   bool DoLoading(void);
246   void LoadDefaultValue();
247   void LoadXml(const xmlNode* xml);
248 
249   /* this is mutable in order to be able to load config on fly when calling
250    * GetObjectConfig() witch is not supposed to modify the object itself */
251   mutable std::map<std::string, ObjectConfig *> config_set;
252 };
253 
254 #endif
255