Skip to content

Commit

Permalink
fixed crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
denyskryvytskyi committed Mar 27, 2024
1 parent 03a62b2 commit 95933dd
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 48 deletions.
11 changes: 7 additions & 4 deletions Engine/src/Resources/AudioManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ void AudioManager::Init()

void AudioManager::Shutdown()
{
for (auto it : m_sounds) {
for (auto& it : m_sounds) {
if (it.second.sound) {
it.second.sound->stop();
it.second.sound->drop();
it.second.sound = nullptr;
it.second.soundSource = nullptr;
}
}

Expand Down Expand Up @@ -61,7 +64,7 @@ void AudioManager::Play(const std::string& name, bool looped)
}
} else {
// first play of the sound using preloaded sound source
it->second.sound = m_engine->play2D(it->second.soundSource, looped, false, true);
it->second.sound = m_engine->play2D(it->second.soundSource, looped, false, true, true);
}
} else {
EL_CORE_WARN("Audio play failed: {0} isn't exist.", name);
Expand All @@ -74,7 +77,7 @@ void AudioManager::Pause(const std::string& name)
if (it != m_sounds.end() && it->second.sound) {
it->second.sound->setIsPaused(true);
} else {
EL_CORE_WARN("Audio play failed: {0} isn't exist.", name);
EL_CORE_WARN("Audio pause failed: {0} isn't exist.", name);
}
}

Expand All @@ -86,7 +89,7 @@ void AudioManager::Stop(const std::string& name)
it->second.sound->drop();
it->second.sound = nullptr;
} else {
EL_CORE_WARN("Audio play failed: {0} isn't exist.", name);
EL_CORE_WARN("Audio stop failed: {0} isn't exist.", name);
}
}
std::vector<std::string> AudioManager::GetSounds() const
Expand Down
5 changes: 5 additions & 0 deletions Engine/src/Scene/Components/SceneComponents.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ void from_json(const nlohmann::json& j, TextComponent& t)
j.at("is_visible").get_to(t.isVisible);
}

SoundComponent::~SoundComponent()
{
Stop();
}

