Panda3D

audioVolumeAttrib.cxx

00001 // Filename: audioVolumeAttrib.cxx
00002 // Created by:  darren (15Dec06)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #include "audioVolumeAttrib.h"
00016 #include "graphicsStateGuardianBase.h"
00017 #include "dcast.h"
00018 #include "bamReader.h"
00019 #include "bamWriter.h"
00020 #include "datagram.h"
00021 #include "datagramIterator.h"
00022 #include "config_pgraph.h"
00023 
00024 CPT(RenderAttrib) AudioVolumeAttrib::_identity_attrib;
00025 TypeHandle AudioVolumeAttrib::_type_handle;
00026 int AudioVolumeAttrib::_attrib_slot;
00027 
00028 ////////////////////////////////////////////////////////////////////
00029 //     Function: AudioVolumeAttrib::Constructor
00030 //       Access: Protected
00031 //  Description: Use AudioVolumeAttrib::make() to construct a new
00032 //               AudioVolumeAttrib object.
00033 ////////////////////////////////////////////////////////////////////
00034 AudioVolumeAttrib::
00035 AudioVolumeAttrib(bool off, PN_stdfloat volume) :
00036   _off(off),
00037   _volume(volume)
00038 {
00039   nassertv(_volume >= 0.f);
00040   _has_volume = !IS_NEARLY_EQUAL(_volume, 1.0f);
00041 }
00042 
00043 ////////////////////////////////////////////////////////////////////
00044 //     Function: AudioVolumeAttrib::make_identity
00045 //       Access: Published, Static
00046 //  Description: Constructs an identity audio volume attrib.
00047 ////////////////////////////////////////////////////////////////////
00048 CPT(RenderAttrib) AudioVolumeAttrib::
00049 make_identity() {
00050   // We make identity a special case and store a pointer forever once
00051   // we find it the first time.
00052   if (_identity_attrib == (AudioVolumeAttrib *)NULL) {
00053     AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, 1.0f);;
00054     _identity_attrib = return_new(attrib);
00055   }
00056 
00057   return _identity_attrib;
00058 }
00059 
00060 ////////////////////////////////////////////////////////////////////
00061 //     Function: AudioVolumeAttrib::make
00062 //       Access: Published, Static
00063 //  Description: Constructs a new AudioVolumeAttrib object that indicates
00064 //               audio volume should be scaled by the indicated factor.
00065 ////////////////////////////////////////////////////////////////////
00066 CPT(RenderAttrib) AudioVolumeAttrib::
00067 make(PN_stdfloat volume) {
00068   AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, volume);
00069   return return_new(attrib);
00070 }
00071 
00072 ////////////////////////////////////////////////////////////////////
00073 //     Function: AudioVolumeAttrib::make_off
00074 //       Access: Published, Static
00075 //  Description: Constructs a new AudioVolumeAttrib object that ignores
00076 //               any AudioVolumeAttrib inherited from above.  You may
00077 //               also specify an additional volume scale to apply to
00078 //               geometry below (using set_volume()).
00079 ////////////////////////////////////////////////////////////////////
00080 CPT(RenderAttrib) AudioVolumeAttrib::
00081 make_off() {
00082   AudioVolumeAttrib *attrib = 
00083     new AudioVolumeAttrib(true, 1.0f);
00084   return return_new(attrib);
00085 }
00086 
00087 ////////////////////////////////////////////////////////////////////
00088 //     Function: AudioVolumeAttrib::make_default
00089 //       Access: Published, Static
00090 //  Description: Returns a RenderAttrib that corresponds to whatever
00091 //               the standard default properties for render attributes
00092 //               of this type ought to be.
00093 ////////////////////////////////////////////////////////////////////
00094 CPT(RenderAttrib) AudioVolumeAttrib::
00095 make_default() {
00096   return return_new(new AudioVolumeAttrib(false, 1.0f));
00097 }
00098 
00099 ////////////////////////////////////////////////////////////////////
00100 //     Function: AudioVolumeAttrib::set_volume
00101 //       Access: Published
00102 //  Description: Returns a new AudioVolumeAttrib, just like this one, but
00103 //               with the volume changed to the indicated value.
00104 ////////////////////////////////////////////////////////////////////
00105 CPT(RenderAttrib) AudioVolumeAttrib::
00106 set_volume(PN_stdfloat volume) const {
00107   AudioVolumeAttrib *attrib = new AudioVolumeAttrib(*this);
00108   assert(volume >= 0.f);
00109   attrib->_volume = volume;
00110   attrib->_has_volume = !IS_NEARLY_EQUAL(volume, 1.0f);
00111   return return_new(attrib);
00112 }
00113 
00114 ////////////////////////////////////////////////////////////////////
00115 //     Function: AudioVolumeAttrib::output
00116 //       Access: Public, Virtual
00117 //  Description: 
00118 ////////////////////////////////////////////////////////////////////
00119 void AudioVolumeAttrib::
00120 output(ostream &out) const {
00121   out << get_type() << ":";
00122   if (is_off()) {
00123     out << "off";
00124   }
00125   if (has_volume()) {
00126     out << "(" << get_volume() << ")";
00127 
00128   } else if (!is_off()) {
00129     out << "identity";
00130   }
00131 }
00132 
00133 ////////////////////////////////////////////////////////////////////
00134 //     Function: AudioVolumeAttrib::compare_to_impl
00135 //       Access: Protected, Virtual
00136 //  Description: Intended to be overridden by derived AudioVolumeAttrib
00137 //               types to return a unique number indicating whether
00138 //               this AudioVolumeAttrib is equivalent to the other one.
00139 //
00140 //               This should return 0 if the two AudioVolumeAttrib objects
00141 //               are equivalent, a number less than zero if this one
00142 //               should be sorted before the other one, and a number
00143 //               greater than zero otherwise.
00144 //
00145 //               This will only be called with two AudioVolumeAttrib
00146 //               objects whose get_type() functions return the same.
00147 ////////////////////////////////////////////////////////////////////
00148 int AudioVolumeAttrib::
00149 compare_to_impl(const RenderAttrib *other) const {
00150   const AudioVolumeAttrib *ta;
00151   DCAST_INTO_R(ta, other, 0);
00152 
00153   if (_off != ta->_off) {
00154     return (int)_off - (int)ta->_off;
00155   }
00156 
00157   if (_volume != ta->_volume) {
00158     return _volume < ta->_volume ? -1 : 1;
00159   }
00160 
00161   return 0;
00162 }
00163 
00164 ////////////////////////////////////////////////////////////////////
00165 //     Function: AudioVolumeAttrib::get_hash_impl
00166 //       Access: Protected, Virtual
00167 //  Description: Intended to be overridden by derived RenderAttrib
00168 //               types to return a unique hash for these particular
00169 //               properties.  RenderAttribs that compare the same with
00170 //               compare_to_impl(), above, should return the same
00171 //               hash; RenderAttribs that compare differently should
00172 //               return a different hash.
00173 ////////////////////////////////////////////////////////////////////
00174 size_t AudioVolumeAttrib::
00175 get_hash_impl() const {
00176   size_t hash = 0;
00177   hash = int_hash::add_hash(hash, (int)_off);
00178   hash = float_hash().add_hash(hash, _volume);
00179   return hash;
00180 }
00181 
00182 ////////////////////////////////////////////////////////////////////
00183 //     Function: AudioVolumeAttrib::compose_impl
00184 //       Access: Protected, Virtual
00185 //  Description: Intended to be overridden by derived RenderAttrib
00186 //               types to specify how two consecutive RenderAttrib
00187 //               objects of the same type interact.
00188 //
00189 //               This should return the result of applying the other
00190 //               RenderAttrib to a node in the scene graph below this
00191 //               RenderAttrib, which was already applied.  In most
00192 //               cases, the result is the same as the other
00193 //               RenderAttrib (that is, a subsequent RenderAttrib
00194 //               completely replaces the preceding one).  On the other
00195 //               hand, some kinds of RenderAttrib (for instance,
00196 //               ColorTransformAttrib) might combine in meaningful
00197 //               ways.
00198 ////////////////////////////////////////////////////////////////////
00199 CPT(RenderAttrib) AudioVolumeAttrib::
00200 compose_impl(const RenderAttrib *other) const {
00201   const AudioVolumeAttrib *ta;
00202   DCAST_INTO_R(ta, other, 0);
00203 
00204   if (ta->is_off()) {
00205     return ta;
00206   }
00207 
00208   AudioVolumeAttrib *attrib = new AudioVolumeAttrib(is_off(), ta->_volume * _volume);
00209   return return_new(attrib);
00210 }
00211 
00212 ////////////////////////////////////////////////////////////////////
00213 //     Function: AudioVolumeAttrib::invert_compose_impl
00214 //       Access: Protected, Virtual
00215 //  Description: Intended to be overridden by derived RenderAttrib
00216 //               types to specify how two consecutive RenderAttrib
00217 //               objects of the same type interact.
00218 //
00219 //               See invert_compose() and compose_impl().
00220 ////////////////////////////////////////////////////////////////////
00221 CPT(RenderAttrib) AudioVolumeAttrib::
00222 invert_compose_impl(const RenderAttrib *other) const {
00223   if (is_off()) {
00224     return other;
00225   }
00226   const AudioVolumeAttrib *ta;
00227   DCAST_INTO_R(ta, other, 0);
00228   PN_stdfloat new_volume = _volume == 0.0f ? 1.0f : ta->_volume / _volume;
00229 
00230   AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, new_volume);
00231   return return_new(attrib);
00232 }
00233 
00234 ////////////////////////////////////////////////////////////////////
00235 //     Function: AudioVolumeAttrib::register_with_read_factory
00236 //       Access: Public, Static
00237 //  Description: Tells the BamReader how to create objects of type
00238 //               AudioVolumeAttrib.
00239 ////////////////////////////////////////////////////////////////////
00240 void AudioVolumeAttrib::
00241 register_with_read_factory() {
00242   BamReader::get_factory()->register_factory(get_class_type(), make_from_bam);
00243 }
00244 
00245 ////////////////////////////////////////////////////////////////////
00246 //     Function: AudioVolumeAttrib::write_datagram
00247 //       Access: Public, Virtual
00248 //  Description: Writes the contents of this object to the datagram
00249 //               for shipping out to a Bam file.
00250 ////////////////////////////////////////////////////////////////////
00251 void AudioVolumeAttrib::
00252 write_datagram(BamWriter *manager, Datagram &dg) {
00253   RenderAttrib::write_datagram(manager, dg);
00254 
00255   // We cheat, and modify the bam stream without upping the bam
00256   // version.  We can do this since we know that no existing bam files
00257   // have an AudioVolumeAttrib in them.
00258   dg.add_bool(_off);
00259   dg.add_stdfloat(_volume);
00260 }
00261 
00262 ////////////////////////////////////////////////////////////////////
00263 //     Function: AudioVolumeAttrib::make_from_bam
00264 //       Access: Protected, Static
00265 //  Description: This function is called by the BamReader's factory
00266 //               when a new object of type AudioVolumeAttrib is encountered
00267 //               in the Bam file.  It should create the AudioVolumeAttrib
00268 //               and extract its information from the file.
00269 ////////////////////////////////////////////////////////////////////
00270 TypedWritable *AudioVolumeAttrib::
00271 make_from_bam(const FactoryParams &params) {
00272   AudioVolumeAttrib *attrib = new AudioVolumeAttrib(false, 1.0f);
00273   DatagramIterator scan;
00274   BamReader *manager;
00275 
00276   parse_params(params, scan, manager);
00277   attrib->fillin(scan, manager);
00278 
00279   return attrib;
00280 }
00281 
00282 ////////////////////////////////////////////////////////////////////
00283 //     Function: AudioVolumeAttrib::fillin
00284 //       Access: Protected
00285 //  Description: This internal function is called by make_from_bam to
00286 //               read in all of the relevant data from the BamFile for
00287 //               the new AudioVolumeAttrib.
00288 ////////////////////////////////////////////////////////////////////
00289 void AudioVolumeAttrib::
00290 fillin(DatagramIterator &scan, BamReader *manager) {
00291   RenderAttrib::fillin(scan, manager);
00292 
00293   _off = scan.get_bool();
00294   _volume = scan.get_stdfloat();
00295   nassertv(_volume >= 0.f);
00296   _has_volume = !IS_NEARLY_EQUAL(_volume, 1.0f);
00297 }
 All Classes Functions Variables Enumerations