uhhhhhhhhhh yeeeeeeea
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m28s

This commit is contained in:
2024-07-16 14:19:39 -04:00
parent e261b610c2
commit abd691b648
11 changed files with 251 additions and 99 deletions

View File

@@ -38,6 +38,12 @@ CPMAddPackage(
NAME Event
URL https://git.redacted.cc/josh/Event/archive/Release-6.zip
)
CPMAddPackage(
NAME ReTexture
URL https://git.redacted.cc/Redacted/ReTexture/archive/Prerelease-2.zip
)
if (WIN32)
#CPMAddPackage(
#NAME harfbuzz
@@ -76,6 +82,7 @@ target_include_directories(JGL PUBLIC
${ReWindow_SOURCE_DIR}/include
${glad_SOURCE_DIR}/include
${jlog_SOURCE_DIR}/include
${ReTexture_SOURCE_DIR}/include
)
add_executable(JGL_Demo main.cpp)
@@ -93,4 +100,4 @@ if (WIN32)
target_link_libraries(JGL PUBLIC ${OPENGL_LIBRARIES} J3ML ReWindowLibrary glad jlog Event)
endif()
target_link_libraries(JGL_Demo PUBLIC JGL)
target_link_libraries(JGL_Demo PUBLIC JGL ReTexture)

30
include/JGL/Font.h Normal file
View File

@@ -0,0 +1,30 @@
#pragma once
#include <vector>
#include <filesystem>
#include <J3ML/LinearAlgebra.h>
#include <filesystem>
#include <iostream>
namespace JGL
{
bool InitTextEngine();
/// A Font class implementation.
/// Wraps the font's FreeType asset handle and provides helper functions.
class Font {
public:
/// Default constructor does not initialize any members
Font() = default;
Font(std::filesystem::path path);
/// Destructor handles freeing of the underlying asset handle.
~Font();
static Font LoadTTF(std::filesystem::path filepath);
static std::vector<Font> GetLoadedFonts();
Vector2 MeasureString(const std::string& text, float ptSize);
public:
int index = 0;
};
}

View File

@@ -39,8 +39,8 @@ public:
CachedGlyph* getGlyph(char c);
std::map<char, CachedGlyph*> getGlyphs();
const GLuint* getTexture();
const GLsizei getTextureWidth() const;
const GLsizei getTextureHeight() const;
GLsizei getTextureWidth() const;
GLsizei getTextureHeight() const;
CachedFont(GLuint texture_id, GLsizei texture_width, GLsizei texture_height, unsigned int font_size, unsigned int font_index);
};

View File

