General cleanups and conversion towards Common::FormatString

This commit is contained in:
archshift 2014-12-10 22:24:26 -08:00
parent 75575b4a41
commit 4709c969fa
9 changed files with 99 additions and 96 deletions

View File

@ -1,5 +1,6 @@
#include "string_funcs.h"
#include <algorithm>
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
@ -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);
}
}

View File

@ -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);
}

View File

@ -1,52 +1,53 @@
#include <stdlib.h>
#include <stdio.h>
#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();

View File

@ -1,98 +1,69 @@
#include "output.h"
#include <algorithm>
#include <string>
#include <cstring>
#include <cmath>
#include <cstdarg>
#include <cstdio>
#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);
}

View File

@ -1,8 +1,9 @@
#pragma once
#include <3ds.h>
#include <string>
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();

View File

@ -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

View File

@ -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);

View File

@ -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"));
}

View File

@ -1,14 +1,16 @@
#pragma once
#include <functional>
#include <string>
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 <typename T>
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;