soundsource.hpp

Go to the documentation of this file.
00001 /* $Id: soundsource.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2006 - 2012 by Karol Nowak <grzywacz@sul.uni.lodz.pl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 #ifndef SOUNDSOURCE_HPP_INCLUDED
00016 #define SOUNDSOURCE_HPP_INCLUDED
00017 
00018 #include <map>
00019 
00020 #include "generic_event.hpp"
00021 #include "map_location.hpp"
00022 #include "savegame_config.hpp"
00023 
00024 class config;
00025 class display;
00026 
00027 namespace soundsource {
00028 
00029 class sourcespec;
00030 class manager;
00031 
00032 /*
00033  * Sound source is an object on a map (a location) which has one or more
00034  * sounds effects associated with it, which are played randomly and with
00035  * appropriate delays, when sound emiting object is visible on screen.
00036  */
00037 class positional_source {
00038     unsigned int last_played_;
00039     int min_delay_;
00040     int chance_;
00041     int loops_;
00042     const unsigned int id_;
00043     int range_;
00044     int faderange_;
00045     bool check_fogged_;
00046     bool check_shrouded_;
00047     std::string files_;
00048     std::vector<map_location> locations_;
00049 
00050     // Last assigned id; this can, of course, overflow, but I'd
00051     // never expect to see 4 billions sound sources being created...
00052     static unsigned int last_id;
00053 
00054 public:
00055     // min_delay is a minimum time in seconds, which must pass before
00056     // this sound source can be played again if it remains visible
00057     //
00058     // chance is a chance ;-) (in %) that the sound source will emit
00059     // sound every second after the delay has passed or once the source
00060     // becomes visible
00061     positional_source(const sourcespec &spec);
00062     ~positional_source();
00063 
00064     bool is_global();
00065 
00066     void update(unsigned int time, const display &disp);
00067     void update_positions(unsigned int time, const display &disp);
00068 
00069     int calculate_volume(const map_location &loc, const display &disp);
00070 
00071     /**
00072      * Serializes attributes as WML config.
00073      * @param cfg A reference to a [sound_source] tag object.
00074      */
00075     void write_config(config& cfg) const;
00076 };
00077 
00078 class manager : public events::observer, public savegame::savegame_config {
00079 
00080     typedef std::map<std::string, positional_source *> positional_source_map;
00081     typedef positional_source_map::iterator            positional_source_iterator;
00082     typedef positional_source_map::const_iterator      positional_source_const_iterator;
00083 
00084     positional_source_map sources_;
00085     const display &disp_;
00086 
00087 public:
00088     manager(const display &disp);
00089     ~manager();
00090 
00091     // event interface
00092     void handle_generic_event(const std::string &event_name);
00093 
00094     // add or replace a soundsource
00095     void add(const sourcespec &source);
00096     void remove(const std::string &id);
00097     void update();
00098 
00099     // checks which sound sources are visible
00100     void update_positions();
00101 
00102     /**
00103      * Serializes information into cfg as new children of key
00104      * "sound_source", appendend to existing content.
00105      */
00106     void write_sourcespecs(config& cfg) const;
00107 
00108     config to_config() const;
00109 };
00110 
00111 /**
00112  * Sound source info class.
00113  * Encapsulates sound source parameters, so that they're easier to pass
00114  * around/extend/read.
00115  */
00116 class sourcespec
00117 {
00118     const std::string id_;
00119     const std::string files_;
00120 
00121     int min_delay_;
00122     int chance_;
00123 
00124     int loops_;
00125     int range_;
00126     int faderange_;
00127     bool check_fogged_;
00128     bool check_shrouded_;
00129 
00130     std::vector<map_location> locations_;
00131 
00132 public:
00133     /** Parameter-list constructor. */
00134     sourcespec(const std::string& id, const std::string& files, int min_delay, int chance) :
00135         id_(id),
00136         files_(files),
00137         min_delay_(min_delay),
00138         chance_(chance),
00139         loops_(0),
00140         range_(3),
00141         faderange_(14),
00142         check_fogged_(false),
00143         check_shrouded_(false),
00144         locations_()
00145     {}
00146 
00147     /** WML constructor. */
00148     sourcespec(const config& cfg);
00149 
00150     /**
00151      * Serializes information into cfg as a new (appended)
00152      * child of key "sound_source".
00153      */
00154     void write(config& cfg) const;
00155 
00156     int loops() const { return loops_; }
00157 
00158     void set_loops(int value) {
00159         loops_ = value;
00160     }
00161 
00162     bool check_fogged() const { return check_fogged_; }
00163     bool check_shrouded() const { return check_shrouded_; }
00164 
00165     void set_check_fogged(bool value) {
00166         check_fogged_ = value;
00167     }
00168 
00169     void set_check_shrouded(bool value) {
00170         check_shrouded_ = value;
00171     }
00172 
00173     const std::vector<map_location>& get_locations() const {
00174         return locations_;
00175     }
00176 
00177     int full_range() const { return range_; }
00178 
00179     void set_full_range(int value) {
00180         range_ = value;
00181     }
00182 
00183     int fade_range() const { return faderange_; }
00184 
00185     void set_fade_range(int value) {
00186         faderange_ = value;
00187     }
00188 
00189     int minimum_delay() const { return min_delay_; }
00190 
00191     void set_minimum_delay(int value) {
00192         min_delay_ = value;
00193     }
00194 
00195     int chance() const { return chance_; }
00196 
00197     void set_chance(int value) {
00198         chance_ = value;
00199     }
00200 
00201     const std::string& id() const { return id_; }
00202 
00203     const std::string& files() const { return files_; }
00204 };
00205 
00206 } // namespace soundsource
00207 
00208 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:10 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs