Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
soundrobj.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
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 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WW3D *
24 * *
25 * $Archive:: /Commando/Code/ww3d2/soundrobj.cpp $*
26 * *
27 * Author:: Patrick Smith *
28 * *
29 * $Modtime:: 1/16/02 10:21a $*
30 * *
31 * $Revision:: 4 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
36
37#if noWWAUDIO //(gth) removing dependency on wwaudio
38
39#include "soundrobj.h"
40#include "audiblesound.h"
41#include "sound3d.h"
42#include "wwaudio.h"
43#include "ffactory.h"
44#include "wwfile.h"
45#include "chunkio.h"
46#include "scene.h"
47
48
50// Global variables
52SoundRenderObjLoaderClass _SoundRenderObjLoader;
53
54
56//
57// SoundRenderObjClass
58//
60SoundRenderObjClass::SoundRenderObjClass (void)
61 : Flags (FLAG_STOP_WHEN_HIDDEN),
62 Sound (NULL),
63 IsInitialized (false)
64{
65 return ;
66}
67
68
70//
71// SoundRenderObjClass
72//
74SoundRenderObjClass::SoundRenderObjClass (const SoundRenderObjClass &src)
75 : Flags (FLAG_STOP_WHEN_HIDDEN),
76 Sound (NULL),
77 IsInitialized (false)
78{
79 (*this) = src;
80 return ;
81}
82
83
85//
86// ~SoundRenderObjClass
87//
89SoundRenderObjClass::~SoundRenderObjClass (void)
90{
91 //
92 // Remove the old sound from the world (if necessary)
93 //
94 if (Sound != NULL) {
95 Sound->Attach_To_Object (NULL);
96 Sound->Remove_From_Scene ();
97 REF_PTR_RELEASE (Sound);
98 }
99
100 return ;
101}
102
103
105//
106// operator=
107//
109const SoundRenderObjClass &
110SoundRenderObjClass::operator= (const SoundRenderObjClass &src)
111{
112 //
113 // Create a definition from the src sound object
114 //
116 definition.Initialize_From_Sound (src.Sound);
117
118 //
119 // Create the internal sound object from the definition
120 //
121 Set_Sound (&definition);
122
123 //
124 // Copy the other misc settings
125 //
126 Name = src.Name;
127 return *this;
128}
129
130
132//
133// Set_Sound
134//
136void
137SoundRenderObjClass::Set_Sound (AudibleSoundDefinitionClass *definition)
138{
139 //
140 // Remove the old sound from the world (if necessary)
141 //
142 if (Sound != NULL) {
143 Sound->Remove_From_Scene ();
144 REF_PTR_RELEASE (Sound);
145 }
146
147 //
148 // Create the sound object from its definition
149 //
150 if (definition != NULL) {
151 Sound = (AudibleSoundClass *)definition->Create ();
152 }
153
154 return ;
155}
156
157
159//
160// On_Frame_Update
161//
163void
164SoundRenderObjClass::On_Frame_Update (void)
165{
166 //
167 // Stop the sound from playing (if necessary)
168 //
169 if ( Sound != NULL &&
170 Sound->Is_In_Scene () &&
171 Sound->Is_Playing () == false)
172 {
173 Sound->Attach_To_Object (NULL);
174 Sound->Remove_From_Scene ();
175 }
176
177 return ;
178}
179
180
182//
183// Set_Hidden
184//
186void
187SoundRenderObjClass::Set_Hidden (int onoff)
188{
189 int before = Is_Not_Hidden_At_All ();
191
192 //
193 // If we've changed state, then update the visibility
194 //
195 if (IsInitialized == false || Is_Not_Hidden_At_All () != before) {
196 IsInitialized = true;
197 Update_On_Visibilty ();
198 }
199
200 return ;
201}
202
203
205//
206// Set_Visible
207//
209void
210SoundRenderObjClass::Set_Visible (int onoff)
211{
212 int before = Is_Not_Hidden_At_All ();
214
215 //
216 // If we've changed state, then update the visibility
217 //
218 if (IsInitialized == false || Is_Not_Hidden_At_All () != before) {
219 IsInitialized = true;
220 Update_On_Visibilty ();
221 }
222
223 return ;
224}
225
226
228//
229// Set_Animation_Hidden
230//
232void
233SoundRenderObjClass::Set_Animation_Hidden (int onoff)
234{
235 int before = Is_Not_Hidden_At_All ();
237
238 //
239 // If we've changed state, then update the visibility
240 //
241 if (IsInitialized == false || Is_Not_Hidden_At_All () != before) {
242 IsInitialized = true;
243 Update_On_Visibilty ();
244 }
245
246 return ;
247}
248
249
251//
252// Set_Force_Visible
253//
255void
256SoundRenderObjClass::Set_Force_Visible (int onoff)
257{
258 int before = Is_Not_Hidden_At_All ();
260
261 //
262 // If we've changed state, then update the visibility
263 //
264 if (IsInitialized == false || Is_Not_Hidden_At_All () != before) {
265 IsInitialized = true;
266 Update_On_Visibilty ();
267 }
268
269 return ;
270}
271
272
274//
275// Update_On_Visibilty
276//
278void
279SoundRenderObjClass::Update_On_Visibilty (void)
280{
281 if (Sound == NULL) {
282 return ;
283 }
284
285 //
286 // Ensure our transform is correct
287 //
288 Validate_Transform ();
289
290 //
291 // Either add the sound object to the sound scene
292 // or remove it from the sound scene depending
293 // on the visibility state of the render object.
294 //
295 if ( Is_Not_Hidden_At_All () &&
296 Sound->Is_In_Scene () == false &&
297 Peek_Scene () != NULL)
298 {
299 //
300 // Make sure the sound is properly attached to this render
301 // object and then add it to the scene
302 //
303 Sound->Attach_To_Object (this);
304 Sound->Add_To_Scene (true);
305
306 } else if ((Is_Not_Hidden_At_All () == false) || (Peek_Scene () == NULL)) {
307
308 //
309 // Remove the sound from the scene (it will stop playing)
310 //
311 if ((Flags & FLAG_STOP_WHEN_HIDDEN) != 0 || (Peek_Scene () == NULL)) {
312 Sound->Attach_To_Object (NULL);
313 Sound->Remove_From_Scene ();
314 }
315 }
316
317 return ;
318}
319
320
322//
323// Get_Sound
324//
327SoundRenderObjClass::Get_Sound (void) const
328{
329 if (Sound != NULL) {
330 Sound->Add_Ref ();
331 }
332
333 return Sound;
334}
335
336
338//
339// Set_Flag
340//
342void
343SoundRenderObjClass::Set_Flag (uint32 flag, bool onoff)
344{
345 Flags &= ~flag;
346 if (onoff) {
347 Flags |= flag;
348 }
349
350 return ;
351}
352
353
355//
356// Notify_Added
357//
359void
360SoundRenderObjClass::Notify_Added (SceneClass *scene)
361{
364
365 Update_On_Visibilty ();
366 return ;
367}
368
369
371//
372// Notify_Removed
373//
375void
376SoundRenderObjClass::Notify_Removed (SceneClass *scene)
377{
380
381 Update_On_Visibilty ();
382 return ;
383}
384
385
387//
388// Set_Transform
389//
391void
392SoundRenderObjClass::Set_Transform (const Matrix3D &tm)
393{
395
396 if (IsInitialized == false) {
397 IsInitialized = true;
398 Update_On_Visibilty ();
399 }
400
401 return ;
402}
403
404
406//
407// Set_Position
408//
410void
411SoundRenderObjClass::Set_Position (const Vector3 &pos)
412{
414
415 if (IsInitialized == false) {
416 IsInitialized = true;
417 Update_On_Visibilty ();
418 }
419
420 return ;
421}
422
423
424//*********************************************************************************
425//
426// Start of SoundRenderObjDefClass
427//
428//*********************************************************************************
429
430
432//
433// SoundRenderObjDefClass
434//
436SoundRenderObjDefClass::SoundRenderObjDefClass (void)
438 Flags (SoundRenderObjClass::FLAG_STOP_WHEN_HIDDEN)
439{
440 return ;
441}
442
443
445//
446// SoundRenderObjDefClass
447//
449SoundRenderObjDefClass::SoundRenderObjDefClass (SoundRenderObjClass &render_obj)
451 Flags (SoundRenderObjClass::FLAG_STOP_WHEN_HIDDEN)
452{
453 Initialize (render_obj);
454 return ;
455}
456
457
459//
460// SoundRenderObjDefClass
461//
463SoundRenderObjDefClass::SoundRenderObjDefClass (const SoundRenderObjDefClass &src)
465 Flags (SoundRenderObjClass::FLAG_STOP_WHEN_HIDDEN)
466{
467 (*this) = src;
468 return ;
469}
470
471
473//
474// ~SoundRenderObjDefClass
475//
477SoundRenderObjDefClass::~SoundRenderObjDefClass (void)
478{
479 return ;
480}
481
482
484//
485// operator=
486//
488const SoundRenderObjDefClass &
489SoundRenderObjDefClass::operator= (const SoundRenderObjDefClass &src)
490{
491 Definition = src.Definition;
492 Version = src.Version;
493 Name = src.Name;
494 return (*this);
495}
496
497
499//
500// Create
501//
504SoundRenderObjDefClass::Create (void)
505{
506 //
507 // Allocate and initialize a new instance
508 //
509 SoundRenderObjClass *render_obj = W3DNEW SoundRenderObjClass;
510 render_obj->Set_Name (Name);
511 render_obj->Set_Sound (&Definition);
512 render_obj->Set_Flags (Flags);
513
514 return render_obj;
515}
516
517
519//
520// Initialize
521//
523void
524SoundRenderObjDefClass::Initialize (SoundRenderObjClass &render_obj)
525{
526 //
527 // Copy the settings from the sound object into our definition
528 //
529 Definition.Initialize_From_Sound (render_obj.Peek_Sound ());
530
531 //
532 // Copy the flags from the render object
533 //
534 Flags = (SoundRenderObjClass::FLAGS)render_obj.Get_Flags ();
535
536 //
537 // Copy the name
538 //
539 Name = render_obj.Get_Name ();
540 return ;
541}
542
543
545//
546// Load
547//
550SoundRenderObjDefClass::Load_W3D (ChunkLoadClass &cload)
551{
553
554 //
555 // Attempt to read the different sections of the definition
556 //
557 if ((Read_Header (cload) == WW3D_ERROR_OK) &&
558 (Read_Definition (cload) == WW3D_ERROR_OK))
559 {
560 retval = WW3D_ERROR_OK;
561 }
562
563 return retval;
564}
565
566
568//
569// Save_W3D
570//
573SoundRenderObjDefClass::Save_W3D (ChunkSaveClass &csave)
574{
576
577 //
578 // Begin a chunk that identifies a sound render object
579 //
580 if (csave.Begin_Chunk (W3D_CHUNK_SOUNDROBJ) == TRUE) {
581
582 //
583 // Attempt to save the different sections of the aggregate definition
584 //
585 if ((Write_Header (csave) == WW3D_ERROR_OK) &&
586 (Write_Definition (csave) == WW3D_ERROR_OK))
587 {
588 retval = WW3D_ERROR_OK;
589 }
590
591 csave.End_Chunk ();
592 }
593
594 return retval;
595}
596
597
599//
600// Read_Header
601//
604SoundRenderObjDefClass::Read_Header (ChunkLoadClass &cload)
605{
607
608 //
609 // Is this the header chunk?
610 //
611 if (cload.Open_Chunk () &&
613 {
614 //
615 // Read the header from the chunk
616 //
617 W3dSoundRObjHeaderStruct header = { 0 };
618 if (cload.Read (&header, sizeof (header)) == sizeof (header)) {
619
620 //
621 // Copy the names from the header structure
622 //
623 Name = header.Name;
624 Version = header.Version;
625 Flags = (SoundRenderObjClass::FLAGS)header.Flags;
626 retval = WW3D_ERROR_OK;
627 }
628
629 cload.Close_Chunk ();
630 }
631
632 return retval;
633}
634
635
637//
638// Read_Definition
639//
642SoundRenderObjDefClass::Read_Definition (ChunkLoadClass &cload)
643{
645
646 //
647 // Is this the right chunk?
648 //
649 if (cload.Open_Chunk () &&
651 {
652 //
653 // Ask the definition to load its settings from the chunk
654 //
655 if (Definition.Load (cload)) {
656 retval = WW3D_ERROR_OK;
657 }
658
659 cload.Close_Chunk ();
660 }
661
662 return retval;
663}
664
665
667//
668// Write_Header
669//
672SoundRenderObjDefClass::Write_Header (ChunkSaveClass &csave)
673{
675
676 //
677 // Begin a chunk that identifies the aggregate
678 //
680
681 //
682 // Fill the header structure
683 //
684 W3dSoundRObjHeaderStruct header = { 0 };
686 header.Flags = Flags;
687 ::lstrcpyn (header.Name, (const char *)Name, sizeof (header.Name));
688 header.Name[sizeof (header.Name) - 1] = 0;
689
690 //
691 // Write the header out to the chunk
692 //
693 if (csave.Write (&header, sizeof (header)) == sizeof (header)) {
694 retval = WW3D_ERROR_OK;
695 }
696
697 // End the header chunk
698 csave.End_Chunk ();
699 }
700
701 return retval;
702}
703
704
706//
707// Write_Definition
708//
711SoundRenderObjDefClass::Write_Definition (ChunkSaveClass &csave)
712{
714
715 //
716 // Save the definition to its own chunk
717 //
719 if (Definition.Save (csave)) {
720 retval = WW3D_ERROR_OK;
721 }
722 csave.End_Chunk ();
723 }
724
725 return retval;
726}
727
728
729//*********************************************************************************
730//
731// Start of SoundRenderObjDefClass
732//
733//*********************************************************************************
734
735
737//
738// Load_W3D
739//
742SoundRenderObjLoaderClass::Load_W3D (ChunkLoadClass &cload)
743{
744 SoundRenderObjPrototypeClass *prototype = NULL;
745
746 //
747 // Create a definition object
748 //
749 SoundRenderObjDefClass *definition = W3DNEW SoundRenderObjDefClass;
750 if (definition != NULL) {
751
752 //
753 // Ask the definition object to load the sound data
754 //
755 if (definition->Load_W3D (cload) == WW3D_ERROR_OK) {
756
757 //
758 // Success! Create a prototype from the definition
759 //
760 prototype = W3DNEW SoundRenderObjPrototypeClass (definition);
761 }
762
763 REF_PTR_RELEASE (definition);
764 }
765
766 return prototype;
767}
768
769#endif // noWWAUDIO
770
771
#define NULL
Definition BaseType.h:92
#define TRUE
Definition BaseType.h:109
#define W3D_CURRENT_AGGREGATE_VERSION
Definition w3d_file.h:1962
#define W3D_CURRENT_SOUNDROBJ_VERSION
Definition w3d_file.h:2156
@ W3D_CHUNK_SOUNDROBJ_DEFINITION
Definition w3d_file.h:489
@ W3D_CHUNK_SOUNDROBJ
Definition w3d_file.h:487
@ W3D_CHUNK_SOUNDROBJ_HEADER
Definition w3d_file.h:488
#define W3DNEW
Definition always.h:109
unsigned long uint32
Definition bittype.h:46
@ false
Definition bool.h:59
virtual PersistClass * Create(void) const
virtual void Initialize_From_Sound(AudibleSoundClass *sound)
bool Close_Chunk()
Definition chunkio.cpp:448
uint32 Cur_Chunk_ID()
Definition chunkio.cpp:484
uint32 Read(void *buf, uint32 nbytes)
Definition chunkio.cpp:692
bool Open_Chunk()
Definition chunkio.cpp:412
uint32 Write(const void *buf, uint32 nbytes)
Definition chunkio.cpp:264
bool Begin_Chunk(uint32 id)
Definition chunkio.cpp:108
bool End_Chunk()
Definition chunkio.cpp:148
virtual void Set_Transform(const Matrix3D &m)
Definition rendobj.cpp:423
virtual void Set_Visible(int onoff)
Definition rendobj.h:465
virtual void Set_Position(const Vector3 &v)
Definition rendobj.cpp:444
virtual void Notify_Added(SceneClass *scene)
Definition rendobj.cpp:862
virtual void Set_Hidden(int onoff)
Definition rendobj.h:472
virtual void Set_Force_Visible(int onoff)
Definition rendobj.h:476
virtual void Notify_Removed(SceneClass *scene)
Definition rendobj.cpp:884
virtual void Set_Animation_Hidden(int onoff)
Definition rendobj.h:474
virtual void Register(RenderObjClass *obj, RegType for_what)=0
@ ON_FRAME_UPDATE
Definition scene.h:165
virtual void Unregister(RenderObjClass *obj, RegType for_what)=0
Version()
Definition version.cpp:36
else return(RetVal)
#define REF_PTR_RELEASE(x)
Definition refcount.h:80
char Name[W3D_NAME_LEN]
Definition w3d_file.h:2166
unsigned char flag
Definition vchannel.cpp:273
WW3DErrorType
Definition w3derr.h:51
@ WW3D_ERROR_LOAD_FAILED
Definition w3derr.h:54
@ WW3D_ERROR_OK
Definition w3derr.h:52
@ WW3D_ERROR_SAVE_FAILED
Definition w3derr.h:55