File: game.cpp

package info (click to toggle)
plee-the-bear 0.6.0-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 149,644 kB
  • ctags: 15,149
  • sloc: lisp: 463,017; cpp: 100,071; xml: 4,008; sh: 748; python: 313; makefile: 79
file content (477 lines) | stat: -rw-r--r-- 14,696 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
  Bear Engine

  Copyright (C) 20052011 Julien Jorge, Sebastien Angibaud

  This program is free software; you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the
  Free Software Foundation; either version 2 of the License, or (at your
  option) any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along
  with this program; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

  contact: plee-the-bear@gamned.org

  Please add the tag [Bear] in the subject of your mails.
*/
/**
 * \file game.cpp
 * \brief Implementation of the bear::engine::game class.
 * \author Julien Jorge
 */
#include "engine/game.hpp"

#include "engine/game_local_client.hpp"
#include "engine/variable/variable_saver.hpp"

#include <claw/assert.hpp>

/*----------------------------------------------------------------------------*/
bear::engine::game* bear::engine::game::s_instance(NULL);

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the instance of the game.
 * \pre s_instance != NULL
 */
bear::engine::game& bear::engine::game::get_instance()
{
  CLAW_PRECOND( s_instance != NULL );

  return *s_instance;
} // game::get_instance()

/*----------------------------------------------------------------------------*/
/**
 * \brief Print the options of the program.
 */
void bear::engine::game::print_help()
{
  game_local_client::print_help();
} // game::print_help()

/*----------------------------------------------------------------------------*/
/**
 * \brief Constructor.
 * \param argc Number of program arguments.
 * \param argv Program arguments.
 */
bear::engine::game::game( int& argc, char** &argv )
{
  CLAW_PRECOND( s_instance == NULL );
  s_instance = this;

  m_game = new game_local_client(argc, argv);
} // game::game()

/*----------------------------------------------------------------------------*/
/**
 * \brief Destructor.
 */
bear::engine::game::~game()
{
  delete m_game;
  s_instance = NULL;
} // game::~game()

/*----------------------------------------------------------------------------*/
/**
 * \brief Run the game.
 */
void bear::engine::game::run()
{
  m_game->run();
} // game::run()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the time step between to progress on the level.
 */
bear::systime::milliseconds_type bear::engine::game::get_time_step() const
{
  return m_game->get_time_step();
} // game::get_time_step()

/*----------------------------------------------------------------------------*/
/**
 * \brief Turn on/off the full screen mode.
 * \param b Tell if we must activate the fullscreen mode.
 */
void bear::engine::game::set_fullscreen( bool full )
{
  m_game->set_fullscreen(full);
} // game::set_fullscreen()

/*----------------------------------------------------------------------------*/
/**
 * \brief Return the full screen mode.
 */
bool bear::engine::game::get_fullscreen() const
{
  return m_game->get_fullscreen();
} // game::get_fullscreen()

/*----------------------------------------------------------------------------*/
/**
 * \brief Toggle the status of the fullscreen mode.
 */
void bear::engine::game::toggle_fullscreen()
{
  set_fullscreen( !get_fullscreen() );
} // game::toggle_fullscreen()

/*----------------------------------------------------------------------------*/
/**
 * \brief Mute/unmute the sounds.
 * \param m The mute status.
 */
void bear::engine::game::set_sound_muted( bool m )
{
  m_game->set_sound_muted(m);
} // game::set_sound_muted()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if the sound is muted.
 */
bool bear::engine::game::get_sound_muted() const
{
  return m_game->get_sound_muted();
} // game::get_sound_muted()

/*----------------------------------------------------------------------------*/
/**
 * \brief Change the "mute" status of the sounds.
 */
void bear::engine::game::toggle_sound_muted()
{
  set_sound_muted( !get_sound_muted() );
} // game::toggle_sound_muted()

/*----------------------------------------------------------------------------*/
/**
 * \brief Set the volume of the sounds.
 * \param v The volume.
 */
