Compare commits

...

6 Commits

Author SHA1 Message Date
cf72d92c28 Merge branch 'master' of https://git.redacted.cc/josh/JGL 2024-05-29 22:18:41 -04:00
5d99f8ec1f update 2024-05-29 22:18:40 -04:00
a388ee8021 Implement proper GL state management in DrawString2D, DrawString3D 2024-05-29 15:27:24 -04:00
41916a4089 Update JGL.cpp 2024-05-29 15:05:06 -04:00
222dd346fb Fix memleak 2024-05-27 20:48:22 -04:00
e6e567725b Color 2024-05-23 12:05:00 -04:00
3 changed files with 39 additions and 19 deletions

View File

@@ -31,7 +31,7 @@ CPMAddPackage(
CPMAddPackage(
NAME GLAD
URL https://git.redacted.cc/Redacted/glad/archive/v2.1.zip
URL https://git.redacted.cc/Redacted/glad/archive/v2.1ext_mt.zip
)
file(COPY "assets" DESTINATION "${PROJECT_BINARY_DIR}")
file(GLOB_RECURSE HEADERS "include/*.h" "include/*.hpp")

View File

@@ -82,7 +82,7 @@ public:
JGL::J2D::FillTriangle2D(JGL::Colors::Yellow, tri);
JGL::J3D::DrawString3D(JGL::Colors::White, "JGL Sample Text", {1, -120, 0.5f}, 2.f);
JGL::J3D::DrawString3D(JGL::Colors::Red, "JGL Sample Text", {1, -120, 0.5f}, 2.f);
JGL::J2D::DrawString2D(JGL::Colors::Green, "William J. Tomasine II ", 0.f, -120.f, 1.f);
JGL::J2D::DrawLine2D(JGL::Colors::Greens::DarkGreen, {10, 10}, {200, 300});

View File

@@ -2,6 +2,7 @@
// Created by dawsh on 1/17/24.
//
#include <vector>
#include <glad/glad.h>
#include <JGL/JGL.h>
#include <freetype2/ft2build.h>
@@ -9,9 +10,7 @@
#include <JGL/Color3.h>
#include <iostream>
GLuint program;
GLuint texture;
namespace JGL
{
@@ -148,21 +147,27 @@ namespace JGL
void DrawString2D(const Color3& color, std::string text, float x, float y, float scale, u32 size) {
glUseProgram(0); // Fixed-function pipeline.
GLfloat currentColor[4];
glGetFloatv(GL_CURRENT_COLOR, currentColor);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
FT_Set_Pixel_Sizes(face, 0, size);
const char* c;
for (c = text.c_str(); *c; c++)
GLuint textures[text.size()];
for (int i = 0; i < text.length(); i++)
{
if (FT_Load_Char(face, *c, FT_LOAD_RENDER))
if (FT_Load_Char(face, text.c_str()[i], FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &textures[i]);
glBindTexture(GL_TEXTURE_2D, textures[i]);
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);
@@ -200,9 +205,14 @@ namespace JGL
x += (g->advance.x >> 6) * scale;
y += (g->advance.y >> 6) * scale;
}
for (auto& t : textures)
glDeleteTextures(1, &t);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glColor4f(currentColor[0], currentColor[1], currentColor[2], currentColor[3]); //Set draw color back to whatever it was before.
}
}
@@ -211,7 +221,6 @@ namespace JGL
{
void DrawLine3D(const Color3& color, const Vector3& A, const Vector3& B, float thickness)
{
glBegin(GL_LINES);
glLineWidth(thickness);
glColor3f(color.r/255.f, color.g/255.f, color.b/255.f);
@@ -225,22 +234,27 @@ namespace JGL
float x = pos.x;
float y = pos.y;
float z = pos.z;
GLfloat currentColor[4];
GLuint textures[text.size()];
glGetFloatv(GL_CURRENT_COLOR, currentColor);
glUseProgram(0); // Fixed-function pipeline.
glColor3f(1.0f, 1.0f, 1.0f);
glColor4f(color.r, color.g, color.b, 1.0f);
FT_Set_Pixel_Sizes(face, 0, size);
const char* c;
for (c = text.c_str(); *c; c++)
//for (c = text.c_str(); *c; c++)
for (int i = 0; i < text.length(); i++)
{
if (FT_Load_Char(face, *c, FT_LOAD_RENDER))
if (FT_Load_Char(face, text.c_str()[i], FT_LOAD_RENDER))
continue;
FT_GlyphSlot g = face->glyph;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &textures[i]);
glBindTexture(GL_TEXTURE_2D, textures[i]);
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);
@@ -281,7 +295,13 @@ namespace JGL
y += (g->advance.y >> 6) * scale;
}
for (auto& t : textures)
glDeleteTextures(1, &t);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glColor4f(currentColor[0], currentColor[1], currentColor[2], currentColor[3]); //Set draw color back to whatever it was before.
}
}