Compare commits

...

14 Commits

Author SHA1 Message Date
9dda4bebc5 Make measure string go brrrrrrr
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 4m9s
2024-07-31 12:01:57 -04:00
a5bfb4972a Fill quad
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m32s
2024-07-30 23:37:47 -04:00
4348f2708a Gradient triangle
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m32s
2024-07-30 15:49:13 -04:00
32dd2b54ca incorporate builddeps.reci
All checks were successful
Run ReCI Build Test / Explore-Gitea-Actions (push) Successful in 1m40s
2024-07-29 19:30:23 -04:00
e8b601aa25 install build deps reci script
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 45s
2024-07-29 19:29:11 -04:00
d5fd3e1a49 reci lua edition
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 48s
2024-07-29 19:24:55 -04:00
f593a0beac Cleanup & Update ReTexture.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m29s
2024-07-19 17:16:38 -04:00
0b7af6fd31 Cleanup
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m46s
2024-07-19 01:34:43 -04:00
eca4309e85 DrawSprite
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m21s
2024-07-18 22:24:19 -04:00
4be97f52d9 Minor documentation additions.
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m19s
2024-07-18 14:51:19 -04:00
613a13618c Merge branch 'master' of https://git.redacted.cc/Josh/JGL
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Has been cancelled
2024-07-18 14:49:19 -04:00
4150c93c85 Make it like, go faster. 2024-07-18 14:49:15 -04:00
9dee59fd45 Merge remote-tracking branch 'origin/master'
Some checks failed
Run ReCI Build Test / Explore-Gitea-Actions (push) Failing after 1m32s
2024-07-18 14:04:16 -04:00
b6b2ca1bfe Remove old LoadFont function. 2024-07-18 14:04:11 -04:00
15 changed files with 229 additions and 98 deletions

View File

@@ -17,8 +17,6 @@ jobs:
- run: echo "The ${{ gitea.repository }} repository has been cloned to the runner."
- run: echo "The workflow is now ready to run your tests on the runner."
- run: echo "Install toolchain and run ReCI build test"
- run: apt-get update && apt-get install -y git && git clone $RECI_GIT $RECI
- run: bash $RECI/scripts/setup_build_tools.sh
- run: bash reci/scripts/install_build_dependencies.sh
- run: bash $RECI/scripts/run_buildtest.sh ${{ gitea.repository }}
- run: apt-get update && apt-get install -y lua5.3 git && git clone $RECI_GIT $RECI
- run: lua $RECI/reci.lua -f $RECI/scripts/buildtools.reci -f reci/scripts/builddeps.reci -f $RECI/scripts/buildtest.reci
- run: echo "This job's status is ${{ job.status }}."

View File

