Remove redundant text rendering code, use printf
This commit is contained in:
parent
eb7f4a8d88
commit
f6349215ac
@ -1,9 +0,0 @@
|
|||||||
#include <3ds.h>
|
|
||||||
#include "font.h"
|
|
||||||
|
|
||||||
font_s fontDefault = {
|
|
||||||
font1Data,
|
|
||||||
font1Desc,
|
|
||||||
16,
|
|
||||||
{ 0xFF, 0xFF, 0xFF }
|
|
||||||
};
|
|
@ -1,34 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
struct Glyph {
|
|
||||||
// Glyph representation
|
|
||||||
char c;
|
|
||||||
|
|
||||||
// x and y origin of the character.
|
|
||||||
int x, y;
|
|
||||||
|
|
||||||
// width and height in pixels.
|
|
||||||
int w, h;
|
|
||||||
|
|
||||||
// x and y offset
|
|
||||||
int xo, yo;
|
|
||||||
|
|
||||||
// Pixels after this character to begin
|
|
||||||
// drawing the next one.
|
|
||||||
int xa;
|
|
||||||
|
|
||||||
// Glyph data.
|
|
||||||
u8* data;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct font_s {
|
|
||||||
u8* data;
|
|
||||||
Glyph* desc;
|
|
||||||
u8 height;
|
|
||||||
u8 color[3];
|
|
||||||
};
|
|
||||||
|
|
||||||
extern u8 font1Data[];
|
|
||||||
extern Glyph font1Desc[];
|
|
||||||
|
|
||||||
extern font_s fontDefault;
|
|
261
source/font1.cpp
261
source/font1.cpp
File diff suppressed because one or more lines are too long
@ -6,7 +6,6 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <3ds.h>
|
#include <3ds.h>
|
||||||
|
|
||||||
#include "output.h"
|
|
||||||
#include "utils/shared_font/shared_font.h"
|
#include "utils/shared_font/shared_font.h"
|
||||||
#include "utils/savedatacheck/savedatacheck.h"
|
#include "utils/savedatacheck/savedatacheck.h"
|
||||||
|
|
||||||
@ -18,24 +17,22 @@ static void (*utils[]) (void) = {
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
srvInit();
|
|
||||||
aptInit();
|
aptInit();
|
||||||
hidInit(NULL);
|
|
||||||
fsInit();
|
|
||||||
gfxInitDefault();
|
gfxInitDefault();
|
||||||
gfxSet3D(false);
|
consoleInit(GFX_TOP, nullptr);
|
||||||
|
|
||||||
clearScreens();
|
consoleClear();
|
||||||
print(GFX_TOP, "Press A to begin...\n");
|
printf("Press A to begin...\n");
|
||||||
|
|
||||||
while (aptMainLoop()) {
|
while (aptMainLoop()) {
|
||||||
drawFrames();
|
gfxFlushBuffers();
|
||||||
|
gfxSwapBuffers();
|
||||||
|
|
||||||
hidScanInput();
|
hidScanInput();
|
||||||
if (hidKeysDown() & KEY_START) {
|
if (hidKeysDown() & KEY_START) {
|
||||||
break;
|
break;
|
||||||
} else if (hidKeysDown() & KEY_A) {
|
} else if (hidKeysDown() & KEY_A) {
|
||||||
clearScreen(GFX_TOP);
|
consoleClear();
|
||||||
|
|
||||||
if (util_counter < (sizeof(utils) / sizeof(utils[0]))) {
|
if (util_counter < (sizeof(utils) / sizeof(utils[0]))) {
|
||||||
utils[util_counter]();
|
utils[util_counter]();
|
||||||
@ -44,19 +41,16 @@ int main()
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
print(GFX_TOP, "\n");
|
printf("\n");
|
||||||
print(GFX_TOP, "Press A to continue...\n");
|
printf("Press A to continue...\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
gspWaitForEvent(GSPEVENT_VBlank0, false);
|
gspWaitForEvent(GSPEVENT_VBlank0, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
clearScreens();
|
consoleClear();
|
||||||
|
|
||||||
gfxExit();
|
gfxExit();
|
||||||
fsExit();
|
|
||||||
hidExit();
|
|
||||||
aptExit();
|
aptExit();
|
||||||
srvExit();
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,98 +0,0 @@
|
|||||||
#include "output.h"
|
|
||||||
|
|
||||||
#include <algorithm>
|
|
||||||
#include <string>
|
|
||||||
#include <cstring>
|
|
||||||
#include <cmath>
|
|
||||||
#include <cstdarg>
|
|
||||||
#include <cstdio>
|
|
||||||
|
|
||||||
#include <3ds.h>
|
|
||||||
|
|
||||||
#include "text.h"
|
|
||||||
|
|
||||||
static std::string bufferTop;
|
|
||||||
static std::string bufferBottom;
|
|
||||||
|
|
||||||
static int countLines(const std::string& str)
|
|
||||||
{
|
|
||||||
if (str.empty())
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
return 1 + std::count_if(str.begin(), str.end(), [](char c) { return c == '\n'; });
|
|
||||||
}
|
|
||||||
|
|
||||||
static void deleteFirstLine(std::string* str)
|
|
||||||
{
|
|
||||||
if (str->empty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
size_t linebreak = str->find_first_of('\n');
|
|
||||||
|
|
||||||
if (linebreak == std::string::npos || linebreak + 1 > str->length()) {
|
|
||||||
*str = {};
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
*str = str->substr(linebreak + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void drawFrame(gfxScreen_t screen, char b, char g, char r)
|
|
||||||
{
|
|
||||||
int screenHeight = 240;
|
|
||||||
int screenWidth = (screen == GFX_TOP) ? 400 : 320;
|
|
||||||
std::string& textBuffer = (screen == GFX_TOP) ? bufferTop : bufferBottom;
|
|
||||||
|
|
||||||
u8* bufAdr = gfxGetFramebuffer(screen, GFX_LEFT, nullptr, nullptr);
|
|
||||||
for (int i = 0; i < screenWidth * screenHeight * 3; i += 3) {
|
|
||||||
bufAdr[i] = b;
|
|
||||||
bufAdr[i+1] = g;
|
|
||||||
bufAdr[i+2] = r;
|
|
||||||
}
|
|
||||||
|
|
||||||
int lines = countLines(textBuffer);
|
|
||||||
while (lines > (screenHeight / fontDefault.height - 3)) {
|
|
||||||
deleteFirstLine(&textBuffer);
|
|
||||||
lines--;
|
|
||||||
}
|
|
||||||
gfxDrawText(screen, GFX_LEFT, nullptr, textBuffer, screenHeight - fontDefault.height * 3, 10);
|
|
||||||
}
|
|
||||||
|
|
||||||
void drawFrames()
|
|
||||||
{
|
|
||||||
drawFrame(GFX_TOP, 0x88, 0x66, 0x00);
|
|
||||||
drawFrame(GFX_BOTTOM, 0x00, 0x00, 0x00);
|
|
||||||
gfxFlushBuffers();
|
|
||||||
gfxSwapBuffers();
|
|
||||||
}
|
|
||||||
|
|
||||||
void print(gfxScreen_t screen, const char* format, ...)
|
|
||||||
{
|
|
||||||
std::string& textBuffer = (screen == GFX_TOP) ? bufferTop : bufferBottom;
|
|
||||||
|
|
||||||
va_list arguments;
|
|
||||||
char *vaStr;
|
|
||||||
|
|
||||||
va_start(arguments, format);
|
|
||||||
vasprintf(&vaStr, format, arguments);
|
|
||||||
va_end(arguments);
|
|
||||||
|
|
||||||
textBuffer += std::string(vaStr);
|
|
||||||
svcOutputDebugString(vaStr, strlen(vaStr));
|
|
||||||
free(vaStr);
|
|
||||||
|
|
||||||
drawFrames();
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearScreen(gfxScreen_t screen)
|
|
||||||
{
|
|
||||||
std::string& textBuffer = (screen == GFX_TOP) ? bufferTop : bufferBottom;
|
|
||||||
textBuffer.clear();
|
|
||||||
drawFrames();
|
|
||||||
}
|
|
||||||
|
|
||||||
void clearScreens()
|
|
||||||
{
|
|
||||||
clearScreen(GFX_TOP);
|
|
||||||
clearScreen(GFX_BOTTOM);
|
|
||||||
}
|
|
@ -1,8 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <3ds.h>
|
|
||||||
|
|
||||||
void drawFrames();
|
|
||||||
void print(gfxScreen_t screen, const char* format, ...);
|
|
||||||
void clearScreen(gfxScreen_t screen);
|
|
||||||
void clearScreens();
|
|
@ -1,80 +0,0 @@
|
|||||||
#include <stdlib.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <3ds.h>
|
|
||||||
#include "text.h"
|
|
||||||
|
|
||||||
#include "font.h"
|
|
||||||
|
|
||||||
//this code is not meant to be readable
|
|
||||||
int drawCharacter(u8* fb, font_s* font, char c, s16 x, s16 y, u16 w, u16 h)
|
|
||||||
{
|
|
||||||
Glyph* cd = &font->desc[(int)c];
|
|
||||||
|
|
||||||
if (!cd->data)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
x += cd->xo; y += font->height - cd->yo - cd->h;
|
|
||||||
|
|
||||||
if (x < 0 || x + cd->w >= w || y < -cd->h || y >= h + cd->h)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
u8* charData = cd->data;
|
|
||||||
s16 cy = y, ch = cd->h, cyo = 0;
|
|
||||||
|
|
||||||
if (y < 0) {
|
|
||||||
cy = 0;
|
|
||||||
cyo = -y;
|
|
||||||
ch = cd->h-cyo;
|
|
||||||
} else if (y + ch > h) {
|
|
||||||
ch = h - y;
|
|
||||||
}
|
|
||||||
|
|
||||||
fb += (x * h + cy) * 3;
|
|
||||||
const u8 r = font->color[0];
|
|
||||||
const u8 g = font->color[1];
|
|
||||||
const u8 b = font->color[2];
|
|
||||||
|
|
||||||
for (int i = 0; i < cd->w; i++) {
|
|
||||||
charData += cyo;
|
|
||||||
for (int j = 0; j < ch; j++) {
|
|
||||||
u8 v = *(charData++);
|
|
||||||
if (v) {
|
|
||||||
fb[0] = (fb[0] * (0xFF - v) + (b * v)) >> 8;
|
|
||||||
fb[1] = (fb[1] * (0xFF - v) + (g * v)) >> 8;
|
|
||||||
fb[2] = (fb[2] * (0xFF - v) + (r * v)) >> 8;
|
|
||||||
}
|
|
||||||
fb += 3;
|
|
||||||
}
|
|
||||||
charData += (cd->h - (cyo + ch));
|
|
||||||
fb += (h - ch) * 3;
|
|
||||||
}
|
|
||||||
return cd->xa;
|
|
||||||
}
|
|
||||||
|
|
||||||
void drawString(u8* fb, font_s* f, const std::string& str, s16 x, s16 y, u16 w, u16 h)
|
|
||||||
{
|
|
||||||
if (!f || !fb)
|
|
||||||
return;
|
|
||||||
|
|
||||||
int dx = 0, dy = 0;
|
|
||||||
for (const char& c : str)
|
|
||||||
{
|
|
||||||
dx += drawCharacter(fb, f, c, x + dx, y + dy, w, h);
|
|
||||||
if (c == '\n') {
|
|
||||||
dx = 0;
|
|
||||||
dy -= f->height;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void gfxDrawText(gfxScreen_t screen, gfx3dSide_t side, font_s* font, const std::string& str, s16 x, s16 y)
|
|
||||||
{
|
|
||||||
if (!font)
|
|
||||||
font = &fontDefault;
|
|
||||||
|
|
||||||
u16 fbWidth, fbHeight;
|
|
||||||
u8* fbAdr = gfxGetFramebuffer(screen, side, &fbWidth, &fbHeight);
|
|
||||||
|
|
||||||
drawString(fbAdr, font, str, y, x, fbHeight, fbWidth);
|
|
||||||
}
|
|
@ -1,9 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#include "font.h"
|
|
||||||
|
|
||||||
int drawCharacter(u8* fb, font_s* f, char c, s16 x, s16 y, u16 w, u16 h);
|
|
||||||
void drawString(u8* fb, font_s* f, const std::string& str, s16 x, s16 y, u16 w, u16 h);
|
|
||||||
void gfxDrawText(gfxScreen_t screen, gfx3dSide_t side, font_s* f, const std::string& str, s16 x, s16 y);
|
|
@ -8,7 +8,6 @@
|
|||||||
|
|
||||||
#include <3ds.h>
|
#include <3ds.h>
|
||||||
|
|
||||||
#include "output.h"
|
|
||||||
#include "utils/shared_font/shared_font.h"
|
#include "utils/shared_font/shared_font.h"
|
||||||
|
|
||||||
namespace SaveDataCheck {
|
namespace SaveDataCheck {
|
||||||
@ -34,7 +33,7 @@ void DumpSharedRomFS(u32* archive_binary_lowpath) {
|
|||||||
u8 file_binary_lowpath[20] = {};
|
u8 file_binary_lowpath[20] = {};
|
||||||
FS_path romfs_path = { PATH_BINARY, 20, file_binary_lowpath };
|
FS_path romfs_path = { PATH_BINARY, 20, file_binary_lowpath };
|
||||||
|
|
||||||
print(GFX_TOP, "Dumping SaveDataCheck RomFS (%s)... ", output_file.c_str());
|
printf("Dumping SaveDataCheck RomFS (%s)... ", output_file.c_str());
|
||||||
|
|
||||||
FSUSER_OpenFileDirectly(NULL, &romfs_handle, savedatacheck_archive, romfs_path, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
|
FSUSER_OpenFileDirectly(NULL, &romfs_handle, savedatacheck_archive, romfs_path, FS_OPEN_READ, FS_ATTRIBUTE_NONE);
|
||||||
FSFILE_GetSize(romfs_handle, &romfs_size);
|
FSFILE_GetSize(romfs_handle, &romfs_size);
|
||||||
@ -50,9 +49,9 @@ void DumpSharedRomFS(u32* archive_binary_lowpath) {
|
|||||||
fclose(out_file);
|
fclose(out_file);
|
||||||
|
|
||||||
if (bytes_written == romfs_size)
|
if (bytes_written == romfs_size)
|
||||||
print(GFX_TOP, "Done!\n");
|
printf("Done!\n");
|
||||||
else
|
else
|
||||||
print(GFX_TOP, "Failed!\n");
|
printf("Failed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void Dump() {
|
void Dump() {
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
|
|
||||||
#include <3ds.h>
|
#include <3ds.h>
|
||||||
|
|
||||||
#include "output.h"
|
|
||||||
#include "utils/shared_font/shared_font.h"
|
#include "utils/shared_font/shared_font.h"
|
||||||
|
|
||||||
namespace SharedFont {
|
namespace SharedFont {
|
||||||
@ -16,13 +15,13 @@ static const u32 SHARED_FONT_SIZE = 0x300000;
|
|||||||
void Dump() {
|
void Dump() {
|
||||||
static const char* path = "/shared_font.bin";
|
static const char* path = "/shared_font.bin";
|
||||||
|
|
||||||
print(GFX_TOP, "Dumping shared system font (%s)... ", path);
|
printf("Dumping shared system font (%s)... ", path);
|
||||||
|
|
||||||
// Connect to APT service...
|
// Connect to APT service...
|
||||||
|
|
||||||
Handle apt_handle;
|
Handle apt_handle;
|
||||||
srvGetServiceHandle(&apt_handle, "APT:U");
|
srvGetServiceHandle(&apt_handle, "APT:U");
|
||||||
u32* cmdbuf=getThreadCommandBuffer();
|
u32* cmdbuf = getThreadCommandBuffer();
|
||||||
|
|
||||||
// Call APT::GetSharedFont function to load font into memory...
|
// Call APT::GetSharedFont function to load font into memory...
|
||||||
|
|
||||||
@ -46,12 +45,10 @@ void Dump() {
|
|||||||
size_t bytes_written = fwrite(shared_font_addr, 1, SHARED_FONT_SIZE, out_file);
|
size_t bytes_written = fwrite(shared_font_addr, 1, SHARED_FONT_SIZE, out_file);
|
||||||
fclose(out_file);
|
fclose(out_file);
|
||||||
|
|
||||||
// Check result...
|
|
||||||
|
|
||||||
if (bytes_written == SHARED_FONT_SIZE)
|
if (bytes_written == SHARED_FONT_SIZE)
|
||||||
print(GFX_TOP, "Done!\n");
|
printf("Done!\n");
|
||||||
else
|
else
|
||||||
print(GFX_TOP, "Failed!\n");
|
printf("Failed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
Loading…
x
Reference in New Issue
Block a user