-
Notifications
You must be signed in to change notification settings - Fork 0
/
texturemanager.cpp
81 lines (73 loc) · 2.28 KB
/
texturemanager.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
70
71
72
73
74
75
76
77
78
79
80
#include "texture.h"
#include "texturemanager.h"
#include "tgaloader.h"
#include <map>
#include <string>
#include <stdio.h>
TextureManager* TextureManager::instance = 0;
bool TextureManager::textureexists(std::string name)
{
std::map<std::string,Texture*>::iterator itt = textures.begin();
return (textures.find(name) != textures.end());
}
bool TextureManager::loadtexture(std::string fname, std::string name)
{
if ( fname.length() == 0 || name.length() == 0 ) return false;
if ( textureexists(name) ) return false; // texture name has to be unique
// TODO handle file types other than targa
TGAFILE tgafile; // will scope out and delete the image data
if ( !loadtga(fname.c_str(), &tgafile) )
return false;
// create texture
textures[name] = new Texture(tgafile.imageData, tgafile.imageWidth, tgafile.imageHeight);
return true;
}
void TextureManager::cleanup()
{
std::map<std::string,Texture*>::iterator itt;
Texture* texture;
while ( !textures.empty() )
{
if ( itt == textures.end() )
printf("wtf is going on\n");
itt=textures.begin();
std::string key = itt->first;
printf("Deleting %s\n", key.c_str());
texture=itt->second;
delete texture;
texture=0;
textures.erase(itt);
}
}
TextureManager::TextureManager()
{
}
TextureManager::~TextureManager()
{
cleanup();
// set instance to zeeerooo
instance=0;
}
int TextureManager::gettex(std::string name)
{
if ( !textureexists(name) )
return 0;
return textures[name]->getid();
}
void TextureManager::removeTexture(std::map<std::string, Texture*>::iterator itt)
{
if ( itt != textures.end() ){
Texture* tex = itt->second;
printf("removeTexture: Deleting %s\n", itt->first.c_str());
delete tex;
tex=0;
textures.erase(itt);
}
}
void TextureManager::removeTexture(std::string name)
{
if ( textureexists(name) ) {
std::map<std::string, Texture*>::iterator itt = textures.find(name);
removeTexture(itt);
}
}