void SoundComponent::Play()
{
gAudioManager.Play(soundName, isLooped);
Expand Down
2 changes: 2 additions & 0 deletions Engine/src/Scene/Components/SceneComponents.h
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ struct TagComponent {

struct SoundComponent {

~SoundComponent();

void Play();
void Pause();
void Stop();
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Elven Engine is primarily a 2D/3D game engine that is being developed from scrat
- [ ] Shadows
+ [x] ECS (investigated different techniques and my particular architecture in the [article](https://denyskryvytskyi.github.io/ecs))
- [x] Data-oriented cache-friendly components system architecture
- [x] In-Engine components: Transform, Sprite, Text, Quad, StaticMesh, Camera, RectTransform, AABB, Tag
- [x] In-Engine components: Transform, Sprite, Text, Quad, StaticMesh, Camera, RectTransform, AABB, Tag, Sound
- [x] Components serialization
- [x] Behavior component as the entity brain
- [x] Optional Systems for usability
Expand Down
Binary file added Sandbox3D/assets/sounds/back.flac
Binary file not shown.
Binary file removed Sandbox3D/assets/sounds/back.mp3
Binary file not shown.
67 changes: 32 additions & 35 deletions Sandbox3D/src/MeshModelSandbox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,46 +23,44 @@ MeshModelSandbox::MeshModelSandbox()
auto& scene = elv::GetScene();

if (true) {
const auto knight = scene.CreateEntity();
m_models.emplace_back(knight);
scene.AddComponent<elv::TagComponent>(knight, "Knight");
scene.AddComponent<elv::TransformComponent>(knight, lia::vec3(3.0f, 0.0f, -2.0f), lia::vec3(0.01f));
scene.AddComponent<elv::StaticMeshComponent>(knight, "knight", fmt::format("{}{}", elv::fileSystem::MODELS_PATH, "dark_knight/scene.gltf"));
const auto knights = scene.CreateEntity();
scene.AddComponent<elv::TagComponent>(knights, "knights");
for (size_t i = 0; i < 8; i++) {
const auto knight = scene.CreateChildEntity(knights);
scene.AddComponent<elv::TagComponent>(knights, fmt::format("knight_{}", i));
scene.AddComponent<elv::TransformComponent>(knight, lia::vec3(3.0f + i, 0.0f, -2.0f), lia::vec3(0.01f));
scene.AddComponent<elv::StaticMeshComponent>(knight, "knight", fmt::format("{}{}", elv::fileSystem::MODELS_PATH, "dark_knight/scene.gltf"));
}
}

if (true) {
const auto walle = scene.CreateEntity();
m_models.emplace_back(walle);
scene.AddComponent<elv::TagComponent>(walle, "Walle");
scene.AddComponent<elv::TransformComponent>(walle, lia::vec3(0.0f, 0.0f, 0.0f), lia::vec3(1.0f));
scene.AddComponent<elv::StaticMeshComponent>(walle, "walle", fmt::format("{}{}", elv::fileSystem::MODELS_PATH, "walle/walle.obj"));
}

if (false) {
const auto backpack = scene.CreateEntity();
m_models.emplace_back(backpack);
scene.AddComponent<elv::TagComponent>(backpack, "Backpack");
scene.AddComponent<elv::TransformComponent>(backpack, lia::vec3(3.0f, 0.0f, 0.0f), lia::vec3(0.01f));
scene.AddComponent<elv::StaticMeshComponent>(backpack, "backpack", fmt::format("{}{}", elv::fileSystem::MODELS_PATH, "backpack_gltf/scene.gltf"));
}

if (false) {
const auto robot = scene.CreateEntity();
m_models.emplace_back(robot);
scene.AddComponent<elv::TagComponent>(robot, "Robot");
scene.AddComponent<elv::TransformComponent>(robot, lia::vec3(20.0f, 0.0f, 0.0f), lia::vec3(1.0f));
scene.AddComponent<elv::StaticMeshComponent>(robot, "robot", fmt::format("{}{}", elv::fileSystem::MODELS_PATH, "robot/robot.obj"));
}

const auto entity = scene.CreateEntity();
m_models.emplace_back(entity);
scene.AddComponent<elv::TagComponent>(entity, "Cerberus");
scene.AddComponent<elv::TransformComponent>(entity, lia::vec3(-2.0f, 2.0f, 0.0f), lia::vec3(0.01f));
auto& cerberusMesh = scene.AddComponent<elv::StaticMeshComponent>(entity, "cerberus", fmt::format("{}{}", elv::fileSystem::MODELS_PATH, "cerberus/cerberus.fbx"));
cerberusMesh.SetMaterialTexture(elv::Material::TextureSlot::Specular, "Cerberus_M.tga", fmt::format("{}{}", elv::fileSystem::MODELS_PATH, "cerberus/Textures"));

const auto sponza = scene.CreateEntity();
m_models.emplace_back(sponza);
scene.AddComponent<elv::TagComponent>(sponza, "Sponza");
scene.AddComponent<elv::TransformComponent>(sponza, lia::vec3(0.0f), lia::vec3(0.01f));
scene.AddComponent<elv::StaticMeshComponent>(sponza, "sponza", fmt::format("{}{}", elv::fileSystem::MODELS_PATH, "sponza/sponza.obj"));
Expand All @@ -73,7 +71,6 @@ MeshModelSandbox::MeshModelSandbox()
// sphere
const auto sphere = scene.CreateEntity();
scene.AddComponent<elv::TagComponent>(sphere, "Sphere");
m_primitives.emplace_back(sphere);

auto& transform = scene.AddComponent<elv::TransformComponent>(sphere);
transform.pos = lia::vec3(-2.0f, 0.5f, 0.0f);
Expand All @@ -86,31 +83,31 @@ MeshModelSandbox::MeshModelSandbox()
// sphereMesh.SetMaterialTexture(elv::Material::TextureSlot::Diffuse, "sphere.jpg", "assets/images");

// =================== LIGHT =======================
m_dirLightEntity = scene.CreateEntity();
scene.AddComponent<elv::TagComponent>(m_dirLightEntity, "Directional light");
auto& dirLightComponent = scene.AddComponent<elv::DirectionalLightComponent>(m_dirLightEntity);
auto dirLightEntity = scene.CreateEntity();
scene.AddComponent<elv::TagComponent>(dirLightEntity, "Directional light");
auto& dirLightComponent = scene.AddComponent<elv::DirectionalLightComponent>(dirLightEntity);
dirLightComponent.direction = lia::vec3(0.0f, -1.0f, 0.0f);
dirLightComponent.enabled = true;

/* m_flashLightEntity = scene.CreateEntity();
scene.AddComponent<elv::TagComponent>(m_flashLightEntity, "Spot light");
scene.AddComponent<elv::TransformComponent>(m_flashLightEntity);
auto& spotLightComponent = scene.AddComponent<elv::SpotLightComponent>(m_flashLightEntity);
spotLightComponent.enabled = false;
/* auto flashLightEntity = scene.CreateEntity();
scene.AddComponent<elv::TagComponent>(flashLightEntity, "Spot light");
scene.AddComponent<elv::TransformComponent>(flashLightEntity);
auto& spotLightComponent = scene.AddComponent<elv::SpotLightComponent>(flashLightEntity);
spotLightComponent.enabled = false;
for (size_t i = 0; i < kPointLightsAmount; ++i) {
m_pointLightEntities[i] = scene.CreateEntity();
scene.AddComponent<elv::TagComponent>(m_pointLightEntities[i], fmt::format("Point light {}", i));
scene.AddComponent<elv::TransformComponent>(m_pointLightEntities[i], kPointLightPositions[i]);
auto& pointLightComponent = scene.AddComponent<elv::PointLightComponent>(m_pointLightEntities[i]);
pointLightComponent.enabled = false;
}*/
for (size_t i = 0; i < kPointLightsAmount; ++i) {
auto pointLightEntity = scene.CreateEntity();
scene.AddComponent<elv::TagComponent>(pointLightEntity, fmt::format("Point light {}", i));
scene.AddComponent<elv::TransformComponent>(pointLightEntity, kPointLightPositions[i]);
auto& pointLightComponent = scene.AddComponent<elv::PointLightComponent>(pointLightEntity);
pointLightComponent.enabled = false;
}*/

// default environment
SetEnvironment(0);

// audio
elv::gAudioManager.AddSound("background music", "back.mp3");
elv::gAudioManager.AddSound("background music", "back.flac");
elv::gAudioManager.AddSound("test", "test.wav");
}

Expand Down Expand Up @@ -163,11 +160,13 @@ void MeshModelSandbox::SetEnvironment(const int envIndex)
elv::Renderer::SetClearColor(env.clearColor);

// directional
auto& dirLight = scene.GetComponent<elv::DirectionalLightComponent>(m_dirLightEntity);
dirLight.ambient = env.dirLight.ambient;
dirLight.diffuse = env.dirLight.diffuse;
dirLight.specular = env.dirLight.specular;
dirLight.direction = env.dirLight.direction;
auto& dirLights = scene.GetComponents<elv::DirectionalLightComponent>();
if (dirLights.size() > 0) {
dirLights[0].ambient = env.dirLight.ambient;
dirLights[0].diffuse = env.dirLight.diffuse;
dirLights[0].specular = env.dirLight.specular;
dirLights[0].direction = env.dirLight.direction;
}

// spotlights
for (auto& spotlight : scene.GetComponents<elv::SpotLightComponent>()) {
Expand All @@ -178,7 +177,7 @@ void MeshModelSandbox::SetEnvironment(const int envIndex)

// point lights
auto& pointLights = scene.GetComponents<elv::PointLightComponent>();
for (size_t i = 0; i < kPointLightsAmount; ++i) {
for (size_t i = 0; i < kPointLightsAmount && i < pointLights.size(); ++i) {
pointLights[i].ambient = env.pointLightColors[i] * 0.1f;
pointLights[i].diffuse = env.pointLightColors[i];
pointLights[i].specular = env.pointLightColors[i];
Expand All @@ -191,7 +190,6 @@ void MeshModelSandbox::SetupCubes()

auto mainCube = scene.CreateEntity();
scene.AddComponent<elv::TagComponent>(mainCube, "Cube");
m_primitives.emplace_back(mainCube);

auto& transform = scene.AddComponent<elv::TransformComponent>(mainCube);
transform.pos = lia::vec3(0.0f, 0.5f, 0.0f);
Expand All @@ -204,7 +202,6 @@ void MeshModelSandbox::SetupCubes()

auto cube = scene.CreateChildEntity(mainCube);
scene.AddComponent<elv::TagComponent>(cube, "Small cube");
m_primitives.emplace_back(cube);

auto& transform = scene.AddComponent<elv::TransformComponent>(cube, kCubePositions[i], lia::vec3(0.5f));
auto& childMesh = scene.AddComponent<elv::StaticMeshComponent>(cube, "cube");
Expand Down
8 changes: 0 additions & 8 deletions Sandbox3D/src/MeshModelSandbox.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,4 @@ class MeshModelSandbox : public elv::Application {

private:
elv::Timer m_timer;

std::vector<elv::ecs::Entity> m_primitives;

std::vector<elv::ecs::Entity> m_models;

elv::ecs::Entity m_dirLightEntity { elv::ecs::INVALID_COMPONENT_TYPE_ID };
elv::ecs::Entity m_flashLightEntity { elv::ecs::INVALID_COMPONENT_TYPE_ID };
elv::ecs::Entity m_pointLightEntities[kPointLightsAmount] { elv::ecs::INVALID_COMPONENT_TYPE_ID };
};

0 comments on commit 95933dd

Please sign in to comment.