@@ -41,7 +41,7 @@ CPMAddPackage(
CPMAddPackage(
NAME ReTexture
URL https://git.redacted.cc/Redacted/ReTexture/archive/Prerelease-2.zip
URL https://git.redacted.cc/Redacted/ReTexture/archive/Release-1.zip
)
if (WIN32)
@@ -74,6 +74,9 @@ endif()
set_target_properties(JGL PROPERTIES LINKER_LANGUAGE CXX)
#Don't expose this one because it's only to be used in the demo program.
include_directories(${ReTexture_SOURCE_DIR}/include)
target_include_directories(JGL PUBLIC
${PROJECT_SOURCE_DIR}/include
${OPENGL_INCLUDE_DIRS}
@@ -82,7 +85,6 @@ 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)

BIN
assets/sprites/Re3D.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.0 KiB

View File

@@ -5,6 +5,7 @@
namespace JGL
{
using namespace J3ML;
/// Represents a 3-channel color value, with Red, Green and Blue components.
class Color3 {
public:
u8 r;
@@ -12,16 +13,27 @@ namespace JGL
u8 b;
public:
/// Explicitly constructs a Color3 from the given Red, Green, and Blue values.
Color3(u8 R, u8 G, u8 B);
/// Returns a Color3 parsed from the given hexadecimal string.
static Color3 FromHex(const std::string& hexCode);
public:
/// Returns a Color3 that is somewhere in-between this and the given Color3, determined by the alpha [0-1].
Color3 Lerp(const Color3& rhs, float alpha) const;
/// Returns the red channel [0-255].
u8 RedChannel () const;
/// Returns the green channel [0-255].
u8 GreenChannel() const;
/// Returns the blue channel [0-255].
u8 BlueChannel () const;
/// Returns the red channel normalized from [0-255] to [0-1].
float RedChannelNormalized () const;
float BlueChannelNormalized() const;
/// Returns the green channel normalized from [0-255] to [0-1].
float GreenChannelNormalized() const;
/// Returns the blue channel normalized from [0-255] to [0-1].
float BlueChannelNormalized() const;
};

View File

@@ -4,6 +4,8 @@
namespace JGL
{
/// Represents a 4-channel color value, with Red, Green, Blue, and Alpha components.
class Color4 {
public:
u8 r;

View File

@@ -6,14 +6,15 @@
#include <filesystem>
#include <iostream>
// LMFAO
/// Defines external C references to FreeType Data Structures
extern "C" typedef struct FT_FaceRec_* FT_Face;
extern "C" typedef struct FT_LibraryRec_* FT_Library;
namespace JGL
{
/// Initializes FreeType engine.
/// Initializes FreeType engine. Remember to call this during program initialization.
bool InitTextEngine();
/// A Font class implementation.
@@ -31,7 +32,7 @@ namespace JGL
/// @param text The string to measure.
/// @param ptSize The font size at which to measure.
/// @return The size-in-pixels that would contain the entire text.
Vector2 MeasureString(const std::string& text, float ptSize);
Vector2 MeasureString(const std::string& text, unsigned int ptSize);
public:
int index = 0;
FT_Face face;

View File

@@ -61,3 +61,7 @@ public:
void eraseFont(CachedFont* font);
void purgeCache();
};
namespace JGL {
inline FontCache fontCache;
}

View File

@@ -1,12 +0,0 @@
#pragma once
#include <J3ML/J3ML.h>
namespace JGL {
enum class Gradient : u8 {
Vertical = 0,
Horizontal = 1,
DiagonalTopLeft = 2,
DiagonalBottomLeft = 3
};
}

View File

@@ -15,7 +15,7 @@
#include <string>
#include <iostream>
#include <JGL/Color4.h>
#include <JGL/Gradient.h>
#include <JGL/enums.h>
#include <JGL/FontCache.h>
#include <JGL/Font.h>
#include <J3ML/LinearAlgebra.h>
@@ -102,16 +102,26 @@ namespace JGL {
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);
void DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size, u8 opacity = 255, Inversion::Inversion inversion = Inversion::None);
void DrawSprite(GLuint texture, float x, float y, float w, float h, u8 opacity = 255, Inversion::Inversion inversion = Inversion::None);
///Draws a non axis-aligned fill rect to the screen.
///The order of the vertices must be such that if you were to connect them you'd never go diagonally across the quad.
void FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
void FillQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4);
///Draws a non axis-aligned outline rect to the screen.
///The order of the vertices must be such that if you were to connect them you'd never go diagonally across the quad.
void OutlineQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);
void OutlineQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness = 1);
/// 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);
/// Draws a filled rectangle where the color transitions across it.
void FillGradientRect(const Color4& color1, const Color4& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size);
void FillGradientRect(const Color3& color1, const Color3& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size);
void FillGradientRect(const Color4& color1, const Color4& color2, const Gradient::Gradient& gradient, const Vector2& pos, const Vector2& size);
void FillGradientRect(const Color3& color1, const Color3& color2, const Gradient::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);
@@ -131,8 +141,12 @@ namespace JGL {
// TODO: Implement an overload that simply takes 3 Vector3's
/// Draws a filled triangle on the screen.
void FillTriangle(const Color4& color, const Triangle2D &tri);
void FillTriangle(const Color4& color, const Triangle2D& tri);
void FillTriangle(const Color3& color, const Triangle2D& tri);
/// Draws a triangle where each corner is defined by a given color, Smoothly transitioning between them.
void FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Triangle2D& tri);
void FillGradientTriangle(const Color3& a_color, const Color3& b_color, const Color3& c_color, const Triangle2D& tri);
// TODO: Implement an overload that simply takes 3 Vector3's

26
include/JGL/enums.h Normal file
View File

