Skip to content
This repository was archived by the owner on Jan 5, 2024. It is now read-only.

Commit 9617058

Browse files
committed
Fixed a crash that could happen during door MaterialDrawOverride
Also renamed MaterialDrawOverride and cleaned up a bunch of stuff in Door.cpp while I was here: Swapped soundcontainers to unique ptrs, cleaned up some logic, onelined some things, added some missing static_casts
1 parent aafa453 commit 9617058

File tree

3 files changed

+49
-63
lines changed

3 files changed

+49
-63
lines changed

Entities/ADoor.cpp

Lines changed: 31 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ namespace RTE {
7979
m_DrawMaterialLayerWhenClosed = reference.m_DrawMaterialLayerWhenClosed;
8080
m_DoorMaterialID = reference.m_DoorMaterialID;
8181
m_DoorMaterialTempErased = reference.m_DoorMaterialTempErased;
82-
if (reference.m_DoorMoveStartSound) { m_DoorMoveStartSound = dynamic_cast<SoundContainer*>(reference.m_DoorMoveStartSound->Clone()); }
83-
if (reference.m_DoorMoveSound) { m_DoorMoveSound = dynamic_cast<SoundContainer*>(reference.m_DoorMoveSound->Clone()); }
84-
if (reference.m_DoorDirectionChangeSound) { m_DoorDirectionChangeSound = dynamic_cast<SoundContainer*>(reference.m_DoorDirectionChangeSound->Clone()); }
85-
if (reference.m_DoorMoveEndSound) { m_DoorMoveEndSound = dynamic_cast<SoundContainer*>(reference.m_DoorMoveEndSound->Clone()); }
82+
if (reference.m_DoorMoveStartSound) { m_DoorMoveStartSound.reset(dynamic_cast<SoundContainer*>(reference.m_DoorMoveStartSound->Clone())); }
83+
if (reference.m_DoorMoveSound) { m_DoorMoveSound.reset(dynamic_cast<SoundContainer*>(reference.m_DoorMoveSound->Clone())); }
84+
if (reference.m_DoorDirectionChangeSound) { m_DoorDirectionChangeSound.reset(dynamic_cast<SoundContainer*>(reference.m_DoorDirectionChangeSound->Clone())); }
85+
if (reference.m_DoorMoveEndSound) { m_DoorMoveEndSound.reset(dynamic_cast<SoundContainer*>(reference.m_DoorMoveEndSound->Clone())); }
8686

8787
return 0;
8888
}
@@ -131,17 +131,13 @@ namespace RTE {
131131
} else if (propName == "DrawMaterialLayerWhenClosed") {
132132
reader >> m_DrawMaterialLayerWhenClosed;
133133
} else if (propName == "DoorMoveStartSound") {
134-
m_DoorMoveStartSound = new SoundContainer;
135-
reader >> m_DoorMoveStartSound;
134+
m_DoorMoveStartSound.reset(dynamic_cast<SoundContainer *>(g_PresetMan.ReadReflectedPreset(reader)));
136135
} else if (propName == "DoorMoveSound") {
137-
m_DoorMoveSound = new SoundContainer;
138-
reader >> m_DoorMoveSound;
136+
m_DoorMoveSound.reset(dynamic_cast<SoundContainer *>(g_PresetMan.ReadReflectedPreset(reader)));
139137
} else if (propName == "DoorDirectionChangeSound") {
140-
m_DoorDirectionChangeSound = new SoundContainer;
141-
reader >> m_DoorDirectionChangeSound;
138+
m_DoorDirectionChangeSound.reset(dynamic_cast<SoundContainer *>(g_PresetMan.ReadReflectedPreset(reader)));
142139
} else if (propName == "DoorMoveEndSound") {
143-
m_DoorMoveEndSound = new SoundContainer;
144-
reader >> m_DoorMoveEndSound;
140+
m_DoorMoveEndSound.reset(dynamic_cast<SoundContainer *>(g_PresetMan.ReadReflectedPreset(reader)));
145141
} else {
146142
return Actor::ReadProperty(propName, reader);
147143
}
@@ -180,13 +176,13 @@ namespace RTE {
180176
writer.NewProperty("DrawMaterialLayerWhenClosed");
181177
writer << m_DrawMaterialLayerWhenClosed;
182178
writer.NewProperty("DoorMoveStartSound");
183-
writer << m_DoorMoveStartSound;
179+
writer << m_DoorMoveStartSound.get();
184180
writer.NewProperty("DoorMoveSound");
185-
writer << m_DoorMoveSound;
181+
writer << m_DoorMoveSound.get();
186182
writer.NewProperty("DoorDirectionChangeSound");
187-
writer << m_DoorDirectionChangeSound;
183+
writer << m_DoorDirectionChangeSound.get();
188184
writer.NewProperty("DoorMoveEndSound");
189-
writer << m_DoorMoveEndSound;
185+
writer << m_DoorMoveEndSound.get();
190186

191187
return 0;
192188
}
@@ -200,11 +196,6 @@ namespace RTE {
200196
if (m_DoorMoveEndSound) { m_DoorMoveEndSound->Stop(); }
201197
if (!notInherited) { Actor::Destroy(); }
202198

203-
delete m_DoorMoveStartSound;
204-
delete m_DoorMoveSound;
205-
delete m_DoorDirectionChangeSound;
206-
delete m_DoorMoveEndSound;
207-
208199
for (ADSensor &sensor : m_Sensors) {
209200
sensor.Destroy();
210201
}
@@ -249,8 +240,8 @@ namespace RTE {
249240

250241
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
251242

252-
bool ADoor::EraseDoorMaterial(bool updateMaterialArea, bool keepMaterialDrawnFlag) {
253-
if (!keepMaterialDrawnFlag) { m_DoorMaterialDrawn = false; }
243+
bool ADoor::EraseDoorMaterial(bool updateMaterialArea) {
244+
m_DoorMaterialDrawn = false;
254245

255246
if (!g_SceneMan.GetTerrain() || !g_SceneMan.GetTerrain()->GetMaterialBitmap()) {
256247
return false;
@@ -270,22 +261,21 @@ namespace RTE {
270261

271262
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
272263

273-
void ADoor::MaterialDrawOverride(bool enable) {
264+
void ADoor::TempEraseOrRedrawDoorMaterial(bool erase) {
274265
if (!g_SceneMan.GetTerrain() || !g_SceneMan.GetTerrain()->GetMaterialBitmap()) {
275266
return;
276267
}
277268

278-
if (enable) {
279-
// Erase the material temporarily if we have drawn it and the override isn't already in effect
280-
if (m_DoorMaterialDrawn && m_DoorMaterialTempErased != enable) { EraseDoorMaterial(true, true); }
281-
} else {
282-
// Draw the door back if we were indeed temporarily suppressing it before
283-
if (m_DoorMaterialDrawn && m_DoorMaterialTempErased != enable) {
284-
m_Door->Draw(g_SceneMan.GetTerrain()->GetMaterialBitmap(), Vector(), g_DrawDoor, true);
285-
g_SceneMan.GetTerrain()->AddUpdatedMaterialArea(m_Door->GetBoundingBox());
286-
}
269+
bool doorMaterialDrawnState = m_DoorMaterialDrawn;
270+
if (erase && m_DoorMaterialDrawn && !m_DoorMaterialTempErased) {
271+
EraseDoorMaterial(true);
272+
m_DoorMaterialDrawn = doorMaterialDrawnState;
273+
} else if (!erase && m_DoorMaterialDrawn && m_DoorMaterialTempErased) {
274+
DrawDoorMaterial(true);
275+
m_DoorMaterialDrawn = doorMaterialDrawnState;
287276
}
288-
m_DoorMaterialTempErased = enable;
277+
278+
m_DoorMaterialTempErased = erase;
289279
}
290280

291281
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@@ -370,7 +360,6 @@ namespace RTE {
370360
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
371361

372362
void ADoor::Update() {
373-
374363
if (m_Door) {
375364
if (m_DoorState != STOPPED && m_SensorTimer.IsPastSimMS(m_SensorInterval)) { UpdateSensors(); }
376365
UpdateDoorAttachableActions();
@@ -383,10 +372,8 @@ namespace RTE {
383372

384373
Actor::Update();
385374

386-
if (!m_Door) {
387-
// Start the spinning out of control animation for the motor, start it slow
388-
m_SpriteAnimDuration *= 4;
389-
}
375+
// Start the spinning out of control animation for the motor, start it slow
376+
if (!m_Door) { m_SpriteAnimDuration *= 4; }
390377

391378
if (m_SpriteAnimMode == LOOPWHENOPENCLOSE && m_FrameCount > 1 && (m_DoorState == OPENING || m_DoorState == CLOSING) && m_SpriteAnimTimer.IsPastSimMS(m_SpriteAnimDuration)) {
392379
m_Frame = (m_Frame + 1) % m_FrameCount;
@@ -396,11 +383,11 @@ namespace RTE {
396383
// Lose health when door is lost, spinning out of control until grinds to halt
397384
if (!m_Door && m_Status != DYING && m_Status != DEAD) {
398385
m_SpriteAnimMode = ALWAYSLOOP;
399-
m_SpriteAnimDuration = LERP(0, m_MaxHealth, 10, m_InitialSpriteAnimDuration, m_Health);
386+
m_SpriteAnimDuration = static_cast<int>(LERP(0, m_MaxHealth, 10.0F, static_cast<float>(m_InitialSpriteAnimDuration), m_Health));
400387

401388
if (m_DoorMoveSound) {
402389
if (!m_DoorMoveSound->IsBeingPlayed()) { m_DoorMoveSound->Play(m_Pos); }
403-
m_DoorMoveSound->SetPitch(LERP(10, m_InitialSpriteAnimDuration, 2, 1, m_SpriteAnimDuration));
390+
m_DoorMoveSound->SetPitch(LERP(10.0F, static_cast<float>(m_InitialSpriteAnimDuration), 2.0F, 1.0F, static_cast<float>(m_SpriteAnimDuration)));
404391
}
405392

406393
m_Health -= 0.4F;
@@ -411,7 +398,7 @@ namespace RTE {
411398
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
412399

413400
void ADoor::UpdateSensors() {
414-
const Actor *foundActor = 0;
401+
const Actor *foundActor = nullptr;
415402
bool anySensorInput = false;
416403

417404
for (ADSensor &sensor : m_Sensors) {
@@ -422,7 +409,7 @@ namespace RTE {
422409
if (m_Team == Activity::NoTeam) {
423410
OpenDoor();
424411
break;
425-
// If a sensor has found an enemy Actor, close the door and break so we don't accidentally open it for a friendly Actor.
412+
// If a sensor has found an enemy Actor, close the door and stop looking, so we don't accidentally open it for a friendly Actor.
426413
} else if (foundActor->GetTeam() != m_Team) {
427414
CloseDoor();
428415
break;
@@ -500,7 +487,7 @@ namespace RTE {
500487
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
501488

502489
void ADoor::DrawHUD(BITMAP *targetBitmap, const Vector &targetPos, int whichScreen, bool playerControlled) {
503-
m_HUDStack = -m_CharHeight / 2;
490+
m_HUDStack = -static_cast<int>(m_CharHeight) / 2;
504491

505492
if (!m_HUDVisible) {
506493
return;

Entities/ADoor.h

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -101,49 +101,49 @@ namespace RTE {
101101
/// Gets this ADoor's door move start sound. Ownership is NOT transferred!
102102
/// </summary>
103103
/// <returns>The SoundContainer for this ADoor's door move start sound.</returns>
104-
SoundContainer * GetDoorMoveStartSound() const { return m_DoorMoveStartSound; }
104+
SoundContainer * GetDoorMoveStartSound() const { return m_DoorMoveStartSound.get(); }
105105

106106
/// <summary>
107107
/// Sets this ADoor's door move start sound. Ownership IS transferred!
108108
/// </summary>
109109
/// <param name="newSound">The new SoundContainer for this ADoor's door move start sound.</param>
110-
void SetDoorMoveStartSound(SoundContainer *newSound) { m_DoorMoveStartSound = newSound; }
110+
void SetDoorMoveStartSound(SoundContainer *newSound) { m_DoorMoveStartSound.reset(newSound); }
111111

112112
/// <summary>
113113
/// Gets this ADoor's door move sound. Ownership is NOT transferred!
114114
/// </summary>
115115
/// <returns>The SoundContainer for this ADoor's door move sound.</returns>
116-
SoundContainer * GetDoorMoveSound() const { return m_DoorMoveSound; }
116+
SoundContainer * GetDoorMoveSound() const { return m_DoorMoveSound.get(); }
117117

118118
/// <summary>
119119
/// Sets this ADoor's door move sound. Ownership IS transferred!
120120
/// </summary>
121121
/// <param name="newSound">The new SoundContainer for this ADoor's door move sound.</param>
122-
void SetDoorMoveSound(SoundContainer *newSound) { m_DoorMoveSound = newSound; }
122+
void SetDoorMoveSound(SoundContainer *newSound) { m_DoorMoveSound.reset(newSound); }
123123

124124
/// <summary>
125125
/// Gets this ADoor's door direction change sound. Ownership is NOT transferred!
126126
/// </summary>
127127
/// <returns>The SoundContainer for this ADoor's door direction change sound.</returns>
128-
SoundContainer * GetDoorDirectionChangeSound() const { return m_DoorDirectionChangeSound; }
128+
SoundContainer * GetDoorDirectionChangeSound() const { return m_DoorDirectionChangeSound.get(); }
129129

130130
/// <summary>
131131
/// Sets this ADoor's door direction change sound. Ownership IS transferred!
132132
/// </summary>
133133
/// <param name="newSound">The new SoundContainer for this ADoor's door direction change sound.</param>
134-
void SetDoorDirectionChangeSound(SoundContainer *newSound) { m_DoorDirectionChangeSound = newSound; }
134+
void SetDoorDirectionChangeSound(SoundContainer *newSound) { m_DoorDirectionChangeSound.reset(newSound); }
135135

136136
/// <summary>
137137
/// Gets this ADoor's door move end sound. Ownership is NOT transferred!
138138
/// </summary>
139139
/// <returns>The SoundContainer for this ADoor's door move end sound.</returns>
140-
SoundContainer * GetDoorMoveEndSound() const { return m_DoorMoveEndSound; }
140+
SoundContainer * GetDoorMoveEndSound() const { return m_DoorMoveEndSound.get(); }
141141

142142
/// <summary>
143143
/// Sets this ADoor's door move end sound. Ownership IS transferred!
144144
/// </summary>
145145
/// <param name="newSound">The new SoundContainer for this ADoor's door move end sound.</param>
146-
void SetDoorMoveEndSound(SoundContainer *newSound) { m_DoorMoveEndSound = newSound; }
146+
void SetDoorMoveEndSound(SoundContainer *newSound) { m_DoorMoveEndSound.reset(newSound); }
147147
#pragma endregion
148148

149149
#pragma region Concrete Methods
@@ -163,10 +163,10 @@ namespace RTE {
163163
void StopDoor();
164164

165165
/// <summary>
166-
/// Used to temporarily remove or add back the material drawing of this in the scene. Used for making pathfinding work through doors.
166+
/// Used to temporarily remove or add back the material drawing of this in the Scene. Used for making pathfinding work through doors.
167167
/// </summary>
168-
/// <param name="enable">Whether to enable the override or not.</param>
169-
void MaterialDrawOverride(bool enable);
168+
/// <param name="erase">Whether to erase door material (true) or draw it (false).</param>
169+
void TempEraseOrRedrawDoorMaterial(bool erase);
170170
#pragma endregion
171171

172172
#pragma region Virtual Override Methods
@@ -235,10 +235,10 @@ namespace RTE {
235235
bool m_DoorMaterialTempErased; //!< Whether the drawing override is enabled and the door material is erased to allow better pathfinding.
236236
Vector m_LastDoorMaterialPos; //!< The position the door attachable had when its material was drawn to the material bitmap. This is used to erase the previous material representation.
237237

238-
SoundContainer *m_DoorMoveStartSound; //!< Sound played when the door starts moving from fully open/closed position towards the opposite end.
239-
SoundContainer *m_DoorMoveSound; //!< Sound played while the door is moving between open/closed position.
240-
SoundContainer *m_DoorDirectionChangeSound; //!< Sound played when the door is interrupted while moving and changes directions.
241-
SoundContainer *m_DoorMoveEndSound; //!< Sound played when the door stops moving and is at fully open/closed position.
238+
std::unique_ptr<SoundContainer> m_DoorMoveStartSound; //!< Sound played when the door starts moving from fully open/closed position towards the opposite end.
239+
std::unique_ptr<SoundContainer> m_DoorMoveSound; //!< Sound played while the door is moving between open/closed position.
240+
std::unique_ptr<SoundContainer> m_DoorDirectionChangeSound; //!< Sound played when the door is interrupted while moving and changes directions.
241+
std::unique_ptr<SoundContainer> m_DoorMoveEndSound; //!< Sound played when the door stops moving and is at fully open/closed position.
242242

243243
private:
244244

@@ -270,9 +270,8 @@ namespace RTE {
270270
/// This is to get rid of the material footprint made with DrawDoorMaterial when the door part starts to move.
271271
/// </summary>
272272
/// <param name="updateMaterialArea">Whether to update the MaterialArea after erasing or not. Used for DrawDoorMaterial().</param>
273-
/// <param name="keepMaterialDrawnFlag">Whether to keep the DoorMaterialDrawn flag or not. Used for MaterialDrawOverride().</param>
274273
/// <returns>Whether the fill erasure was successful (if the same material as the door was found and erased).</returns>
275-
bool EraseDoorMaterial(bool updateMaterialArea = true, bool keepMaterialDrawnFlag = false);
274+
bool EraseDoorMaterial(bool updateMaterialArea = true);
276275

277276
/// <summary>
278277
/// Clears all the member variables of this ADoor, effectively resetting the members of this abstraction level only.

Managers/MovableMan.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,7 +1550,7 @@ void MovableMan::OverrideMaterialDoors(bool enable, int team)
15501550
{
15511551
// Update first so the door attachable piece is in the right position and doesn't take out a werid chunk of the terrain
15521552
pDoor->Update();
1553-
pDoor->MaterialDrawOverride(enable);
1553+
pDoor->TempEraseOrRedrawDoorMaterial(enable);
15541554
}
15551555
}
15561556
// Also check all doors added this frame
@@ -1561,7 +1561,7 @@ void MovableMan::OverrideMaterialDoors(bool enable, int team)
15611561
{
15621562
// Update first so the door attachable piece is in the right position and doesn't take out a werid chunk of the terrain
15631563
pDoor->Update();
1564-
pDoor->MaterialDrawOverride(enable);
1564+
pDoor->TempEraseOrRedrawDoorMaterial(enable);
15651565
}
15661566
}
15671567
}

0 commit comments

Comments
 (0)