Big CleanUp

This commit is contained in:
2022-07-29 13:19:31 +02:00
parent bca1c36f75
commit 61cb41ae29
26 changed files with 1424 additions and 1294 deletions

386
source/BitmapPrinter.cpp Normal file
View File

@ -0,0 +1,386 @@
#include <renderd7/BitmapPrinter.hpp>
#include <renderd7/Ovl.hpp>
#include <renderd7/Toast.hpp>
#include <renderd7/stringtool.hpp>
extern bool shouldbe_disabled;
extern std::string csvpc;
RenderD7::BitmapPrinter::BitmapPrinter(int w, int h)
{
BMP newmap(w, h, true);
bitmap = newmap;
//renderframe.LoadPFromBuffer(BitmapConverter::ConvertData(bitmap.DATA()));
blank = newmap;
}
RenderD7::BitmapPrinter::~BitmapPrinter()
{
if(this->renderframe.loadet) this->renderframe.Unload();
}
bool RenderD7::BitmapPrinter::DecodeFile(std::string file)
{
unsigned error = bitmap.read(file.c_str());
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter", "Error Code: " + std::to_string(error)));
return false;
}
return true;
}
bool RenderD7::BitmapPrinter::DecodeMem(std::vector<unsigned char> buffer)
{
unsigned error = bitmap.read_mem(buffer);
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter", "Error Code: " + std::to_string(error)));
return false;
}
return true;
}
void RenderD7::BitmapPrinter::DrawPixel(int x, int y, u8 b, u8 g, u8 r, u8 a)
{
unsigned error = bitmap.set_pixel(x, bitmap.bmp_info_header.height - y, b, g, r, a);
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter->Pixel", "Error Code: " + std::to_string(error)));
}
}
void RenderD7::BitmapPrinter::DrawRect(int x, int y, int w, int h, u8 line_w, u8 b, u8 g, u8 r, u8 a)
{
unsigned error = bitmap.draw_rectangle(x, bitmap.bmp_info_header.height - y - h, w, h, b, g, r, a, line_w);
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter->Rect", "Error Code: " + std::to_string(error)));
}
}
void RenderD7::BitmapPrinter::DrawRectFilled(int x, int y, int w, int h, u8 b, u8 g, u8 r, u8 a)
{
unsigned error = bitmap.fill_region(x, bitmap.bmp_info_header.height - h - y, w, h, b, g, r, a);
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter->RectF", "Error Code: " + std::to_string(error)));
}
}
void RenderD7::BitmapPrinter::SaveBmp(std::string name)
{
if(!RenderD7::NameIsEndingWith(name, {"bmp"}))
{
name += ".bmp";
}
bitmap.write(name.c_str());
}
void RenderD7::BitmapPrinter::SavePng(std::string name)
{
if(!RenderD7::NameIsEndingWith(name, {"png"}))
{
name += ".png";
}
std::vector<unsigned char> ImageBuffer;
ImageBuffer = BitmapConverter::ConvertData(bitmap.DATA());
lodepng::save_file(ImageBuffer, name);
}
void RenderD7::BitmapPrinter::CreateScreen(C3D_RenderTarget *target)
{
isscreen = true;
targetr = target;
if (target == Top)
{
bitmap = BMP(400, 240, true);
blank = BMP(400, 240, true);
}
if (target == TopRight)
{
bitmap = BMP(400, 240, true);
blank = BMP(400, 240, true);
}
if (target == Bottom)
{
bitmap = BMP(320, 240, true);
blank = BMP(320, 240, true);
}
renderframe.LoadPFromBuffer(BitmapConverter::ConvertData(bitmap.DATA()));
}
bool RenderD7::BitmapPrinter::DrawScreenDirectF(int framerate)
{
bool updtt = false;
if (isscreen)
{
if(frame == (60/framerate)){
RenderD7::OnScreen(targetr);
if(renderframe.loadet) renderframe.Unload();
renderframe.LoadFromBitmap(bitmap);
frame = 0;
updtt = true;
}
if(renderframe.loadet) renderframe.Draw(0, 0);
frame++;
}
return updtt;
}
bool RenderD7::BitmapPrinter::DrawScreenDirect()
{
bool updtt = false;
if (isscreen)
{
RenderD7::OnScreen(targetr);
if(renderframe.loadet) renderframe.Unload();
renderframe.LoadFromBitmap(bitmap);
updtt = true;
if(renderframe.loadet) renderframe.Draw(0, 0);
}
return updtt;
}
RenderD7::Image RenderD7::BitmapPrinter::GetImage()
{
RenderD7::Image img;
img.LoadFromBitmap(bitmap);
return img;
}
void RenderD7::BitmapPrinter::UsePreMap(BMP map)
{
bitmap = map;
}
void RenderD7::BitmapPrinter::UsePrePrintMap(BitmapPrinter printmap)
{
bitmap = printmap.GetBitmap();
}
void RenderD7::BitmapPrinter::Clear(u8 b, u8 g, u8 r, u8 a)
{
bitmap.fill_region(0, 0, bitmap.bmp_info_header.width, bitmap.bmp_info_header.height, b, g, r, a);
}
void RenderD7::BitmapPrinter::ClearBlank()
{
bitmap = blank;
}
void RenderD7::BitmapPrinter::DrawScreenF(int framerate)
{
if (isscreen)
{
if(frame == (60/framerate)){
RenderD7::OnScreen(targetr);
frame = 0;
}
if(renderframe.loadet) renderframe.Draw(0, 0);
frame++;
}
}
void RenderD7::BitmapPrinter::DrawScreen()
{
if (isscreen)
{
RenderD7::OnScreen(targetr);
if(renderframe.loadet) renderframe.Draw(0, 0);
}
}
bool RenderD7::BitmapPrinter::UpdateScreenF(int framerate)
{
bool updtt = false;
if (isscreen)
{
if(frame == (60/framerate)){
if(renderframe.loadet) renderframe.Unload();
renderframe.LoadFromBitmap(bitmap);
frame = 0;
updtt = true;
}
frame++;
}
return updtt;
}
bool RenderD7::BitmapPrinter::UpdateScreen()
{
bool updtt = false;
if (isscreen)
{
if(renderframe.loadet) renderframe.Unload();
renderframe.LoadFromBitmap(bitmap);
updtt = true;
}
return updtt;
}
#define TICKS_PER_MSEC 268111.856
void RenderD7::BitmapPrinter::Benchmark()
{
if(setupbenchmark)
{
frametime = 0;
renderedframes = 0;
timer = 0;
setupbenchmark = false;
lastTime = svcGetSystemTick();
}
if(benchmark)
{
if(timer >= 60)
{
std::string renderedf = std::to_string(renderedframes);
std::string avgdtt = std::to_string(mhdtt);
float alldtt = 0;
for (size_t i = 1; i < hdttt.size(); i++)
{
alldtt += hdttt[i];
}
float alldtt2 = 0;
for (size_t i = 0; i < hdttt2.size(); i++)
{
alldtt2 += hdttt2[i];
}
float alldtt3 = 0;
for (size_t i = 0; i < hdttt3.size(); i++)
{
alldtt3 += hdttt3[i];
}
int allfps = 0;
for (size_t f = 1; f < fpscountc.size(); f++)
{
allfps += fpscountc[f];
}
std::string avgcpu = std::to_string((alldtt/(float)hdttt.size()-1));
std::string avgcpu2 = std::to_string(((alldtt2/(float)hdttt2.size())*1000));
std::string avgcpu3 = std::to_string(((alldtt3/(float)hdttt3.size())*1000));
std::string avgfps = std::to_string((allfps/(int)fpscountc.size()-1));
std::string resultt = "Frames Rendered: " + renderedf + "\nMax Cpu Time: " + avgdtt + "\nAvg Cpu Time: " + avgcpu + "\nAvg Fps: " + avgfps + "\nAvg RenderTime: " + avgcpu2 + "ms/f\nAvg ConvertTime: " + avgcpu3 + "ms\n";
this->ClearBlank();
this->DrawText(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));
benchmark = false;
}
uint64_t currentTime = svcGetSystemTick();
dtt = ((float)(currentTime / (float)TICKS_PER_MSEC) - (float)(lastTime / (float)TICKS_PER_MSEC)) / 1000.f;
lastTime = currentTime;
lastTime = currentTime;
frameCounter++;
fpsClock += dtt;
if (fpsClock >= 1.f) {
fps = frameCounter;
frameCounter = 0;
fpsClock = 0.f;
}
uint64_t lastTime2 = svcGetSystemTick();
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, 20, 0.5f, RenderD7::Color::Hex("#ff0000"), "Fps: " + std::to_string(fps));
this->DrawText(0, 40, 0.5f, RenderD7::Color::Hex("#ff0000"), "dt: " + std::to_string(dtt));
this->DrawText(0, 60, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxRenderTime: " + std::to_string(mdtt2*1000) + "ms/f");
this->DrawText(0, 80, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxConvertTime: " + 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);
lastTime2 = svcGetSystemTick();
bool updgg = this->UpdateScreenF(testfps);
currentTime2 = svcGetSystemTick();
dtt3 = ((float)(currentTime2 / (float)TICKS_PER_MSEC) - (float)(lastTime2 / (float)TICKS_PER_MSEC)) / 1000.f;
if(updgg) hdttt3.push_back(dtt3);
if (!shouldbe_disabled) this->DrawScreen();
renderedframes++;
if(mdtt2 < dtt2)
{
mdtt2 = dtt2;
}
if(mdtt3 < dtt3 && updgg)
{
mdtt3 = dtt3;
}
timer+= 1*dtt;
float hdtt = C3D_GetProcessingTime();
hdttt.push_back(hdtt);
fpscountc.push_back(fps);
if(mhdtt < hdtt)
{
mhdtt = C3D_GetProcessingTime();
}
/*if (!shouldbe_disabled)
{
RenderD7::OnScreen(Bottom);
RenderD7::DrawText(0, 0, 0.5f, RenderD7::Color::Hex("#ff0000"), "Time: " + std::to_string(timer));
RenderD7::DrawText(0, 20, 0.5f, RenderD7::Color::Hex("#ff0000"), "Fps: " + std::to_string(fps));
RenderD7::DrawText(0, 40, 0.5f, RenderD7::Color::Hex("#ff0000"), "dt: " + std::to_string(dtt));
RenderD7::DrawText(0, 60, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxRenderTime: " + std::to_string(mdtt2*1000) + "ms/f");
RenderD7::DrawText(0, 80, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxConvertTime: " + std::to_string(mdtt3*1000) + "ms");
}*/
}
}
void RenderD7::BitmapPrinter::SetupBenchmark(int framerate)
{
benchmark = true;
setupbenchmark = true;
this->testfps = framerate;
}
#include <renderd7/debugfont.h>
void RenderD7::BitmapPrinter::DrawChar(u32 posX, u32 posY, u32 color, char character)
{
for(u32 y = 0; y < 8; y++)
{
char charPos = debugfont[character * 8 + y];
for(u32 x = 0; x < 8; x++)
if(((charPos >> (7 - x)) & 1) == 1)
{
DrawPixel((int)posX + x + 1, (int)posY + y + 1, 255, 255, 255, 255);
}
}
}
#define SPACING_Y 10
#define SPACING_X 8
void RenderD7::BitmapPrinter::DrawText(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->DrawChar((u32)x + line_i * SPACING_X, (u32)y, color, text[i]);
line_i++;
break;
}
}