@@ -0,0 +1,26 @@
#pragma once
namespace JGL::Inversion {
enum Inversion {
None = 0,
Vertical = 1,
Horizontal = 2,
};
inline Inversion operator|(Inversion a, Inversion b) {
return static_cast<Inversion>(static_cast<int>(a) | static_cast<int>(b));
}
}
namespace JGL::Gradient {
enum Gradient {
Vertical = 0,
Horizontal = 1,
DiagonalTopLeft = 2,
DiagonalBottomLeft = 3
};
inline Gradient operator|(Gradient a, Gradient b) {
return static_cast<Gradient>(static_cast<int>(a) | static_cast<int>(b));
}
}

View File

@@ -4,11 +4,15 @@
#include <JGL/Colors.h>
#include <J3ML/LinearAlgebra/Vector2.h>
#include <JGL/Font.h>
#include "jlog/jlog.hpp"
#include <jlog/jlog.hpp>
#include <ReTexture/Texture.h>
using J3ML::LinearAlgebra::Vector2;
using namespace JGL;
using namespace ReTexture;
Texture* image;
GLuint imageID;
//The Re3D style base projection.
std::vector<GLfloat> perspective(float fov, float aspect, float nearPlane, float farPlane) {
std::vector<float> result(16);
@@ -86,6 +90,21 @@ public:
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glDepthMask(GL_TRUE);
image = new Texture("assets/sprites/Re3D.png");
glGenTextures(1, &imageID);
glBindTexture(GL_TEXTURE_2D, imageID);
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);
if (image->getTextureFormat() == TextureFormat::RGBA)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->getWidth(), image->getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixelData.data());
if (image->getTextureFormat() == TextureFormat::RGB)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image->getWidth(), image->getHeight(), 0, GL_RGB, GL_UNSIGNED_BYTE, image->pixelData.data());
glBindTexture(GL_TEXTURE_2D, 0);
}
Vector3 textAngle = {0,0,0};
@@ -105,9 +124,10 @@ public:
J3D::DrawString(JGL::Colors::Red, "JGL Sample Text", {-0.33, -0.1, 1.0f},textAngle, 1.f, 32, FreeSans);
J3D::End();
J2D::Begin();
J2D::FillQuad(Color4(Colors::Red), {500, 52}, {500, 152}, {600, 152}, {600, 52});
J2D::FillRect(Colors::Blue, {0,52}, {100,100});
J2D::DrawSprite(imageID, {0, 52}, {(float) image->getWidth(), (float) image->getHeight()}, 128);
J2D::FillRect(Color4::FromColor3(Colors::Pinks::HotPink), {68, 120}, {32, 32});
J2D::FillGradientRect(Colors::Red, Colors::Blue, Gradient::DiagonalBottomLeft, {100,52}, {100,100});
J2D::FillRoundedRect(JGL::Colors::Red, {200, 52}, {100, 100}, 8, 8);
@@ -116,12 +136,11 @@ public:
J2D::FillCircle(JGL::Colors::White, {52, 204}, 50, 24);
J2D::OutlineCircle(JGL::Colors::White, {153, 204}, 50, 24);
J2D::FillTriangle(Colors::Red, {{0, 275}, {0, 375}, {100, 375}});
//J2D::FillTriangle(Colors::Red, {{0, 275}, {0, 375}, {100, 375}});
J2D::FillGradientTriangle(Color4(Colors::Red), Color4(Colors::Green), Color4(Colors::Blue), {{0, 275}, {0, 375}, {100, 375}});
J2D::OutlineTriangle(Colors::Blue, {{100, 275}, {0, 275}, {100, 375}});
J2D::DrawGradientLine(JGL::Colors::Red, JGL::Colors::Blue, {105, 375}, {200, 275}, 2);
auto result = Jupiteroid.MeasureString("Jupiteroid Font", 16);
//DEBUG(std::format("Result: {},{}", result.x, result.y ));
J2D::FillRect(JGL::Colors::Gray, {0, 0}, result);
J2D::DrawString(JGL::Colors::Green, "Jupteroid Font", 0.f, 0, 1.f, 16, Jupiteroid);

View File

@@ -0,0 +1 @@
Main:new("Install build dependencies", "apt-get install -yq libgl1-mesa-dev libfreetype-dev")

View File