void bear::engine::game::set_sound_volume( double v )
{
  m_game->set_sound_volume(v);
} // game::set_sound_volume()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the volume of the sounds.
 */
double bear::engine::game::get_sound_volume() const
{
  return m_game->get_sound_volume();
} // game::get_sound_colume()

/*----------------------------------------------------------------------------*/
/**
 * \brief Mute/unmute the music.
 * \param m The mute status.
 */
void bear::engine::game::set_music_muted( bool m )
{
  m_game->set_music_muted(m);
} // game::set_music_muted()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if the music is muted.
 */
bool bear::engine::game::get_music_muted() const
{
  return m_game->get_music_muted();
} // game::get_music_muted()

/*----------------------------------------------------------------------------*/
/**
 * \brief Change the "mute" status of the music.
 */
void bear::engine::game::toggle_music_muted()
{
  set_music_muted( !get_music_muted() );
} // game::toggle_music_muted()

/*----------------------------------------------------------------------------*/
/**
 * \brief Set the volume of the musics.
 * \param v The volume.
 */
void bear::engine::game::set_music_volume( double v )
{
  m_game->set_music_volume(v);
} // game::set_music_volume()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the volume of the musics.
 */
double bear::engine::game::get_music_volume() const
{
  return m_game->get_music_volume();
} // game::get_music_volume()

/*----------------------------------------------------------------------------*/
/**
 * \brief Take a shot of the screen.
 * \param img The image in which we save the screen.
 */
void bear::engine::game::screenshot( claw::graphic::image& img ) const
{
  m_game->screenshot( img );
} // game::screenshot()

/*----------------------------------------------------------------------------*/
/**
 * \brief Take a shot of the whole level.
 * \param img The image in which we save the level.
 */
void bear::engine::game::levelshot( claw::graphic::image& img ) const
{
  m_game->levelshot( img );
} // game::levelshot()

/*----------------------------------------------------------------------------*/
/**
 * \brief End the game.
 */
void bear::engine::game::end()
{
  m_game->end();
} // game::end()

#include "engine/variable/variable_list_reader.hpp"

/*----------------------------------------------------------------------------*/
/**
 * \brief Give the name of a level to load as soon as possible.
 * \param path The path of the level to load.
 */
void bear::engine::game::set_waiting_level( const std::string& path )
{
  m_game->set_waiting_level(path);
} // game::set_waiting_level()

/*----------------------------------------------------------------------------*/
/**
 * \brief Set the level to run as soon as possible.
 * \param the_level The level to run as soon as possible.
 */
void bear::engine::game::set_waiting_level( level* the_level )
{
  m_game->set_waiting_level(the_level);
} // game::set_waiting_level()

/*----------------------------------------------------------------------------*/
/**
 * \brief Give the name of a level to load as soon as possible but keep the
 *        current level in memory for future restoration.
 * \param path The path of the level to load.
 */
void bear::engine::game::push_level( const std::string& path )
{
  m_game->push_level(path);
} // game::push_level()

/*----------------------------------------------------------------------------*/
/**
 * \brief Restore the level at the top of the stack.
 */
void bear::engine::game::pop_level()
{
  m_game->pop_level();
} // game::pop_level()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the size of the screen.
 */
const claw::math::coordinate_2d<unsigned int>&
bear::engine::game::get_screen_size() const
{
  return m_game->get_screen_size();
} // game::get_screen_size()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the margin of the active area around the camera.
 */
double bear::engine::game::get_active_area_margin() const
{
  return m_game->get_active_area_margin();
} // game::get_active_area_margin()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the path of a file in the user's game directory.
 * \param name The name of the file.
 */
std::string
bear::engine::game::get_custom_game_file( const std::string& name ) const
{
  return m_game->get_custom_game_file(name);
} // game::get_full_user_path()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the value of a global variable.
 * \param val (in/out) The variable for which we want the value.
 */
void bear::engine::game::get_game_variable( base_variable& val ) const
{
  m_game->get_game_variable(val);
} // game::get_game_variable()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the game variables whose name match a given regular
 *        expression;
 * \param vars The list of matched game variables.
 * \param pattern The expression that has to be matched by the variable names.
 */