View File

@ -1,20 +0,0 @@
#include <renderd7/Clock.hpp>
namespace rnd7 {
////////////////////////////////////////////////////////////
Time Clock::getElapsedTime() const {
return getCurrentTime() - m_startTime;
}
////////////////////////////////////////////////////////////
Time Clock::restart() {
Time now = getCurrentTime();
Time elapsed = now - m_startTime;
m_startTime = now;
return elapsed;
}
}

92
source/Color.cpp Normal file
View File

@ -0,0 +1,92 @@
#include <renderd7/Color.hpp>
#define RGBA8(r, g, b, a) ((((r) & 0xFF) << 0) | (((g) & 0xFF) << 8) | (((b) & 0xFF) << 16) | (((a) & 0xFF) << 24))
uint32_t RenderD7::Color::Hex(const std::string color, uint8_t a)
{
if (color.length() < 7 || std::regex_search(color.substr(1), std::regex("[^0-9A-Fa-f]"))) { // invalid color.
return RenderD7::Color::Hex("#000000", 0);
}
int r = std::stoi(color.substr(1, 2), nullptr, 16);
int g = std::stoi(color.substr(3, 2), nullptr, 16);
int b = std::stoi(color.substr(5, 2), nullptr, 16);
return RGBA8(r, g, b, a);
}
std::string RenderD7::Color::RGB2Hex(int r, int g, int b)
{
std::stringstream ss;
ss << "#";
ss << std::hex << (r << 16 | g << 8 | b );
return ss.str();
}
uint32_t RenderD7::Color::Convert(uint32_t src, RenderD7::Color::ColorFmt srcFormat, RenderD7::Color::ColorFmt dstFormat)
{
uint32_t result = 0x00000000;
//uint8_t red, green, blue, alpha = 0x00;
/*switch(srcFormat)
{
case RGBA8:
{
red, green, blue, alpha = (UNPACK_RGBA(src));
switch (dstFormat)
{
case RGBA8:
{
result = ((((red) & 0xFF) << 0) | (((green) & 0xFF) << 8) | (((blue) & 0xFF) << 16) | (((alpha) & 0xFF) << 24));
break;
}
case RGB8:
{
result = ((((red) & 0xFF) << 0) | (((green) & 0xFF) << 8) | (((blue) & 0xFF) << 16));
break;
}
case RGB565:
{
//NOTYET
break;
}
case BGRA8:
{
result = ((((blue) & 0xFF) << 16) | (((green) & 0xFF) << 8) | (((red) & 0xFF) << 0 | (((alpha) & 0xFF) << 24)));
break;
}
case BGR8:
{
result = ((((blue) & 0xFF) << 16) | (((green) & 0xFF) << 8) | (((red) & 0xFF) << 0));
break;
}
default:
break;
}
break;
}
case RGB8:
{
result[2] = src[2];
result[1] = src[1];
result[0] = src[0];
break;
}
case RGB565:
{
// thanks neobrain
uint16_t px = *(uint16_t *)src;
blue = px & 0x1F;
green = (px >> 5) & 0x3F;
red = (px >> 11) & 0x1F;
result[0] = (blue << 3) | (blue >> 2);
result[1] = (green << 2) | (green >> 4);
result[2] = (red << 3) | (red >> 2);
break;
}
default: break;
}*/
return result;
}

255
source/Image.cpp Normal file
View File

