From 4709c969facd7ca4ba40e58106b1e4794ff64d7e Mon Sep 17 00:00:00 2001 From: archshift Date: Wed, 10 Dec 2014 22:24:26 -0800 Subject: [PATCH] General cleanups and conversion towards Common::FormatString --- source/common/string_funcs.cpp | 24 +++++++++ source/common/string_funcs.h | 10 ++++ source/main.cpp | 31 +++++------ source/output.cpp | 99 ++++++++++++---------------------- source/output.h | 9 ++-- source/tests/fs/fs.cpp | 7 --- source/tests/fs/fs_sdmc.cpp | 2 +- source/tests/test.cpp | 5 +- source/tests/test.h | 8 +-- 9 files changed, 99 insertions(+), 96 deletions(-) diff --git a/source/common/string_funcs.cpp b/source/common/string_funcs.cpp index 71fc65e..eea49d6 100644 --- a/source/common/string_funcs.cpp +++ b/source/common/string_funcs.cpp @@ -1,5 +1,6 @@ #include "string_funcs.h" +#include #include #include #include @@ -22,4 +23,27 @@ std::string FormatString(const char* format, ...) return out_str; } +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'; }); +} + +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); +} + } diff --git a/source/common/string_funcs.h b/source/common/string_funcs.h index 434b029..feaa852 100644 --- a/source/common/string_funcs.h +++ b/source/common/string_funcs.h @@ -6,4 +6,14 @@ namespace Common { std::string FormatString(const char* format, ...); +/** + * Returns the number of lines (broken by '\n') in the string + */ +int CountLines(const std::string& str); + +/** + * Deletes the first line (broken by '\n') from the string + */ +void DeleteFirstLine(std::string* str); + } diff --git a/source/main.cpp b/source/main.cpp index 0219966..91d8edf 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,52 +1,53 @@ -#include -#include #include <3ds.h> #include "output.h" +#include "tests/test.h" #include "tests/fs/fs.h" #include "tests/cpu/cputests.h" -static unsigned int testCounter = 0; -static void (*tests[]) (void) = { +static unsigned int test_counter = 0; +static TestCaller tests[] = { FS::TestAll, CPU::Integer::TestAll }; -int main() +int main(int argc, char** argv) { srvInit(); aptInit(); hidInit(NULL); gfxInit(); gfxSet3D(false); + fsInit(); - clearScreens(); - print(GFX_TOP, "Press A to begin...\n"); + ClearScreens(); + Print(GFX_TOP, "Press A to begin...\n"); while (aptMainLoop()) { - drawFrames(); + DrawFrames(); hidScanInput(); if (hidKeysDown() & KEY_START) { break; } else if (hidKeysDown() & KEY_A) { - clearScreen(GFX_TOP); + ClearScreen(GFX_TOP); - if (testCounter < (sizeof(tests) / sizeof(tests[0]))) { - tests[testCounter](); - testCounter++; + if (test_counter < (sizeof(tests) / sizeof(tests[0]))) { + tests[test_counter](); + test_counter++; } else { break; } - print(GFX_TOP, "\n"); - print(GFX_TOP, "Press A to continue...\n"); + Print(GFX_TOP, "\n"); + Print(GFX_TOP, "Press A to continue...\n"); } gspWaitForEvent(GSPEVENT_VBlank0, false); } - clearScreens(); + ClearScreens(); + fsExit(); gfxExit(); hidExit(); aptExit(); diff --git a/source/output.cpp b/source/output.cpp index 7c77ce5..f6ecb43 100644 --- a/source/output.cpp +++ b/source/output.cpp @@ -1,98 +1,69 @@ #include "output.h" -#include -#include -#include #include -#include -#include #include <3ds.h> #include "text.h" +#include "common/string_funcs.h" -static std::string bufferTop; -static std::string bufferBottom; +static std::string buffer_top; +static std::string buffer_bottom; -static int countLines(const std::string& str) +static std::string& GetTextBuffer(gfxScreen_t screen) { - if (str.empty()) - return 0; - - return 1 + std::count_if(str.begin(), str.end(), [](char c) { return c == '\n'; }); + switch (screen) { + case GFX_TOP: return buffer_top; + case GFX_BOTTOM: return buffer_bottom; + } + return buffer_top; } -static void deleteFirstLine(std::string* str) +static void DrawFrame(gfxScreen_t screen, char b, char g, char r) { - 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); -} + int screen_height = 240; + int screen_width = (screen == GFX_TOP) ? 400 : 320; + std::string& text_buffer = GetTextBuffer(screen); -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; + u8* fb_addr = gfxGetFramebuffer(screen, GFX_LEFT, nullptr, nullptr); + for (int i = 0; i < screen_width * screen_height * 3; i += 3) { + fb_addr[i] = b; + fb_addr[i+1] = g; + fb_addr[i+2] = r; } - int lines = countLines(textBuffer); - while (lines > (screenHeight / fontDefault.height - 3)) { - deleteFirstLine(&textBuffer); + int lines = Common::CountLines(text_buffer); + while (lines > (screen_height / fontDefault.height - 3)) { + Common::DeleteFirstLine(&text_buffer); lines--; } - gfxDrawText(screen, GFX_LEFT, nullptr, textBuffer, screenHeight - fontDefault.height * 3, 10); + gfxDrawText(screen, GFX_LEFT, nullptr, text_buffer, screen_height - fontDefault.height * 3, 10); } -void drawFrames() +void DrawFrames() { - drawFrame(GFX_TOP, 0x88, 0x66, 0x00); - drawFrame(GFX_BOTTOM, 0x00, 0x00, 0x00); + DrawFrame(GFX_TOP, 0x88, 0x66, 0x00); + DrawFrame(GFX_BOTTOM, 0x00, 0x00, 0x00); gfxFlushBuffers(); gfxSwapBuffers(); } -void print(gfxScreen_t screen, const char* format, ...) +void Print(gfxScreen_t screen, const std::string& text) { - std::string& textBuffer = (screen == GFX_TOP) ? bufferTop : bufferBottom; - - va_list arguments; - char *vaStr; + GetTextBuffer(screen) += text; + svcOutputDebugString(text.c_str(), text.length()); - va_start(arguments, format); - vasprintf(&vaStr, format, arguments); - va_end(arguments); - - textBuffer += std::string(vaStr); - svcOutputDebugString(vaStr, strlen(vaStr)); - free(vaStr); - - drawFrames(); + DrawFrames(); } -void clearScreen(gfxScreen_t screen) +void ClearScreen(gfxScreen_t screen) { - std::string& textBuffer = (screen == GFX_TOP) ? bufferTop : bufferBottom; - textBuffer.clear(); - drawFrames(); + GetTextBuffer(screen).clear(); + DrawFrames(); } -void clearScreens() +void ClearScreens() { - clearScreen(GFX_TOP); - clearScreen(GFX_BOTTOM); + ClearScreen(GFX_TOP); + ClearScreen(GFX_BOTTOM); } diff --git a/source/output.h b/source/output.h index 6ce9cf8..5219bcd 100644 --- a/source/output.h +++ b/source/output.h @@ -1,8 +1,9 @@ #pragma once #include <3ds.h> +#include -void drawFrames(); -void print(gfxScreen_t screen, const char* format, ...); -void clearScreen(gfxScreen_t screen); -void clearScreens(); +void DrawFrames(); +void Print(gfxScreen_t screen, const std::string& text); +void ClearScreen(gfxScreen_t screen); +void ClearScreens(); diff --git a/source/tests/fs/fs.cpp b/source/tests/fs/fs.cpp index 3593245..1d8d020 100644 --- a/source/tests/fs/fs.cpp +++ b/source/tests/fs/fs.cpp @@ -1,6 +1,3 @@ -#include <3ds.h> - -#include "tests/test.h" #include "tests/fs/fs.h" #include "tests/fs/fs_sdmc.h" @@ -8,11 +5,7 @@ namespace FS { void TestAll() { - Test("FS", "Initializing service", fsInit(), 0L); - SDMC::TestAll(); - - Test("FS", "Exiting service", fsExit(), 0L); } } // namespace diff --git a/source/tests/fs/fs_sdmc.cpp b/source/tests/fs/fs_sdmc.cpp index 614b7f4..577cdfb 100644 --- a/source/tests/fs/fs_sdmc.cpp +++ b/source/tests/fs/fs_sdmc.cpp @@ -158,7 +158,7 @@ static bool TestDirRename(FS_archive sdmcArchive) void TestAll() { - FS_archive sdmcArchive = (FS_archive) { 0x00000009, { PATH_EMPTY, 1, (u8*) "" } }; + FS_archive sdmcArchive = { 0x00000009, { PATH_EMPTY, 1, (u8*) "" } }; Test("SDMC", "Opening archive", FSUSER_OpenArchive(NULL, &sdmcArchive), 0L); Test("SDMC", "Creating and deleting file", TestFileCreateDelete(sdmcArchive), true); diff --git a/source/tests/test.cpp b/source/tests/test.cpp index 12817bb..4a92cab 100644 --- a/source/tests/test.cpp +++ b/source/tests/test.cpp @@ -3,8 +3,9 @@ #include <3ds.h> #include "output.h" +#include "common/string_funcs.h" -void PrintSuccess(std::string group, std::string name, bool val) +void PrintSuccess(const std::string& group, const std::string& name, bool val) { - print(GFX_TOP, "%s: %s - %s\n", group.c_str(), name.c_str(), val ? "SUCCESS" : "FAILURE"); + Print(GFX_TOP, Common::FormatString("%s: %s - %s\n", group.c_str(), name.c_str(), val ? "SUCCESS" : "FAILURE")); } diff --git a/source/tests/test.h b/source/tests/test.h index 0954cef..c50c20e 100644 --- a/source/tests/test.h +++ b/source/tests/test.h @@ -1,14 +1,16 @@ #pragma once -#include +#include + +typedef void (*TestCaller)(void); // If the condition fails, return false #define SoftAssert(cond) do { if (!(cond)) { return false; } } while (0) -void PrintSuccess(std::string group, std::string name, bool val); +void PrintSuccess(const std::string& group, const std::string& name, bool val); template -bool Test(std::string group, std::string name, T result, T expected) +bool Test(const std::string& group, const std::string& name, T result, T expected) { PrintSuccess(group, name, result == expected); return result == expected;