Add GTrace

Make Fonts an Asset
Add FormatMillis and FormatNanos
This commit is contained in:
2025-12-08 18:37:12 +01:00
parent 88e367a299
commit d85610a0bd
11 changed files with 298 additions and 18 deletions

View File

@@ -18,6 +18,7 @@ add_library(${PROJECT_NAME} STATIC
source/assets.cpp
source/amethyst.cpp
source/color.cpp
source/godtrace.cpp
source/image.cpp
source/renderer.cpp
source/texture.cpp

View File

@@ -4,18 +4,17 @@
class Example : public Amy::App {
public:
Example() {
Amy::GTrace::Scope s("Init");
gfxInitDefault();
romfsInit();
consoleInit(GFX_BOTTOM, NULL);
C3D::Init();
Top = C3D::CreateScreen(GFX_TOP);
Mgr = new Amy::AssetMgr();
Mgr->AutoLoad("icon", "romfs:/icon.png");
Iron::Init();
Mgr->AutoLoad("icon", "romfs:/icon.png");
Mgr->AutoLoad("font", "romfs:/ComicNeue.ttf");
dl = new Iron::Drawlist();
Fnt = Iron::Font::New();
Fnt->LoadTTF("romfs:/ComicNeue.ttf");
dl->SetFont(Fnt);
dl->SetFont(Mgr->Get<Iron::Font>("font"));
}
~Example() {
@@ -33,15 +32,18 @@ class Example : public Amy::App {
if (hidKeysDown() & KEY_START) {
Exit();
}
std::cout << std::format("\x1b[1;1HDelta: {:.3f} -> {:.3} FPS\x1b[K",
this->Delta(), 1000.0 / this->Delta());
std::cout << std::format("\x1b[2;1HTime: {:.3f}\x1b[K", this->Time());
std::cout << std::format(
"\x1b[3;1H\nLin: {}\nVertices: {}\nIndices: "
"{}\x1b[K",
Amy::Utils::FormatBytes(linearSpaceFree()), Iron::VerticesDrawn(),
Iron::IndicesDrawn());
std::string stats = std::format(
"Delta: {:.3} -> {:.3} FPS\nTime: {}\nLinearRam: {}\nVIT: "
"[{}/{}/{}]\nAssets: {}\nMain: {}",
this->Delta(), 1000.0 / this->Delta(),
Amy::Utils::FormatMillis(this->Time() * 1000.f),
Amy::Utils::FormatBytes(linearSpaceFree()), Amy::Iron::VerticesDrawn(),
Amy::Iron::IndicesDrawn(), Amy::Iron::IndicesDrawn() / 3,
Mgr->CountAll(),
Amy::Utils::FormatNanos(
Amy::GTrace::GetTraceRef("Main")->GetLastDiff()));
C3D::StartFrame();
Amy::GTrace::Beg("Main");
Top->Clear();
Top->Use();
dl->DrawTex(Mgr->Get<Amy::Texture>("icon"));
@@ -50,19 +52,19 @@ class Example : public Amy::App {
dl->DrawCircleFilled(Amy::fvec2(200, 120), 50, Amy::Color("#ffffff"), 40);
dl->DrawSolid();
dl->DrawRectFilled(0, 50, Amy::Color(0.f, 1.f, 0.f, 1.f));
dl->DrawText(Amy::fvec2(5, 50), "Hello World!", Amy::Color(255, 0, 255));
dl->DrawText(Amy::fvec2(5, 50), stats, Amy::Color(255, 0, 255));
Iron::NewFrame();
Iron::DrawOn(Top);
Iron::Draw(*dl);
dl->Clear();
C3D::EndFrame();
Amy::GTrace::End("Main");
}
C3D::Screen* Top;
Amy::AssetMgr* Mgr;
Iron::Drawlist* dl;
Iron::Font::Ref Fnt;
};
int main() {

View File

@@ -4,6 +4,7 @@
#include <amethyst/assets.hpp>
#include <amethyst/c3d.hpp>
#include <amethyst/color.hpp>
#include <amethyst/godtrace.hpp>
#include <amethyst/image.hpp>
#include <amethyst/iron.hpp>
#include <amethyst/renderer.hpp>
@@ -14,4 +15,6 @@ void RegisterCxxExceptionHandler();
}
using Iron = Amy::Iron;
using C3D = Amy::C3D;
using C3D = Amy::C3D;
using GTrace = Amy::GTrace;
using Utils = Amy::Utils;

View File

@@ -50,6 +50,8 @@ class AssetMgr {
return std::dynamic_pointer_cast<T>(r->second) != nullptr;
}
size_t CountAll() const { return pAssets.size(); }
template <typename T>
size_t Count() const {
size_t ret = 0;

View File

@@ -1,6 +1,7 @@
#pragma once
#include <string>
#include <amethyst/types.hpp>
#include <amethyst/id.hpp>
namespace Amy {
class GTrace {
@@ -8,6 +9,204 @@ class GTrace {
GTrace() = default;
~GTrace() = default;
class Stats {
public:
/**
* Constructor taking a lengh for the List
* @param l Lengh of the data list
*/
Stats(int l) : len(l), val(l, 0) {}
~Stats() = default;
AMY_SHARED(Stats);
/**
* Add a New Value to the list
* @param v value to add
*/
void Add(ull v) {
val[idx] = v;
idx = next(idx);
num_val = std::min(num_val + 1, len);
}
/**
* Get Avarage Num
* @return Average
*/
ull GetAverage() {
if (!num_val) return 0.f;
ull res = 0;
for (int i = 0; i < num_val; i++) {
res += val[smart_idx(i)];
}
return res / num_val;
}
/**
* Get Minimum Num
* @return Minimum value
*/
ull GetMin() {
if (!num_val) return 0.f;
ull res = std::numeric_limits<ull>::max();
for (int i = 0; i < num_val; i++) {
res = std::min(val[smart_idx(i)], res);
}
return res;
}
/**
* Get Maximum Value
* @return Max Value
*/
ull GetMax() {
if (!num_val) return 0.f;
ull res = 0;
for (int i = 0; i < num_val; i++) {
res = std::max(val[smart_idx(i)], res);
}
return res;
}
/**
* Clear the List
*/
void Clear() {
val.assign(len, 0);
idx = 0;
num_val = 0;
}
/**
* Get Data Buffer
* @return data bufer (not edidable)
*/
const std::vector<ull>& GetData() { return val; }
/**
* Access an element in the list [not edidable]
* @return value to access
*/
const ull& operator[](int i) { return val[smart_idx(i)]; }
/**
* Get List Lengh
* @return Lengh
*/
const size_t GetLen() { return len; }
/**
* Get Number of Values
* @return number of values
*/
const size_t GetNumValues() { return num_val; }
private:
/**
* Get the Next Position to write to
* @param c current position
* @return next position
*/
size_t next(size_t c) const { return (c + 1) % len; }
/**
* Smart Indexing in for loops to make sure to
* not index a value that was not set yet
* @param v pos in for loop
* @return indexing pos
*/
size_t smart_idx(size_t v) const { return (idx + len - num_val + v) % len; }
/** Lengh of the list */
int len = 0;
/** Value Storage */
std::vector<ull> val;
int idx = 0;
int num_val = 0;
};
class Trace {
public:
Trace() { protocol = Stats::New(60); }
~Trace() = default;
AMY_SHARED(Trace);
void SetID(ksr v) { Name = v; }
kstr GetID() { return Name; }
void SetStart(ull v) { Start = v; }
ull GetStart() { return Start; }
void SetEnd(ull v) {
End = v;
Diff = End - Start;
protocol->Add(GetLastDiff());
}
ull GetEnd() { return End; }
ull GetLastDiff() { return Diff; }
Stats::Ref GetProtocol() { return protocol; }
private:
str Name;
ull Start = 0;
ull End = 0;
ull Diff = 0;
Stats::Ref protocol;
};
/**
* Begin a Trace
* @param id Name of the Trace
*/
static void Beg(ksr id);
/**
* End a Trace
* @param id Name of the Trace
*/
static void End(ksr id);
/**
* Collect Start end end of the trace by tracking
* when the Scope object goes out of scope
*
* Example:
* ```cpp
* void SomeFunction() {
* // Create a Scoped Trace called "SomeFunc"
* PD::TT::Scope st("SomeFunc");
* // Do your functions stuff
* // End at the end it goes out of
* // scope which collects the end time
* }
* ```
*/
class Scope {
public:
/**
* Constructor requiring a Name for the Trace
* @param id Name of the Trace
*/
Scope(ksr id) {
this->ID = id;
GTrace::Beg(id);
}
/**
* Deconstructor getting the end time when going out of scope
*/
~Scope() { GTrace::End(ID); }
private:
/** Trace Name/ID */
str ID;
};
using TraceMap = std::unordered_map<std::string, Trace::Ref>;
static TraceMap& GetTraceMap() { return GetTraceMap(); }
static Trace::Ref& GetTraceRef(ksr id) {
if (!pTraces.count(id)) {
pTraces[id] = Trace::New();
pTraces[id]->SetID(id);
}
return pTraces[id];
}
static bool TraceExist(ksr id) { return pTraces.count(id); }
private:
static TraceMap pTraces;
};
} // namespace Amy

View File

@@ -65,7 +65,7 @@ class Iron {
Texture::Ref Tex = nullptr;
};
class Font {
class Font : public Asset {
public:
struct Codepoint {
ui Cp = 0;

View File

@@ -18,6 +18,23 @@ ull GetTimeMicro();
ull GetTimeMilli();
void String2U16(us* res, ksr src, size_t max);
std::string U16toU8(us* in, size_t max);
/**
* Generate a Formatted String by an Nanoseconds Input
* @param nanos Nanoseconds Input
* @return Result String
*/
kstr FormatNanos(ull nanos);
/**
* Generate a Formatted String by an Milliseconds Input
* @param millis Milliseconds Input
* @return Result String
*/
kstr FormatMillis(ull millis);
/**
* Create a formatted String by an input bytes value
* @param bytes value in bytes
* @result Formatted String for example `2.5MB`
*/
/**
* FNV Hash functiom (32 Bit)
* https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function

View File

@@ -27,6 +27,7 @@ void NpiD7CxxExceptionHandler() {
#endif
namespace Amy {
void RegisterCxxExceptionHandler() {
#ifdef AMY_3DS
std::set_terminate(NpiD7CxxExceptionHandler);

View File

@@ -1,5 +1,6 @@
#include <amethyst/assets.hpp>
#include <amethyst/c3d.hpp>
#include <amethyst/iron.hpp>
#include <amethyst/texture.hpp>
namespace Amy {
@@ -13,6 +14,10 @@ void AssetMgr::AutoLoad(const ID& id, ksr path) {
auto shader = C3D::Shader::New();
shader->Load(path);
Add(id, shader);
} else if (path.ends_with(".ttf")) {
auto font = Iron::Font::New();
font->LoadTTF(path);
Add(id, font);
} else {
throw std::runtime_error("[amy]: assets: " + id.GetName() + " (" + path +
") is unsupported for AssetMgr::AutoLoad!");

14
source/godtrace.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include <amethyst/godtrace.hpp>
#include <amethyst/utils.hpp>
Amy::GTrace::TraceMap Amy::GTrace::pTraces;
void Amy::GTrace::Beg(ksr id) {
auto trace = GetTraceRef(id);
trace->SetStart(Amy::Utils::GetTimeNano());
}
void Amy::GTrace::End(ksr id) {
auto trace = GetTraceRef(id);
trace->SetEnd(Amy::Utils::GetTimeNano());
}

View File

@@ -144,6 +144,42 @@ std::string U16toU8(us* in, size_t max) {
return result;
}
kstr FormatNanos(ull nanos) {
// Based on some code of my minecraft plugins
if (nanos < 1000) {
return std::format("{}ns", nanos);
} else if (nanos < 1000000) {
ull micros = nanos / 1000;
return std::format("{}us {}ns", micros, nanos % 1000);
} else if (nanos < 1000000000) {
ull millis = nanos / 1000000;
return std::format("{}ms {}us", millis, (nanos % 1000000) / 1000);
} else if (nanos < 60000000000ULL) {
ull seconds = nanos / 1000000000;
return std::format("{}s {}ms", seconds, (nanos % 1000000000) / 1000000);
} else {
ull minutes = nanos / 60000000000ULL;
ull seconds = (nanos % 60000000000ULL) / 1000000000;
return std::format("{}m {}s", minutes, seconds);
}
return "";
}
kstr FormatMillis(ull millis) {
// Original Code can be found in some of my mc plugins
if (millis < 1000) {
return std::format("{}ms", millis);
} else if (millis < 60000) {
ull seconds = millis / 1000;
return std::format("{}s {}ms", seconds, (millis % 1000));
} else {
ull minutes = millis / 60000;
ull seconds = (millis % 60000) / 1000;
return std::format("{}m {}s {}ms", minutes, seconds, (millis % 1000));
}
return "";
}
namespace Image {
void ReverseBuf(vec<uc>& buf, int w, int h, int c) {
vec<uc> cpy = buf;