Skip to content

Commit

Permalink
refactored assets into resources
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavo-tomas committed Jun 28, 2022
1 parent 7d050f0 commit c76c559
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 0 deletions.
28 changes: 28 additions & 0 deletions header/Resources.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#ifndef RESOURCES_H
#define RESOURCES_H

#define INCLUDE_SDL_IMAGE
#define INCLUDE_SDL_MIXER

#include "SDL_include.h"
#include "Game.h"
#include <unordered_map>

using namespace std;

class Resources {
public:
static SDL_Texture* GetImage(const char* file);
static Mix_Music* GetMusic(const char* file);
static Mix_Chunk* GetSound(const char* file);
static void ClearImages();
static void ClearMusics();
static void ClearSounds();

private:
static unordered_map<const char*, SDL_Texture*> imageTable;
static unordered_map<const char*, Mix_Music*> musicTable;
static unordered_map<const char*, Mix_Chunk*> soundTable;
};

#endif // RESOURCES_H
100 changes: 100 additions & 0 deletions src/Resources.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#include "../header/Resources.h"

// Create resources
unordered_map<const char*, SDL_Texture*> Resources::imageTable;
unordered_map<const char*, Mix_Music*> Resources::musicTable;
unordered_map<const char*, Mix_Chunk*> Resources::soundTable;

SDL_Texture* Resources::GetImage(const char* file)
{
// Check if texture is already loaded
if (imageTable.find(file) != imageTable.end())
return imageTable[file];

SDL_Texture* texture;
if ((texture = IMG_LoadTexture(Game::GetInstance().GetRenderer(), file)) == nullptr)
{
cout << "Error loading texture " << file << endl;
cout << SDL_GetError() << endl;
exit(1);
}

else
{
imageTable.insert(make_pair(file, texture));
// imageTable[file] = texture; // @TODO: possible solution?
cout << "\nTexture " << file << " loaded successfully!" << endl;
return imageTable[file];
}
}

Mix_Music* Resources::GetMusic(const char* file)
{
// Check if music is already loaded
if (musicTable.find(file) != musicTable.end())
return musicTable[file];

Mix_Music* music;
if ((music = Mix_LoadMUS(file)) == nullptr)
{
cout << "Error loading music " << file << endl;
cout << SDL_GetError() << endl;
exit(1);
}

else
{
musicTable.insert(make_pair(file, music));
cout << "\nMusic " << file << " loaded successfully!" << endl;
return musicTable[file];
}
}

Mix_Chunk* Resources::GetSound(const char* file)
{
// Check if sound is already loaded
if (soundTable.find(file) != soundTable.end())
return soundTable[file];

Mix_Chunk* chunk;
if ((chunk = Mix_LoadWAV(file)) == nullptr)
{
cout << "Error loading sound" << endl;
cout << SDL_GetError() << endl;
exit(1);
}

else
{
soundTable.insert(make_pair(file, chunk));
cout << "\nSound " << file << " loaded successfully!" << endl;
return soundTable[file];
}
}

void Resources::ClearImages()
{
for (auto i : imageTable)
SDL_DestroyTexture(i.second);

imageTable.clear();
cout << "Textures destroyed successfully!" << endl;
}

void Resources::ClearMusics()
{
for (auto m : musicTable)
Mix_FreeMusic(m.second);

musicTable.clear();
cout << "Musics freed successfully!" << endl;
}

void Resources::ClearSounds()
{
for (auto c : soundTable)
Mix_FreeChunk(c.second);

soundTable.clear();
cout << "Sounds freed successfully!" << endl;
}

0 comments on commit c76c559

Please sign in to comment.