-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtexture.cpp
More file actions
59 lines (56 loc) · 1.97 KB
/
texture.cpp
File metadata and controls
59 lines (56 loc) · 1.97 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
#include "texture.h"
void addTexture(const std::string& id, const std::string& img, TextureHash& texhash)
{
unsigned int tex;
GLenum texture_format;
int nOfColors;
int width, height;
SDL_Surface *imgsurface = SDL_LoadBMP(img.c_str());
if ( (imgsurface->w & (imgsurface->w - 1)) != 0 ) {
std::cerr << "warning: image.bmp's width is not a power of 2" << std::endl;
}
if ( (imgsurface->h & (imgsurface->h - 1)) != 0 ) {
std::cerr << "warning: image.bmp's height is not a power of 2" << std::endl;
}
nOfColors = imgsurface->format->BytesPerPixel;
if (nOfColors == 4) // contains an alpha channel
{
if (imgsurface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
} else if (nOfColors == 3) // no alpha channel
{
if (imgsurface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
} else {
std::cerr << "warning: image loading may break" << std::endl;
}
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
glTexImage2D(GL_TEXTURE_2D, 0, nOfColors, imgsurface->w, imgsurface->h, 0,
texture_format, GL_UNSIGNED_BYTE, imgsurface->pixels );
texhash[id] = tex;
SDL_FreeSurface(imgsurface);
}
void deleteTexture(const std::string& id, TextureHash& texhash)
{
unsigned int tex = texhash[id];
glDeleteTextures(1, &tex);
texhash.erase(id);
}
void deleteTextures(TextureHash& texhash)
{
TextureHashIter iter;
for(iter = texhash.begin(); iter != texhash.end(); ++iter)
{
unsigned int tex = iter->second;
glDeleteTextures(1, &tex);
}
}