@@ -7,7 +7,8 @@
#include <JGL/Color3.h>
#include <jlog/jlog.hpp>
GLfloat oldColor[4] = {0, 0, 0, 255};
GLfloat oldColor[4] = {0, 0, 0, 1};
GLfloat baseColor[4] = {1, 1, 1, 1};
bool inJ2D = false;
bool inJ3D = false;
bool wasTexture2DEnabled = false;
@@ -37,7 +38,11 @@ namespace JGL {
glPushMatrix();
glLoadIdentity();
glGetIntegerv(GL_ACTIVE_TEXTURE, &activeTextureUnit);
//Get what the draw color was before we did anything.
glGetFloatv(GL_CURRENT_COLOR, oldColor);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
glGetIntegerv(GL_ACTIVE_TEXTURE,& activeTextureUnit);
activeTextureUnit = activeTextureUnit - GL_TEXTURE0;
if (activeTextureUnit != 0)
glActiveTexture(GL_TEXTURE0);
@@ -111,40 +116,72 @@ 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]);
inJ2D = false;
}
void J2D::DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size) {
//TODO rotation, I'm unsure if @josh wants degrees or radians.
void J2D::DrawSprite(GLuint texture, const Vector2& pos, const Vector2& size, u8 opacity, Inversion::Inversion inversion) {
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}};
std::array<Vector2, 4> textureCoordinates = {Vector2(0, 0), Vector2(0, 1), Vector2(1, 1), Vector2(1, 0)};;
const Vector2 vertices[] = {{pos.x, pos.y}, {pos.x, pos.y + size.y}, {pos.x + size.x, pos.y + size.y}, {pos.x + size.x, pos.y}};
if (inversion& Inversion::Vertical)
textureCoordinates = {Vector2(0, 1), Vector2(0, 0), Vector2(1, 0), Vector2(1, 1)};
if (inversion& Inversion::Horizontal)
textureCoordinates = {Vector2(1, 0), Vector2(1, 1), Vector2(0, 1), Vector2(0, 0)};
if ((inversion& Inversion::Horizontal) && (inversion& Inversion::Vertical))
textureCoordinates = {Vector2(1, 1), Vector2(1, 0), Vector2(0, 0), Vector2(0, 1)};
glColor4f(baseColor[0],baseColor[1],baseColor[2],opacity / 255.f);
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());
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glTexCoordPointer(2, GL_FLOAT, sizeof(Vector2), textureCoordinates.data());
glDrawArrays(GL_QUADS, 0, 4);
glBindTexture(GL_TEXTURE_2D, 0);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::DrawSprite(GLuint texture, float x, float y, float w, float h) {
J2D::DrawSprite(texture, {x, y}, {w, h});
void J2D::FillQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {v1, v2, v3, v4};
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_QUADS, 0, 4);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4) {
J2D::FillQuad(Color4(color), v1, v2, v3, v4);
}
void J2D::OutlineQuad(const Color4& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {v1, v2, v3, v4};
glLineWidth(thickness);
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 4);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::OutlineQuad(const Color3& color, const Vector2& v1, const Vector2& v2, const Vector2& v3, const Vector2& v4, float thickness) {
J2D::OutlineQuad(Color4(color), v1, v2, v3, v4);
}
void J2D::DrawSprite(GLuint texture, float x, float y, float w, float h, u8 opacity, Inversion::Inversion inversion) {
J2D::DrawSprite(texture, {x, y}, {w, h}, opacity, inversion);
}
void J2D::FillRect(const Color4& color, const Vector2& pos, const Vector2& size) {
@@ -155,13 +192,14 @@ namespace JGL {
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_QUADS, 0, 4);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillRect(const Color3& color, const Vector2& pos, const Vector2& size) {
J2D::FillRect({color.r, color.g, color.b, 255}, pos, size);
}
void J2D::FillGradientRect(const Color4& color1, const Color4& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size) {
void J2D::FillGradientRect(const Color4& color1, const Color4& color2, const Gradient::Gradient& gradient, const Vector2& pos, const Vector2& size) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
@@ -191,13 +229,14 @@ namespace JGL {
glColorPointer(4, GL_FLOAT, 0, colors.data());
glDrawArrays(GL_QUADS, 0, 4);
glDisableClientState(GL_COLOR_ARRAY);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillGradientRect(const Color3& color1, const Color3& color2, const Gradient& gradient, const Vector2& pos, const Vector2& size) {
void J2D::FillGradientRect(const Color3& color1, const Color3& color2, const Gradient::Gradient& gradient, const Vector2& pos, const Vector2& size) {
J2D::FillGradientRect({color1.r, color1.g, color1.b, 255}, {color2.r, color2.g, color2.b, 255}, gradient, pos, size);
}
void J2D::FillRoundedRect(const Color4 &color, const Vector2 &pos, const Vector2 &size, float radius, unsigned int subdivisions) {
void J2D::FillRoundedRect(const Color4& color, const Vector2& pos, const Vector2& size, float radius, unsigned int subdivisions) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
@@ -224,6 +263,7 @@ namespace JGL {
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 4);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::OutlineRect(const Color3& color, const Vector2& pos, const Vector2& size, float thickness) {
@@ -240,6 +280,7 @@ namespace JGL {
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 2);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::DrawLine(const Color3& color, const Vector2& A, const Vector2& B, float thickness) {
@@ -268,6 +309,7 @@ namespace JGL {
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 2);
glDisableClientState(GL_COLOR_ARRAY);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::DrawGradientLine(const Color3& color1, const Color3& color2, const Vector2& A, const Vector2& B, float thickness) {
@@ -290,6 +332,7 @@ namespace JGL {
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINES, 0, 1);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::DrawPixel(const Color3& color, const Vector2& coordinates) {
@@ -322,6 +365,7 @@ namespace JGL {
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_LINE_LOOP, 0, vertices.size());
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::OutlineCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions, float thickness) {
@@ -344,6 +388,7 @@ namespace JGL {
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices.data());
glDrawArrays(GL_TRIANGLE_FAN, 0, vertices.size());
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillCircle(const Color3& color, const Vector2& center, float radius, unsigned int subdivisions) {
@@ -360,13 +405,14 @@ namespace JGL {
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_LINE_LOOP, 0, 3);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::OutlineTriangle(const Color3& color, const Triangle2D& tri, float thickness) {
J2D::OutlineTriangle({color.r, color.g, color.b, 255}, tri, thickness);
}
void J2D::FillTriangle(const Color4& color, const Triangle2D &tri) {
void J2D::FillTriangle(const Color4& color, const Triangle2D& tri) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
@@ -375,9 +421,32 @@ namespace JGL {
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillTriangle(const Color3& color, const Triangle2D &tri) {
void J2D::FillGradientTriangle(const Color4& a_color, const Color4& b_color, const Color4& c_color, const Triangle2D& tri) {
if (!inJ2D)
ERROR("Attempt to Render J2D element before J2D begin.")
Vector2 vertices[] = {{tri.A.x, tri.A.y}, {tri.B.x, tri.B.y}, {tri.C.x, tri.C.y}};
GLfloat colors[] = {a_color.r / 255.f, a_color.g / 255.f, a_color.b / 255.f, a_color.a / 255.f,b_color.r / 255.f,
b_color.g / 255.f, b_color.b / 255.f, b_color.a / 255.f,c_color.r / 255.f, c_color.g / 255.f, c_color.b / 255.f,
c_color.a / 255.f };
glEnableClientState(GL_COLOR_ARRAY);
glColorPointer(4, GL_FLOAT, sizeof(Color4), colors);
glVertexPointer(2, GL_FLOAT, sizeof(Vector2), vertices);
glDrawArrays(GL_TRIANGLES, 0, 3);
glDisableClientState(GL_COLOR_ARRAY);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J2D::FillGradientTriangle(const Color3& a_color, const Color3& b_color, const Color3& c_color, const Triangle2D& tri) {
J2D::FillGradientTriangle(Color4(a_color), Color4(b_color), Color4(c_color), tri);
}
void J2D::FillTriangle(const Color3& color, const Triangle2D& tri) {
J2D::FillTriangle({color.r, color.g, color.b, 255}, tri);
}
@@ -385,6 +454,7 @@ namespace JGL {
//Get what the draw color was before we did anything.
glGetFloatv(GL_CURRENT_COLOR, oldColor);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
wasDepthTestEnabled = false;
if (glIsEnabled(GL_DEPTH_TEST))
@@ -401,7 +471,7 @@ namespace JGL {
wasTexture2DEnabled = false,
glEnable(GL_TEXTURE_2D);
//TODO We won't always want this but for now we do.
// TODO: implement bool drawBackface as DrawString parameter.
wasCullFaceEnabled = false;
if (glIsEnabled(GL_CULL_FACE))
wasCullFaceEnabled = true,
@@ -452,6 +522,7 @@ namespace JGL {
glColor4f(color.r / 255.f, color.g / 255.f, color.b / 255.f, color.a / 255.f);
glVertexPointer(3, GL_FLOAT, sizeof(Vector3), vertices);
glDrawArrays(GL_LINES, 0, 2);
glColor4f(baseColor[0], baseColor[1], baseColor[2], baseColor[3]);
}
void J3D::DrawLine(const Color3& color, const Vector3& A, const Vector3& B, float thickness) {

View File

@@ -8,6 +8,8 @@
#include <freetype2/ft2build.h>
#include FT_FREETYPE_H
#include FT_OUTLINE_H
#include <JGL/FontCache.h>
#endif
#if _WIN32
#include <ft2build.h>
@@ -18,36 +20,10 @@
namespace JGL::Detail
{
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;
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 : fonts)
if (f.index >= newIndex)
newIndex = f.index + 1;
font.index = newIndex;
fonts.push_back(font);
std::cout << "Loaded font from " << font_path << " with index " << newIndex << std::endl;
return newIndex;
}
bool InitTextEngine() {
glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // NOTE: This MUST be called for text rendering to work properly!!!
@@ -120,18 +96,40 @@ namespace JGL
return Font(path);
}
Vector2 Font::MeasureString(const std::string &text, float ptSize) {
Vector2 Font::MeasureString(const std::string &text, unsigned int ptSize) {
// TODO: Check if a font of that size is in the cache and if so use the info from that because this is sloooooooooooooow.
// That'll make it go vroom vroom.
// TODO: Work in-progress implementation unfortunately.
// This is likely returning slightly incorrect results for likely several reasons.
// Half-ass solution for now ~ dawsh.
FT_BBox glyph_bbox;
Vector2 extents = Vector2(0,0);
bool font_of_size_in_cache = false;
for(const auto& f : fontCache.getFonts()) {
if (f->getFontSize() == ptSize) {
font_of_size_in_cache = true;
break;
}
}
if (font_of_size_in_cache) {
CachedFont* font;
for (auto* f: fontCache.getFonts())
if (f->getFontSize() == ptSize)
font = f;
for (const char& c : text)
extents.x += font->getGlyph(c)->advanceX;
extents.y = ptSize;
return extents;
}
FT_Set_Pixel_Sizes(this->face, ptSize, ptSize);
Vector2 extents = Vector2(0,0);
for (const char& c : text) {
FT_GlyphSlot slot = face->glyph;
@@ -160,8 +158,6 @@ namespace JGL
}
}
return extents;
}

View File

@@ -18,8 +18,6 @@
namespace JGL {
FontCache fontCache; // <-- Not implemented yet
void PurgeFontCache() {
fontCache.purgeCache();
}
@@ -121,8 +119,8 @@ namespace JGL {
//Texture parameters are restored when the texture is bound
glBindTexture(GL_TEXTURE_2D, *cachedFont->getTexture());
std::vector<GLfloat> vertices;
std::vector<GLfloat> texcoords;
std::vector<std::array<GLfloat, 12>> vertices(text.size());
std::vector<std::array<GLfloat, 12>> texcoords(text.size());
for (int i = 0; i < text.length(); i++) {
float x2, y2, w, h;
@@ -145,16 +143,15 @@ namespace JGL {
x2 + w, y2
};
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());
vertices[i] = glyph_vertices;
texcoords[i] = glyph_texcoords;
}
glVertexPointer(2, GL_FLOAT, sizeof(GLfloat) * 2, vertices.data());
glTexCoordPointer(2, GL_FLOAT, sizeof(GLfloat) * 2, texcoords.data());
glDrawArrays(GL_TRIANGLES, 0, vertices.size() / 2);
glDrawArrays(GL_TRIANGLES, 0, vertices.size() * 6);
glBindTexture(GL_TEXTURE_2D, 0);
glColor4f(1, 1, 1, 1);
}
void J2D::DrawString(const Color3& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font) {
@@ -169,7 +166,7 @@ namespace JGL {
float x = pos.x;
float y = pos.y;
float z = pos.z;
std::vector<GLuint> textures(text.length());;
std::vector<GLuint> textures(text.length());
glUseProgram(0); // Fixed-function pipeline.
glColor4f(color.r, color.g, color.b, 1.0f);
@@ -245,7 +242,7 @@ namespace JGL {
glDeleteTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, 0); // Unbind texture
glColor4f(1, 1, 1, 1);
glPopMatrix();
}
}