The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
soundsource.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2016 by Karol Nowak <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
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  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 #ifndef SOUNDSOURCE_HPP_INCLUDED
15 #define SOUNDSOURCE_HPP_INCLUDED
16 
17 #include <map>
18 
19 #include "generic_event.hpp"
20 #include "map/location.hpp"
21 
22 class config;
23 class display;
24 
25 namespace soundsource {
26 
27 class sourcespec;
28 class manager;
29 
30 /*
31  * Sound source is an object on a map (a location) which has one or more
32  * sounds effects associated with it, which are played randomly and with
33  * appropriate delays, when sound emitting object is visible on screen.
34  */
36  unsigned int last_played_;
38  int chance_;
39  int loops_;
40  const unsigned int id_;
41  int range_;
46  std::vector<map_location> locations_;
47 
48  // Last assigned id; this can, of course, overflow, but I'd
49  // never expect to see 4 billions sound sources being created...
50  static unsigned int last_id;
51 
52 public:
53  // min_delay is a minimum time in seconds, which must pass before
54  // this sound source can be played again if it remains visible
55  //
56  // chance is a chance ;-) (in %) that the sound source will emit
57  // sound every second after the delay has passed or once the source
58  // becomes visible
59  positional_source(const sourcespec &spec);
61 
62  bool is_global();
63 
64  void update(unsigned int time, const display &disp);
65  void update_positions(unsigned int time, const display &disp);
66 
67  int calculate_volume(const map_location &loc, const display &disp);
68 
69  /**
70  * Serializes attributes as WML config.
71  * @param cfg A reference to a [sound_source] tag object.
72  */
73  void write_config(config& cfg) const;
74 };
75 
76 class manager : public events::observer
77 {
78 
79  typedef std::map<std::string, positional_source *> positional_source_map;
81  typedef positional_source_map::const_iterator positional_source_const_iterator;
82 
83  positional_source_map sources_;
84  const display &disp_;
85 
86 public:
87  manager(const display &disp);
88  ~manager();
89 
90  // event interface
91  void handle_generic_event(const std::string &event_name);
92 
93  // add or replace a soundsource
94  void add(const sourcespec &source);
95  void remove(const std::string &id);
96  config get(const std::string &id);
97  void update();
98 
99  // checks which sound sources are visible
100  void update_positions();
101 
102  /**
103  * Serializes information into cfg as new children of key
104  * "sound_source", appended to existing content.
105  */
106  void write_sourcespecs(config& cfg) const;
107 };
108 
109 /**
110  * Sound source info class.
111  * Encapsulates sound source parameters, so that they're easier to pass
112  * around/extend/read.
113  */
115 {
118 
120  int chance_;
121 
122  int loops_;
123  int range_;
127 
128  std::vector<map_location> locations_;
129 
130 public:
131  /** Parameter-list constructor. */
132  sourcespec(const std::string& id, const std::string& files, int min_delay, int chance) :
133  id_(id),
134  files_(files),
135  min_delay_(min_delay),
136  chance_(chance),
137  loops_(0),
138  range_(3),
139  faderange_(14),
140  check_fogged_(false),
141  check_shrouded_(false),
142  locations_()
143  {}
144 
145  /** WML constructor. */
146  sourcespec(const config& cfg);
147 
148  /**
149  * Serializes information into cfg as a new (appended)
150  * child of key "sound_source".
151  */
152  void write(config& cfg) const;
153 
154  int loops() const { return loops_; }
155 
156  void set_loops(int value) {
157  loops_ = value;
158  }
159 
160  bool check_fogged() const { return check_fogged_; }
161  bool check_shrouded() const { return check_shrouded_; }
162 
163  void set_check_fogged(bool value) {
164  check_fogged_ = value;
165  }
166 
168  check_shrouded_ = value;
169  }
170 
171  const std::vector<map_location>& get_locations() const {
172  return locations_;
173  }
174 
175  int full_range() const { return range_; }
176 
177  void set_full_range(int value) {
178  range_ = value;
179  }
180 
181  int fade_range() const { return faderange_; }
182 
183  void set_fade_range(int value) {
184  faderange_ = value;
185  }
186 
187  int minimum_delay() const { return min_delay_; }
188 
190  min_delay_ = value;
191  }
192 
193  int chance() const { return chance_; }
194 
195  void set_chance(int value) {
196  chance_ = value;
197  }
198 
199  const std::string& id() const { return id_; }
200 
201  const std::string& files() const { return files_; }
202 };
203 
204 } // namespace soundsource
205 
206 #endif
int calculate_volume(const map_location &loc, const display &disp)
int minimum_delay() const
const std::string & files() const
const std::vector< map_location > & get_locations() const
void set_fade_range(int value)
void set_minimum_delay(int value)
std::map< std::string, positional_source * > positional_source_map
Definition: soundsource.hpp:79
const std::string & id() const
positional_source_map::iterator positional_source_iterator
Definition: soundsource.hpp:80
void update(unsigned int time, const display &disp)
void handle_generic_event(const std::string &event_name)
Definition: soundsource.cpp:51
std::vector< map_location > locations_
void write(config &cfg) const
Serializes information into cfg as a new (appended) child of key "sound_source".
positional_source_map sources_
Definition: soundsource.hpp:83
GLuint id
Definition: glew.h:1647
void set_full_range(int value)
GLsizei const GLfloat * value
Definition: glew.h:1817
bool check_fogged() const
void write_config(config &cfg) const
Serializes attributes as WML config.
void set_loops(int value)
void update_positions(unsigned int time, const display &disp)
Encapsulates the map of the game.
Definition: location.hpp:38
void set_check_shrouded(bool value)
std::vector< map_location > locations_
Definition: soundsource.hpp:46
Sound source info class.
void add(const sourcespec &source)
Definition: soundsource.cpp:57
positional_source(const sourcespec &spec)
sourcespec(const std::string &id, const std::string &files, int min_delay, int chance)
Parameter-list constructor.
manager(const display &disp)
Definition: soundsource.cpp:33
void set_chance(int value)
const display & disp_
Definition: soundsource.hpp:84
const std::string files_
bool check_shrouded() const
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
const std::string id_
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void set_check_fogged(bool value)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
GLsizei GLsizei GLchar * source
Definition: glew.h:1800
positional_source_map::const_iterator positional_source_const_iterator
Definition: soundsource.hpp:81
static unsigned int last_id
Definition: soundsource.hpp:50
void write_sourcespecs(config &cfg) const
Serializes information into cfg as new children of key "sound_source", appended to existing content...