Partial Implement RenderText
This commit is contained in:
@@ -1,10 +1,20 @@
|
||||
//
|
||||
// Created by dawsh on 1/17/24.
|
||||
//
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <JGL/JGL.h>
|
||||
#include <GL/glut.h>
|
||||
#include "J3ML/LinearAlgebra/Transform2D.h"
|
||||
#include <rewindow/types/window.h>
|
||||
#include <freetype2/ft2build.h>
|
||||
#include FT_FREETYPE_H
|
||||
#include <iostream>
|
||||
|
||||
|
||||
GLuint program;
|
||||
GLuint texture;
|
||||
|
||||
namespace JGL
|
||||
{
|
||||
Vector2 ScreenToViewport(const Vector2 &v) {
|
||||
@@ -28,6 +38,80 @@ namespace JGL
|
||||
};
|
||||
}
|
||||
|
||||
FT_Face face;
|
||||
FT_Library ft;
|
||||
|
||||
bool InitTextEngine() {
|
||||
if (FT_Init_FreeType(&ft))
|
||||
{
|
||||
std::cout << "Error::FREETYPE: " << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if (FT_New_Face(ft, "content/FreeSans.ttf", 0, &face))
|
||||
{
|
||||
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
FT_Set_Pixel_Sizes(face, 0, 48);
|
||||
return true;
|
||||
}
|
||||
|
||||
void RenderText(std::string text, float x, float y, float scale) {
|
||||
glUseProgram(0); // Fixed-function pipeline.
|
||||
glColor3f(1.0f, 1.0f, 1.0f);
|
||||
|
||||
const char* c;
|
||||
for (c = text.c_str(); *c; c++)
|
||||
{
|
||||
if (FT_Load_Char(face, *c, FT_LOAD_RENDER))
|
||||
continue;
|
||||
|
||||
FT_GlyphSlot g = face->glyph;
|
||||
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, g->bitmap.width, g->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE, g->bitmap.buffer);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
|
||||
float x2 = x + g->bitmap_left * scale;
|
||||
float y2 = -y - g->bitmap_top * scale; // Adjust y-coordinate
|
||||
float w = g->bitmap.width * scale;
|
||||
float h = g->bitmap.rows * scale;
|
||||
|
||||
glBegin(GL_TRIANGLES);
|
||||
|
||||
glTexCoord2f(0, 0);
|
||||
glVertex2f(x2, y2);
|
||||
|
||||
glTexCoord2f(0, 1);
|
||||
glVertex2f(x2, y2 + h);
|
||||
|
||||
glTexCoord2f(1, 1);
|
||||
glVertex2f(x2 + w, y2 + h);
|
||||
|
||||
glTexCoord2f(0, 0);
|
||||
glVertex2f(x2, y2);
|
||||
|
||||
glTexCoord2f(1, 1);
|
||||
glVertex2f(x2 + w, y2 + h);
|
||||
|
||||
glTexCoord2f(1, 0);
|
||||
glVertex2f(x2 + w, y2);
|
||||
|
||||
glEnd();
|
||||
|
||||
x += (g->advance.x >> 6) * scale;
|
||||
y += (g->advance.y >> 6) * scale;
|
||||
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
|
||||
}
|
||||
|
||||
namespace J2D
|
||||
{
|
||||
void FillRect2D(const Color3 &color, const Vector2 &pos, const Vector2 &size) {
|
||||
|
Reference in New Issue
Block a user