void bear::engine::game::get_game_variables
( var_map& vars, const std::string& pattern )
{
  m_game->get_game_variables(vars, pattern);
} // game::get_game_variables()

/*----------------------------------------------------------------------------*/
/**
 * \brief Set the value of a global variable.
 * \param val (in/out) The variable for which we set the value.
 */
void bear::engine::game::set_game_variable( const base_variable& val )
{
  m_game->set_game_variable(val);
} // game::set_game_variable()

/*----------------------------------------------------------------------------*/
/**
 * \brief Set several game variables at once.
 * \param vars The variables.
 */
void bear::engine::game::set_game_variables( const var_map& vars )
{
  m_game->set_game_variables(vars);
} // game::set_game_variables()

/*----------------------------------------------------------------------------*/
/**
 * \brief Erase the variables whose name match a given pattern.
 * \param pattern The pattern to match.
 */
void bear::engine::game::erase_game_variables( const std::string& pattern )
{
  m_game->erase_game_variables(pattern);
} // game::erase_game_variables()

/*----------------------------------------------------------------------------*/
/**
 * \brief Tell if a given variable exists in the game.
 * \param val (in/out) The variable to check.
 */
bool bear::engine::game::game_variable_exists( const base_variable& val ) const
{
  return m_game->game_variable_exists(val);
} // game::game_variable_exists()

/*----------------------------------------------------------------------------*/
/**
 * \brief Save all the game variables whose name match a given regular
 *        expression;
 * \param os The stream in which the variables are saved.
 * \param pattern The expression that has to be matched by the variable names.
 */
void bear::engine::game::save_game_variables
( std::ostream& os, const std::string& pattern )
{
  var_map vars;
  m_game->get_all_game_variables(vars);

  vars.for_each( variable_saver(os, boost::regex(pattern)) );
} // game::save_game_variables()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add a listener to follow the change on a game variable of type int.
 * \param name The name of the variable.
 * \param f The listener.
 */
boost::signals2::connection
bear::engine::game::listen_int_variable_change
( const std::string& name, const boost::function<void (int)>& f )
{
  return m_game->listen_int_variable_change( name, f );
} // game::listen_int_variable_change()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add a listener to follow the change on a game variable of type
 *        unsigned int.
 * \param name The name of the variable.
 * \param f The listener.
 */
boost::signals2::connection
bear::engine::game::listen_uint_variable_change
( const std::string& name, const boost::function<void (unsigned int)>& f )
{
  return m_game->listen_uint_variable_change( name, f );
} // game::listen_uint_variable_change()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add a listener to follow the change on a game variable of type bool.
 * \param name The name of the variable.
 * \param f The listener.
 */
boost::signals2::connection
bear::engine::game::listen_bool_variable_change
( const std::string& name, const boost::function<void (bool)>& f )
{
  return m_game->listen_bool_variable_change( name, f );
} // game::listen_bool_variable_change()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add a listener to follow the change on a game variable of type double.
 * \param name The name of the variable.
 * \param f The listener.
 */
boost::signals2::connection
bear::engine::game::listen_double_variable_change
( const std::string& name, const boost::function<void (double)>& f )
{
  return m_game->listen_double_variable_change( name, f );
} // game::listen_double_variable_change()

/*----------------------------------------------------------------------------*/
/**
 * \brief Add a listener to follow the change on a game variable of type string.
 * \param name The name of the variable.
 * \param f The listener.
 */
boost::signals2::connection
bear::engine::game::listen_string_variable_change
( const std::string& name, const boost::function<void (std::string)>& f )
{
  return m_game->listen_string_variable_change( name, f );
} // game::listen_string_variable_change()

/*----------------------------------------------------------------------------*/
/**
 * \brief Get the name of the game.
 */
const std::string& bear::engine::game::get_name() const
{
  return m_game->get_name();
} // game::get_name()