forked from MetaCipher/the-sdl-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureBank.cpp
69 lines (51 loc) · 1.96 KB
/
TextureBank.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//=============================================================================
#include "TextureBank.h"
#include "App.h"
#include "FileManager.h"
#include "Log.h"
//=============================================================================
std::map<std::string, Texture*> TextureBank::TexList;
//=============================================================================
bool TextureBank::Init() {
Cleanup();
SDL_Renderer* Renderer = App::GetInstance()->GetRenderer();
if(!Renderer) return false;
std::vector<std::string> Files = FileManager::GetFilesInFolder("Textures"); // Relative to CWD
for(auto Filename : Files) {
std::string Ext = FileManager::GetFilenameExt(Filename);
std::string ID = FileManager::GetFilenameWithoutExt(Filename);
// Skip all non-png files
if(Ext != "png") continue;
//Log("Add Texture : ID = %s : Filename = %s : Ext = %s", ID.c_str(), Filename.c_str(), Ext.c_str());
AddTexture(Renderer, ID, Filename);
}
return true;
}
//-----------------------------------------------------------------------------
void TextureBank::Cleanup() {
if(TexList.size() <= 0) return;
for(auto& Iterator : TexList) {
Texture* TheTexture = (Texture*)Iterator.second;
if(TheTexture) {
delete TheTexture;
TheTexture = NULL;
}
}
TexList.clear();
}
//=============================================================================
void TextureBank::AddTexture(SDL_Renderer* Renderer, std::string ID, std::string Filename) {
if(ID == "") return;
Texture* NewTexture = new Texture();
if(NewTexture->Load(Renderer, Filename) == false) {
Log("Unable to Load Texture: %s", ID.c_str());
return;
}
TexList[ID] = NewTexture;
}
//-----------------------------------------------------------------------------
Texture* TextureBank::Get(std::string ID) {
if(TexList.find(ID) == TexList.end()) return 0;
return TexList[ID];
}
//=============================================================================