forked from rtpHarry/Sokoban
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextureCache.cpp
85 lines (68 loc) · 1.77 KB
/
TextureCache.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
81
82
83
84
85
#include "TextureCache.h"
#include <il/il.h>
#include <il/ilut.h>
TextureCache::TextureCache(void)
{
}
TextureCache::~TextureCache(void)
{
}
// Add a new sample into the cache
// Note: Check return value, if its AL_NONE then sample has not been cached
TextureID TextureCache::LoadTexture(string FileName)
{
// check for file in loaded sounds list
vector<TextureCacheObject>::iterator ii;
for(ii=m_TextureCache.begin(); ii!=m_TextureCache.end(); ii++)
{
// check if sample matches
if ( ii->FileName == FileName )
{
// found: +1 to its references, return its OpenALSoundID
ii->References++;
return ii->OpenGLTextureID;
}
}
// not found:
TextureCacheObject NewTexture;
// load sample in OpenGL
NewTexture.OpenGLTextureID = ilutGLLoadImage((ILstring)FileName.c_str());
// updates its references
NewTexture.References = 1;
// set the filename
NewTexture.FileName = FileName;
// add it to the cache list
m_TextureCache.push_back(NewTexture);
// return its TextureID
return NewTexture.OpenGLTextureID;
}
// Unload a texture from the system
unsigned int TextureCache::FreeTexture(TextureID TexID)
{
// check for file in loaded textures cache
vector<TextureCacheObject>::iterator ii;
for(ii=m_TextureCache.begin(); ii!=m_TextureCache.end(); ii++)
{
// check if texture ID matches
if ( ii->OpenGLTextureID == TexID )
{
// found: -1 to its references
if(--ii->References)
{
// still has references (in use) so just return number of references
return ii->OpenGLTextureID;
}
// no more references left so unload from graphics card
else
{
// remove from graphics card
glDeleteTextures(1, &ii->OpenGLTextureID);
// remove from cache list
m_TextureCache.erase(ii);
return false;
}
}
}
// not found
return false;
}