Implement Experiment of NFontApi

This commit is contained in:
2022-07-31 01:23:39 +02:00
parent ca52e6dae6
commit 9213a1d5dd
11 changed files with 5387 additions and 1656 deletions

View File

@ -254,7 +254,7 @@ void RenderD7::BitmapPrinter::Benchmark()
std::string resultt = "TestMode: " + std::to_string(testfpsd) + "fps" + "\nRendered Frames: " + renderedf + "\nMax Cpu Time: " + avgdtt + "\nAvg Cpu Time: " + avgcpu + "\nAvg Fps: " + avgfps + "\nAvg EncodeTime: " + avgcpu2 + "ms/f\nAvg DecodeTime: " + avgcpu3 + "ms\n";
this->ClearBlank();
this->DrawRectFilled(0, 0, this->bitmap.bmp_info_header.width, this->bitmap.bmp_info_header.height, 0, 0, 0, 255);
this->DrawText(0, 0, 0, RenderD7::Color::Hex("#ffffff"), resultt);
this->DrawDebugText(0, 0, 0, RenderD7::Color::Hex("#ffffff"), resultt);
std::string outname = csvpc + "/benchmark_" + RenderD7::GetTimeStr() + ".png";
this->SavePng(outname);
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("Benchmark", "Saved to: \n" + outname));
@ -275,12 +275,12 @@ void RenderD7::BitmapPrinter::Benchmark()
this->ClearBlank();
this->DrawRectFilled(0, 0, this->bitmap.bmp_info_header.width, this->bitmap.bmp_info_header.width, 255, 255, 255, 255);
this->DrawRect(5, 5, this->bitmap.bmp_info_header.width - 10, this->bitmap.bmp_info_header.height - 10, 5, 0, 0, 0, 0);
//this->DrawText(20, 20, 0, RenderD7::Color::Hex("#ffffff"), "Fps: " + std::to_string(fps));
this->DrawText(0, 0, 0.5f, RenderD7::Color::Hex("#ff0000"), "Time: " + std::to_string(timer));
this->DrawText(0, 10, 0.5f, RenderD7::Color::Hex("#ff0000"), "Fps: " + std::to_string(fps));
this->DrawText(0, 20, 0.5f, RenderD7::Color::Hex("#ff0000"), "dt: " + std::to_string(dtt));
this->DrawText(0, 30, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxEncodeTime: " + std::to_string(mdtt2*1000) + "ms/f");
this->DrawText(0, 40, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxDecodeTime: " + std::to_string(mdtt3*1000) + "ms");
//this->DrawDebugText(20, 20, 0, RenderD7::Color::Hex("#ffffff"), "Fps: " + std::to_string(fps));
this->DrawDebugText(0, 0, 0.5f, RenderD7::Color::Hex("#ff0000"), "Time: " + std::to_string(timer));
this->DrawDebugText(0, 10, 0.5f, RenderD7::Color::Hex("#ff0000"), "Fps: " + std::to_string(fps));
this->DrawDebugText(0, 20, 0.5f, RenderD7::Color::Hex("#ff0000"), "dt: " + std::to_string(dtt));
this->DrawDebugText(0, 30, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxEncodeTime: " + std::to_string(mdtt2*1000) + "ms/f");
this->DrawDebugText(0, 40, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxDecodeTime: " + std::to_string(mdtt3*1000) + "ms");
uint64_t currentTime2 = svcGetSystemTick();
dtt2 = ((float)(currentTime2 / (float)TICKS_PER_MSEC) - (float)(lastTime2 / (float)TICKS_PER_MSEC)) / 1000.f;
hdttt2.push_back(dtt2);
@ -330,8 +330,9 @@ void RenderD7::BitmapPrinter::SetupBenchmark(int framerate)
#include <renderd7/debugfont.h>
void RenderD7::BitmapPrinter::DrawDebugChar(u32 posX, u32 posY, u32 color, char character)
void RenderD7::BitmapPrinter::DrawDebugChar(u32 posX, u32 posY, int t_size, u32 color, char character)
{
bool isscale = (t_size > 1) ? true : false;
for(u32 y = 0; y < 8; y++)
{
char charPos = debugfont[character * 8 + y];
@ -339,60 +340,31 @@ void RenderD7::BitmapPrinter::DrawDebugChar(u32 posX, u32 posY, u32 color, char
for(u32 x = 0; x < 8; x++)
if(((charPos >> (7 - x)) & 1) == 1)
{
DrawPixel((int)posX + x + 1, (int)posY + y + 1, UNPACK_BGRA(color));
if (!isscale) DrawPixel((int)posX + x + 1, (int)posY + y + 1, UNPACK_BGRA(color));
if (isscale) DrawRectFilled(((int)posX) + (x*t_size) + 1, ((int)posY) + (y*t_size) + 1, t_size, t_size, UNPACK_BGRA(color));
}
}
}
void RenderD7::BitmapPrinter::DrawChar(u32 posX, u32 posY, int t_size, u32 color, char character)
void RenderD7::BitmapPrinter::DrawChar(int posX, int posY, float t_size, u32 color, char character, RenderD7::NFontApi font)
{
for(u32 y = 0; y < (uint32_t)(8*t_size); y++)
for(int y = 0; y < font.GetGlyphHeight(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str())); y++)
{
char charPos = debugfont[character * 8 + y/t_size];
char charPos = (char)font.GetGlyphBitmap(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str()))[character * font.GetGlyphHeight(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str())) + y];
for(u32 x = 0; x < (uint32_t)(8*t_size); x++)
if(((charPos >> (7 - x)) & 1) == 1)
for(int x = 0; x < font.GetGlyphWidth(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str())); x++)
if(((charPos >> (font.GetGlyphWidth(RenderD7::utf8_decode(RenderD7::IntToUtf8((int)character).c_str()) - 1) - x)) & 1) == 1)
{
DrawPixel((int)posX + x + 1, (int)posY + y + 1, UNPACK_BGRA(color));
DrawPixel(posX + x + 1, posY + y + 1, UNPACK_BGRA(color));
}
}
}
#define SPACING_Y 10
#define SPACING_X 8
void RenderD7::BitmapPrinter::DrawDebugText(int x, int y, float t_size, u32 color, std::string text)
{
for(u32 i = 0, line_i = 0; i < strlen(text.c_str()); i++)
switch(text[i])
{
case '\n':
y += SPACING_Y;
line_i = 0;
break;
case '\t':
line_i += 2;
break;
default:
//Make sure we never get out of the screen
if(line_i >= (((u32)this->bitmap.bmp_info_header.width) - (u32)x) / SPACING_X)
{
y += SPACING_Y;
line_i = 1; //Little offset so we know the same text continues
if(text[i] == ' ') break; //Spaces at the start look weird
}
this->DrawDebugChar((u32)x + line_i * SPACING_X, (u32)y, color, text[i]);
line_i++;
break;
}
}
void RenderD7::BitmapPrinter::DrawText(int x, int y, int t_size, u32 color, std::string text)
void RenderD7::BitmapPrinter::DrawDebugText(int x, int y, int t_size, u32 color, std::string text)
{
if (t_size < 1)
{
@ -403,24 +375,59 @@ void RenderD7::BitmapPrinter::DrawText(int x, int y, int t_size, u32 color, std:
switch(text[i])
{
case '\n':
y += SPACING_Y*t_size;
y += (SPACING_Y*t_size);
line_i = 0;
break;
case '\t':
line_i += 2*t_size;
line_i += 2;
break;
default:
//Make sure we never get out of the screen
if(line_i >= (((u32)this->bitmap.bmp_info_header.width) - (u32)x) / SPACING_X*t_size)
if(line_i >= (((u32)this->bitmap.bmp_info_header.width) - (u32)x) / (SPACING_X*t_size))
{
y += SPACING_Y*t_size;
y += (SPACING_Y*t_size);
line_i = 1; //Little offset so we know the same text continues
if(text[i] == ' ') break; //Spaces at the start look weird
}
this->DrawChar((u32)x + line_i * SPACING_X, (u32)y, t_size, color, text[i]);
this->DrawDebugChar((u32)x + line_i * (SPACING_X*t_size), (u32)y, t_size, color, text[i]);
line_i++;
break;
}
}
void RenderD7::BitmapPrinter::DrawText(int x, int y, float t_size, u32 color, std::string text, RenderD7::NFontApi font)
{
if (t_size < 1)
{
t_size = 1;
}
for(u32 i = 0, line_i = 0; i < strlen(text.c_str()); i++)
switch(text[i])
{
case '\n':
y += (font.GetLineHeight());
line_i = 0;
break;
case '\t':
line_i += 2;
break;
default:
//Make sure we never get out of the screen
if(line_i >= (((u32)this->bitmap.bmp_info_header.width) - (u32)x) / (u32)(font.GetGlyphWidth(RenderD7::utf8_decode(&text[i]))*t_size))
{
y += (SPACING_Y*t_size);
line_i = 1; //Little offset so we know the same text continues
if(text[i] == ' ') break; //Spaces at the start look weird
}
this->DrawChar(x + line_i * (font.GetGlyphWidth(RenderD7::utf8_decode(&text[i])*t_size)), y, t_size, color, text[i], font);
line_i++;
break;

70
source/NFontApi.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <renderd7/Fonts/NFontApi.hpp>
#define STB_TRUETYPE_IMPLEMENTATION // force following include to generate implementation
#include <renderd7/external/stb_truetype.h>
RenderD7::NFontApi::NFontApi()
{
}
RenderD7::NFontApi::~NFontApi()
{
}
void RenderD7::NFontApi::LoadTTF(std::string path)
{
/////READ FILE
unsigned char* buffer;
long size = 0;
FILE *ttf__ = fopen(path.c_str(), "rb");
fseek(ttf__, 0, SEEK_END);
size = ftell(ttf__);
fseek(ttf__, 0, SEEK_SET);
buffer = (unsigned char*)malloc(size);
fread(buffer, size, 1, ttf__);
fclose(ttf__);
/////Setup Font
if (!stbtt_InitFont(&font, buffer, 0))
{
printf("failed\n");
status+="failed\n";
return;
}
status+="success!\n";
l_h = 24; /* line height */
scale = stbtt_ScaleForPixelHeight(&font, l_h);
stbtt_GetFontVMetrics(&font, &ascent,&decent,0);
baseline = (int) (ascent*scale);
height = (int) ((ascent - decent)*scale);
}
std::vector<unsigned char> RenderD7::NFontApi::GetGlyphBitmap(int glyph)
{
stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
w = x1-x0;
h = y1-y0;
std::vector<unsigned char> bitmap;
bitmap.resize(h*w);
stbtt_MakeGlyphBitmap(&font, bitmap.data(), w, h, w, scale, scale, glyph);
return bitmap;
}
int RenderD7::NFontApi::GetGlyphHeight(int glyph)
{
stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
w = x1-x0;
h = y1-y0;
return h;
}
int RenderD7::NFontApi::GetGlyphWidth(int glyph)
{
stbtt_GetGlyphBitmapBox(&font, glyph, scale, scale, &x0, &y0, &x1, &y1);
w = x1-x0;
h = y1-y0;
return w;
}

View File

@ -3,6 +3,11 @@
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <unistd.h>
#include <fstream>
#include <string.h>
#include <string>
std::string RenderD7::FormatString(std::string fmt_str, ...)
{
@ -17,7 +22,10 @@ std::string RenderD7::FormatString(std::string fmt_str, ...)
std::string RenderD7::GetTimeStr(void)
{
time_t unixTime = time(NULL);
struct tm* timeStruct = gmtime((const time_t*)&unixTime);
return RenderD7::FormatString("%02i-%02i-%02i", timeStruct->tm_hour, timeStruct->tm_min, timeStruct->tm_sec);
time_t unixTime;
struct tm timeStruct;
time(&unixTime);
localtime_r(&unixTime, &timeStruct);
return FormatString("%04i-%02i-%02i_%02i-%02i-%02i", timeStruct.tm_year + 1900, timeStruct.tm_mon + 1, timeStruct.tm_mday,
timeStruct.tm_hour, timeStruct.tm_min, timeStruct.tm_sec);
}

View File

@ -8,8 +8,8 @@ RenderD7::Toast::Toast(std::string head, std::string msg)
this->toast.ClearBlank();
this->toast.DrawRectFilled(0, 0, 400, 70, 40, 40, 40, 255);
this->toast.DrawRectFilled(0, 0, 400, 25, 70, 70, 70, 255);
this->toast.DrawText(4, 5, 0, RenderD7::Color::Hex("#ffffff"), this->head);
this->toast.DrawText(4, 40, 0, RenderD7::Color::Hex("#ffffff"), this->msg);
this->toast.DrawDebugText(4, 5, 0, RenderD7::Color::Hex("#ffffff"), this->head);
this->toast.DrawDebugText(4, 40, 0, RenderD7::Color::Hex("#ffffff"), this->msg);
this->toastrendered->LoadPFromBuffer(BitmapConverter::ConvertData(toast.GetBitmap().DATA()));
}

View File

@ -242,9 +242,9 @@ void RenderD7::Error::DisplayFatalError(std::string toptext, std::string errorte
C2D_TargetClear(Bottom, DSEVENBLACK);
RenderD7::BitmapPrinter errorss(400, 240);
errorss.DrawRectFilled(0, 0, 400, 240, 0, 0, 0, 255);
errorss.DrawText(4, 4, 2, RenderD7::Color::Hex("#ff0000"), toptext);
errorss.DrawText(4, 26, 1, RenderD7::Color::Hex("#000000"), errortext);
errorss.DrawText(4, 230, 1, RenderD7::Color::Hex("#000000"), "Press Start to Exit!");
errorss.DrawDebugText(4, 4, 2, RenderD7::Color::Hex("#ff0000"), toptext);
errorss.DrawDebugText(4, 26, 1, RenderD7::Color::Hex("#000000"), errortext);
errorss.DrawDebugText(4, 230, 1, RenderD7::Color::Hex("#000000"), "Press Start to Exit!");
RenderD7::Image img;
img.LoadFromBitmap(errorss.GetBitmap());
RenderD7::OnScreen(Top);