From f07619547248dd96bd19945fe34cc8a1e67ee887 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 21 Apr 2023 21:37:34 +1000 Subject: [PATCH] 3DS: System fonts draw something now Even if that something is just boxes --- src/Drawer2D.c | 114 ++++++++++++++++++++++++++++++++++++++++++++- src/Logger.c | 2 +- src/Platform_3DS.c | 15 +++--- 3 files changed, 121 insertions(+), 10 deletions(-) diff --git a/src/Drawer2D.c b/src/Drawer2D.c index 45e6e164c..b970319a3 100644 --- a/src/Drawer2D.c +++ b/src/Drawer2D.c @@ -1388,7 +1388,7 @@ static int Font_SysTextWidth(struct DrawTextArgs* args) { static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int x, int y, cc_bool shadow) { interop_SysTextDraw(args, bmp, x, y, shadow); } -#elif defined CC_BUILD_PSP || defined CC_BUILD_3DS +#elif defined CC_BUILD_PSP void SysFonts_Register(const cc_string* path) { } const cc_string* SysFonts_UNSAFE_GetDefault(void) { return &String_Empty; } @@ -1419,4 +1419,114 @@ static int Font_SysTextWidth(struct DrawTextArgs* args) { static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int x, int y, cc_bool shadow) { } -#endif +#elif defined CC_BUILD_3DS +#include <3ds.h> + +void SysFonts_Register(const cc_string* path) { } + +const cc_string* SysFonts_UNSAFE_GetDefault(void) { return &String_Empty; } + +void SysFonts_GetNames(struct StringsBuffer* buffer) { } + +cc_result SysFont_Make(struct FontDesc* desc, const cc_string* fontName, int size, int flags) { + desc->size = size; + desc->flags = flags; + desc->height = Drawer2D_AdjHeight(size); + + desc->handle = fontGetSystemFont(); + return 0; +} + +void SysFont_MakeDefault(struct FontDesc* desc, int size, int flags) { + SysFont_Make(desc, NULL, size, flags); +} + +void Font_Free(struct FontDesc* desc) { + desc->size = 0; + desc->handle = NULL; +} + +static int Font_SysTextWidth(struct DrawTextArgs* args) { + int width = 0; + cc_string left = args->text, part; + char colorCode = 'f'; + CFNT_s* font = (CFNT_s*)args->font->handle; + + while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode)) + { + for (int i = 0; i < part.length; i++) + { + cc_unichar cp = Convert_CP437ToUnicode(part.buffer[i]); + int glyphIndex = fontGlyphIndexFromCodePoint(font, cp); + if (glyphIndex < 0) continue; + + charWidthInfo_s* wInfo = fontGetCharWidthInfo(font, glyphIndex); + width += wInfo->charWidth; + } + } + return max(1, width); +} + +static void DrawGlyph(CFNT_s* font, struct Bitmap* bmp, int x, int y, int glyphIndex) { + TGLP_s* tglp = font->finf.tglp; + + //int glyphsPerSheet = tglp->nRows * tglp->nLines; + //int sheetIdx = glyphIndex / glyphsPerSheet; + //int sheetPos = glyphIndex % glyphsPerSheet; + //u8* sheet = tglp->sheetData + (sheetIdx * tglp->sheetSize); + + //int rowY = sheetPos / tglp->nRows; + //int rowX = sheetPos % tglp->nRows; + + //int FMT = tglp->sheetFmt; + //Platform_Log1("FMT: %i", &FMT); + + charWidthInfo_s* wInfo = fontGetCharWidthInfo(font, glyphIndex); + + /*for (int Y = 0; Y < tglp->cellHeight; Y++) + { + for (int X = wInfo->left; X < wInfo->left + wInfo->glyphWidth; X++) + { + int XX = x + X, YY = y + Y; + if (XX < 0 || YY < 0 || XX >= bmp->width || YY >= bmp->height) continue; + + int srcX = X + rowX * tglp->cellWidth; + int srcY = Y + rowY * tglp->cellHeight; // TODO wrong. morton offset too? + + u8 VALUE = ((sheet + srcY * (tglp->sheetWidth >> 1))[srcX >> 1] & 0x0F) << 4; + Bitmap_GetPixel(bmp, XX, YY) = BitmapColor_RGB(VALUE, VALUE, VALUE); + } + }*/ + + for (int Y = 0; Y < tglp->cellHeight; Y++) + { + for (int X = wInfo->left; X < wInfo->left + wInfo->glyphWidth; X++) + { + int XX = x + X, YY = y + Y; + if (XX < 0 || YY < 0 || XX >= bmp->width || YY >= bmp->height) continue; + + Bitmap_GetPixel(bmp, XX, YY) = BITMAPCOLOR_WHITE; + } + } +} + +static void Font_SysTextDraw(struct DrawTextArgs* args, struct Bitmap* bmp, int x, int y, cc_bool shadow) { + cc_string left = args->text, part; + char colorCode = 'f'; + CFNT_s* font = (CFNT_s*)args->font->handle; + + while (Drawer2D_UNSAFE_NextPart(&left, &part, &colorCode)) + { + for (int i = 0; i < part.length; i++) + { + cc_unichar cp = Convert_CP437ToUnicode(part.buffer[i]); + int glyphIndex = fontGlyphIndexFromCodePoint(font, cp); + if (glyphIndex < 0) continue; + + DrawGlyph(font, bmp, x, y, glyphIndex); + charWidthInfo_s* wInfo = fontGetCharWidthInfo(font, glyphIndex); + x += wInfo->charWidth; + } + } +} +#endif \ No newline at end of file diff --git a/src/Logger.c b/src/Logger.c index 974b51333..61cd8078b 100644 --- a/src/Logger.c +++ b/src/Logger.c @@ -36,7 +36,7 @@ #endif /* Only show up to 40 frames in backtrace */ -#define MAX_BACKTRACE_FRAMES 40x +#define MAX_BACKTRACE_FRAMES 40 static void AbortCommon(cc_result result, const char* raw_msg, void* ctx); diff --git a/src/Platform_3DS.c b/src/Platform_3DS.c index 48e99f0e8..048cfa3bd 100644 --- a/src/Platform_3DS.c +++ b/src/Platform_3DS.c @@ -20,7 +20,6 @@ #include #include #include -#include #include #include #include @@ -360,9 +359,9 @@ int Socket_ValidAddress(const cc_string* address) { } cc_result Socket_Connect(cc_socket* s, const cc_string* address, int port) { - int addrSize, blocking_raw = -1; /* non-blocking mode */ union SocketAddress addr; cc_result res; + int flags; *s = -1; if (!ParseAddress(&addr, address)) @@ -370,13 +369,15 @@ cc_result Socket_Connect(cc_socket* s, const cc_string* address, int port) { *s = socket(AF_INET, SOCK_STREAM, 0); // https://www.3dbrew.org/wiki/SOCU:socket if (*s == -1) return errno; - ioctl(*s, FIONBIO, &blocking_raw); + + // non blocking mode + flags = fcntl(*s, F_GETFL, 0); + if (flags >= 0) fcntl(*s, F_SETFL, flags | O_NONBLOCK); - addr.v4.sin_family = AF_INET; - addr.v4.sin_port = htons(port); - addrSize = sizeof(addr.v4); + addr.v4.sin_family = AF_INET; + addr.v4.sin_port = htons(port); - res = connect(*s, &addr.raw, addrSize); + res = connect(*s, &addr.raw, sizeof(addr.v4)); return res == -1 ? errno : 0; }