-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureManager.d
73 lines (59 loc) · 1.89 KB
/
TextureManager.d
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
module TextureManager;
import std.string : toStringz;
import std.stdio : writefln;
import derelict.opengl.gl;
import derelict.opengl.extfuncs : glGenerateMipmap;
import derelict.devil.il;
import IManagers;
class TextureManager : IManager {
uint texture;
uint font;
bool init() {
DerelictIL.load();
ilInit();
ilEnable(IL_ORIGIN_SET);
ilOriginFunc(IL_ORIGIN_UPPER_LEFT);
font = loadImage("data\\font.gif", true);
texture = loadImage("data\\terrain.png");
return true;
}
void enableFont() {
glBindTexture(GL_TEXTURE_2D, font);
}
void disableFont() {
glBindTexture(GL_TEXTURE_2D, texture);
}
bool update(float delta) {
return true;
}
void cleanup() {
}
// Function load a image, turn it into a texture, and return the texture ID as a GLuint for use
GLuint loadImage(const char* theFileName, bool containsAlpha = false)
{
ILuint imageID;
GLuint textureID;
ilGenImages(1, &imageID);
ilBindImage(imageID);
if (ilLoadImage(theFileName)) {
if (!ilConvertImage(containsAlpha ? IL_RGBA : IL_RGB, IL_UNSIGNED_BYTE)) {
writefln("Image conversion failed - IL reports error: ", ilGetError());
return 0;
}
glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT),
0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
} else {
writefln("Image load failed - IL reports error: %d", ilGetError());
return 0;
}
ilDeleteImages(1, &imageID);
return textureID;
}
}