soundsource.hpp

Go to the documentation of this file.
00001 /* $Id: soundsource.hpp 24709 2008-03-16 15:02:39Z grzywacz $ */
00002 /*
00003    Copyright (C) 2006 - 2008 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 version 2
00008    or at your option any later version.
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY.
00011 
00012    See the COPYING file for more details.
00013 */
00014 #ifndef SOUNDSOURCE_HPP_INCLUDED
00015 #define SOUNDSOURCE_HPP_INCLUDED
00016 
00017 #include <map>
00018 #include <string>
00019 #include <vector>
00020 
00021 #include "generic_event.hpp"
00022 #include "map.hpp"
00023 
00024 class display;
00025 
00026 namespace soundsource {
00027 
00028 class sourcespec;
00029 class manager;
00030 
00031 /*
00032  * Sound source is an object on a map (a location) which has one or more
00033  * sounds effects associated with it, which are played randomly and with
00034  * appropriate delays, when sound emiting object is visible on screen.
00035  */
00036 class positional_source {
00037     friend class manager;
00038 
00039     unsigned int _last_played;
00040     unsigned int _min_delay;
00041     unsigned int _chance;
00042     unsigned int _loops;
00043     const unsigned int _id;
00044     unsigned int _range;
00045     unsigned int _faderange;
00046     bool _check_fogged;
00047     std::string _files;
00048     std::vector<gamemap::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     // min_delay is a minimum time in seconds, which must pass before
00055     // this sound source can be played again if it remains visible
00056     //
00057     // chance is a chance ;-) (in %) that the sound source will emit
00058     // sound every second after the delay has passed or once the source
00059     // becomes visible
00060     positional_source(const sourcespec &spec);
00061     ~positional_source();
00062 
00063     void update(unsigned int time, const display &disp);
00064     void update_positions(unsigned int time, const display &disp);
00065 
00066     void add_location(const gamemap::location &loc);
00067     void remove_location(const gamemap::location &loc);
00068     void replace_location(const gamemap::location &oldloc, const gamemap::location &newloc);
00069     
00070     int calculate_volume(const gamemap::location &loc, const display &disp);
00071 };
00072 
00073 class manager : public events::observer {
00074 
00075     typedef std::map<std::string, positional_source *> positional_source_map;
00076     typedef positional_source_map::iterator positional_source_iterator;
00077 
00078     positional_source_map _sources;
00079     const display &_disp;
00080 
00081     // checks which sound sources are visible
00082     void update_positions();
00083 
00084 public:
00085     manager(const display &disp);
00086     ~manager();
00087 
00088     // event interface
00089     void handle_generic_event(const std::string &event_name);
00090 
00091     // add or replace a soundsource
00092     void add(const sourcespec &source);
00093     void remove(const std::string &id);
00094     void update();
00095 
00096     void add_location(const std::string &id, const gamemap::location &loc);
00097 };
00098 
00099 /*
00100  * A class encapsulating parameters, so that they're easier to pass around/extend/read.
00101  */
00102 class sourcespec {
00103     const std::string &id;
00104     const std::string &files;
00105 
00106     int min_delay;
00107     int chance;
00108 
00109     int loops;
00110     int range;
00111     int faderange;
00112     bool check_fogged;
00113 
00114     std::vector<gamemap::location> locations;
00115 
00116 public:
00117     sourcespec(const std::string &id_, const std::string &files_, int min_delay_, int chance_)
00118         : id(id_), files(files_), min_delay(min_delay_), chance(chance_), loops(0), 
00119             range(1), faderange(range), check_fogged(false)
00120     { 
00121     }
00122 
00123     sourcespec& loop(int loops_) {
00124         loops = loops_;
00125         return *this;
00126     }
00127 
00128     sourcespec& check_fog(bool fogged) {
00129         check_fogged = fogged;
00130         return *this;
00131     }
00132 
00133     sourcespec& location(const gamemap::location &loc) {
00134         locations.push_back(loc);
00135         return *this;
00136     }
00137 
00138     sourcespec& full_range(int range_) {
00139         range = maximum<int>(1, range_);
00140         return *this;
00141     }
00142 
00143     sourcespec& fade_range(int range_) {
00144         faderange = range_;
00145         return *this;
00146     }
00147 
00148     friend class manager;
00149     friend class positional_source;
00150 };
00151 
00152 } // namespace soundsourcespec
00153 
00154 #endif

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs