-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d050f0
commit c76c559
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |