forked from Alfwich/CubicEngine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GLTextureContainer.cpp
69 lines (53 loc) · 1.86 KB
/
GLTextureContainer.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 "GLTextureContainer.h"
std::unordered_map<std::string,Texture*> GLTextureContainer::GLTextures;
bool isPng( std::string path )
{
int length = path.size();
if( path[length-3] == 'p' && path[length-2] == 'n' && path[length-1] == 'g' )
return true;
return false;
}
Texture* GLTextureContainer::addTexture( std::string path, std::string name )
{
if( hasTexture( name ) )
return GLTextures[name];
SDL_Surface* tempSurface;
tempSurface = IMG_Load( path.c_str() );
if( !tempSurface )
return NULL;
GLTextures[name] = new Texture;
glGenTextures( 1, &GLTextures[name]->textureID );
glBindTexture( GL_TEXTURE_2D, GLTextures[name]->textureID );
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
if( isPng( path ) ) // Shitty method of determining alpha images
glTexImage2D( GL_TEXTURE_2D, 0, 4, tempSurface->w, tempSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tempSurface->pixels );
else
glTexImage2D( GL_TEXTURE_2D, 0, 3, tempSurface->w, tempSurface->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tempSurface->pixels );
GLTextures[name]->textureSize.w = tempSurface->w;
GLTextures[name]->textureSize.h = tempSurface->h;
SDL_FreeSurface( tempSurface );
return GLTextures[name];
}
void GLTextureContainer::clear()
{
for( auto iter = GLTextures.begin(); iter != GLTextures.end(); iter++ )
{
glDeleteTextures( 1, &iter->second->textureID );
}
GLTextures.clear();
}
Texture* GLTextureContainer::getTexture( std::string name )
{
if( GLTextures.find( name ) == GLTextures.end() )
return NULL;
return GLTextures[name];
}
bool GLTextureContainer::hasTexture( std::string name )
{
if( GLTextures.find( name ) == GLTextures.end() )
return false;
return true;
}