@ -0,0 +1,255 @@
#include <renderd7/Image.hpp>
#include <renderd7/Ovl.hpp>
#include <renderd7/Toast.hpp>
extern bool usedbgmsg;
static u32 GetNextPowerOf2(u32 v) {
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return (v >= 64 ? v : 64);
}
static bool C3DTexToC2DImage(C2D_Image *texture, u32 width, u32 height, u8 *buf) {
if (width >= 1024 || height >= 1024)
return false;
C3D_Tex *tex = new C3D_Tex[sizeof(C3D_Tex)];
Tex3DS_SubTexture *subtex = new Tex3DS_SubTexture[sizeof(Tex3DS_SubTexture)];
subtex->width = static_cast<u16>(width);
subtex->height = static_cast<u16>(height);
// RGBA -> ABGR
for (u32 row = 0; row < subtex->width; row++) {
for (u32 col = 0; col < subtex->height; col++) {
u32 z = (row + col * subtex->width) * 4;
u8 r = *(u8 *)(buf + z);
u8 g = *(u8 *)(buf + z + 1);
u8 b = *(u8 *)(buf + z + 2);
u8 a = *(u8 *)(buf + z + 3);
*(buf + z) = a;
*(buf + z + 1) = b;
*(buf + z + 2) = g;
*(buf + z + 3) = r;
}
}
u32 w_pow2 = GetNextPowerOf2(subtex->width);
u32 h_pow2 = GetNextPowerOf2(subtex->height);
subtex->left = 0.f;
subtex->top = 1.f;
subtex->right = (subtex->width /static_cast<float>(w_pow2));
subtex->bottom = (1.0 - (subtex->height / static_cast<float>(h_pow2)));
C3D_TexInit(tex, static_cast<u16>(w_pow2), static_cast<u16>(h_pow2), GPU_RGBA8);
C3D_TexSetFilter(tex, GPU_NEAREST, GPU_NEAREST);
std::memset(tex->data, 0, tex->size);
for (u32 x = 0; x < subtex->width; x++) {
for (u32 y = 0; y < subtex->height; y++) {
u32 dst_pos = ((((y >> 3) * (w_pow2 >> 3) + (x >> 3)) << 6) + ((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) | ((x & 4) << 2) | ((y & 4) << 3))) * 4;
u32 src_pos = (y * subtex->width + x) * 4;
std::memcpy(&(static_cast<u8 *>(tex->data))[dst_pos], &(static_cast<u8 *>(buf))[src_pos], 4);
}
}
C3D_TexFlush(tex);
tex->border = RenderD7::Color::Hex("#000000", 0);
C3D_TexSetWrap(tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
if (tex && subtex) {
texture->tex = tex;
texture->subtex = subtex;
return true;
}
return false;
}
extern "C"
{
#include <renderd7/external/libnsbmp/libnsbmp.h>
}
static const u32 BYTES_PER_PIXEL = 4;
#define MAX_IMAGE_BYTES (48 * 1024 * 1024)
namespace LIBBMP {
static void *bitmap_create(int width, int height, [[maybe_unused]] unsigned int state) {
/* ensure a stupidly large (>50Megs or so) bitmap is not created */
if ((static_cast<long long>(width) * static_cast<long long>(height)) > (MAX_IMAGE_BYTES/BYTES_PER_PIXEL))
return nullptr;
return std::calloc(width * height, BYTES_PER_PIXEL);
}
static unsigned char *bitmap_get_buffer(void *bitmap) {
assert(bitmap);
return static_cast<unsigned char *>(bitmap);
}
static size_t bitmap_get_bpp([[maybe_unused]] void *bitmap) {
return BYTES_PER_PIXEL;
}
static void bitmap_destroy(void *bitmap) {
assert(bitmap);
std::free(bitmap);
}
}
unsigned Image_to_C3D(C2D_Image img, const std::vector<unsigned char>& bmpc) {
bmp_bitmap_callback_vt bitmap_callbacks = {
LIBBMP::bitmap_create,
LIBBMP::bitmap_destroy,
LIBBMP::bitmap_get_buffer,
LIBBMP::bitmap_get_bpp
};
bmp_result code = BMP_OK;
bmp_image bmp;
bmp_create(&bmp, &bitmap_callbacks);
code = bmp_analyse(&bmp, bmpc.size(), (u8*)bmpc.data());
if (code != BMP_OK) {
bmp_finalise(&bmp);
return 1;
}
code = bmp_decode(&bmp);
if (code != BMP_OK) {
if ((code != BMP_INSUFFICIENT_DATA) && (code != BMP_DATA_ERROR)) {
bmp_finalise(&bmp);
return 2;
}
/* skip if the decoded image would be ridiculously large */
if ((bmp.width * bmp.height) > 200000) {
bmp_finalise(&bmp);
return 3;
}
}
C2D_Image* texture = new C2D_Image();
bool ret = C3DTexToC2DImage(texture, static_cast<u32>(bmp.width), static_cast<u32>(bmp.height), static_cast<u8 *>(bmp.bitmap));
bmp_finalise(&bmp);
delete texture;
if (!ret)
{
return 4;
}
return 0;
}
void RenderD7::Image::LoadPng(const std::string path)
{
if (usedbgmsg)
{
//RenderD7::Msg::Display("RenderD7", "Loading Png:" + path, Top);
}
std::vector<u8> ImageBuffer;
unsigned width, height;
if (loadet)
{
C3D_TexDelete(this->img.tex);
loadet = false;
}
lodepng::decode(ImageBuffer, width, height, path);
this->img.tex = new C3D_Tex;
this->img.subtex = new Tex3DS_SubTexture({(u16)width, (u16)height, 0.0f, 1.0f, width / 1024.0f, 1.0f - (height / 1024.0f)});
C3D_TexInit(this->img.tex, 1024, 1024, GPU_RGBA8);
C3D_TexSetFilter(this->img.tex, GPU_LINEAR, GPU_LINEAR);
this->img.tex->border = 0xFFFFFFFF;
C3D_TexSetWrap(this->img.tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
for (u32 x = 0; x < width && x < 1024; x++) {
for (u32 y = 0; y < height && y < 1024; y++) {
const u32 dstPos = ((((y >> 3) * (1024 >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) |
((x & 4) << 2) | ((y & 4) << 3))) * 4;
const u32 srcPos = (y * width + x) * 4;
((uint8_t *)this->img.tex->data)[dstPos + 0] = ImageBuffer.data()[srcPos + 3];
((uint8_t *)this->img.tex->data)[dstPos + 1] = ImageBuffer.data()[srcPos + 2];
((uint8_t *)this->img.tex->data)[dstPos + 2] = ImageBuffer.data()[srcPos + 1];
((uint8_t *)this->img.tex->data)[dstPos + 3] = ImageBuffer.data()[srcPos + 0];
}
}
loadet = true;
}
RenderD7::Image::~Image()
{
if(loadet) C3D_TexDelete(img.tex);
loadet = false;
}
void RenderD7::Image::Unload()
{
if(loadet) C3D_TexDelete(img.tex);
loadet = false;
}
void RenderD7::Image::LoadPFromBuffer(const std::vector<u8> &buffer)
{
std::vector<u8> ImageBuffer;
if (loadet)
{
C3D_TexDelete(this->img.tex);
loadet = false;
}
unsigned width, height;
lodepng::decode(ImageBuffer, width, height, buffer);
img.tex = new C3D_Tex;
img.subtex = new Tex3DS_SubTexture({(u16)width, (u16)height, 0.0f, 1.0f, width / 512.0f, 1.0f - (height / 512.0f)});
C3D_TexInit(img.tex, 512, 512, GPU_RGBA8);
C3D_TexSetFilter(img.tex, GPU_LINEAR, GPU_LINEAR);
img.tex->border = 0xFFFFFFFF;
C3D_TexSetWrap(img.tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
for (u32 x = 0; x < width && x < 512; x++) {
for (u32 y = 0; y < height && y < 512; y++) {
const u32 dstPos = ((((y >> 3) * (512 >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) |
((x & 4) << 2) | ((y & 4) << 3))) * 4;
const u32 srcPos = (y * width + x) * 4;
((uint8_t *)img.tex->data)[dstPos + 0] = ImageBuffer.data()[srcPos + 3];
((uint8_t *)img.tex->data)[dstPos + 1] = ImageBuffer.data()[srcPos + 2];
((uint8_t *)img.tex->data)[dstPos + 2] = ImageBuffer.data()[srcPos + 1];
((uint8_t *)img.tex->data)[dstPos + 3] = ImageBuffer.data()[srcPos + 0];
}
}
}
void RenderD7::Image::FromSheet(RenderD7::Sheet sheet, size_t index)
{
}
bool RenderD7::Image::Draw(float x, float y, float scaleX, float scaleY)
{
if(loadet) return C2D_DrawImageAt(this->img, x, y, 0.5f, nullptr, scaleX, scaleY);
return false;
}
void RenderD7::Image::LoadFromBitmap(BMP bitmap)
{
loadet = false;
unsigned error = Image_to_C3D(this->img, bitmap.DATA());
if (error == 0)
{
this->loadet = true;
}
if(error) {
std::cout << "BMP decoding error " << error << std::endl;
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("Bmp - Error", "Code: " + std::to_string(error)));
}
}

9
source/Screen.cpp Normal file
View File

@ -0,0 +1,9 @@
#include <renderd7/Screen.hpp>
extern bool currentScreen;
void RenderD7::OnScreen(C3D_RenderTarget *target)
{
C2D_SceneBegin(target);
currentScreen = (target == Top || target == TopRight) ? 1 : 0;
}

21
source/Sheet.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <renderd7/Sheet.hpp>
RenderD7::Sheet::Sheet()
{
//
}
RenderD7::Sheet::~Sheet()
{
//
}
Result RenderD7::Sheet::Load(const char *path)
{
this->spritesheet = C2D_SpriteSheetLoad(path);
return 0;
}
void RenderD7::Sheet::Free()
{
C2D_SpriteSheetFree(this->spritesheet);
}

60
source/Sprite.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <renderd7/Sprite.hpp>
RenderD7::Sprite::Sprite()
{
//
}
RenderD7::Sprite::~Sprite()
{
//
}
void RenderD7::Sprite::FromSheet(RenderD7::Sheet *sheet, size_t index)
{
C2D_SpriteFromSheet(&this->sprite, sheet->spritesheet, index);
}
bool RenderD7::Sprite::Draw()
{
return C2D_DrawSprite(&this->sprite);
}
void RenderD7::Sprite::SetCenter(float x, float y)
{
C2D_SpriteSetCenter(&this->sprite, x, y);
}
void RenderD7::Sprite::SetPos(float x, float y)
{
C2D_SpriteSetPos(&this->sprite, x, y);
}
void RenderD7::Sprite::SetRotation(float rotation)
{
C2D_SpriteSetRotation(&this->sprite, rotation);
}
void RenderD7::Sprite::Rotate(float speed)
{
C2D_SpriteRotateDegrees(&this->sprite, speed);
}
float RenderD7::Sprite::getHeigh()
{
return this->sprite.params.pos.h;
}
float RenderD7::Sprite::getWidth()
{
return this->sprite.params.pos.w;
}
float RenderD7::Sprite::getPosX()
{
return this->sprite.params.pos.x;
}
float RenderD7::Sprite::getPosY()
{
return this->sprite.params.pos.y;
}
void RenderD7::Sprite::FromImage(RenderD7::Image *img)
{
C2D_SpriteFromImage(&this->sprite, img->img);
}
void RenderD7::Sprite::SetScale(float x, float y)
{
C2D_SpriteScale(&this->sprite, x, y);
}

View File

@ -0,0 +1,41 @@
#include <renderd7/SpriteAnimation.hpp>
#include <renderd7/log.hpp>
extern Log renderd7log;
RenderD7::SpriteSheetAnimation::SpriteSheetAnimation()
{
renderd7log.Write("SpriteSheetAnimation createt!");
}
RenderD7::SpriteSheetAnimation::~SpriteSheetAnimation()
{
//
}
void RenderD7::SpriteSheetAnimation::Setup(RenderD7::Sheet *sheet, size_t imagecount, size_t startimage, float frame_begin, float frame_finish)
{
D_totaltime = frame_begin;
renderd7log.Write("frame_begin success");
this->images = imagecount;
renderd7log.Write("imagecount success");
this->sheet = sheet;
renderd7log.Write("sheet success");
this->time = frame_finish;
renderd7log.Write("frame_finish success");
RenderD7::SpriteSheetAnimation::FromSheet(this->sheet, startimage);
}
void RenderD7::SpriteSheetAnimation::Play(float timespeed)
{
D_totaltime += timespeed;
if (D_totaltime >= time)
{
D_totaltime -= time;
imgs++;
if (imgs == images)
{
imgs = 0;
}
}
RenderD7::SpriteSheetAnimation::FromSheet(sheet, imgs);
//RenderD7::SpriteSheetAnimation::Draw();
}

View File

@ -1,200 +1,23 @@
#include <renderd7/Time.hpp>
namespace rnd7 {
////////////////////////////////////////////////////////////
const Time Time::Zero_;
////////////////////////////////////////////////////////////
Time::Time() :
m_microseconds(0) {
}
////////////////////////////////////////////////////////////
float Time::asSeconds() const {
return m_microseconds / 1000000.f;
}
////////////////////////////////////////////////////////////
int Time::asMilliseconds() const {
return static_cast<int>(m_microseconds / 1000);
}
////////////////////////////////////////////////////////////
long Time::asMicroseconds() const {
return m_microseconds;
}
////////////////////////////////////////////////////////////
Time::Time(long microseconds) :
m_microseconds(microseconds) {
}
////////////////////////////////////////////////////////////
Time seconds(float amount) {
return Time(static_cast<long>(amount * 1000000));
}
////////////////////////////////////////////////////////////
Time milliseconds(int amount) {
return Time(static_cast<long>(amount) * 1000);
}
////////////////////////////////////////////////////////////
Time microseconds(long amount) {
return Time(amount);
}
////////////////////////////////////////////////////////////
bool operator==(Time left, Time right) {
return left.asMicroseconds() == right.asMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator!=(Time left, Time right) {
return left.asMicroseconds() != right.asMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator<(Time left, Time right) {
return left.asMicroseconds() < right.asMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator>(Time left, Time right) {
return left.asMicroseconds() > right.asMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator<=(Time left, Time right) {
return left.asMicroseconds() <= right.asMicroseconds();
}
////////////////////////////////////////////////////////////
bool operator>=(Time left, Time right) {
return left.asMicroseconds() >= right.asMicroseconds();
}
////////////////////////////////////////////////////////////
Time operator-(Time right) {
return microseconds(-right.asMicroseconds());
}
////////////////////////////////////////////////////////////
Time operator+(Time left, Time right) {
return microseconds(left.asMicroseconds() + right.asMicroseconds());
}
////////////////////////////////////////////////////////////
Time &operator+=(Time &left, Time right) {
return left = left + right;
}
////////////////////////////////////////////////////////////
Time operator-(Time left, Time right) {
return microseconds(left.asMicroseconds() - right.asMicroseconds());
}
////////////////////////////////////////////////////////////
Time &operator-=(Time &left, Time right) {
return left = left - right;
}
////////////////////////////////////////////////////////////
Time operator*(Time left, float right) {
return seconds(left.asSeconds() * right);
}
////////////////////////////////////////////////////////////
Time operator*(Time left, long right) {
return microseconds(left.asMicroseconds() * right);
}
////////////////////////////////////////////////////////////
Time operator*(float left, Time right) {
return right * left;
}
////////////////////////////////////////////////////////////
Time operator*(long left, Time right) {
return right * left;
}
////////////////////////////////////////////////////////////
Time &operator*=(Time &left, float right) {
return left = left * right;
}
////////////////////////////////////////////////////////////
Time &operator*=(Time &left, long right) {
return left = left * right;
}
////////////////////////////////////////////////////////////
Time operator/(Time left, float right) {
return seconds(left.asSeconds() / right);
}
////////////////////////////////////////////////////////////
Time operator/(Time left, long right) {
return microseconds(left.asMicroseconds() / right);
}
////////////////////////////////////////////////////////////
Time &operator/=(Time &left, float right) {
return left = left / right;
}
////////////////////////////////////////////////////////////
Time &operator/=(Time &left, long right) {
return left = left / right;
}
////////////////////////////////////////////////////////////
float operator/(Time left, Time right) {
return left.asSeconds() / right.asSeconds();
}
////////////////////////////////////////////////////////////
Time operator%(Time left, Time right) {
return microseconds(left.asMicroseconds() % right.asMicroseconds());
}
////////////////////////////////////////////////////////////
Time &operator%=(Time &left, Time right) {
return left = left % right;
}
#include <memory>
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
std::string RenderD7::FormatString(std::string fmt_str, ...)
{
va_list ap;
char* fp = NULL;
va_start(ap, fmt_str);
vasprintf(&fp, fmt_str.c_str(), ap);
va_end(ap);
std::unique_ptr<char, decltype(free)*> formatted(fp, free);
return std::string(formatted.get());
}
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);
}

36
source/Toast.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <renderd7/Toast.hpp>
RenderD7::Toast::Toast(std::string head, std::string msg)
{
this->head = head;
this->msg = msg;
this->toast = RenderD7::BitmapPrinter(400, 70);
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->toastrendered->LoadPFromBuffer(this->toast.GetBitmap().DATA());
}
void RenderD7::Toast::Draw(void) const
{
RenderD7::OnScreen(Top);
/*RenderD7::DrawRect(0, msgposy, 400, 70, RenderD7::Color::Hex("#111111"));
RenderD7::DrawRect(0, msgposy, 400, 25, RenderD7::Color::Hex("#222222"));
RenderD7::DrawText(2, msgposy+3, 0.7f, RenderD7::Color::Hex("#ffffff"), head);
RenderD7::DrawText(2, msgposy+30, 0.6f, RenderD7::Color::Hex("#ffffff"), msg);*/
toastrendered->Draw(0, 0);
}
void RenderD7::Toast::Logic()
{
this->delay++/*=1*(int)RenderD7::GetDeltaTime()*/;
if (msgposy > 170 && delay < 2*60) msgposy--/*=(int)RenderD7::GetDeltaTime()*/;
if (delay >= 5*60)
{
msgposy++/*=(int)RenderD7::GetDeltaTime*/;
if(msgposy > 400) this->Kill();
}
}

View File

@ -2,7 +2,6 @@
#include <renderd7/log.hpp>
#include <regex>
#define RGBA8(r, g, b, a) ((((r) & 0xFF) << 0) | (((g) & 0xFF) << 8) | (((b) & 0xFF) << 16) | (((a) & 0xFF) << 24))
#define D7_NOTHING C2D_Color32(0, 0, 0, 0)
#define CFGVER "3"
Log renderd7log;
@ -124,15 +123,6 @@ void screenon()
gspLcdExit();
}
RenderD7::SpriteSheetAnimation::SpriteSheetAnimation()
{
renderd7log.Write("SpriteSheetAnimation createt!");
}
RenderD7::SpriteSheetAnimation::~SpriteSheetAnimation()
{
//
}
float RenderD7::GetDeltaTime()
{
return delta_time;
@ -218,33 +208,6 @@ void RenderD7::SetupLog()
{
renderd7log.Init("RenderD7/RenderD7.log");
}
void RenderD7::SpriteSheetAnimation::Setup(RenderD7::Sheet *sheet, size_t imagecount, size_t startimage, float frame_begin, float frame_finish)
{
D_totaltime = frame_begin;
renderd7log.Write("frame_begin success");
this->images = imagecount;
renderd7log.Write("imagecount success");
this->sheet = sheet;
renderd7log.Write("sheet success");
this->time = frame_finish;
renderd7log.Write("frame_finish success");
RenderD7::SpriteSheetAnimation::FromSheet(this->sheet, startimage);
}
void RenderD7::SpriteSheetAnimation::Play(float timespeed)
{
D_totaltime += timespeed;
if (D_totaltime >= time)
{
D_totaltime -= time;
imgs++;
if (imgs == images)
{
imgs = 0;
}
}
RenderD7::SpriteSheetAnimation::FromSheet(sheet, imgs);
//RenderD7::SpriteSheetAnimation::Draw();
}
void RenderD7::Error::DisplayError(std::string toptext, std::string errortext, int timesec)
{
@ -287,24 +250,6 @@ void RenderD7::Error::DisplayFatalError(std::string toptext, std::string errorte
}
}
}
u32 RenderD7::Color::Hex(const std::string color, u8 a)
{
if (color.length() < 7 || std::regex_search(color.substr(1), std::regex("[^0-9A-Fa-f]"))) { // invalid color.
return D7_NOTHING;
}
int r = std::stoi(color.substr(1, 2), nullptr, 16);
int g = std::stoi(color.substr(3, 2), nullptr, 16);
int b = std::stoi(color.substr(5, 2), nullptr, 16);
return RGBA8(r, g, b, a);
}
std::string RenderD7::Color::RGB2Hex(int r, int g, int b)
{
std::stringstream ss;
ss << "#";
ss << std::hex << (r << 16 | g << 8 | b );
return ss.str();
}
void RenderD7::Scene::doDraw() {
if(!RenderD7::Scene::scenes.empty())
@ -326,12 +271,6 @@ void RenderD7::Scene::Back() {
RenderD7::Scene::scenes.pop();
}
void RenderD7::OnScreen(C3D_RenderTarget *target)
{
C2D_SceneBegin(target);
currentScreen = (target == Top || target == TopRight) ? 1 : 0;
}
void frameloop()
{
frames++;
@ -382,86 +321,6 @@ bool RenderD7::MainLoop()
return running;
}
RenderD7::Sheet::Sheet()
{
//
}
RenderD7::Sheet::~Sheet()
{
//
}
Result RenderD7::Sheet::Load(const char *path)
{
this->spritesheet = C2D_SpriteSheetLoad(path);
return 0;
}
void RenderD7::Sheet::Free()
{
C2D_SpriteSheetFree(this->spritesheet);
}
RenderD7::Sprite::Sprite()
{
//
}
RenderD7::Sprite::~Sprite()
{
//
}
void RenderD7::Sprite::FromSheet(RenderD7::Sheet *sheet, size_t index)
{
C2D_SpriteFromSheet(&this->sprite, sheet->spritesheet, index);
}
bool RenderD7::Sprite::Draw()
{
return C2D_DrawSprite(&this->sprite);
}
void RenderD7::Sprite::SetCenter(float x, float y)
{
C2D_SpriteSetCenter(&this->sprite, x, y);
}
void RenderD7::Sprite::SetPos(float x, float y)
{
C2D_SpriteSetPos(&this->sprite, x, y);
}
void RenderD7::Sprite::SetRotation(float rotation)
{
C2D_SpriteSetRotation(&this->sprite, rotation);
}
void RenderD7::Sprite::Rotate(float speed)
{
C2D_SpriteRotateDegrees(&this->sprite, speed);
}
float RenderD7::Sprite::getHeigh()
{
return this->sprite.params.pos.h;
}
float RenderD7::Sprite::getWidth()
{
return this->sprite.params.pos.w;
}
float RenderD7::Sprite::getPosX()
{
return this->sprite.params.pos.x;
}
float RenderD7::Sprite::getPosY()
{
return this->sprite.params.pos.y;
}
void RenderD7::Sprite::FromImage(RenderD7::Image *img)
{
C2D_SpriteFromImage(&this->sprite, img->img);
}
void RenderD7::Sprite::SetScale(float x, float y)
{
C2D_SpriteScale(&this->sprite, x, y);
}
void RenderD7::ClearTextBufs(void)
{
C2D_TextBufClear(TextBuf);
@ -895,21 +754,6 @@ void RenderD7::DrawSTObject(std::vector<RenderD7::TObject> tobject, int tobjecti
RenderD7::DrawText(tobject[tobjectindex].x + (tobject[tobjectindex].w/2) - RenderD7::GetTextHeight(tobject[tobjectindex].txtsize , tobject[tobjectindex].text) + tobject[tobjectindex].correctx, tobject[tobjectindex].y + (tobject[tobjectindex].h/2) - RenderD7::GetTextHeight(tobject[tobjectindex].txtsize, tobject[tobjectindex].text) + tobject[tobjectindex].correcty, tobject[tobjectindex].txtsize, txtcolor, tobject[tobjectindex].text);
}
bool RenderD7::NameIsEndingWith(const std::string &name, const std::vector<std::string> &extensions) {
if (name.substr(0, 2) == "._") return false;
if (name.size() == 0) return false;
if (extensions.size() == 0) return true;
for(int i = 0; i < (int)extensions.size(); i++) {
const std::string ext = extensions.at(i);
if (strcasecmp(name.c_str() + name.size() - ext.size(), ext.c_str()) == 0) return true;
}
return false;
}
bool dirEntryPredicate(const RenderD7::DirContent &lhs, const RenderD7::DirContent &rhs) {
if (!lhs.isDir && rhs.isDir) return false;
if (lhs.isDir && !rhs.isDir) return true;
@ -949,172 +793,11 @@ void RenderD7::GetDirContents(std::vector<RenderD7::DirContent> &dircontent) {
RenderD7::GetDirContentsExt(dircontent, {});
}
void RenderD7::Image::LoadPng(const std::string path)
{
if (usedbgmsg)
{
RenderD7::Msg::Display("RenderD7", "Loading Png:" + path, Top);
}
std::vector<u8> ImageBuffer;
unsigned width, height;
if (loadet)
{
C3D_TexDelete(this->img.tex);
loadet = false;
}
lodepng::decode(ImageBuffer, width, height, path);
this->img.tex = new C3D_Tex;
this->img.subtex = new Tex3DS_SubTexture({(u16)width, (u16)height, 0.0f, 1.0f, width / 1024.0f, 1.0f - (height / 1024.0f)});
C3D_TexInit(this->img.tex, 1024, 1024, GPU_RGBA8);
C3D_TexSetFilter(this->img.tex, GPU_LINEAR, GPU_LINEAR);
this->img.tex->border = 0xFFFFFFFF;
C3D_TexSetWrap(this->img.tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
for (u32 x = 0; x < width && x < 1024; x++) {
for (u32 y = 0; y < height && y < 1024; y++) {
const u32 dstPos = ((((y >> 3) * (1024 >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) |
((x & 4) << 2) | ((y & 4) << 3))) * 4;
const u32 srcPos = (y * width + x) * 4;
((uint8_t *)this->img.tex->data)[dstPos + 0] = ImageBuffer.data()[srcPos + 3];
((uint8_t *)this->img.tex->data)[dstPos + 1] = ImageBuffer.data()[srcPos + 2];
((uint8_t *)this->img.tex->data)[dstPos + 2] = ImageBuffer.data()[srcPos + 1];
((uint8_t *)this->img.tex->data)[dstPos + 3] = ImageBuffer.data()[srcPos + 0];
}
}
loadet = true;
}
RenderD7::Image::~Image()
{
if(loadet) C3D_TexDelete(img.tex);
loadet = false;
}
void RenderD7::Image::Unload()
{
if(loadet) C3D_TexDelete(img.tex);
loadet = false;
}
void RenderD7::Image::LoadPFromBuffer(const std::vector<u8> &buffer)
{
std::vector<u8> ImageBuffer;
if (loadet)
{
C3D_TexDelete(this->img.tex);
loadet = false;
}
unsigned width, height;
lodepng::decode(ImageBuffer, width, height, buffer);
img.tex = new C3D_Tex;
img.subtex = new Tex3DS_SubTexture({(u16)width, (u16)height, 0.0f, 1.0f, width / 512.0f, 1.0f - (height / 512.0f)});
C3D_TexInit(img.tex, 512, 512, GPU_RGBA8);
C3D_TexSetFilter(img.tex, GPU_LINEAR, GPU_LINEAR);
img.tex->border = 0xFFFFFFFF;
C3D_TexSetWrap(img.tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
for (u32 x = 0; x < width && x < 512; x++) {
for (u32 y = 0; y < height && y < 512; y++) {
const u32 dstPos = ((((y >> 3) * (512 >> 3) + (x >> 3)) << 6) +
((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) |
((x & 4) << 2) | ((y & 4) << 3))) * 4;
const u32 srcPos = (y * width + x) * 4;
((uint8_t *)img.tex->data)[dstPos + 0] = ImageBuffer.data()[srcPos + 3];
((uint8_t *)img.tex->data)[dstPos + 1] = ImageBuffer.data()[srcPos + 2];
((uint8_t *)img.tex->data)[dstPos + 2] = ImageBuffer.data()[srcPos + 1];
((uint8_t *)img.tex->data)[dstPos + 3] = ImageBuffer.data()[srcPos + 0];
}
}
}
static u32 GetNextPowerOf2(u32 v) {
v--;
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
v |= v >> 8;
v |= v >> 16;
v++;
return (v >= 64 ? v : 64);
}
static bool C3DTexToC2DImage(C2D_Image *texture, u32 width, u32 height, u8 *buf) {
if (width >= 1024 || height >= 1024)
return false;
C3D_Tex *tex = new C3D_Tex[sizeof(C3D_Tex)];
Tex3DS_SubTexture *subtex = new Tex3DS_SubTexture[sizeof(Tex3DS_SubTexture)];
subtex->width = static_cast<u16>(width);
subtex->height = static_cast<u16>(height);
// RGBA -> ABGR
for (u32 row = 0; row < subtex->width; row++) {
for (u32 col = 0; col < subtex->height; col++) {
u32 z = (row + col * subtex->width) * 4;
u8 r = *(u8 *)(buf + z);
u8 g = *(u8 *)(buf + z + 1);
u8 b = *(u8 *)(buf + z + 2);
u8 a = *(u8 *)(buf + z + 3);
*(buf + z) = a;
*(buf + z + 1) = b;
*(buf + z + 2) = g;
*(buf + z + 3) = r;
}
}
u32 w_pow2 = GetNextPowerOf2(subtex->width);
u32 h_pow2 = GetNextPowerOf2(subtex->height);
subtex->left = 0.f;
subtex->top = 1.f;
subtex->right = (subtex->width /static_cast<float>(w_pow2));
subtex->bottom = (1.0 - (subtex->height / static_cast<float>(h_pow2)));
C3D_TexInit(tex, static_cast<u16>(w_pow2), static_cast<u16>(h_pow2), GPU_RGBA8);
C3D_TexSetFilter(tex, GPU_NEAREST, GPU_NEAREST);
std::memset(tex->data, 0, tex->size);
for (u32 x = 0; x < subtex->width; x++) {
for (u32 y = 0; y < subtex->height; y++) {
u32 dst_pos = ((((y >> 3) * (w_pow2 >> 3) + (x >> 3)) << 6) + ((x & 1) | ((y & 1) << 1) | ((x & 2) << 1) | ((y & 2) << 2) | ((x & 4) << 2) | ((y & 4) << 3))) * 4;
u32 src_pos = (y * subtex->width + x) * 4;
std::memcpy(&(static_cast<u8 *>(tex->data))[dst_pos], &(static_cast<u8 *>(buf))[src_pos], 4);
}
}
C3D_TexFlush(tex);
tex->border = RenderD7::Color::Hex("#000000", 0);
C3D_TexSetWrap(tex, GPU_CLAMP_TO_BORDER, GPU_CLAMP_TO_BORDER);
if (tex && subtex) {
texture->tex = tex;
texture->subtex = subtex;
return true;
}
return false;
}
void RenderD7::Image::FromSheet(RenderD7::Sheet sheet, size_t index)
{
}
bool RenderD7::DrawImage(C2D_Image img, float x, float y, float scaleX, float scaleY)
{
return C2D_DrawImageAt(img, x, y, 0.5f, nullptr, scaleX, scaleY);
}
bool RenderD7::Image::Draw(float x, float y, float scaleX, float scaleY)
{
if(loadet) return C2D_DrawImageAt(this->img, x, y, 0.5f, nullptr, scaleX, scaleY);
return false;
}
bool RenderD7::FS::FileExist(const std::string& path)
{
FILE *test = fopen(path.c_str(), "r");
@ -1473,449 +1156,3 @@ bool RenderD7::Console::Update()
bool dr_sc = true;
return dr_sc;
}
std::string RenderD7::FormatString(std::string fmt_str, ...)
{
va_list ap;
char* fp = NULL;
va_start(ap, fmt_str);
vasprintf(&fp, fmt_str.c_str(), ap);
va_end(ap);
std::unique_ptr<char, decltype(free)*> formatted(fp, free);
return std::string(formatted.get());
}
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);
}
extern "C"
{
#include <renderd7/external/libnsbmp/libnsbmp.h>
}
static const u32 BYTES_PER_PIXEL = 4;
#define MAX_IMAGE_BYTES (48 * 1024 * 1024)
namespace LIBBMP {
static void *bitmap_create(int width, int height, [[maybe_unused]] unsigned int state) {
/* ensure a stupidly large (>50Megs or so) bitmap is not created */
if ((static_cast<long long>(width) * static_cast<long long>(height)) > (MAX_IMAGE_BYTES/BYTES_PER_PIXEL))
return nullptr;
return std::calloc(width * height, BYTES_PER_PIXEL);
}
static unsigned char *bitmap_get_buffer(void *bitmap) {
assert(bitmap);
return static_cast<unsigned char *>(bitmap);
}
static size_t bitmap_get_bpp([[maybe_unused]] void *bitmap) {
return BYTES_PER_PIXEL;
}
static void bitmap_destroy(void *bitmap) {
assert(bitmap);
std::free(bitmap);
}
}
unsigned Image_to_C3D(C2D_Image img, const std::vector<unsigned char>& bmpc) {
bmp_bitmap_callback_vt bitmap_callbacks = {
LIBBMP::bitmap_create,
LIBBMP::bitmap_destroy,
LIBBMP::bitmap_get_buffer,
LIBBMP::bitmap_get_bpp
};
bmp_result code = BMP_OK;
bmp_image bmp;
bmp_create(&bmp, &bitmap_callbacks);
code = bmp_analyse(&bmp, bmpc.size(), (u8*)bmpc.data());
if (code != BMP_OK) {
bmp_finalise(&bmp);
return 1;
}
code = bmp_decode(&bmp);
if (code != BMP_OK) {
if ((code != BMP_INSUFFICIENT_DATA) && (code != BMP_DATA_ERROR)) {
bmp_finalise(&bmp);
return 2;
}
/* skip if the decoded image would be ridiculously large */
if ((bmp.width * bmp.height) > 200000) {
bmp_finalise(&bmp);
return 3;
}
}
C2D_Image* texture = new C2D_Image();
bool ret = C3DTexToC2DImage(texture, static_cast<u32>(bmp.width), static_cast<u32>(bmp.height), static_cast<u8 *>(bmp.bitmap));
bmp_finalise(&bmp);
delete texture;
if (!ret)
{
return 4;
}
return 0;
}
void RenderD7::Image::LoadFromBitmap(BMP bitmap)
{
loadet = false;
unsigned error = Image_to_C3D(this->img, bitmap.DATA());
if (error == 0)
{
this->loadet = true;
}
if(error) {
std::cout << "BMP decoding error " << error << std::endl;
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("Bmp - Error", "Code: " + std::to_string(error)));
}
}
RenderD7::Toast::Toast(std::string head, std::string msg)
{
this->head = head;
this->msg = msg;
}
void RenderD7::Toast::Draw(void) const
{
RenderD7::OnScreen(Top);
RenderD7::DrawRect(0, msgposy, 400, 70, RenderD7::Color::Hex("#111111"));
RenderD7::DrawRect(0, msgposy, 400, 25, RenderD7::Color::Hex("#222222"));
RenderD7::DrawText(2, msgposy+3, 0.7f, RenderD7::Color::Hex("#ffffff"), head);
RenderD7::DrawText(2, msgposy+30, 0.6f, RenderD7::Color::Hex("#ffffff"), msg);
}
void RenderD7::Toast::Logic()
{
this->delay++/*=(int)RenderD7::GetDeltaTime()*/;
if (msgposy > 170 && delay < 2*60) msgposy--/*=(int)RenderD7::GetDeltaTime()*/;
if (delay >= 5*60)
{
msgposy++/*=(int)RenderD7::GetDeltaTime*/;
if(msgposy > 400) this->Kill();
}
}
RenderD7::BitmapPrinter::BitmapPrinter(int w, int h)
{
BMP newmap(w, h, true);
bitmap = newmap;
//renderframe.LoadPFromBuffer(BitmapConverter::ConvertData(bitmap.DATA()));
blank = newmap;
}
RenderD7::BitmapPrinter::~BitmapPrinter()
{
if(this->renderframe.loadet) this->renderframe.Unload();
}
bool RenderD7::BitmapPrinter::DecodeFile(std::string file)
{
unsigned error = bitmap.read(file.c_str());
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter", "Error Code: " + std::to_string(error)));
return false;
}
return true;
}
bool RenderD7::BitmapPrinter::DecodeMem(std::vector<unsigned char> buffer)
{
unsigned error = bitmap.read_mem(buffer);
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter", "Error Code: " + std::to_string(error)));
return false;
}
return true;
}
void RenderD7::BitmapPrinter::DrawPixel(int x, int y, u8 b, u8 g, u8 r, u8 a)
{
unsigned error = bitmap.set_pixel(x, bitmap.bmp_info_header.height - y, b, g, r, a);
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter->Pixel", "Error Code: " + std::to_string(error)));
}
}
void RenderD7::BitmapPrinter::DrawRect(int x, int y, int w, int h, u8 line_w, u8 b, u8 g, u8 r, u8 a)
{
unsigned error = bitmap.draw_rectangle(x, bitmap.bmp_info_header.height - y - h, w, h, b, g, r, a, line_w);
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter->Rect", "Error Code: " + std::to_string(error)));
}
}
void RenderD7::BitmapPrinter::DrawRectFilled(int x, int y, int w, int h, u8 b, u8 g, u8 r, u8 a)
{
unsigned error = bitmap.fill_region(x, bitmap.bmp_info_header.height - h - y, w, h, b, g, r, a);
if (error)
{
RenderD7::AddOvl(std::make_unique<RenderD7::Toast>("BitmapPrinter->RectF", "Error Code: " + std::to_string(error)));
}
}
void RenderD7::BitmapPrinter::SaveBmp(std::string name)
{
if(!RenderD7::NameIsEndingWith(name, {"bmp"}))
{
name += ".bmp";
}
bitmap.write(name.c_str());
}
void RenderD7::BitmapPrinter::SavePng(std::string name)
{
if(!RenderD7::NameIsEndingWith(name, {"png"}))
{
name += ".png";
}
std::vector<unsigned char> ImageBuffer;
ImageBuffer = BitmapConverter::ConvertData(bitmap.DATA());
lodepng::save_file(ImageBuffer, name);
}
void RenderD7::BitmapPrinter::CreateScreen(C3D_RenderTarget *target)
{
isscreen = true;
targetr = target;
if (target == Top)
{
bitmap = BMP(400, 240, true);
blank = BMP(400, 240, true);
}
if (target == TopRight)
{
bitmap = BMP(400, 240, true);
blank = BMP(400, 240, true);
}
if (target == Bottom)
{
bitmap = BMP(320, 240, true);
blank = BMP(320, 240, true);
}
renderframe.LoadPFromBuffer(BitmapConverter::ConvertData(bitmap.DATA()));
}
void RenderD7::BitmapPrinter::DrawScreenDirectF(int framerate)
{
if (isscreen)
{
if(frame == (60/framerate)){
RenderD7::OnScreen(targetr);
if(renderframe.loadet) renderframe.Unload();
renderframe.LoadFromBitmap(bitmap);
frame = 0;
}
if(renderframe.loadet) renderframe.Draw(0, 0);
frame++;
}
}
void RenderD7::BitmapPrinter::DrawScreenDirect()
{
if (isscreen)
{
RenderD7::OnScreen(targetr);
if(renderframe.loadet) renderframe.Unload();
renderframe.LoadFromBitmap(bitmap);
if(renderframe.loadet) renderframe.Draw(0, 0);
}
}
RenderD7::Image RenderD7::BitmapPrinter::GetImage()
{
RenderD7::Image img;
img.LoadFromBitmap(bitmap);
return img;
}
void RenderD7::BitmapPrinter::UsePreMap(BMP map)
{
bitmap = map;
}
void RenderD7::BitmapPrinter::UsePrePrintMap(BitmapPrinter printmap)
{
bitmap = printmap.GetBitmap();
}
void RenderD7::BitmapPrinter::Clear(u8 b, u8 g, u8 r, u8 a)
{
bitmap.fill_region(0, 0, bitmap.bmp_info_header.width, bitmap.bmp_info_header.height, b, g, r, a);
}
void RenderD7::BitmapPrinter::ClearBlank()
{
bitmap = blank;
}
void RenderD7::BitmapPrinter::DrawScreenF(int framerate)
{
if (isscreen)
{
if(frame == (60/framerate)){
RenderD7::OnScreen(targetr);
frame = 0;
}
if(renderframe.loadet) renderframe.Draw(0, 0);
frame++;
}
}
void RenderD7::BitmapPrinter::DrawScreen()
{
if (isscreen)
{
RenderD7::OnScreen(targetr);
if(renderframe.loadet) renderframe.Draw(0, 0);
}
}
void RenderD7::BitmapPrinter::UpdateScreenF(int framerate)
{
if (isscreen)
{
if(frame == (60/framerate)){
if(renderframe.loadet) renderframe.Unload();
renderframe.LoadFromBitmap(bitmap);
frame = 0;
}
frame++;
}
}
void RenderD7::BitmapPrinter::UpdateScreen()
{
if (isscreen)
{
if(renderframe.loadet) renderframe.Unload();
renderframe.LoadFromBitmap(bitmap);
}
}
#define TICKS_PER_MSEC 268111.856
void RenderD7::BitmapPrinter::Benchmark()
{
if(setupbenchmark)
{
frametime = 0;
renderedframes = 0;
timer = 0;
setupbenchmark = false;
lastTime = svcGetSystemTick();
}
if(benchmark)
{
if(timer >= 60)
{
std::string renderedf = std::to_string(renderedframes);
std::string avgdtt = std::to_string(mhdtt);
float alldtt = 0;
for (size_t i = 1; i < hdttt.size(); i++)
{
alldtt += hdttt[i];
}
float alldtt2 = 0;
for (size_t i = 1; i < hdttt2.size(); i++)
{
alldtt2 += hdttt2[i];
}
float alldtt3 = 0;
for (size_t i = 1; i < hdttt3.size(); i++)
{
alldtt3 += hdttt3[i];
}
int allfps = 0;
for (size_t f = 1; f < fpscountc.size(); f++)
{
allfps += fpscountc[f];
}
std::string avgcpu = std::to_string((alldtt/(float)hdttt.size()-1));
std::string avgcpu2 = std::to_string(((alldtt2/(float)hdttt2.size()-1)*1000));
std::string avgcpu3 = std::to_string(((alldtt3/(float)hdttt3.size()-1)*1000));
std::string avgfps = std::to_string((allfps/(int)fpscountc.size()-1));
std::string resultt = "Frames Rendered: " + renderedf + "\nMax Cpu Time: " + avgdtt + "\nAvg Cpu Time: " + avgcpu + "\nAvg Fps: " + avgfps + "\nAvg RenderTime: " + avgcpu2 + "ms/f\nAvg ConvertTime: " + avgcpu3 + "ms\n";
RenderD7::Error::DisplayError("Result", resultt, 30);
benchmark = false;
}
uint64_t currentTime = svcGetSystemTick();
dtt = ((float)(currentTime / (float)TICKS_PER_MSEC) - (float)(lastTime / (float)TICKS_PER_MSEC)) / 1000.f;
lastTime = currentTime;
last_time = currentTime;
frameCounter++;
fpsClock += dtt;
if (fpsClock >= 1.f) {
fps = frameCounter;
frameCounter = 0;
fpsClock = 0.f;
}
uint64_t lastTime2 = svcGetSystemTick();
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);
uint64_t currentTime2 = svcGetSystemTick();
dtt2 = ((float)(currentTime2 / (float)TICKS_PER_MSEC) - (float)(lastTime2 / (float)TICKS_PER_MSEC)) / 1000.f;
hdttt2.push_back(dtt2);
lastTime2 = svcGetSystemTick();
this->UpdateScreenF(testfps);
currentTime2 = svcGetSystemTick();
dtt3 = ((float)(currentTime2 / (float)TICKS_PER_MSEC) - (float)(lastTime2 / (float)TICKS_PER_MSEC)) / 1000.f;
hdttt3.push_back(dtt3);
if (!shouldbe_disabled) this->DrawScreen();
renderedframes++;
if(mdtt2 < dtt2)
{
mdtt2 = dtt2;
}
if(mdtt3 < dtt3)
{
mdtt3 = dtt3;
}
timer+= 1*dtt;
float hdtt = C3D_GetProcessingTime();
hdttt.push_back(hdtt);
fpscountc.push_back(fps);
if(mhdtt < hdtt)
{
mhdtt = C3D_GetProcessingTime();
}
if (!shouldbe_disabled)
{
RenderD7::OnScreen(Bottom);
RenderD7::DrawText(0, 0, 0.5f, RenderD7::Color::Hex("#ff0000"), "Time: " + std::to_string(timer));
RenderD7::DrawText(0, 20, 0.5f, RenderD7::Color::Hex("#ff0000"), "Fps: " + std::to_string(fps));
RenderD7::DrawText(0, 40, 0.5f, RenderD7::Color::Hex("#ff0000"), "dt: " + std::to_string(dtt));
RenderD7::DrawText(0, 60, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxRenderTime: " + std::to_string(mdtt2*1000) + "ms/f");
RenderD7::DrawText(0, 80, 0.5f, RenderD7::Color::Hex("#ff0000"), "MaxConvertTime: " + std::to_string(mdtt3*1000) + "ms");
}
}
}
void RenderD7::BitmapPrinter::SetupBenchmark(int framerate)
{
benchmark = true;
setupbenchmark = true;
this->testfps = framerate;
}
void RenderD7::BitmapPrinter::DrawText(int x, int y, float t_size, u32 color, std::string text)
{
//Todo
}