Skip to content

Commit

Permalink
fix crash on scene change
Browse files Browse the repository at this point in the history
  • Loading branch information
goopey7 committed Jan 5, 2024
1 parent 0abcd9e commit da25f3a
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 22 deletions.
5 changes: 3 additions & 2 deletions components/PlayerInput.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
void PlayerInput::init()
{
CustomComponent::init();
goop::rm->loadSfx("res/blast.mp3");
goop::loadSfx("res/blast.mp3");
}

// Gets called every frame
Expand Down Expand Up @@ -63,7 +63,6 @@ void PlayerInput::update(float dt)

if (goop::isKeyPressed(ImGuiKey_F))
{
goop::playSfx("res/blast.mp3");
auto e = spawnEntity();
auto& tc = e.addComponent<goop::TransformComponent>();
tc.position = e.getScene()->getCurrentCamera()->getPosition() +
Expand All @@ -76,6 +75,8 @@ void PlayerInput::update(float dt)
auto& mc = e.addComponent<goop::MeshComponent>(goop::Box(), "res/texture.jpg", "box");
goop::rm->loadMesh(&mc);
goop::rm->loadTexture(&mc);

goop::playSfx("res/blast.mp3");
}
}

Expand Down
4 changes: 1 addition & 3 deletions goop/Sound.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
#include <goop/sys/Sfx.h>
namespace goop
{
static std::map<std::string, int> sfxMap;
static void loadSfx(const std::string& path)
{
goop::rm->loadSfx(path);
sfxMap[path] = sfxMap.size();
}

static void playSfx(const std::string& path)
{
goop::rm->playSfx(sfxMap[path]);
goop::rm->playSfx(path);
}
}
16 changes: 14 additions & 2 deletions goop/sys/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,11 @@ bool ResourceManager::unloadMesh(MeshComponent* mesh)
return true;
}

bool ResourceManager::loadSfx(const std::string& path) { return sfx->load(path); }
bool ResourceManager::loadSfx(const std::string& path)
{
loadedSfx[path] = sfx->load(path);
return true;
}

int ResourceManager::destroy()
{
Expand All @@ -112,7 +116,7 @@ int ResourceManager::destroy()
return 0;
}

void ResourceManager::playSfx(uint32_t id) const { sfx->playSfx(id); }
void ResourceManager::playSfx(const std::string& path) const { sfx->playSfx(loadedSfx.at(path)); }

bool ResourceManager::loadTexture(MeshComponent* mesh, const char* oldPath)
{
Expand Down Expand Up @@ -156,3 +160,11 @@ bool ResourceManager::unloadTexture(MeshComponent* mesh)
}
return true;
}

bool ResourceManager::unloadSfx(const std::string& path)
{
unloadedSfxSlots.push(loadedSfx[path]);
sfx->unload(path);
loadedSfx.erase(path);
return true;
}
6 changes: 4 additions & 2 deletions goop/sys/ResourceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ class ResourceManager : public Subsystem
bool unloadTexture(MeshComponent* mesh);

bool loadSfx(const std::string& path);

void playSfx(uint32_t id) const;
bool unloadSfx(const std::string& path);
void playSfx(const std::string& path) const;

const MeshLoader* getMeshLoader() const { return meshLoader.get(); }
MeshLoader* getMeshLoader() { return meshLoader.get(); }
Expand All @@ -37,6 +37,8 @@ class ResourceManager : public Subsystem
std::map<std::string, uint32_t> loadedMeshes;
std::map<std::string, uint32_t> numLoadedMeshes;
std::map<std::string, uint32_t> numLoadedTextures;
std::map<std::string, uint32_t> loadedSfx;
std::queue<uint32_t> unloadedSfxSlots;
};
} // namespace goop::sys
namespace goop
Expand Down
2 changes: 1 addition & 1 deletion goop/sys/platform/bullet/Physics_Bullet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void Physics_Bullet::addRigidBody(RigidbodyComponent* rbc, TransformComponent* t

void Physics_Bullet::removeRigidBody(RigidbodyComponent* rbc)
{
if (bIsInitialized)
if (bIsInitialized && rigidBodies.find(rbc) != rigidBodies.end())
{
dynamicsWorld->removeRigidBody(rigidBodies[rbc]);
delete rigidBodies[rbc]->getMotionState();
Expand Down
16 changes: 13 additions & 3 deletions goop/sys/platform/soloud/Sfx_SoLoud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,19 @@ int Sfx_SoLoud::destroy()

uint32_t Sfx_SoLoud::load(const std::string& path)
{
sfx.push_back(SoLoud::Wav());
sfx.back().load(path.c_str());
return sfx.size() - 1;
if (!unloadedSfxSlots.empty())
{
uint32_t id = unloadedSfxSlots.front();
unloadedSfxSlots.pop();
sfx[id].load(path.c_str());
return id;
}
else
{
uint32_t id = sfx.size();
sfx[id].load(path.c_str());
return id;
}
}

void Sfx_SoLoud::playSfx(uint32_t id) { engine.play(sfx[id]); }
Expand Down
4 changes: 3 additions & 1 deletion goop/sys/platform/soloud/Sfx_SoLoud.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <soloud.h>
#include <soloud_wav.h>
#include <vector>
#include <queue>

namespace goop::sys::platform::soloud
{
Expand All @@ -25,6 +26,7 @@ class Sfx_SoLoud : public Sfx

private:
SoLoud::Soloud engine;
std::vector<SoLoud::Wav> sfx;
std::map<uint32_t, SoLoud::Wav> sfx;
std::queue<uint32_t> unloadedSfxSlots;
};
} // namespace goop::sys::platform::soloud
16 changes: 8 additions & 8 deletions imgui.ini
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ DockId=0x00000006,0

[Window][Viewport]
Pos=262,27
Size=1731,1039
Size=1735,1073
Collapsed=0
DockId=0x00000009,0

[Window][Inspector]
Pos=1995,27
Size=249,1435
Pos=1999,27
Size=249,1469
Collapsed=0
DockId=0x00000004,0

Expand All @@ -27,7 +27,7 @@ Collapsed=0

[Window][Dockspace]
Pos=0,19
Size=2252,1451
Size=2256,1485
Collapsed=0

[Window][Example: Log]
Expand All @@ -54,18 +54,18 @@ Collapsed=0

[Window][Scene]
Pos=8,27
Size=252,1435
Size=252,1469
Collapsed=0
DockId=0x00000007,0

[Window][Scene Browser]
Pos=262,1068
Size=1731,394
Pos=262,1102
Size=1735,394
Collapsed=0
DockId=0x0000000A,0

[Docking][Data]
DockSpace ID=0x33675C32 Window=0x5B816B74 Pos=10,59 Size=2236,1435 Split=Y
DockSpace ID=0x33675C32 Window=0x5B816B74 Pos=8,27 Size=2240,1469 Split=Y
DockNode ID=0x00000001 Parent=0x33675C32 SizeRef=1954,445 Split=X Selected=0x13926F0B
DockNode ID=0x00000005 Parent=0x00000001 SizeRef=1806,941 Split=X Selected=0x13926F0B
DockNode ID=0x00000007 Parent=0x00000005 SizeRef=252,1435 Selected=0xE192E354
Expand Down

0 comments on commit da25f3a

Please sign in to comment.