@@ -1,6 +1,15 @@
//
// Created by dawsh on 1/17/24.
//
/// Josh's Graphics Library
/// A C++20 Library for rendering 2D and 3D primitives in an OpenGL context.
/// Developed and Maintained by Josh O'Leary @ Redacted Software.
/// Special Thanks to William Tomasine II and Maxine Hayes.
/// (c) 2024 Redacted Software
/// This work is dedicated to the public domain.
/// @file JGL.h
/// @desc All JGL usable functions are defined here. This is the public API.
/// @edit 2024-07-16
#pragma once
#include <string>
@@ -8,6 +17,7 @@
#include <JGL/Color4.h>
#include <JGL/Gradient.h>
#include <JGL/FontCache.h>
#include <JGL/Font.h>
#include <J3ML/LinearAlgebra.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <J3ML/LinearAlgebra/Vector3.h>
@@ -18,19 +28,10 @@
// OpenGL Wrapper for rendering 2D graphics primitives in both a 2D and 3D context
namespace JGL {
using J3ML::LinearAlgebra::Vector2;
using J3ML::LinearAlgebra::Vector3;
using J3ML::LinearAlgebra::Matrix3x3;
using J3ML::LinearAlgebra::Matrix4x4;
using J3ML::LinearAlgebra::AxisAngle;
using J3ML::LinearAlgebra::Quaternion;
using J3ML::Geometry::Sphere;
using J3ML::Geometry::OBB;
using J3ML::Geometry::Capsule;
using J3ML::Geometry::TriangleMesh;
using J3ML::Geometry::Plane;
using namespace J3ML::LinearAlgebra;
using namespace J3ML::Geometry;
/// TODO:
struct HSV {
float hue;
float saturation;
@@ -53,10 +54,10 @@ namespace JGL {
bool Update(const Vector2& window_size);
bool InitTextEngine();
int LoadFont(const std::string& font_path);
Font LoadFont(const std::string& font_path); // TODO: Fully deprecate
void PurgeFontCache();
void UnloadFont(int font_index);
void SetActiveFont(const Font& font); // TODO: Implement
// TODO: implement correct coloring
/// Drawing functions for primitive 2D Shapes.
@@ -86,7 +87,7 @@ namespace JGL {
/// @param B The end point of the line segment.
/// @param thickness The width at which to render the line.
void DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness = 1);
void DrawLine(const Color3 &color, float x, float y, float w, float h, float thickness = 1);
void DrawLine(const Color3& color, float x, float y, float w, float h, float thickness = 1);
void DrawLine(const Color4& color, const Vector2& A, const Vector2& B, float thickness = 1);
void DrawLine(const Color4& color, float x1, float y1, float x2, float y2, float thickness = 1);
@@ -100,6 +101,10 @@ namespace JGL {
void OutlineRect(const Color4& color, const Vector2& pos, const Vector2& size, float thickness = 1);
void OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness = 1);
///Draws a sprite to the screen.
void DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size);
void DrawSprite(GLuint texture, float x, float y, float w, float h);
/// Draws a filled rectangle on the screen.
void FillRect(const Color4& color, const Vector2& pos, const Vector2& size);
void FillRect(const Color3& color, const Vector2& pos, const Vector2& size);
@@ -109,7 +114,7 @@ namespace JGL {
void FillGradientRect(const Color3& color1, const Color3& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size);
/// Draws a filled rectangle with rounded corners on the screen.
void FillRoundedRect(const Color4 &color, const Vector2 &pos, const Vector2 &size, float radius = 5, unsigned int subdivisions = 8);
void FillRoundedRect(const Color4& color, const Vector2 &pos, const Vector2 &size, float radius = 5, unsigned int subdivisions = 8);
void FillRoundedRect(const Color3& color, const Vector2& pos, const Vector2& size, float radius = 5, unsigned int subdivisions = 8);
/// Draws an outline of a circle on the screen.
@@ -130,14 +135,14 @@ namespace JGL {
void FillTriangle(const Color3& color, const Triangle2D& tri);
// TODO: Implement an overload that simply takes 3 Vector3's
/// Draws a text string on the screen with a given point-size and font.
void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index);
void DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index);
void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font);
void DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font);
// TODO: Implement the following:
void FillTexturedTriangle();
void FillTexturedPolygon();
void DrawSprite();
void DrawPartialSprite();
void DrawCubicBezierCurve();
void OutlinePolygon (const Color4& color, std::vector<Vector2> points);
@@ -159,7 +164,7 @@ namespace JGL {
void WireframeCapsule(const Color3& color, const Capsule& cap, float thickness = 1);
void FillTriangleMesh(const Color3& color, const TriangleMesh& mesh);
void WireframeTriangleMesh(const Color3& color, const TriangleMesh& mesh, float thickness = 1);
void DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, unsigned int font_index);
void DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, const Font& font);
void DrawMatrixGizmo (const Matrix3x3&, const Vector3&);
void DrawMatrixGizmo (const Matrix4x4&);

View File

