Skip to content

Commit

Permalink
Avoid loading same asset multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
ufrshubham committed Jun 30, 2024
1 parent 383ac7b commit dda8a09
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 8 deletions.
5 changes: 5 additions & 0 deletions include/AssetMan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,10 @@ namespace Engine
const sf::Font &GetFont(int id) const;
sf::Music &GetSoundTrack(int id);
const sf::SoundBuffer &GetSoundEffect(int id) const;

bool HasTexture(int id) const;
bool HasFont(int id) const;
bool HasSoundTrack(int id) const;
bool HasSoundEffect(int id) const;
};
} // namespace Engine
20 changes: 20 additions & 0 deletions src/AssetMan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,23 @@ const sf::SoundBuffer &Engine::AssetMan::GetSoundEffect(int id) const
{
return *(m_soundEffects.at(id).get());
}

bool Engine::AssetMan::HasTexture(int id) const
{
return m_textures.find(id) != m_textures.end();
}

bool Engine::AssetMan::HasFont(int id) const
{
return m_fonts.find(id) != m_fonts.end();
}

bool Engine::AssetMan::HasSoundTrack(int id) const
{
return m_soundTracks.find(id) != m_soundTracks.end();
}

bool Engine::AssetMan::HasSoundEffect(int id) const
{
return m_soundEffects.find(id) != m_soundEffects.end();
}
19 changes: 16 additions & 3 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,22 @@ Game::Game() : m_context(std::make_shared<Context>())

auto cwd = std::filesystem::current_path();
m_context->m_window->create(sf::VideoMode(640, 352), "Snake Game", sf::Style::Close);
m_context->m_assets->AddSoundTrack(MAIN_SOUND_TRACK, "assets/audio/Spinning out.ogg", true);
m_context->m_assets->AddSoundEffect(COIN_SFX, "assets/audio/Simple Coin 006.wav");
m_context->m_assets->AddSoundEffect(DEATH_SFX, "assets/audio/Death 002.wav");

if (!m_context->m_assets->HasSoundTrack(MAIN_SOUND_TRACK))
{
m_context->m_assets->AddSoundTrack(MAIN_SOUND_TRACK, "assets/audio/Spinning out.ogg", true);
}

if (!m_context->m_assets->HasSoundEffect(COIN_SFX))
{
m_context->m_assets->AddSoundEffect(COIN_SFX, "assets/audio/Simple Coin 006.wav");
}

if (!m_context->m_assets->HasSoundEffect(DEATH_SFX))
{
m_context->m_assets->AddSoundEffect(DEATH_SFX, "assets/audio/Death 002.wav");
}

m_context->m_states->Add(std::make_unique<MainMenu>(m_context));
}

Expand Down
23 changes: 19 additions & 4 deletions src/GamePlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,25 @@ GamePlay::~GamePlay()

void GamePlay::Init()
{
m_context->m_assets->AddTexture(GRASS, "assets/textures/grass.png", true);
m_context->m_assets->AddTexture(FOOD, "assets/textures/food.png");
m_context->m_assets->AddTexture(WALL, "assets/textures/wall.png", true);
m_context->m_assets->AddTexture(SNAKE, "assets/textures/snake.png");
if (!m_context->m_assets->HasTexture(GRASS))
{
m_context->m_assets->AddTexture(GRASS, "assets/textures/grass.png", true);
}

if (!m_context->m_assets->HasTexture(FOOD))
{
m_context->m_assets->AddTexture(FOOD, "assets/textures/food.png");
}

if (!m_context->m_assets->HasTexture(WALL))
{
m_context->m_assets->AddTexture(WALL, "assets/textures/wall.png", true);
}

if (!m_context->m_assets->HasTexture(SNAKE))
{
m_context->m_assets->AddTexture(SNAKE, "assets/textures/snake.png");
}

m_grass.setTexture(m_context->m_assets->GetTexture(GRASS));
m_grass.setTextureRect(m_context->m_window->getViewport(m_context->m_window->getDefaultView()));
Expand Down
5 changes: 4 additions & 1 deletion src/MainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ MainMenu::~MainMenu()

void MainMenu::Init()
{
m_context->m_assets->AddFont(MAIN_FONT, "assets/fonts/Pacifico-Regular.ttf");
if (!m_context->m_assets->HasFont(MAIN_FONT))
{
m_context->m_assets->AddFont(MAIN_FONT, "assets/fonts/Pacifico-Regular.ttf");
}

// Title
m_gameTitle.setFont(m_context->m_assets->GetFont(MAIN_FONT));
Expand Down

0 comments on commit dda8a09

Please sign in to comment.