hwtests/source/draw.cpp

99 lines
2.3 KiB
C++
Raw Normal View History

#include "draw.h"
#include <cstdlib>
#include <cstdio>
#include <cstring>
2014-10-26 18:47:14 -07:00
#include <3ds.h>
#include "font.h"
Rect GetScreenSize(gfxScreen_t screen)
{
return { (screen == GFX_TOP) ? 400 : 320, 240 };
}
// This code is not meant to be readable -- Smea
int DrawCharacter(u8* fb, font_s* font, char c, s16 x, s16 y, u16 w, u16 h)
2014-10-26 18:47:14 -07:00
{
2014-11-24 18:57:59 -05:00
Glyph* cd = &font->desc[(int)c];
2014-10-29 23:40:49 -07:00
if (!cd->data)
2014-11-22 12:21:12 -08:00
return 0;
2014-10-29 23:40:49 -07:00
x += cd->xo; y += font->height - cd->yo - cd->h;
2014-10-29 23:40:49 -07:00
if (x < 0 || x + cd->w >= w || y < -cd->h || y >= h + cd->h)
2014-11-22 12:21:12 -08:00
return 0;
2014-10-29 23:40:49 -07:00
u8* charData = cd->data;
s16 cy = y, ch = cd->h, cyo = 0;
2014-10-29 23:40:49 -07:00
if (y < 0) {
2014-11-22 12:21:12 -08:00
cy = 0;
cyo = -y;
ch = cd->h-cyo;
} else if (y + ch > h) {
2014-11-22 12:21:12 -08:00
ch = h - y;
}
2014-10-29 23:40:49 -07:00
fb += (x * h + cy) * 3;
const u8 r = font->color[0];
const u8 g = font->color[1];
const u8 b = font->color[2];
2014-10-29 23:40:49 -07:00
for (int i = 0; i < cd->w; i++) {
2014-11-22 12:21:12 -08:00
charData += cyo;
for (int j = 0; j < ch; j++) {
2014-11-22 12:21:12 -08:00
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;
2014-10-26 18:47:14 -07:00
}
void DrawString(u8* fb, font_s* f, const std::string& str, s16 x, s16 y, u16 w, u16 h)
2014-10-26 18:47:14 -07:00
{
if (!f || !fb)
2014-11-22 12:21:12 -08:00
return;
2014-10-29 23:40:49 -07:00
int dx = 0, dy = 0;
for (const char& c : str)
{
dx += DrawCharacter(fb, f, c, x + dx, y + dy, w, h);
if (c == '\n') {
2014-11-22 12:21:12 -08:00
dx = 0;
dy -= f->height;
}
}
2014-10-26 18:47:14 -07:00
}
void DrawText(gfxScreen_t screen, gfx3dSide_t side, font_s* font, const std::string& str, s16 x, s16 y)
2014-10-26 18:47:14 -07:00
{
if (!font)
2014-11-22 12:21:12 -08:00
font = &fontDefault;
2014-10-26 18:47:14 -07:00
u16 fbWidth, fbHeight;
u8* fbAdr = gfxGetFramebuffer(screen, side, &fbWidth, &fbHeight);
2014-10-26 18:47:14 -07:00
DrawString(fbAdr, font, str, y, x, fbHeight, fbWidth);
}
void FillScreen(gfxScreen_t screen, u8 bg_r, u8 bg_g, u8 bg_b)
{
Rect screen_size = GetScreenSize(screen);
u8* fb_addr = gfxGetFramebuffer(screen, GFX_LEFT, nullptr, nullptr);
for (int i = 0; i < screen_size.w * screen_size.h * 3; i += 3) {
fb_addr[i] = bg_b;
fb_addr[i+1] = bg_g;
fb_addr[i+2] = bg_r;
}
2014-10-26 18:47:14 -07:00
}