Unfiy all sub projects back into 1 libpalladium
This commit is contained in:
81
source/lithium/command.cpp
Normal file
81
source/lithium/command.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <pd/lithium/command.hpp>
|
||||
|
||||
PD_LITHIUM_API PD::Li::Command::Ref PD::Li::CmdPool::NewCmd() {
|
||||
if (pPoolIdx >= pPool.size()) {
|
||||
Resize(pPool.size() + 128);
|
||||
}
|
||||
Command::Ref nu = pPool[pPoolIdx++];
|
||||
nu->Layer = Layer;
|
||||
nu->Index = pPoolIdx - 1;
|
||||
return nu;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Init(size_t initial_size) {
|
||||
Resize(initial_size);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Deinit() {
|
||||
for (auto it : pPool) {
|
||||
Command::Delete(it);
|
||||
}
|
||||
pPool.clear();
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Resize(size_t nulen) {
|
||||
if (nulen <= pPool.size()) {
|
||||
return; // no idea yet
|
||||
}
|
||||
size_t oldlen = pPool.size();
|
||||
pPool.resize(nulen);
|
||||
for (size_t i = oldlen; i < pPool.size(); i++) {
|
||||
pPool[i] = Command::New();
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Reset() {
|
||||
for (u32 i = 0; i < pPoolIdx; i++) {
|
||||
pPool[i]->Clear();
|
||||
}
|
||||
pPoolIdx = 0;
|
||||
}
|
||||
|
||||
PD::Li::Command::Ref PD::Li::CmdPool::GetCmd(size_t idx) const {
|
||||
return pPool[idx];
|
||||
}
|
||||
PD::Li::Command::Ref PD::Li::CmdPool::GetCmd(size_t idx) { return pPool[idx]; }
|
||||
|
||||
size_t PD::Li::CmdPool::Size() const { return pPoolIdx; }
|
||||
size_t PD::Li::CmdPool::Cap() const { return pPool.size(); }
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Merge(CmdPool& p) {
|
||||
Copy(p);
|
||||
p.Reset();
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Copy(CmdPool& p) {
|
||||
if (pPoolIdx + p.Size() > pPool.size()) {
|
||||
Resize(pPoolIdx + p.Size());
|
||||
}
|
||||
for (size_t i = 0; i < p.Size(); i++) {
|
||||
size_t idx = pPoolIdx++;
|
||||
*pPool[idx] = *p.GetCmd(i);
|
||||
pPool[idx]->Index = idx;
|
||||
pPool[idx]->Layer += Layer;
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void PD::Li::CmdPool::Sort() {
|
||||
if (pPoolIdx < 2) return;
|
||||
std::sort(begin(), end(), pTheOrder);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API bool PD::Li::CmdPool::pTheOrder(const Command::Ref& a,
|
||||
const Command::Ref& b) {
|
||||
if (a->Layer == b->Layer) {
|
||||
if (a->Tex == b->Tex) {
|
||||
return a->Index < b->Index;
|
||||
}
|
||||
return a->Tex < b->Tex;
|
||||
}
|
||||
return a->Layer < b->Layer;
|
||||
}
|
||||
320
source/lithium/drawlist.cpp
Executable file
320
source/lithium/drawlist.cpp
Executable file
@@ -0,0 +1,320 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/lithium/drawlist.hpp>
|
||||
#include <pd/lithium/renderer.hpp>
|
||||
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
namespace PD {
|
||||
namespace Li {
|
||||
PD_LITHIUM_API DrawList::DrawList(int initial_size) {
|
||||
DrawSolid();
|
||||
pPool.Init(initial_size);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API DrawList::~DrawList() {
|
||||
Clear();
|
||||
pPool.Deinit();
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawSolid() { CurrentTex = Gfx::GetSolidTex(); }
|
||||
|
||||
PD_LITHIUM_API void DrawList::Clear() {
|
||||
pNumIndices = 0;
|
||||
pNumVertices = 0;
|
||||
pPool.Reset();
|
||||
pPath.clear();
|
||||
if (pCurrentFont) {
|
||||
pCurrentFont->CleanupTMS();
|
||||
}
|
||||
while (!pClipRects.empty()) {
|
||||
pClipRects.pop();
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::Merge(DrawList::Ref list) {
|
||||
pPool.Merge(list->pPool);
|
||||
/*for (size_t i = 0; i < list->pDrawList.size(); i++) {
|
||||
pNumIndices += list->pDrawList[i]->IndexBuffer.size();
|
||||
pNumVertices += list->pDrawList[i]->VertexBuffer.size();
|
||||
auto cmd = pPool.NewCmd();
|
||||
pDrawList.push_back(list->pDrawList[i]);
|
||||
}*/
|
||||
/** Make sure The list gets cleared */
|
||||
list->Clear();
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::Copy(DrawList::Ref list) {
|
||||
pPool.Copy(list->pPool);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::Optimize() {
|
||||
#ifndef NDEBUG
|
||||
PD::TT::Scope s("Optimize");
|
||||
#endif
|
||||
/*std::sort(pDrawList.begin(), pDrawList.end(),
|
||||
[](const PD::Li::Command::Ref &a, const PD::Li::Command::Ref &b) {
|
||||
if (a->Layer == b->Layer) { // Same layer
|
||||
if (a->Tex == b->Tex) { // same tex
|
||||
return a->Index < b->Index;
|
||||
}
|
||||
return a->Tex < b->Tex; // order by address
|
||||
}
|
||||
return a->Layer < b->Layer; // Order by layer
|
||||
});*/
|
||||
}
|
||||
|
||||
PD_LITHIUM_API Command::Ref DrawList::GetNewCmd() {
|
||||
Command::Ref cmd = pPool.NewCmd();
|
||||
cmd->Index = pPool.Size() - 1;
|
||||
cmd->Tex = CurrentTex->Address;
|
||||
pClipCmd(cmd);
|
||||
return cmd;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::pClipCmd(Command::Ref cmd) {
|
||||
if (!pClipRects.empty()) {
|
||||
cmd->ScissorOn = true;
|
||||
cmd->ScissorRect = ivec4(pClipRects.top());
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::PathArcToN(const fvec2& c, float radius,
|
||||
float a_min, float a_max,
|
||||
int segments) {
|
||||
// Path.push_back(c);
|
||||
PathReserve(segments + 1);
|
||||
for (int i = 0; i < segments; i++) {
|
||||
float a = a_min + ((float)i / (float)segments) * (a_max - a_min);
|
||||
PathAdd(vec2(c.x + std::cos(a) * radius, c.y + std::sin(a) * radius));
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::PathFastArcToN(const fvec2& c, float r,
|
||||
float amin, float amax, int s) {
|
||||
/**
|
||||
* Funcion with less division overhead
|
||||
* Usefull for stuff where a lot of calculations are required
|
||||
*/
|
||||
float d = (amax - amin) / s;
|
||||
PathReserve(s + 1);
|
||||
for (int i = 0; i <= s; i++) {
|
||||
float a = amin + i * d;
|
||||
PathAdd(fvec2(c.x + std::cos(a) * r, c.y + std::sin(a) * r));
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::PathRect(fvec2 a, fvec2 b, float rounding) {
|
||||
if (rounding == 0.f) {
|
||||
PathAdd(a);
|
||||
PathAdd(vec2(b.x, a.y));
|
||||
PathAdd(b);
|
||||
PathAdd(vec2(a.x, b.y));
|
||||
} else {
|
||||
float r = std::min({rounding, (b.x - a.x) * 0.5f, (b.y - a.y) * 0.5f});
|
||||
/** Calculate Optimal segment count automatically */
|
||||
float corner = M_PI * 0.5f;
|
||||
int segments = std::max(3, int(std::ceil(corner / (6.0f * M_PI / 180.0f))));
|
||||
|
||||
/**
|
||||
* To Correctly render filled shapes with Paths API
|
||||
* The Commands need to be setup clockwise
|
||||
*/
|
||||
/** Top Left */
|
||||
PathAdd(vec2(a.x + r, a.y));
|
||||
PathFastArcToN(vec2(b.x - r, a.y + r), r, -M_PI / 2.0f, 0.0f, segments);
|
||||
/** Top Right */
|
||||
PathAdd(vec2(b.x, b.y - r));
|
||||
PathFastArcToN(vec2(b.x - r, b.y - r), r, 0.0f, M_PI / 2.0f, segments);
|
||||
/** Bottom Right */
|
||||
PathAdd(vec2(a.x + r, b.y));
|
||||
PathFastArcToN(vec2(a.x + r, b.y - r), r, M_PI / 2.0f, M_PI, segments);
|
||||
/** Bottom Left */
|
||||
PathAdd(vec2(a.x, a.y + r));
|
||||
PathFastArcToN(vec2(a.x + r, a.y + r), r, M_PI, 3.0f * M_PI / 2.0f,
|
||||
segments);
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::PathRectEx(fvec2 a, fvec2 b, float rounding,
|
||||
u32 flags) {
|
||||
if (rounding == 0.f) {
|
||||
PathAdd(a);
|
||||
PathAdd(vec2(b.x, a.y));
|
||||
PathAdd(b);
|
||||
PathAdd(vec2(a.x, b.y));
|
||||
} else {
|
||||
float r = std::min({rounding, (b.x - a.x) * 0.5f, (b.y - a.y) * 0.5f});
|
||||
/** Calculate Optimal segment count automatically */
|
||||
float corner = M_PI * 0.5f;
|
||||
int segments = std::max(3, int(std::ceil(corner / (6.0f * M_PI / 180.0f))));
|
||||
|
||||
/**
|
||||
* To Correctly render filled shapes with Paths API
|
||||
* The Commands need to be setup clockwise
|
||||
*/
|
||||
/** Top Left */
|
||||
if (flags & LiPathRectFlags_KeepTopLeft) {
|
||||
PathAdd(a);
|
||||
} else {
|
||||
PathAdd(vec2(a.x + r, a.y));
|
||||
PathFastArcToN(vec2(b.x - r, a.y + r), r, -M_PI / 2.0f, 0.0f, segments);
|
||||
}
|
||||
|
||||
/** Top Right */
|
||||
if (flags & LiPathRectFlags_KeepTopRight) {
|
||||
PathAdd(vec2(b.x, a.y));
|
||||
} else {
|
||||
PathAdd(vec2(b.x, b.y - r));
|
||||
PathFastArcToN(vec2(b.x - r, b.y - r), r, 0.0f, M_PI / 2.0f, segments);
|
||||
}
|
||||
/** Bottom Right */
|
||||
if (flags & LiPathRectFlags_KeepBotRight) {
|
||||
PathAdd(b);
|
||||
} else {
|
||||
PathAdd(vec2(a.x + r, b.y));
|
||||
PathFastArcToN(vec2(a.x + r, b.y - r), r, M_PI / 2.0f, M_PI, segments);
|
||||
}
|
||||
/** Bottom Left */
|
||||
if (flags & LiPathRectFlags_KeepBotLeft) {
|
||||
PathAdd(vec2(a.x, b.y));
|
||||
} else {
|
||||
PathAdd(vec2(a.x, a.y + r));
|
||||
PathFastArcToN(vec2(a.x + r, a.y + r), r, M_PI, 3.0f * M_PI / 2.0f,
|
||||
segments);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawRect(const fvec2& pos, const fvec2& size,
|
||||
u32 color, int thickness) {
|
||||
PathRect(pos, pos + size);
|
||||
// Flags is currently hardcoded (1 = close)
|
||||
PathStroke(color, thickness, 1);
|
||||
}
|
||||
void DrawList::DrawRectFilled(const fvec2& pos, const fvec2& size, u32 color) {
|
||||
PathRect(pos, pos + size);
|
||||
PathFill(color);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawTriangle(const fvec2& a, const fvec2& b,
|
||||
const fvec2& c, u32 color,
|
||||
int thickness) {
|
||||
PathAdd(a);
|
||||
PathAdd(b);
|
||||
PathAdd(c);
|
||||
PathStroke(color, thickness, 1);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawTriangleFilled(const fvec2& a, const fvec2& b,
|
||||
const fvec2& c, u32 color) {
|
||||
PathAdd(a);
|
||||
PathAdd(b);
|
||||
PathAdd(c);
|
||||
PathFill(color);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawCircle(const fvec2& center, float rad,
|
||||
u32 color, int num_segments,
|
||||
int thickness) {
|
||||
if (num_segments <= 0) {
|
||||
// Auto Segment
|
||||
} else {
|
||||
float am = (M_PI * 2.0f) * ((float)num_segments) / (float)num_segments;
|
||||
PathArcToN(center, rad, 0.f, am, num_segments);
|
||||
}
|
||||
DrawSolid(); // Only Solid Color Supported
|
||||
PathStroke(color, thickness, (1 << 0));
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawCircleFilled(const fvec2& center, float rad,
|
||||
u32 color, int num_segments) {
|
||||
if (num_segments <= 0) {
|
||||
// Auto Segment
|
||||
} else {
|
||||
float am = (M_PI * 2.0f) * ((float)num_segments) / (float)num_segments;
|
||||
PathArcToN(center, rad, 0.f, am, num_segments);
|
||||
}
|
||||
PathFill(color);
|
||||
}
|
||||
|
||||
// TODO: Don't render OOS
|
||||
PD_LITHIUM_API void DrawList::DrawPolyLine(const std::vector<fvec2>& points,
|
||||
u32 clr, u32 flags, int thickness) {
|
||||
if (points.size() < 2) {
|
||||
return;
|
||||
}
|
||||
DrawSolid();
|
||||
auto cmd = GetNewCmd();
|
||||
bool close = (flags & (1 << 0));
|
||||
int num_points = close ? (int)points.size() : (int)points.size() - 1;
|
||||
if (flags & (1 << 1)) {
|
||||
// TODO: Find a way to draw less garbage looking lines
|
||||
} else {
|
||||
// Non antialiased lines look awful when rendering with thickness != 1
|
||||
for (int i = 0; i < num_points; i++) {
|
||||
int j = (i + 1) == (int)points.size() ? 0 : (i + 1);
|
||||
auto line = Renderer::PrimLine(points[i], points[j], thickness);
|
||||
Renderer::CmdQuad(cmd, line, vec4(0.f, 1.f, 1.f, 0.f), clr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawConvexPolyFilled(
|
||||
const std::vector<fvec2>& points, u32 clr) {
|
||||
if (points.size() < 3) {
|
||||
return; // Need at least three points
|
||||
}
|
||||
auto cmd = GetNewCmd();
|
||||
Renderer::CmdConvexPolyFilled(cmd, points, clr, CurrentTex);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawText(const fvec2& pos,
|
||||
const std::string& text, u32 color) {
|
||||
if (!pCurrentFont) {
|
||||
return;
|
||||
}
|
||||
pCurrentFont->CmdTextEx(pPool, pos, color, pFontScale, text);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawTextEx(const fvec2& p,
|
||||
const std::string& text, u32 color,
|
||||
LiTextFlags flags, const fvec2& box) {
|
||||
if (!pCurrentFont) {
|
||||
return;
|
||||
}
|
||||
pCurrentFont->CmdTextEx(pPool, p, color, pFontScale, text, flags, box);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void DrawList::DrawLine(const fvec2& a, const fvec2& b,
|
||||
u32 color, int t) {
|
||||
PathAdd(a);
|
||||
PathAdd(b);
|
||||
PathStroke(color, t);
|
||||
}
|
||||
} // namespace Li
|
||||
} // namespace PD
|
||||
432
source/lithium/font.cpp
Normal file
432
source/lithium/font.cpp
Normal file
@@ -0,0 +1,432 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/lithium/font.hpp>
|
||||
|
||||
/** Due to Limitations of Shared Lib Stuff */
|
||||
#ifdef PD_LITHIUM_BUILD_SHARED
|
||||
#define PD_TRUETYPE_IMPLEMENTATION
|
||||
#endif
|
||||
#include <pd/external/stb_truetype.hpp>
|
||||
#include <pd/lithium/renderer.hpp>
|
||||
|
||||
#ifdef PD_LI_INCLUDE_FONTS
|
||||
#include <pd/lithium/fonts.hpp>
|
||||
#endif
|
||||
|
||||
namespace PD {
|
||||
namespace Li {
|
||||
PD_LITHIUM_API void Font::LoadDefaultFont(int id, int pixel_height) {
|
||||
#ifdef PD_LI_INCLUDE_FONTS
|
||||
if (id < pNumFonts) {
|
||||
auto font = pFontData[id];
|
||||
LoadTTF(std::vector<u8>(&pFontsDataRaw[font.StartOff],
|
||||
&pFontsDataRaw[font.StartOff + font.Size]),
|
||||
pixel_height);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Font::LoadTTF(const std::string& path, int height) {
|
||||
/**
|
||||
* Just use LoadFile2Mem which looks way cleaner
|
||||
* and helps not having the font loading code twice
|
||||
* when adding LoadTTF with mem support
|
||||
*/
|
||||
TT::Scope st("LI_LoadTTF_" + path);
|
||||
auto font = PD::IO::LoadFile2Mem(path);
|
||||
LoadTTF(font, height);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Font::pMakeAtlas(bool final, std::vector<u8>& font_tex,
|
||||
int texszs, PD::Li::Texture::Ref tex) {
|
||||
auto t =
|
||||
Gfx::LoadTex(font_tex, texszs, texszs, Texture::RGBA32, Texture::LINEAR);
|
||||
tex->CopyFrom(t);
|
||||
Textures.push_back(tex);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Font::LoadTTF(const std::vector<u8>& data, int height) {
|
||||
/**
|
||||
* Some additional Info:
|
||||
* Removed the stbtt get bitmapbox as we dont need to place
|
||||
* the glyps nicely in the tex. next step would be using the free
|
||||
* space on the y axis to get mor glyphs inside
|
||||
*/
|
||||
PixelHeight = height;
|
||||
int texszs = PD::BitUtil::GetPow2(height * 16);
|
||||
if (texszs > 1024) {
|
||||
texszs = 1024; // Max size
|
||||
}
|
||||
|
||||
pdtt_fontinfo inf;
|
||||
if (!pdtt_InitFont(&inf, data.data(), 0)) {
|
||||
return;
|
||||
}
|
||||
|
||||
float scale = pdtt_ScaleForPixelHeight(&inf, PixelHeight);
|
||||
|
||||
int ascent, descent, lineGap;
|
||||
pdtt_GetFontVMetrics(&inf, &ascent, &descent, &lineGap);
|
||||
int baseline = static_cast<int>(ascent * scale);
|
||||
|
||||
// Cache to not render same codepoint tex twice
|
||||
std::map<u32, int> buf_cache;
|
||||
|
||||
std::vector<u8> font_tex(texszs * texszs * 4, 0);
|
||||
auto tex = Texture::New();
|
||||
fvec2 off;
|
||||
|
||||
bool empty = true;
|
||||
|
||||
for (u32 ii = 0x0000; ii <= 0xFFFF; ii++) {
|
||||
int gi = pdtt_FindGlyphIndex(&inf, ii);
|
||||
if (gi == 0) continue;
|
||||
if (pdtt_IsGlyphEmpty(&inf, gi)) continue;
|
||||
|
||||
int w = 0, h = 0, xo = 0, yo = 0;
|
||||
unsigned char* bitmap =
|
||||
pdtt_GetCodepointBitmap(&inf, scale, scale, ii, &w, &h, &xo, &yo);
|
||||
if (!bitmap || w <= 0 || h <= 0) {
|
||||
if (bitmap) free(bitmap);
|
||||
continue;
|
||||
}
|
||||
|
||||
u32 hashed_map = IO::HashMemory(std::vector<u8>(bitmap, bitmap + (w * h)));
|
||||
if (buf_cache.find(hashed_map) != buf_cache.end()) {
|
||||
Codepoint c = GetCodepoint(buf_cache[hashed_map]);
|
||||
c.pCodepoint = ii;
|
||||
CodeMap[ii] = c;
|
||||
free(bitmap);
|
||||
continue;
|
||||
} else {
|
||||
buf_cache[hashed_map] = ii;
|
||||
}
|
||||
|
||||
// Next row
|
||||
if (off.x + w > texszs) {
|
||||
off.y += PixelHeight;
|
||||
off.x = 0.0f;
|
||||
}
|
||||
// Bake cause we go out of the tex
|
||||
if (off.y + PixelHeight > texszs) {
|
||||
pMakeAtlas(false, font_tex, texszs, tex);
|
||||
tex = Texture::New();
|
||||
off = 0;
|
||||
std::fill(font_tex.begin(), font_tex.end(), 0);
|
||||
empty = true;
|
||||
}
|
||||
|
||||
// UVs & Codepoint
|
||||
Codepoint c;
|
||||
fvec4 uvs;
|
||||
// cast the ints to floats and not the floats...
|
||||
// dont know where my mind was when creating the code
|
||||
uvs.x = off.x / static_cast<float>(texszs);
|
||||
uvs.y = off.y / static_cast<float>(texszs);
|
||||
uvs.z = (off.x + w) / static_cast<float>(texszs);
|
||||
uvs.w = (off.y + h) / static_cast<float>(texszs);
|
||||
// Flip if needed
|
||||
if (Gfx::Flags() & LiBackendFlags_FlipUV_Y) {
|
||||
uvs.y = 1.f - uvs.y;
|
||||
uvs.w = 1.f - uvs.w;
|
||||
}
|
||||
c.SimpleUV = uvs;
|
||||
c.Tex = tex;
|
||||
c.Size = fvec2(w, h);
|
||||
c.Offset = baseline + yo;
|
||||
c.pCodepoint = ii;
|
||||
|
||||
for (int y = 0; y < h; ++y) {
|
||||
for (int x = 0; x < w; ++x) {
|
||||
int map_pos = ((static_cast<int>(off.y) + y) * texszs +
|
||||
(static_cast<int>(off.x) + x)) *
|
||||
4;
|
||||
font_tex[map_pos + 0] = 255;
|
||||
font_tex[map_pos + 1] = 255;
|
||||
font_tex[map_pos + 2] = 255;
|
||||
font_tex[map_pos + 3] = bitmap[x + y * w];
|
||||
}
|
||||
}
|
||||
|
||||
empty = false;
|
||||
CodeMap[ii] = c;
|
||||
free(bitmap);
|
||||
|
||||
// offset by 1 (prevents visual glitches i had)
|
||||
off.x += w + 1;
|
||||
}
|
||||
|
||||
if (!empty) {
|
||||
pMakeAtlas(true, font_tex, texszs, tex);
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API Font::Codepoint& Font::GetCodepoint(u32 cp) {
|
||||
// Check if codepoijt exist or return a static invalid one
|
||||
auto res = CodeMap.find(cp);
|
||||
if (res == CodeMap.end()) {
|
||||
static Codepoint invalid;
|
||||
invalid.pInvalid = true;
|
||||
return invalid;
|
||||
}
|
||||
return res->second;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API fvec2 Font::GetTextBounds(const std::string& text, float scale) {
|
||||
u32 id = PD::FNV1A32(text);
|
||||
if (pTMS.find(id) != pTMS.end()) {
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return pTMS[id].Size;
|
||||
}
|
||||
// Use wstring for exemple for german äöü
|
||||
auto wtext = Strings::MakeWstring(text);
|
||||
// Create a temp position and offset as [0, 0]
|
||||
fvec2 res;
|
||||
float x = 0;
|
||||
// Curent Font Scale
|
||||
float cfs = (DefaultPixelHeight * scale) / (float)PixelHeight;
|
||||
float lh = (float)PixelHeight * cfs;
|
||||
size_t index = 0;
|
||||
for (auto& it : wtext) {
|
||||
if (it == L'\0') {
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
auto cp = GetCodepoint(it);
|
||||
if (cp.pInvalid && it != '\n' && it != '\t' && it != ' ') {
|
||||
continue;
|
||||
}
|
||||
switch (it) {
|
||||
case L'\n':
|
||||
res.y += lh;
|
||||
res.x = std::max(res.x, x);
|
||||
x = 0.f;
|
||||
break;
|
||||
case L'\t':
|
||||
x += 16 * cfs;
|
||||
break;
|
||||
case L' ':
|
||||
x += 4 * cfs;
|
||||
// Fall trough here to get the same result as in
|
||||
// TextCommand if/else Section
|
||||
default:
|
||||
x += cp.Size.x * cfs;
|
||||
if (index != wtext.size()) {
|
||||
x += 2 * cfs;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
res.x = std::max(res.x, x);
|
||||
res.y += lh;
|
||||
pTMS[id].ID = id;
|
||||
pTMS[id].Size = res;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return res;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Font::CmdTextEx(CmdPool& cmds, const fvec2& pos, u32 color,
|
||||
float scale, const std::string& text,
|
||||
LiTextFlags flags, const fvec2& box) {
|
||||
fvec2 off;
|
||||
float cfs = (DefaultPixelHeight * scale) / (float)PixelHeight;
|
||||
float lh = (float)PixelHeight * cfs;
|
||||
fvec2 td;
|
||||
fvec2 rpos = pos;
|
||||
fvec2 rbox = box;
|
||||
std::string txt = text;
|
||||
if (flags & LiTextFlags_Wrap) {
|
||||
txt = pWrapText(txt, scale, box, td);
|
||||
}
|
||||
if (flags & (LiTextFlags_AlignMid | LiTextFlags_AlignRight)) {
|
||||
td = GetTextBounds(text, scale);
|
||||
}
|
||||
if (flags & LiTextFlags_AlignMid) {
|
||||
rpos = rbox * 0.5 - td * 0.5 + pos;
|
||||
}
|
||||
if (flags & LiTextFlags_AlignRight) {
|
||||
rpos.x = rpos.x - td.x;
|
||||
}
|
||||
|
||||
std::vector<std::string> lines;
|
||||
std::istringstream iss(txt);
|
||||
std::string tmp;
|
||||
while (std::getline(iss, tmp)) {
|
||||
lines.push_back(tmp);
|
||||
}
|
||||
|
||||
for (auto& it : lines) {
|
||||
if (flags & LiTextFlags_NoOOS) {
|
||||
if (rpos.y + off.y + lh < 0) {
|
||||
off.y += lh;
|
||||
continue;
|
||||
}
|
||||
if (rpos.y + off.y > box.y && box.y != 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (flags & LiTextFlags_Short) {
|
||||
fvec2 tmp_dim;
|
||||
it = pShortText(it, scale, box - pos, tmp_dim);
|
||||
}
|
||||
auto wline = Strings::MakeWstring(it);
|
||||
auto cmd = cmds.NewCmd();
|
||||
auto Tex = GetCodepoint(wline[0]).Tex;
|
||||
if (Tex) {
|
||||
cmd->Tex = Tex->Address;
|
||||
}
|
||||
for (auto& jt : wline) {
|
||||
auto cp = GetCodepoint(jt);
|
||||
if ((cp.pInvalid && jt != L' ' && jt != L'\n' && jt != L'\t') &&
|
||||
jt != L'\r') {
|
||||
continue;
|
||||
}
|
||||
if (Tex != cp.Tex) {
|
||||
cmd = cmds.NewCmd();
|
||||
Tex = cp.Tex;
|
||||
if (Tex) {
|
||||
cmd->Tex = Tex->Address;
|
||||
}
|
||||
}
|
||||
if (jt == L'\t') {
|
||||
off.x += 16 * cfs;
|
||||
} else {
|
||||
if (jt != L' ') {
|
||||
if (flags & LiTextFlags_Shaddow) {
|
||||
// Draw
|
||||
Rect rec = Renderer::PrimRect(
|
||||
rpos + fvec2(off.x + 1, off.y + (cp.Offset * cfs)) + 1,
|
||||
cp.Size * cfs, 0.f);
|
||||
Renderer::CmdQuad(cmd, rec, cp.SimpleUV, 0xff111111);
|
||||
}
|
||||
// Draw
|
||||
Rect rec = Renderer::PrimRect(
|
||||
rpos + off + fvec2(0, (cp.Offset * cfs)), cp.Size * cfs, 0.f);
|
||||
Renderer::CmdQuad(cmd, rec, cp.SimpleUV, color);
|
||||
off.x += cp.Size.x * cfs + 2 * cfs;
|
||||
} else {
|
||||
off.x += 4 * cfs;
|
||||
}
|
||||
}
|
||||
}
|
||||
off.y += lh;
|
||||
off.x = 0;
|
||||
}
|
||||
}
|
||||
|
||||
PD_LITHIUM_API std::string Font::pWrapText(const std::string& txt, float scale,
|
||||
const PD::fvec2& max,
|
||||
PD::fvec2& dim) {
|
||||
u32 id = PD::FNV1A32(txt);
|
||||
if (pTMS.find(id) != pTMS.end()) {
|
||||
if (pTMS[id].Text.size()) {
|
||||
dim = pTMS[id].Size;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return pTMS[id].Text;
|
||||
}
|
||||
}
|
||||
std::string ret;
|
||||
std::string line;
|
||||
int lx = 0;
|
||||
std::stringstream s(txt);
|
||||
std::string tmp;
|
||||
// Simply go over every word
|
||||
while (s >> tmp) {
|
||||
auto d = GetTextBounds(tmp, scale);
|
||||
if (lx + d.x <= max.x) {
|
||||
line += tmp + ' ';
|
||||
lx += d.x;
|
||||
} else {
|
||||
ret += line + '\n';
|
||||
line = tmp + ' ';
|
||||
lx = GetTextBounds(line, scale).x;
|
||||
}
|
||||
}
|
||||
ret += line;
|
||||
dim = GetTextBounds(ret, scale);
|
||||
pTMS[id].ID = id;
|
||||
pTMS[id].Size = dim;
|
||||
pTMS[id].Text = ret;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return ret;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API std::string Font::pShortText(const std::string& txt, float scale,
|
||||
const PD::fvec2& max,
|
||||
PD::fvec2& dim) {
|
||||
u32 id = PD::FNV1A32(txt);
|
||||
if (pTMS.find(id) != pTMS.end()) {
|
||||
if (pTMS[id].Text.size()) {
|
||||
dim = pTMS[id].Size;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return pTMS[id].Text;
|
||||
}
|
||||
}
|
||||
auto test = GetTextBounds(txt, scale);
|
||||
if (test.x < max.x) {
|
||||
return txt;
|
||||
}
|
||||
std::string ext;
|
||||
std::string ph = "(...)"; // placeholder
|
||||
std::string tmp = txt;
|
||||
std::string ret;
|
||||
auto maxlen = max.x;
|
||||
size_t ext_ = tmp.find_last_of('.');
|
||||
if (ext_ != tmp.npos) {
|
||||
ext = tmp.substr(ext_);
|
||||
tmp = tmp.substr(0, ext_);
|
||||
}
|
||||
maxlen -= GetTextBounds(ext, scale).x;
|
||||
maxlen -= GetTextBounds(ph, scale).x;
|
||||
|
||||
for (auto& it : tmp) {
|
||||
if (GetTextBounds(ret, scale).x > maxlen) {
|
||||
ret += ph;
|
||||
ret += ext;
|
||||
dim = GetTextBounds(ret, scale);
|
||||
return ret;
|
||||
}
|
||||
ret += it;
|
||||
}
|
||||
pTMS[id].ID = id;
|
||||
pTMS[id].Size = dim;
|
||||
pTMS[id].Text = ret;
|
||||
pTMS[id].TimeStamp = PD::OS::GetTime();
|
||||
return ret;
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Font::CleanupTMS() {
|
||||
u64 t = PD::OS::GetTime();
|
||||
for (auto it = pTMS.begin(); it != pTMS.end();) {
|
||||
if (t - it->second.TimeStamp > 1000) {
|
||||
it = pTMS.erase(it);
|
||||
} else {
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace Li
|
||||
} // namespace PD
|
||||
48
source/lithium/fonts.cpp
Normal file
48
source/lithium/fonts.cpp
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
MIT License
|
||||
Copyright (c) 2024 - 2025 René Amthor (tobid7)
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifdef PD_LI_INCLUDE_FONTS
|
||||
#include <pd/lithium/fonts.hpp>
|
||||
|
||||
/** Generated with pdfm */
|
||||
namespace PD {
|
||||
FontFileData pFontData[] = {
|
||||
{
|
||||
"ComicNeue-Bold.ttf",
|
||||
0,
|
||||
1,
|
||||
},
|
||||
{
|
||||
"Roboto-Regular.ttf",
|
||||
0,
|
||||
1,
|
||||
},
|
||||
};
|
||||
size_t pNumFonts = 2;
|
||||
// clang-format off
|
||||
PD::u8 pFontsDataRaw[] = {
|
||||
0x0
|
||||
};
|
||||
// clang-format on
|
||||
} // namespace PD
|
||||
#endif
|
||||
150
source/lithium/renderer.cpp
Executable file
150
source/lithium/renderer.cpp
Executable file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 - 2025 tobid7
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <pd/lithium/renderer.hpp>
|
||||
|
||||
namespace PD {
|
||||
namespace Li {
|
||||
PD_LITHIUM_API bool Renderer::InBox(const fvec2& pos, const fvec2& szs,
|
||||
const fvec4& rect) {
|
||||
return (pos.x + szs.x >= rect.x && pos.y + szs.y >= rect.y &&
|
||||
pos.x <= rect.z && pos.y <= rect.w);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API bool Renderer::InBox(const fvec2& pos, const fvec4& rect) {
|
||||
return (pos.x > rect.x && pos.x < rect.x + rect.z && pos.y > rect.y &&
|
||||
pos.y < rect.y + rect.w);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API bool Renderer::InBox(const fvec2& alpha, const fvec2& bravo,
|
||||
const fvec2& charlie, const fvec4& rect) {
|
||||
return ((alpha.x < rect.z && bravo.x < rect.z && charlie.x < rect.z) ||
|
||||
(alpha.y < rect.w && bravo.y < rect.w && charlie.y < rect.w) ||
|
||||
(alpha.x > 0 && bravo.x > 0 && charlie.x > 0) ||
|
||||
(alpha.y > 0 && bravo.y > 0 && charlie.y > 0));
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Renderer::RotateCorner(fvec2& pos, float sinus,
|
||||
float cosinus) {
|
||||
float x = pos.x * cosinus - pos.y * sinus;
|
||||
float y = pos.y * cosinus - pos.x * sinus;
|
||||
pos = fvec2(x, y);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API Rect Renderer::PrimRect(const fvec2& pos, const fvec2& size,
|
||||
float angle) {
|
||||
fvec2 c = size * 0.5f; // Center
|
||||
fvec2 corner[4] = {
|
||||
fvec2(-c.x, -c.y),
|
||||
fvec2(-c.x + size.x, -c.y),
|
||||
fvec2(-c.x, -c.y + size.y),
|
||||
fvec2(-c.x + size.x, -c.y + size.y),
|
||||
};
|
||||
|
||||
// Only rotate if required
|
||||
if (angle != 0.f) {
|
||||
float s = std::sin(angle);
|
||||
float co = std::cos(angle);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
RotateCorner(corner[i], s, co);
|
||||
}
|
||||
}
|
||||
|
||||
// Return Result
|
||||
return Rect(corner[0] + pos + c, corner[1] + pos + c, corner[2] + pos + c,
|
||||
corner[3] + pos + c);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API Rect Renderer::PrimLine(const fvec2& a, const fvec2& b,
|
||||
int thickness) {
|
||||
// Using the vec maths api makes the code as short as it is
|
||||
vec2 dir = a - b;
|
||||
float len = dir.Len();
|
||||
vec2 unit_dir = dir / len;
|
||||
vec2 perpendicular(-unit_dir.y, unit_dir.x);
|
||||
vec2 off = perpendicular * ((float)thickness * 0.5f);
|
||||
|
||||
return Rect(a + off, b + off, a - off, b - off);
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Renderer::CmdQuad(Command::Ref cmd, const Rect& quad,
|
||||
const Rect& uv, u32 color) {
|
||||
cmd->AddIdx(0).AddIdx(1).AddIdx(2);
|
||||
cmd->AddIdx(0).AddIdx(2).AddIdx(3);
|
||||
cmd->AddVtx(Vertex(quad.BotRight(), uv.BotRight(), color));
|
||||
cmd->AddVtx(Vertex(quad.TopRight(), uv.TopRight(), color));
|
||||
cmd->AddVtx(Vertex(quad.TopLeft(), uv.TopLeft(), color));
|
||||
cmd->AddVtx(Vertex(quad.BotLeft(), uv.BotLeft(), color));
|
||||
}
|
||||
|
||||
PD_LITHIUM_API void Renderer::CmdTriangle(Command::Ref cmd, const fvec2 a,
|
||||
const fvec2 b, const fvec2 c,
|
||||
u32 clr) {
|
||||
cmd->AddIdx(2).AddIdx(1).AddIdx(0);
|
||||
cmd->AddVtx(Vertex(a, vec2(0.f, 1.f), clr));
|
||||
cmd->AddVtx(Vertex(b, vec2(1.f, 1.f), clr));
|
||||
cmd->AddVtx(Vertex(c, vec2(1.f, 0.f), clr));
|
||||
}
|
||||
|
||||
// TODO: Don't render OOS (Probably make it with a define as it
|
||||
// would probably be faster to render out of screen than checking if
|
||||
// it could be skipped)
|
||||
PD_LITHIUM_API void Renderer::CmdConvexPolyFilled(
|
||||
Command::Ref cmd, const std::vector<fvec2>& points, u32 clr,
|
||||
Texture::Ref tex) {
|
||||
if (points.size() < 3 || tex == nullptr) {
|
||||
return; // Need at least three points
|
||||
}
|
||||
|
||||
// Support for Custom Textures (UV calculation)
|
||||
float minX = points[0].x, minY = points[0].y;
|
||||
float maxX = minX, maxY = minY;
|
||||
// Check for the max and min Positions
|
||||
for (const auto& it : points) {
|
||||
if (it.x < minX) minX = it.x;
|
||||
if (it.y < minY) minY = it.y;
|
||||
if (it.x > maxX) maxX = it.x;
|
||||
if (it.y > maxY) maxY = it.y;
|
||||
}
|
||||
// Get Short defines for UV
|
||||
// (Bottom Right is not required)
|
||||
auto uv_tl = tex->UV.TopLeft();
|
||||
auto uv_tr = tex->UV.TopRight();
|
||||
auto uv_bl = tex->UV.BotLeft();
|
||||
|
||||
// Render
|
||||
for (int i = 2; i < (int)points.size(); i++) {
|
||||
cmd->AddIdx(0).AddIdx(i).AddIdx(i - 1);
|
||||
}
|
||||
for (int i = 0; i < (int)points.size(); i++) {
|
||||
// Calculate U and V coords
|
||||
float u =
|
||||
uv_tl.x + ((points[i].x - minX) / (maxX - minX)) * (uv_tr.x - uv_tl.x);
|
||||
float v =
|
||||
uv_tl.y + ((points[i].y - minY) / (maxY - minY)) * (uv_bl.y - uv_tl.y);
|
||||
cmd->AddVtx(Vertex(points[i], fvec2(u, v), clr));
|
||||
}
|
||||
}
|
||||
} // namespace Li
|
||||
} // namespace PD
|
||||
Reference in New Issue
Block a user