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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#include "texture.h"
#include "config.h"
#include <stdio.h>
#include <GL/gl.h>
#include <tqimage.h>
#include <tqgl.h>
#include <tdeglobal.h>
#include <tdeconfig.h>
#include <tqstring.h>
#include <kstandarddirs.h>
KueTexture::KueTexture(const TQString &filename)
{
_filename = filename;
_texture_id = 0;
if (filename.isNull())
{
// filename == TQString::null is an alias for the null texture
_loaded = true;
}
else
{
_loaded = false;
}
}
KueTexture::KueTexture(unsigned int texture_id)
{
_filename = TQString::null;
_texture_id = texture_id;
_loaded = true;
}
KueTexture::KueTexture(const KueTexture &t)
{
// Is the texture file backed?
if (t._filename.isNull())
{
// This is easy, copy over the texture id
_texture_id = t._texture_id;
_loaded = true;
}
else
{
// Yes, copy over the filename
_filename = t._filename;
_loaded = false;
}
}
KueTexture KueTexture::null() {
return KueTexture(0);
}
KueTexture::~KueTexture()
{
// We only "own" the texture ID if we were created from a filename
// Also check that we've allocated a valid texture ID. That means
// that the texture is loaded, and it's non-NULL.
// We don't use isNull(), because that forces a file load
if (_loaded && _texture_id && (!_filename.isNull()))
{
// Free a texture ID and its associated texture
glDeleteTextures(1, &_texture_id);
}
}
bool KueTexture::isNull()
{
load();
return (_texture_id == 0);
}
void KueTexture::load()
{
if (_loaded)
{
// The texture is already loaded, nothing to do here
return;
}
// Get the full pathname for the texture
TQImage raw_image, gl_image;
TQString fullname;
// Find the real filename
fullname = TDEGlobal::dirs()->findResource("appdata", "textures/" + _filename + ".png");
// Try to load the file
if (raw_image.load(fullname))
{
gl_image = TQGLWidget::convertToGLFormat(raw_image);
// Ask OpenGL for a new texture ID
glGenTextures(1, &_texture_id);
// Make it the current texture (blank right now)
glBindTexture(GL_TEXTURE_2D, _texture_id);
// Should we filter textures?
TDEGlobal::config()->setGroup("Graphics");
if (TDEGlobal::config()->readBoolEntry("Filter Textures", false))
{
// Yes, enable smooth scaling
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
else
{
// No, enable fast scaling
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
}
// Load the image data in to the texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, gl_image.width(), gl_image.height(), 0,
GL_RGBA, GL_UNSIGNED_BYTE, gl_image.bits());
}
else
{
// Unable to load image, use null texture
_texture_id = 0;
}
_loaded = true;
}
bool KueTexture::makeCurrent()
{
load();
// Sets the current 2D texture, where 0 means no texture
glBindTexture(GL_TEXTURE_2D, _texture_id);
return true;
}
|