40 LOGI(
"Texture loadData started for %s...\n", filename);
42 FILE* file = fopen(filename,
"rb");
46 LOGE(
"Failed to open '%s'\n", filename);
50 fseek(file, 0, SEEK_END);
52 unsigned int length = ftell(file);
53 unsigned char* loadedTexture = NULL;
57 fseek(file, 0, SEEK_SET);
59 size_t read = fread(loadedTexture,
sizeof(
unsigned char), length, file);
63 LOGE(
"Failed to read in '%s'\n", filename);
69 *textureData = loadedTexture;
75 GLint attrib_location =
GL_CHECK(glGetAttribLocation(program, attrib_name));
77 if (attrib_location == -1)
79 LOGE(
"Cannot retrieve location of %s attribute.\n", attrib_name);
83 return attrib_location;
89 GLint uniform_location =
GL_CHECK(glGetUniformLocation(program, uniform_name));
91 if (uniform_location == -1)
93 LOGE(
"Cannot retrieve location of %s uniform.\n", uniform_name);
97 return uniform_location;
141 GL_CHECK(glActiveTexture(GL_TEXTURE0));
146 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
147 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
148 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
149 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
151 const int textureLength = strlen(resourceDirectory) + strlen(
textureFilename);
155 strcpy(texture, resourceDirectory);
164 GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 48, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData));
181 void Text::addString(
int xPosition,
int yPosition,
const char*
string,
int red,
int green,
int blue,
int alpha)
215 for (
int iChar = 0; iChar < (
signed int)strlen(
string); iChar ++)
217 char cChar =
string[iChar];
231 sBottom_left.
x = iCharX;
232 sBottom_left.
y = iCharY;
234 sBottom_right.
y = iCharY;
235 sTop_left.
x = iCharX;
241 textVertex[iVertexPos++] = xPosition + iChar * textureCharacterWidth *
scale;
245 textVertex[iVertexPos++] = xPosition + (iChar + 1) * textureCharacterWidth * scale;
247 textVertex[iVertexPos++] = 0;
249 textVertex[iVertexPos++] = xPosition + iChar * textureCharacterWidth *
scale;
250 textVertex[iVertexPos++] = yPosition + textureCharacterHeight *
scale;
251 textVertex[iVertexPos++] = 0;
253 textVertex[iVertexPos++] = xPosition + (iChar + 1) * textureCharacterWidth * scale;
254 textVertex[iVertexPos++] = yPosition + textureCharacterHeight *
scale;
255 textVertex[iVertexPos++] = 0;
271 color[iColorPos++] = red / 255.0f;
272 color[iColorPos++] = green / 255.0f;
273 color[iColorPos++] = blue / 255.0f;
274 color[iColorPos++] = alpha / 255.0f;
277 memcpy(&
color[iColorPos], &
color[iColorPos - 4], 4 *
sizeof(
float));
278 memcpy(&
color[iColorPos + 4], &
color[iColorPos], 4 *
sizeof(
float));
279 memcpy(&
color[iColorPos + 8], &
color[iColorPos + 4], 4 *
sizeof(
float));
288 GLint vertexArray = 0;
290 GL_CHECK(glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &vertexArray));
292 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
295 GLint currentProgram = 0;
297 GL_CHECK(glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProgram));
303 LOGI(
"At least one of the attributes and/or uniforms is missing. Have you invoked Text(const char*, int, int) constructor?");
321 GL_CHECK(glActiveTexture(GL_TEXTURE0));
331 GL_CHECK(glUseProgram(currentProgram));
334 GL_CHECK(glBindVertexArray(vertexArray));
340 m_iLocProjection(-1),
GLuint create_program(const char *vertex_source, const char *fragment_source)
Create program object, attach vertex and fragment shader to it. Link program object and check whether...
void loadData(const char *filename, unsigned char **textureData)
Load texture data from a file into memory.
#define MALLOC_CHECK(ptr_type, ptr, size)
GLint get_and_check_attrib_location(GLuint program, const GLchar *attrib_name)
Invoke glGetAttribLocation(), if it has returned a positive value. Otherwise, print a message and exi...
static const int textureCharacterHeight
The height (in pixels) of the characters in the text texture.
float * textTextureCoordinates
void clear(void)
Removes the current string from the class.
void draw(void)
Draw the text to the screen.
GLint get_and_check_uniform_location(GLuint program, const GLchar *uniform_name)
Invoke glGetUniformLocation, if it has returned a positive value. Otherwise, print a message and exit...
const char fontFragmentShaderSource[]
Fragment shader source code for text rendering.
~Text(void)
Overloaded default destructor.
const char fontVertexShaderSource[]
Vertex shader source code for text rendering.
static const int textureCharacterWidth
The width (in pixels) of the characters in the text texture.
static Matrix matrixOrthographic(float left, float right, float bottom, float top, float zNear, float zFar)
Create and return an orthographic projection matrix.
static const char textureFilename[]
unsigned char * textureData
#define REALLOC_CHECK(ptr_type, ptr, size)
Type representing texture coordinates.
Text(void)
Overloaded default constructor.
GLenum GLuint GLenum GLsizei length
static const float scale
Scaling factor to use when rendering the text.
float * getAsArray(void)
Get the matrix elements as a column major order array.
typedef GLuint(GL_APIENTRYP PFNGLGETDEBUGMESSAGELOGKHRPROC)(GLuint count
void addString(int xPosition, int yPosition, const char *string, int red, int green, int blue, int alpha)
Add a std::string to be drawn to the screen.