2014-10-29 23:07:11 -07:00
|
|
|
#include "output.h"
|
|
|
|
|
2014-11-25 08:58:54 -05:00
|
|
|
#include <algorithm>
|
2014-11-21 22:24:04 -08:00
|
|
|
#include <string>
|
|
|
|
#include <cstring>
|
|
|
|
#include <cmath>
|
|
|
|
#include <cstdarg>
|
|
|
|
#include <cstdio>
|
|
|
|
|
2014-10-29 23:07:11 -07:00
|
|
|
#include <3ds.h>
|
|
|
|
|
|
|
|
#include "text.h"
|
|
|
|
|
2014-11-22 12:38:59 -08:00
|
|
|
static std::string bufferTop;
|
|
|
|
static std::string bufferBottom;
|
2014-10-29 23:07:11 -07:00
|
|
|
|
2014-11-21 22:24:04 -08:00
|
|
|
static int countLines(const std::string& str)
|
2014-10-29 23:07:11 -07:00
|
|
|
{
|
2014-11-22 00:58:29 -08:00
|
|
|
if (str.empty())
|
2014-11-22 12:21:12 -08:00
|
|
|
return 0;
|
2014-10-29 23:07:11 -07:00
|
|
|
|
2014-11-25 08:58:54 -05:00
|
|
|
return 1 + std::count_if(str.begin(), str.end(), [](char c) { return c == '\n'; });
|
2014-10-29 23:07:11 -07:00
|
|
|
}
|
|
|
|
|
2014-11-21 22:24:04 -08:00
|
|
|
static void deleteFirstLine(std::string* str)
|
2014-10-29 23:07:11 -07:00
|
|
|
{
|
2014-11-22 00:58:29 -08:00
|
|
|
if (str->empty())
|
2014-11-22 12:21:12 -08:00
|
|
|
return;
|
2014-11-21 22:24:04 -08:00
|
|
|
|
|
|
|
size_t linebreak = str->find_first_of('\n');
|
|
|
|
|
|
|
|
if (linebreak == std::string::npos || linebreak + 1 > str->length()) {
|
|
|
|
*str = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
*str = str->substr(linebreak + 1);
|
2014-10-29 23:07:11 -07:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:21:12 -08:00
|
|
|
static void drawFrame(gfxScreen_t screen, char b, char g, char r)
|
2014-10-29 23:07:11 -07:00
|
|
|
{
|
2014-11-22 12:38:59 -08:00
|
|
|
int screenHeight = 240;
|
|
|
|
int screenWidth = (screen == GFX_TOP) ? 400 : 320;
|
|
|
|
std::string& textBuffer = (screen == GFX_TOP) ? bufferTop : bufferBottom;
|
2014-10-31 22:20:21 -07:00
|
|
|
|
2014-11-25 09:16:26 -05:00
|
|
|
u8* bufAdr = gfxGetFramebuffer(screen, GFX_LEFT, nullptr, nullptr);
|
2014-11-22 12:38:59 -08:00
|
|
|
for (int i = 0; i < screenWidth * screenHeight * 3; i += 3) {
|
|
|
|
bufAdr[i] = b;
|
|
|
|
bufAdr[i+1] = g;
|
|
|
|
bufAdr[i+2] = r;
|
2014-11-22 00:58:29 -08:00
|
|
|
}
|
|
|
|
|
2014-11-22 12:38:59 -08:00
|
|
|
int lines = countLines(textBuffer);
|
|
|
|
while (lines > (screenHeight / fontDefault.height - 3)) {
|
2014-11-22 12:21:12 -08:00
|
|
|
deleteFirstLine(&textBuffer);
|
2014-11-22 12:38:59 -08:00
|
|
|
lines--;
|
2014-11-22 00:58:29 -08:00
|
|
|
}
|
2014-11-25 09:16:26 -05:00
|
|
|
gfxDrawText(screen, GFX_LEFT, nullptr, textBuffer, screenHeight - fontDefault.height * 3, 10);
|
2014-10-31 22:20:21 -07:00
|
|
|
}
|
2014-10-29 23:07:11 -07:00
|
|
|
|
2014-10-31 22:20:21 -07:00
|
|
|
void drawFrames()
|
|
|
|
{
|
2014-11-22 00:58:29 -08:00
|
|
|
drawFrame(GFX_TOP, 0x88, 0x66, 0x00);
|
|
|
|
drawFrame(GFX_BOTTOM, 0x00, 0x00, 0x00);
|
|
|
|
gfxFlushBuffers();
|
|
|
|
gfxSwapBuffers();
|
2014-10-29 23:07:11 -07:00
|
|
|
}
|
2014-10-30 00:25:12 -07:00
|
|
|
|
2014-10-31 22:20:21 -07:00
|
|
|
void print(gfxScreen_t screen, const char* format, ...)
|
2014-10-30 00:25:12 -07:00
|
|
|
{
|
2014-11-22 12:38:59 -08:00
|
|
|
std::string& textBuffer = (screen == GFX_TOP) ? bufferTop : bufferBottom;
|
2014-11-23 00:21:51 -08:00
|
|
|
|
2014-11-22 00:58:29 -08:00
|
|
|
va_list arguments;
|
2014-11-23 00:21:51 -08:00
|
|
|
char *vaStr;
|
2014-10-30 00:25:12 -07:00
|
|
|
|
2014-11-22 00:58:29 -08:00
|
|
|
va_start(arguments, format);
|
2014-11-23 00:21:51 -08:00
|
|
|
vasprintf(&vaStr, format, arguments);
|
2014-11-22 00:58:29 -08:00
|
|
|
va_end(arguments);
|
2014-10-30 00:25:12 -07:00
|
|
|
|
2014-11-23 00:21:51 -08:00
|
|
|
textBuffer += std::string(vaStr);
|
|
|
|
svcOutputDebugString(vaStr, strlen(vaStr));
|
|
|
|
free(vaStr);
|
2014-10-30 00:25:12 -07:00
|
|
|
|
2014-11-22 00:58:29 -08:00
|
|
|
drawFrames();
|
2014-10-31 22:20:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void clearScreen(gfxScreen_t screen)
|
|
|
|
{
|
2014-11-22 12:38:59 -08:00
|
|
|
std::string& textBuffer = (screen == GFX_TOP) ? bufferTop : bufferBottom;
|
|
|
|
textBuffer.clear();
|
2014-11-22 00:58:29 -08:00
|
|
|
drawFrames();
|
2014-10-30 00:25:12 -07:00
|
|
|
}
|
2014-10-30 14:55:01 -07:00
|
|
|
|
2014-10-31 22:20:21 -07:00
|
|
|
void clearScreens()
|
2014-10-30 14:55:01 -07:00
|
|
|
{
|
2014-11-22 00:58:29 -08:00
|
|
|
clearScreen(GFX_TOP);
|
|
|
|
clearScreen(GFX_BOTTOM);
|
2014-10-30 14:55:01 -07:00
|
|
|
}
|