-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathText.cpp
More file actions
42 lines (34 loc) · 1.2 KB
/
Copy pathText.cpp
File metadata and controls
42 lines (34 loc) · 1.2 KB
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
#include "Text.h"
Text::~Text()
{
glDeleteTextures(1, &textureId);
}
void Text::Init(TTF_Font* font, VertexArray* vertexArray, Vector2d screenSize, Vector2d position)
{
this->font = font;
this->vertexArray = vertexArray;
this->screenSize = screenSize;
this->position = position;
glGenTextures(1, &textureId);
}
void Text::SetText(const char* text)
{
SDL_Color color = { 255, 255, 255, 255 };
SDL_Surface* surface = TTF_RenderUTF8_Blended(font, text, color);
int width = surface->w;
int height = surface->h;
glBindTexture(GL_TEXTURE_2D, textureId);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, surface->pixels);
SDL_FreeSurface(surface);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
matrix = Matrix4d::move({ position.x, position.y, 0.0f })
* Matrix4d::scale({ width / screenSize.x, height / screenSize.y, 1.0f });
}
void Text::Render(Shader& shader)
{
shader.SetWorldTransform(matrix);
shader.SetViewProjection(Matrix4d::identity());
glBindTexture(GL_TEXTURE_2D, textureId);
vertexArray->Render();
}