27 namespace AstcTextures
39 LOGI(
"Texture loadData started for %s...\n", filename);
41 FILE* file = fopen(filename,
"rb");
45 LOGE(
"Failed to open '%s'\n", filename);
49 fseek(file, 0, SEEK_END);
51 unsigned int length = ftell(file);
52 unsigned char* loadedTexture = NULL;
56 fseek(file, 0, SEEK_SET);
58 size_t read = fread(loadedTexture,
sizeof(
unsigned char), length, file);
62 LOGE(
"Failed to read in '%s'\n", filename);
68 *textureData = loadedTexture;
112 GL_CHECK(glActiveTexture(GL_TEXTURE0));
117 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
118 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
119 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
120 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
122 const int textureLength = strlen(resourceDirectory) + strlen(
textureFilename);
126 strcpy(texture, resourceDirectory);
129 unsigned char* textureData = NULL;
135 GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 48, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData));
152 void Text::addString(
int xPosition,
int yPosition,
const char*
string,
int red,
int green,
int blue,
int alpha)
154 int length = (
int)strlen(
string);
186 for (
int iChar = 0; iChar < (
signed int)strlen(
string); iChar ++)
188 char cChar =
string[iChar];
202 sBottom_left.x = iCharX;
203 sBottom_left.y = iCharY;
205 sBottom_right.y = iCharY;
206 sTop_left.x = iCharX;
212 textVertex[iVertexPos++] = xPosition + iChar * textureCharacterWidth *
scale;
216 textVertex[iVertexPos++] = xPosition + (iChar + 1) * textureCharacterWidth * scale;
218 textVertex[iVertexPos++] = 0;
220 textVertex[iVertexPos++] = xPosition + iChar * textureCharacterWidth *
scale;
221 textVertex[iVertexPos++] = yPosition + textureCharacterHeight *
scale;
222 textVertex[iVertexPos++] = 0;
224 textVertex[iVertexPos++] = xPosition + (iChar + 1) * textureCharacterWidth * scale;
225 textVertex[iVertexPos++] = yPosition + textureCharacterHeight *
scale;
226 textVertex[iVertexPos++] = 0;
242 color[iColorPos++] = red / 255.0f;
243 color[iColorPos++] = green / 255.0f;
244 color[iColorPos++] = blue / 255.0f;
245 color[iColorPos++] = alpha / 255.0f;
248 memcpy(&
color[iColorPos], &
color[iColorPos - 4], 4 *
sizeof(
float));
249 memcpy(&
color[iColorPos + 4], &
color[iColorPos], 4 *
sizeof(
float));
250 memcpy(&
color[iColorPos + 8], &
color[iColorPos + 4], 4 *
sizeof(
float));
259 GLint vertexArray = 0;
261 GL_CHECK(glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &vertexArray));
263 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
266 GLint currentProgram = 0;
268 GL_CHECK(glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProgram));
274 LOGI(
"At least one of the attributes and/or uniforms is missing. Have you invoked Text(const char*, int, int) constructor?");
278 if (numberOfCharacters == 0)
292 GL_CHECK(glActiveTexture(GL_TEXTURE0));
295 GL_CHECK(glDrawElements(GL_TRIANGLE_STRIP, numberOfCharacters * 6 - 2, GL_UNSIGNED_SHORT,
textIndex));
302 GL_CHECK(glUseProgram(currentProgram));
305 GL_CHECK(glBindVertexArray(vertexArray));
311 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...
const char fontFragmentShaderSource[]
Fragment shader source code for text rendering.
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...
const char fontVertexShaderSource[]
Vertex shader source code for text rendering.
float * textTextureCoordinates
static const int textureCharacterHeight
The height (in pixels) of the characters in the text texture.
void clear(void)
Removes the current string from the class.
Text(void)
Overloaded default constructor.
struct AstcTextures::Vec2 Vec2
Type representing texture coordinates.
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...
static const char textureFilename[]
static Matrix matrixOrthographic(float left, float right, float bottom, float top, float zNear, float zFar)
Create and return an orthographic projection matrix.
static const int textureCharacterWidth
The width (in pixels) of the characters in the text texture.
~Text(void)
Overloaded default destructor.
unsigned char * textureData
float * getAsArray(void)
Get the matrix elements as a column major order array.
#define REALLOC_CHECK(ptr_type, ptr, size)
GLenum GLuint GLenum GLsizei length
static const float scale
Scaling factor to use when rendering the text.
void draw(void)
Draw the text to the screen.
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.