-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTexture.cpp
More file actions
111 lines (83 loc) · 2.25 KB
/
Texture.cpp
File metadata and controls
111 lines (83 loc) · 2.25 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include "Texture.h"
void gen_texture( GLuint handle, SDL_Surface* surface )
{
glBindTexture( GL_TEXTURE_2D, handle );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, surface->w, surface->h, 0,
GL_BGRA, GL_UNSIGNED_BYTE, surface->pixels );
}
Texture::Registry Texture::registery;
Texture::Ref Texture::get_ref()
{
for( size_t i=0; i < registery.size(); i++ )
if( registery[i].key == key )
return i;
return -1;
}
Texture::Texture()
{
ok = false;
}
Texture::Texture( const std::string& filename )
{
key = filename;
Ref ref = get_ref();
ok = true;
if( ref != -1u )
registery[ ref ].refCount++;
else
load( filename );
}
bool Texture::load( const std::string& filaname )
{
Ref ref; // Reference to the Item we'll be working on.
// We only need to reset if we're referencing a texture.
if( key.size() ) {
reset();
}
key = filaname;
ref = get_ref();
// If this Item already exists, and is loaded, we're all set.
if( ref != -1u && ref < registery.size() && registery[ref].refCount++ != 0 )
return true;
// Otherwise, make it.
registery.push_back( Item(key,0,1) );
ref = registery.size() - 1;
glGenTextures( 1, ®istery[ref].glHandle );
// Use SDL to lead the image for simplicity.
SDL_Surface* sdlSurface = SDL_LoadBMP( filaname.c_str() );
if( sdlSurface )
{
gen_texture( registery[ref].glHandle, sdlSurface );
SDL_FreeSurface( sdlSurface );
ok = true;
}
else
{
ok = false;
}
return ok;
}
void Texture::reset()
{
if( ! ok )
return;
Ref ref = get_ref();
if( ref != -1u && --registery[ ref ].refCount <= 0 ) {
glDeleteTextures( 1, ®istery[ ref ].glHandle );
}
ok = false;
}
Texture::~Texture()
{
reset();
}
GLuint Texture::handle()
{
Ref ref = get_ref();
if( ref != -1u )
return registery[ ref ].glHandle;
else
return 0;
}