28 namespace AstcTextures
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;
113 GL_CHECK(glActiveTexture(GL_TEXTURE0));
118 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR));
119 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR));
120 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE));
121 GL_CHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE));
123 const int textureLength = strlen(resourceDirectory) + strlen(
textureFilename);
127 strcpy(texture, resourceDirectory);
136 GL_CHECK(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 256, 48, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData));
153 void Text::addString(
int xPosition,
int yPosition,
const char*
string,
int red,
int green,
int blue,
int alpha)
187 for (
int iChar = 0; iChar < (
signed int)strlen(
string); iChar ++)
189 char cChar =
string[iChar];
203 sBottom_left.
x = iCharX;
204 sBottom_left.
y = iCharY;
206 sBottom_right.
y = iCharY;
207 sTop_left.
x = iCharX;
213 textVertex[iVertexPos++] = xPosition + iChar * textureCharacterWidth *
scale;
217 textVertex[iVertexPos++] = xPosition + (iChar + 1) * textureCharacterWidth * scale;
219 textVertex[iVertexPos++] = 0;
221 textVertex[iVertexPos++] = xPosition + iChar * textureCharacterWidth *
scale;
222 textVertex[iVertexPos++] = yPosition + textureCharacterHeight *
scale;
223 textVertex[iVertexPos++] = 0;
225 textVertex[iVertexPos++] = xPosition + (iChar + 1) * textureCharacterWidth * scale;
226 textVertex[iVertexPos++] = yPosition + textureCharacterHeight *
scale;
227 textVertex[iVertexPos++] = 0;
243 color[iColorPos++] = red / 255.0f;
244 color[iColorPos++] = green / 255.0f;
245 color[iColorPos++] = blue / 255.0f;
246 color[iColorPos++] = alpha / 255.0f;
249 memcpy(&
color[iColorPos], &
color[iColorPos - 4], 4 *
sizeof(
float));
250 memcpy(&
color[iColorPos + 4], &
color[iColorPos], 4 *
sizeof(
float));
251 memcpy(&
color[iColorPos + 8], &
color[iColorPos + 4], 4 *
sizeof(
float));
260 GLint vertexArray = 0;
262 GL_CHECK(glGetIntegerv(GL_VERTEX_ARRAY_BINDING, &vertexArray));
264 GL_CHECK(glBindBuffer(GL_ARRAY_BUFFER, 0));
267 GLint currentProgram = 0;
269 GL_CHECK(glGetIntegerv(GL_CURRENT_PROGRAM, ¤tProgram));
275 LOGI(
"At least one of the attributes and/or uniforms is missing. Have you invoked Text(const char*, int, int) constructor?");
293 GL_CHECK(glActiveTexture(GL_TEXTURE0));
303 GL_CHECK(glUseProgram(currentProgram));
306 GL_CHECK(glBindVertexArray(vertexArray));
312 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
Type representing texture coordinates.
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.
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.