Compare commits
3 Commits
Prerelease
...
Prerelease
Author | SHA1 | Date | |
---|---|---|---|
a22a83d2f8 | |||
76cd48c9b7 | |||
38bb1b22ce |
Binary file not shown.
@@ -28,13 +28,29 @@
|
||||
#include <J3ML/Geometry/Triangle2D.hpp>
|
||||
#include <J3ML/J3ML.hpp>
|
||||
#include <JGL/types/Font.h>
|
||||
#include <JGL/types/VRamList.h>
|
||||
|
||||
// Fonts that are included by default.
|
||||
namespace JGL::Fonts {
|
||||
void Init();
|
||||
// Built in fonts.
|
||||
inline Font Jupiteroid;
|
||||
}
|
||||
|
||||
// Simple shapes that are pre-computed and used in some draw functions.
|
||||
namespace JGL::ShapeCache {
|
||||
inline VRamList* cube_vertex_data = nullptr;
|
||||
inline VRamList* cube_index_data = nullptr;
|
||||
void Init();
|
||||
}
|
||||
|
||||
/// OpenGL Wrapper for rendering 2D & 3D graphics in both a 2D and 3D context.
|
||||
namespace JGL {
|
||||
|
||||
|
||||
using namespace J3ML::LinearAlgebra;
|
||||
using namespace J3ML::Geometry;
|
||||
|
||||
|
||||
[[nodiscard]] bool Init(const Vector2& window_size, float fovY, float far_plane);
|
||||
|
||||
/// @param window_size
|
||||
@@ -336,7 +352,7 @@ namespace JGL::J2D {
|
||||
/// @param scale The value (in both axes) to scale the text by. Defaults to {1,1}.
|
||||
/// @param size The point-size at which to render the font out. Re-using the same point-size allows efficient glyph caching.
|
||||
/// @param font The font to use for rendering. @see Font.
|
||||
void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font);
|
||||
void DrawString(const Color4& color, const std::string& text, float x, float y, float scale, u32 size, const Font& font = Fonts::Jupiteroid);
|
||||
|
||||
|
||||
/// Draws an Arc (section of a circle) to the screen.
|
||||
@@ -652,7 +668,7 @@ namespace JGL::J3D {
|
||||
/// @param font The font object to use when drawing.
|
||||
/// @param angle The orientation in 3D space.
|
||||
/// @param draw_back_face
|
||||
void DrawString(const Color4& color, const std::string& text, const Vector3& pos, float scale, u32 size, const Font& font, const EulerAngleXYZ& angle = {0, 0, 0}, bool draw_back_face = false);
|
||||
void DrawString(const Color4& color, const std::string& text, const Vector3& pos, float scale, u32 size, const Font& font = Fonts::Jupiteroid, const EulerAngleXYZ& angle = {0, 0, 0}, bool draw_back_face = false);
|
||||
|
||||
/// Draws a string of text in 3D space that is always facing the exact direction of the camera projection.
|
||||
void DrawBillboardString();
|
||||
|
@@ -1,9 +0,0 @@
|
||||
#pragma once
|
||||
#include <JGL/types/VRamList.h>
|
||||
#include <array>
|
||||
|
||||
namespace JGL::ShapeCache {
|
||||
inline VRamList* cube_vertex_data = nullptr;
|
||||
inline VRamList* cube_index_data = nullptr;
|
||||
void Init();
|
||||
}
|
@@ -13,8 +13,6 @@ extern "C" typedef struct FT_LibraryRec_* FT_Library;
|
||||
|
||||
namespace JGL
|
||||
{
|
||||
|
||||
//bool Init();
|
||||
bool InitTextEngine();
|
||||
|
||||
/// A Font class implementation.
|
||||
@@ -23,7 +21,8 @@ namespace JGL
|
||||
public:
|
||||
/// Default constructor does not initialize any members
|
||||
Font() = default;
|
||||
Font(const std::filesystem::path& path);
|
||||
explicit Font(const std::filesystem::path& path);
|
||||
Font(const unsigned char* data, const size_t& size);
|
||||
/// Destructor handles freeing of the underlying asset handle.
|
||||
~Font();
|
||||
static Font LoadTTF(const std::filesystem::path& filepath);
|
||||
@@ -35,6 +34,6 @@ namespace JGL
|
||||
Vector2 MeasureString(const std::string& text, unsigned int ptSize);
|
||||
public:
|
||||
int index = 0;
|
||||
FT_Face face;
|
||||
FT_Face face = nullptr;
|
||||
};
|
||||
}
|
22
main.cpp
22
main.cpp
@@ -7,10 +7,10 @@
|
||||
#include <J3ML/Geometry/AABB.hpp>
|
||||
|
||||
using J3ML::LinearAlgebra::Vector2;
|
||||
using namespace JGL::Fonts;
|
||||
using namespace JGL;
|
||||
|
||||
JGL::Font FreeSans;
|
||||
JGL::Font Jupiteroid;
|
||||
float fps = 0.0f;
|
||||
|
||||
class Gizmo
|
||||
@@ -111,7 +111,6 @@ public:
|
||||
Logger::Fatal("Initialization failed.");
|
||||
|
||||
FreeSans = JGL::Font("assets/fonts/FreeSans.ttf");
|
||||
Jupiteroid = JGL::Font("assets/fonts/Jupiteroid.ttf");
|
||||
|
||||
glClearColor(0.f, 0.f, 0.f, 0.f);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
@@ -132,7 +131,8 @@ public:
|
||||
|
||||
void display() {
|
||||
|
||||
float dt = 1.f / fps;
|
||||
float dt = GetDeltaTime();
|
||||
|
||||
JGL::Update(GetSize());
|
||||
|
||||
if (fov_increasing)
|
||||
@@ -232,6 +232,9 @@ public:
|
||||
}
|
||||
|
||||
void OnRefresh(float elapsed) override {
|
||||
|
||||
fps = GetRefreshRate();
|
||||
|
||||
if (IsKeyDown(Keys::RightArrow))
|
||||
camera->angle.y += 45.f * elapsed;
|
||||
if (IsKeyDown(Keys::LeftArrow))
|
||||
@@ -301,12 +304,13 @@ int main(int argc, char** argv) {
|
||||
window->SetVsyncEnabled(false);
|
||||
|
||||
while (window->IsAlive()) {
|
||||
std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
|
||||
window->PollEvents();
|
||||
window->Refresh();
|
||||
std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
|
||||
std::chrono::duration<float> frame_time = stop - start;
|
||||
fps = 1.0f / frame_time.count();
|
||||
window->ManagedRefresh();
|
||||
//std::chrono::high_resolution_clock::time_point start = std::chrono::high_resolution_clock::now();
|
||||
//window->PollEvents();
|
||||
//window->Refresh();
|
||||
//std::chrono::high_resolution_clock::time_point stop = std::chrono::high_resolution_clock::now();
|
||||
//std::chrono::duration<float> frame_time = stop - start;
|
||||
//fps = 1.0f / frame_time.count();
|
||||
}
|
||||
return 0;
|
||||
}
|
8671
src/Fonts.cpp
Normal file
8671
src/Fonts.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -11,7 +11,6 @@
|
||||
#include <J3ML/Geometry/OBB.hpp>
|
||||
#include <JGL/types/VRamList.h>
|
||||
#include <JGL/types/Font.h>
|
||||
#include <JGL/ShapeCache.h>
|
||||
|
||||
JGL::RenderTarget* render_target = nullptr;
|
||||
GLfloat oldColor[4] = {0, 0, 0, 1};
|
||||
@@ -50,6 +49,7 @@ namespace JGL {
|
||||
}
|
||||
|
||||
InitTextEngine();
|
||||
Fonts::Init();
|
||||
ShapeCache::Init();
|
||||
wS = window_size;
|
||||
j3d_fov = fovY;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
#include <JGL/ShapeCache.h>
|
||||
#include <JGL/JGL.h>
|
||||
|
||||
void JGL::ShapeCache::Init() {
|
||||
if (!cube_vertex_data) {
|
||||
|
@@ -4,6 +4,7 @@
|
||||
#include <iostream>
|
||||
#include <glad/glad.h>
|
||||
#include <JGL/logger/logger.h>
|
||||
#include <JGL/JGL.h>
|
||||
|
||||
#if __linux__
|
||||
#include <freetype2/ft2build.h>
|
||||
@@ -58,25 +59,38 @@ namespace JGL {
|
||||
return Detail::InitTextEngine();
|
||||
}
|
||||
|
||||
Font::Font(const unsigned char* data, const size_t& size) {
|
||||
if (Detail::ft == nullptr)
|
||||
throw std::runtime_error("Error::FREETYPE: FT_Library was not initialized before attempting to load a font!");
|
||||
|
||||
if (FT_New_Memory_Face(Detail::ft, data, size, 0, &face))
|
||||
throw std::runtime_error("Error::FREETYPE: Failed to load font!");
|
||||
|
||||
unsigned int new_index = 0;
|
||||
for (const auto& f : Detail::fonts)
|
||||
if (f.index >= new_index)
|
||||
new_index = f.index + 1;
|
||||
index = new_index;
|
||||
|
||||
Detail::fonts.push_back(*this);
|
||||
std::cout << "Loaded font from memory at " << static_cast<const void*>(data) << " with index " << new_index << std::endl;
|
||||
}
|
||||
|
||||
Font::Font(const std::filesystem::path& path) {
|
||||
if (Detail::ft == nullptr)
|
||||
throw std::runtime_error("Error::FREETYPE: FT_Library was not initialized before attempting to load a font!");
|
||||
|
||||
Font font;
|
||||
if (FT_New_Face(Detail::ft, path.string().c_str(), 0, &face)) {
|
||||
std::cout << "Error::FREETYPE: Failed to load font!" << std::endl;
|
||||
if (FT_New_Face(Detail::ft, path.string().c_str(), 0, &face))
|
||||
throw 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;
|
||||
unsigned int new_index = 0;
|
||||
for (const auto& f : Detail::fonts)
|
||||
if (f.index >= new_index)
|
||||
new_index = f.index + 1;
|
||||
|
||||
index = new_index;
|
||||
Detail::fonts.push_back(*this);
|
||||
std::cout << "Loaded font from " << path << " with index " << new_index << std::endl;
|
||||
}
|
||||
|
||||
Font::~Font() {
|
||||
@@ -119,14 +133,14 @@ namespace JGL {
|
||||
return extents;
|
||||
}
|
||||
|
||||
jlog::Warning("Measuring a font size that is not cached, This is *super* slow.");
|
||||
FT_Set_Pixel_Sizes(this->face, ptSize, ptSize);
|
||||
jlog::Warning("Measuring a font size that is not cached, Defaulting to Jupiteroid.");
|
||||
FT_Set_Pixel_Sizes(Fonts::Jupiteroid.face, ptSize, ptSize);
|
||||
|
||||
for (const char& c : text) {
|
||||
FT_GlyphSlot slot = face->glyph;
|
||||
auto glyph_index = FT_Get_Char_Index(this->face, c);
|
||||
FT_GlyphSlot slot = Fonts::Jupiteroid.face->glyph;
|
||||
auto glyph_index = FT_Get_Char_Index(Fonts::Jupiteroid.face, c);
|
||||
|
||||
auto error = FT_Load_Glyph(face, glyph_index, FT_LOAD_DEFAULT);
|
||||
auto error = FT_Load_Glyph(Fonts::Jupiteroid.face, glyph_index, FT_LOAD_DEFAULT);
|
||||
|
||||
if (error)
|
||||
continue;
|
||||
@@ -146,9 +160,6 @@ namespace JGL {
|
||||
if (extents.y < ptSize)
|
||||
extents.y = ptSize;
|
||||
}
|
||||
|
||||
return extents;
|
||||
}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user