@@ -3,6 +3,7 @@
#include <rewindow/types/window.h>
#include <JGL/Colors.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <JGL/Font.h>
using J3ML::LinearAlgebra::Vector2;
using namespace JGL;
@@ -57,8 +58,8 @@ struct point {
GLfloat t;
};
int FreeSans;
int Jupiteroid;
JGL::Font FreeSans;
JGL::Font Jupiteroid;
class JGLDemoWindow : public ReWindow::RWindow
{
@@ -70,8 +71,8 @@ public:
gladLoadGL();
JGL::Update(getSize());
FreeSans = JGL::LoadFont("assets/fonts/FreeSans.ttf");
Jupiteroid = JGL::LoadFont("assets/fonts/Jupiteroid.ttf");
FreeSans = JGL::Font("assets/fonts/FreeSans.ttf");
Jupiteroid = JGL::Font("assets/fonts/Jupiteroid.ttf");
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glMatrixMode(GL_PROJECTION);

View File

@@ -1,5 +1,4 @@
#include <JGL/FontCache.h>
#include <iostream>
using namespace JGL;
@@ -37,6 +36,7 @@ unsigned int JGL::CachedFont::getFontIndex() {
return font_index;
}
//TODO make this code go faster.
CachedGlyph* JGL::CachedFont::getGlyph(char c) {
auto it = glyphs.find(c);
if (it != glyphs.end())
@@ -60,11 +60,11 @@ const GLuint* CachedFont::getTexture() {
return &texture;
}
const GLsizei CachedFont::getTextureWidth() const {
GLsizei CachedFont::getTextureWidth() const {
return texture_width;
}
const GLsizei CachedFont::getTextureHeight() const {
GLsizei CachedFont::getTextureHeight() const {
return texture_height;
}

View File

@@ -112,10 +112,10 @@ namespace JGL {
if (wasColorArrayEnabled)
glEnableClientState(GL_COLOR_ARRAY);
/*
//Select whatever texture unit was selected before.
glActiveTexture(GL_TEXTURE0 + activeTextureUnit);
*/
//Put the draw color back how it was before.
glColor4f(oldColor[0], oldColor[1], oldColor[2], oldColor[3]);
@@ -123,6 +123,31 @@ namespace JGL {
inJ2D = false;
}
void J2D::DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
const std::vector<Vector2> textureCoordinates = {{0,0}, {1, 0}, {1,1}, {0,1}};
const std::vector<Vector2> vertices = {pos, {pos.x + size.x, pos.y}, {pos.x + size.x, pos.y + size.y}, {pos.x, pos.y + size.y}};
glBindTexture(GL_TEXTURE_2D, texture);
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);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), textureCoordinates.data());
glDrawArrays(GL_QUADS, 0, 4);
glBindTexture(GL_TEXTURE_2D, 0);
}
void J2D::DrawSprite(GLuint texture, float x, float y, float w, float h) {
J2D::DrawSprite(texture, {x, y}, {w, h});
}
void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")

6
src/JGL/Color3.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include <JGL/Color3.h>
namespace JGL
{
}

6
src/JGL/Color4.cpp Normal file
View File

@@ -0,0 +1,6 @@
#include <JGL/Color4.h>
namespace JGL
{
}

115
src/JGL/Font.cpp Normal file
View File

@@ -0,0 +1,115 @@
#include <JGL/Font.h>
#include <vector>
#include <string>
#include <iostream>
#if __linux__
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#endif
#if _WIN32
#include <ft2build.h>
#include FT_FREETYPE_H
#endif
namespace JGL::Detail
{
/// ANOTHER Wrapper class to hide FT_Face and it's associated bullshit from user-facing API.
class FontInternals
{
public:
FT_Face face;
};
FT_Library ft;
std::vector<Font> fonts;
// TODO: Deprecating this in favor of class model of resource management.
int LoadFont(const std::string &font_path) {
if (ft == nullptr)
return -1;
Font font;
FT_Face face;
if (FT_New_Face(ft, font_path.c_str(), 0, &face)) {
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
return -1;
}
unsigned int newIndex = 0;
for (const auto& f : fonts)
if (f.index >= newIndex)
newIndex = f.index + 1;
font.index = newIndex;
fonts.push_back(font);
faces.push_back(face);
std::cout << "Loaded font from " << font_path << " with index " << newIndex << std::endl;
return newIndex;
}
bool InitTextEngine() {
if (FT_Init_FreeType(&ft))
return true;
return false;
}
void UnloadFont(int font_index) {
auto iter = fonts.begin();
while (iter != fonts.end())
{
if (iter->index == font_index){
FT_Done_Face(faces[iter->index]);
iter = fonts.erase(iter);
} else ++iter;
}
}
}
namespace JGL
{
Font::Font(std::filesystem::path path)
{
if (Detail::ft == nullptr)
throw new std::runtime_error("Error::FREETYPE: FT_Library was not initialized before attempting to load a font!");
Font font;
FT_Face face;
if (FT_New_Face(Detail::ft, path.c_str(), 0, &face)) {
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
throw new std::runtime_error("Error::FREETYPE: Failed to load font!");
//return -1;
}
unsigned int newIndex = 0;
for (const auto& f : Detail::fonts)
if (f.index >= newIndex)
newIndex = f.index + 1;
index = newIndex;
Detail::fonts.push_back(font);
std::cout << "Loaded font from " << path << " with index " << newIndex << std::endl;
//return newIndex;
}
Font::~Font()
{
Detail::UnloadFont(this->index);
}
std::vector<Font> Font::GetLoadedFonts() {
return Detail::fonts;
}
Font Font::LoadTTF(std::filesystem::path path)
{
return Font(path);
}
}

View File

@@ -1,72 +1,28 @@
#include <JGL/JGL.h>
#if __linux__
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#endif
#if _WIN32
#include <ft2build.h>
#include FT_FREETYPE_H
#endif
#include <JGL/Font.h>
#include <JGL/FontCache.h>
namespace JGL {
FT_Library ft;
struct Font {
int index = 0;
FT_Face face;
};
std::vector<Font> faces;
int LoadFont(const std::string &font_path) {
if (ft == nullptr)
return -1;
Font font;
if (FT_New_Face(ft, font_path.c_str(), 0, &font.face)) {
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
return -1;
}
unsigned int newIndex = 0;
for (const auto& f : faces)
if (f.index >= newIndex)
newIndex = f.index + 1;
font.index = newIndex;
faces.push_back(font);
std::cout << "Loaded font from " << font_path << " with index " << newIndex << std::endl;
return newIndex;
}
bool InitTextEngine() {
if (FT_Init_FreeType(&ft))
return true;
return false;
}
void UnloadFont(int font_index) {
for (int i = 0; i < faces.size(); i++)
if (faces[i].index == font_index)
FT_Done_Face(faces[i].face),
faces.erase(faces.begin() + i);
}
FontCache fontCache;
FontCache fontCache; // <-- Not implemented yet
void PurgeFontCache() {
fontCache.purgeCache();
}
//TODO multi-texturing for 8x speedup. I tried. We'll come back to it later.
void J2D::DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index) {
void J2D::DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font) {
glUseProgram(0); // Fixed-function pipeline.
Font font{};
CachedFont* cachedFont = fontCache.getFont(size, font_index);
CachedFont* cachedFont = fontCache.getFont(size, font.index);
//Set up the regular font.
for (const auto &f : faces)
if (f.index == font_index)
font = f;
//for (const auto &f : Font::GetLoadedFonts())
// if (f.index == font.index)
// font = f;
auto face = GetFTFace();
if (font.face == nullptr)
return;
@@ -106,8 +62,8 @@ namespace JGL {
charcode = FT_Get_Next_Char(font.face, charcode, &gindex);
}
fontCache.newFont(texture_id, width, max_height, size, font_index);
cachedFont = fontCache.getFont(size, font_index);
fontCache.newFont(texture_id, width, max_height, size, font.index);
cachedFont = fontCache.getFont(size, font.index);
glTexImage2D(GL_TEXTURE_2D, 0, GL_ALPHA, width, max_height, 0, GL_ALPHA, GL_UNSIGNED_BYTE, nullptr);
@@ -173,6 +129,7 @@ namespace JGL {
};
auto glyph_texcoords = glyph->getTexCoords();
//TODO make it like go faster because now the profiler says this is the slowest part.
vertices.insert(vertices.end(), glyph_vertices.begin(), glyph_vertices.end());
texcoords.insert(texcoords.end(), glyph_texcoords.begin(), glyph_texcoords.end());
}
@@ -183,11 +140,11 @@ namespace JGL {
glBindTexture(GL_TEXTURE_2D, 0);
}
void J2D::DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, unsigned int font_index) {
J2D::DrawString(Color4::FromColor3(color, 255), text, x, y, scale, size, font_index);
void J2D::DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font) {
J2D::DrawString(Color4::FromColor3(color, 255), text, x, y, scale, size, font);
}
void J3D::DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, unsigned int font_index) {
void J3D::DrawString(const Color3& color, const std::string& text, const Vector3& pos, const Vector3& angle, float scale, u32 size, const Font& font) {
//TODO figure out what the scale should actually be mathematically.
scale = scale * 0.002f;
scale = -scale;
@@ -199,10 +156,10 @@ namespace JGL {
glUseProgram(0); // Fixed-function pipeline.
glColor4f(color.r, color.g, color.b, 1.0f);
Font font;
for (auto& f : faces)
if (f.index == font_index)
font = f;
//Font font;
//for (auto& f : Font::GetLoadedFonts())
// if (f.index == font.index)
// font = f;
if (font.face == NULL) {
std::cout << "null font" << std::endl;
return;