1 /***************************************************************************
2                           epiconfig.cpp  -  description
3                              -------------------
4     begin                : Mon Sep 29 2003
5     copyright            : (C) 2003 by Giuseppe D'Aqui'
6     email                : kumber@tiscalinet.it
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License, Version 2, as published by  *
13  *   the Free Software Foundation.                                   *
14  *                                                                         *
15  ***************************************************************************/
16 
17 #include "epiconfig.h"
18 #include "tinyxml/tinyxml.h"
19 #include <cstdlib>
20 #include "sprite.h"
21 
22 
set_default_values()23 void Epiconfig::set_default_values()
24 {
25 	//size in pixels
26 	m_screen_size_x=640;
27 	m_screen_size_y=480;
28 	m_score_size_y=32;
29 
30 
31 	//size in cells
32 	m_map_size_x=32;
33 	m_map_size_y=32;
34 
35 	//it must be a multiple of 2
36 	m_max_anim_drawn=8;
37 
38 
39 
40 	m_moving_step=k_sprite_size/m_max_anim_drawn;
41 
42 	//m_msec_per_frame=176/m_max_anim_drawn;
43 	//m_msec_per_frame=150/m_max_anim_drawn;
44 	//m_msec_per_frame=240/m_max_anim_drawn;
45 	m_msec_per_frame=140/m_max_anim_drawn;
46 
47 	m_volume_sound = 8;
48 
49 	m_volume_music = 8;
50 
51 	m_last_level = 0;
52 
53 	refresh_game_window_parameters();
54 
55 
56 }
57 
58 
59 
60 
get_screen_size_x()61 Uint32 Epiconfig::get_screen_size_x()
62 {
63 	return m_screen_size_x;
64 }
65 
66 
67 
68 
get_screen_size_y()69 Uint32 Epiconfig::get_screen_size_y()
70 {
71 	return m_screen_size_y;
72 }
73 
74 
75 
76 
get_score_size_y()77 Uint32 Epiconfig::get_score_size_y()
78 {
79 	return m_score_size_y;
80 }
81 
82 
83 
84 
get_map_size_x()85 Uint32 Epiconfig::get_map_size_x()
86 {
87 	return m_map_size_x;
88 }
89 
90 
91 
92 
get_map_size_y()93 Uint32 Epiconfig::get_map_size_y()
94 {
95 	return m_map_size_y;
96 }
97 
98 
99 
100 
get_max_anim_drawn()101 Uint32 Epiconfig::get_max_anim_drawn()
102 {
103 	return m_max_anim_drawn;
104 }
105 
106 
107 
108 
get_moving_step()109 Uint32 Epiconfig::get_moving_step()
110 {
111 	return m_moving_step;
112 }
113 
114 
115 
116 
get_msec_per_frame()117 Uint32 Epiconfig::get_msec_per_frame()
118 {
119 	return m_msec_per_frame;
120 }
121 
122 
123 
124 
get_volume_sound()125 Uint32 Epiconfig::get_volume_sound()
126 {
127 	return m_volume_sound;
128 }
129 
130 
131 
132 
get_volume_music()133 Uint32 Epiconfig::get_volume_music()
134 {
135 	return m_volume_music;
136 }
137 
138 
139 
140 
set_volume_sound(Uint32 volume)141 void Epiconfig::set_volume_sound(Uint32 volume)
142 {
143 	m_volume_sound = volume;
144 }
145 
146 
147 
148 
set_volume_music(Uint32 volume)149 void Epiconfig::set_volume_music(Uint32 volume)
150 {
151 	m_volume_music = volume;
152 }
153 
154 
155 
156 
get_last_level()157 Uint32 Epiconfig::get_last_level()
158 {
159 	return m_last_level;
160 }
161 
162 
163 
164 
set_last_level(Uint32 level)165 void Epiconfig::set_last_level(Uint32 level)
166 {
167 	m_last_level = level;
168 }
169 
170 
171 
172 
refresh_game_window_parameters()173 void Epiconfig::refresh_game_window_parameters()
174 {
175    // m_level_size_x = m_game_size_x - 64;
176    // m_level_size_y = m_level_size_y - 96;
177     //m_game_window_pos_on_screen_x = 32;
178     //m_game_window_pos_on_screen_y = 32;
179 }
180 
181 
182 
183 
read_values_from_file(char * filename)184 void Epiconfig::read_values_from_file(char* filename)
185 {
186 
187 	set_default_values();
188 
189 	TiXmlDocument doc;
190 
191 
192 	if(doc.LoadFile(filename))
193 	{
194 
195 		TiXmlHandle docHandle( &doc );
196 
197 		TiXmlText* screensize_x = docHandle.FirstChild("config").FirstChild("screen_size").FirstChild("x").FirstChild().Text();
198 		if(screensize_x)
199 		{
200 			m_screen_size_x = atoi(screensize_x->Value());
201 			DEBWARN("Setting Screen size x to: "<<m_screen_size_x<<"\n");
202 		}
203 
204 		TiXmlText* screensize_y = docHandle.FirstChild("config").FirstChild("screen_size").FirstChild("y").FirstChild().Text();
205 		if(screensize_y)
206 		{
207 			m_screen_size_y = atoi(screensize_y->Value());
208 			DEBWARN("Setting Screen size y to: "<<m_screen_size_y<<"\n");
209 		}
210 
211 		TiXmlText* last_level = docHandle.FirstChild("user").FirstChild("last_level").FirstChild().Text();
212 		if(last_level)
213 		{
214 			m_last_level = atoi(last_level->Value());
215 			DEBWARN("Setting last level to: "<<m_last_level<<"\n");
216 		}
217 
218 		TiXmlText* sound_volume = docHandle.FirstChild("config").FirstChild("sound_volume").FirstChild().Text();
219 		if(sound_volume)
220 		{
221 			m_volume_sound = atoi(sound_volume->Value());
222 			DEBWARN("Setting sound volume to: "<<m_volume_sound<<"\n");
223 		}
224 
225 		TiXmlText* music_volume = docHandle.FirstChild("config").FirstChild("music_volume").FirstChild().Text();
226 		if(music_volume)
227 		{
228 			m_volume_music = atoi(music_volume->Value());
229 			DEBWARN("Setting music volume to: "<<m_volume_music<<"\n");
230 		}
231 
232 
233 	}
234 	else
235 	{
236 		DEBWARN("Unable to read file: "<<filename<<"\n");
237 
238 		//set_default_values();
239 	}
240 
241     refresh_game_window_parameters();
242 }
243 
244 
245 
save_values_to_file(char * filename)246 void Epiconfig::save_values_to_file(char* filename)
247 {
248 
249 
250 	TiXmlDocument doc;
251 
252 	TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "", "" );
253 
254 	doc.LinkEndChild( decl );
255 
256 	TiXmlElement * e_config = new TiXmlElement( "config" );
257 	doc.LinkEndChild( e_config );
258 
259 	TiXmlElement * e_screen = new TiXmlElement( "screen_size" );
260 	e_config->LinkEndChild( e_screen );
261 
262 	TiXmlElement * e_screen_x = new TiXmlElement( "x" );
263 	e_screen->LinkEndChild( e_screen_x );
264 
265 	TiXmlElement * e_screen_y = new TiXmlElement( "y" );
266 	e_screen->LinkEndChild( e_screen_y );
267 
268 	TiXmlElement * e_user = new TiXmlElement( "user" );
269 	doc.LinkEndChild( e_user );
270 
271 	TiXmlElement * e_last_level = new TiXmlElement( "last_level" );
272 	e_user->LinkEndChild( e_last_level );
273 
274 	TiXmlElement * e_sound_volume = new TiXmlElement( "sound_volume" );
275 	e_config->LinkEndChild( e_sound_volume );
276 
277 	TiXmlElement * e_music_volume = new TiXmlElement( "music_volume" );
278 	e_config->LinkEndChild( e_music_volume );
279 
280 	char* text_screen_size_x = new char[10];
281 
282 	char* text_screen_size_y = new char[10];
283 
284 	char* text_last_level = new char[10];
285 
286 	sprintf(text_last_level, "%d", m_last_level);
287 
288 	sprintf(text_screen_size_x, "%d", m_screen_size_x);
289 
290 	sprintf(text_screen_size_y, "%d", m_screen_size_y);
291 
292 
293 	char* text_sound_volume = new char[10];
294 
295 	sprintf(text_sound_volume, "%d", m_volume_sound);
296 
297 	char* text_music_volume = new char[10];
298 
299 	sprintf(text_music_volume, "%d", m_volume_music);
300 
301 	TiXmlText * screen_x = new TiXmlText( text_screen_size_x );
302 	e_screen_x->LinkEndChild( screen_x );
303 
304 	TiXmlText * screen_y = new TiXmlText( text_screen_size_y );
305 	e_screen_y->LinkEndChild( screen_y );
306 
307 	TiXmlText * last_level = new TiXmlText( text_last_level );
308 	e_last_level->LinkEndChild( last_level );
309 
310 	e_sound_volume->LinkEndChild( new TiXmlText( text_sound_volume ) );
311 
312 	e_music_volume->LinkEndChild( new TiXmlText( text_music_volume ) );
313 
314 	doc.SaveFile( filename );
315 
316 	/*
317 	if(doc.LoadFile(filename))
318 	{
319 
320 		TiXmlHandle docHandle( &doc );
321 
322 		TiXmlText* screensize_x = docHandle.FirstChild("config").FirstChild("screen_size").FirstChild("x").FirstChild().Text();
323 		if(screensize_x)
324 		{
325 			m_screen_size_x = atoi(screensize_x->Value());
326 			DEBWARN("Setting Screen size x to: "<<m_screen_size_x<<"\n");
327 		}
328 
329 		TiXmlText* screensize_y = docHandle.FirstChild("config").FirstChild("screen_size").FirstChild("y").FirstChild().Text();
330 		if(screensize_y)
331 		{
332 			m_screen_size_y = atoi(screensize_y->Value());
333 			DEBWARN("Setting Screen size y to: "<<m_screen_size_y<<"\n");
334 		}
335 
336 		TiXmlText* last_level = docHandle.FirstChild("user").FirstChild("last_level").FirstChild().Text();
337 		if(last_level)
338 		{
339 			m_last_level = atoi(last_level->Value());
340 			DEBWARN("Setting last level to: "<<m_last_level<<"\n");
341 		}
342 
343 	}
344 	else
345 	{
346 		DEBWARN("Unable to read file: "<<filename<<"\n");
347 
348 		//set_default_values();
349 	}
350 
351     refresh_game_window_parameters();
352     */
353 }
354 
355 
356 
357 
358 
359 // begin Singleton stuff
360 
361 Epiconfig* Epiconfig::_instance = 0;
362 
instance()363 Epiconfig* Epiconfig::instance()
364 {
365 	if (_instance == 0)
366 	{
367 		_instance = new Epiconfig;
368 	}
369 	return _instance;
370 }
371 
372 //end Singleton stuff
373