Minor improvements and make functions const

This commit is contained in:
Pk11
2021-08-27 23:38:46 -05:00
parent 118c9cc93d
commit bda2aab14b
11 changed files with 102 additions and 173 deletions
+41 -67
View File
@@ -130,7 +130,7 @@ Font::Font(const std::vector<std::string> &paths) {
}
}
u16 Font::charIndex(char16_t c) {
u16 Font::charIndex(char16_t c) const {
// Try a binary search
int left = 0;
int right = fontMap.size();
@@ -173,7 +173,7 @@ std::u16string Font::utf8to16(std::string_view text) {
return out;
}
int Font::calcWidth(std::u16string_view text) {
int Font::calcWidth(std::u16string_view text) const {
uint x = 0;
for(auto c : text) {
@@ -184,8 +184,8 @@ int Font::calcWidth(std::u16string_view text) {
return x;
}
ITCM_CODE void Font::print(std::u16string_view text, int x, int y, Alignment align, Palette palette,
int maxWidth, float scaleX, float scaleY, bool rtl, Sprite *sprite) {
ITCM_CODE void Font::print(std::u16string_view text, int x, int y, Alignment align, u8 color,
int maxWidth, float scaleX, float scaleY, bool rtl, Sprite *sprite) const {
// If RTL isn't forced, check for RTL text
for(const auto c : text) {
if(c >= 0x0590 && c <= 0x05FF) {
@@ -203,7 +203,7 @@ ITCM_CODE void Font::print(std::u16string_view text, int x, int y, Alignment ali
case Alignment::center: {
size_t newline = text.find('\n');
while(newline != text.npos) {
print(text.substr(0, newline), x, y, align, palette, maxWidth, scaleX, scaleY, rtl, sprite);
print(text.substr(0, newline), x, y, align, color, maxWidth, scaleX, scaleY, rtl, sprite);
text = text.substr(newline + 1);
newline = text.find('\n');
y += tileHeight;
@@ -216,7 +216,7 @@ ITCM_CODE void Font::print(std::u16string_view text, int x, int y, Alignment ali
size_t newline = text.find('\n');
while(newline != text.npos) {
print(text.substr(0, newline), x - (calcWidth(text.substr(0, newline)) * scaleX), y,
Alignment::left, palette, maxWidth, scaleX, scaleY, rtl, sprite);
Alignment::left, color, maxWidth, scaleX, scaleY, rtl, sprite);
text = text.substr(newline + 1);
newline = text.find('\n');
y += tileHeight;
@@ -320,70 +320,44 @@ ITCM_CODE void Font::print(std::u16string_view text, int x, int y, Alignment ali
}
}
u8 *dstBegin;
int width, height;
if(sprite) {
// Don't draw off sprite chars
if(x >= 0 && x < sprite->width() && y >= 0 && y + tileHeight < sprite->height()) {
u16 *dst = sprite->gfx() + x + fontWidths[(index * 3)];
// Use faster integer math if scale is 1
if(scaleX == 1.0f && scaleY == 1.0f) {
for(int i = 0; i < tileHeight; i++) {
for(int j = 0; j < tileWidth; j++) {
u8 px = fontTiles[(index * tileSize) + (i * tileWidth + j) / 4] >>
((3 - ((i * tileWidth + j) % 4)) * 2) &
3;
if(px)
dst[(y + i) * sprite->width() + j] = px + (u8(palette) * 4);
}
}
} else {
for(float i = 0.0f; i < tileHeight; i += 1 / scaleY) {
for(float j = 0.0f; j < tileWidth; j += 1 / scaleY) {
u8 px = fontTiles[(index * tileSize) + (i * tileWidth + j) / 4] >>
((3 - (int(i * tileWidth + j) % 4)) * 2) &
3;
if(px)
dst[int((y + i) * sprite->width() + j)] = px + (u8(palette) * 4);
}
width = sprite->width();
height = sprite->height();
dstBegin = (u8 *)sprite->gfx();
} else {
width = 256;
height = 192;
#ifdef UNIVCORE_TEXT_BUFFERED
dstBegin = textBuf[currentScreen];
#else
dstBegin = (u8 *)bgGetGfxPtr(Gui::top ? 2 : 6);
#endif
}
dstBegin += y * width + x + fontWidths[(index * 3)];
// Don't draw off screen chars
if(x >= 0 && x + fontWidths[(index * 3) + 2] < width && y >= 0 && y + tileHeight < height) {
// Use faster integer math if scale is 1
if(scaleX == 1.0f && scaleY == 1.0f) {
for(int i = 0; i < tileHeight; i++) {
u8 *dst = dstBegin + i * width;
for(int j = 0; j < tileWidth; j++) {
u8 px = fontTiles[(index * tileSize) + (i * tileWidth + j) / 4] >>
((3 - ((i * tileWidth + j) % 4)) * 2) & 3;
if(px)
toncset(dst + j, px + color, 1);
}
}
}
} else {
// Don't draw off screen chars
if(x >= 0 && x + fontWidths[(index * 3) + 2] < 256 && y >= 0 && y + tileHeight < 192) {
#ifdef UNIVCORE_TEXT_BUFFERED
u8 *dst = textBuf[currentScreen] + y * 256 + x + fontWidths[(index * 3)];
#else
u8 *dst = (u8 *)bgGetGfxPtr(Gui::top ? 2 : 6) + y * 256 + x + fontWidths[(index * 3)];
#endif
// Use faster integer math if scale is 1
if(scaleX == 1.0f && scaleY == 1.0f) {
for(int i = 0; i < tileHeight; i++) {
for(int j = 0; j < tileWidth; j++) {
u8 px = fontTiles[(index * tileSize) + (i * tileWidth + j) / 4] >>
((3 - ((i * tileWidth + j) % 4)) * 2) &
3;
if(px) {
#ifdef UNIVCORE_TEXT_BUFFERED
dst[i * 256 + j] = px + (u8(palette) * 4);
#else
toncset(dst + i * 256 + j, px + (u8(palette) * 4), 1);
#endif
}
}
}
} else {
for(int i = 0; i < tileHeight * scaleY; i++) {
for(int j = 0; j < tileWidth * scaleX; j++) {
u8 loc = int(i / scaleY) * tileWidth + int(j / scaleX);
u8 px = fontTiles[index * tileSize + loc / 4] >> ((3 - (loc % 4)) * 2) & 3;
if(px) {
#ifdef UNIVCORE_TEXT_BUFFERED
dst[i * 256 + j] = px + (u8(palette) * 4);
#else
toncset(dst + i * 256 + j, px + (u8(palette) * 4), 1);
#endif
}
}
} else {
for(int i = 0; i < tileHeight * scaleY; i++) {
u8 *dst = dstBegin + i * width;
for(int j = 0; j < tileWidth * scaleX; j++) {
u8 loc = int(i / scaleY) * tileWidth + int(j / scaleX);
u8 px = fontTiles[index * tileSize + loc / 4] >> ((3 - (loc % 4)) * 2) & 3;
if(px)
toncset(dst + j, px + color, 1);
}
}
}
+6 -6
View File
@@ -109,23 +109,23 @@ bool Gui::reinit(const char *FontPath) {
return Gui::init(FontPath);
}
void Gui::DrawStringCentered(int x, int y, float size, Palette palette, const std::string &Text, int maxWidth, int maxHeight, Font *fnt, int flags) {
void Gui::DrawStringCentered(int x, int y, float size, u8 color, const std::string &Text, int maxWidth, int maxHeight, Font *fnt, int flags) {
#ifdef UNIVCORE_3DS_SIZE
Gui::DrawString(x, y, size, palette, Text, maxWidth, maxHeight, fnt, flags | C2D_AlignCenter);
Gui::DrawString(x, y, size, color, Text, maxWidth, maxHeight, fnt, flags | C2D_AlignCenter);
#else
Gui::DrawString(x, y, size, palette, Text, maxWidth, maxHeight, fnt, flags | C2D_AlignCenter);
Gui::DrawString(x, y, size, color, Text, maxWidth, maxHeight, fnt, flags | C2D_AlignCenter);
#endif
}
void Gui::DrawString(int x, int y, float size, Palette palette, const std::string &Text, int maxWidth, int maxHeight, Font *fnt, int flags) {
void Gui::DrawString(int x, int y, float size, u8 color, const std::string &Text, int maxWidth, int maxHeight, Font *fnt, int flags) {
float heightScale = maxHeight == 0 ? size : std::min(size, size * (maxHeight / Gui::GetStringHeight(size, Text, fnt)));
float widthScale = maxWidth == 0 ? size : std::min(size, size * (maxWidth / Gui::GetStringWidth(size, Text, fnt)));
// TODO: Wrapping and such
if(fnt)
fnt->print(x, y, Text, flags & C2D_AlignCenter ? Alignment::center : (flags & C2D_AlignRight ? Alignment::right : Alignment::left), palette, maxWidth, widthScale, heightScale);
fnt->print(x, y, Text, flags & C2D_AlignCenter ? Alignment::center : (flags & C2D_AlignRight ? Alignment::right : Alignment::left), color, maxWidth, widthScale, heightScale);
else
DefaultFont->print(x, y, Text, flags & C2D_AlignCenter ? Alignment::center : (flags & C2D_AlignRight ? Alignment::right : Alignment::left), palette, maxWidth, widthScale, heightScale);
DefaultFont->print(x, y, Text, flags & C2D_AlignCenter ? Alignment::center : (flags & C2D_AlignRight ? Alignment::right : Alignment::left), color, maxWidth, widthScale, heightScale);
}
int Gui::GetStringWidth(float size, const std::string &Text, Font *fnt) {
+9 -8
View File
@@ -138,22 +138,19 @@ void Image::paletteStart(u8 paletteStart) {
}
}
void Image::copyPalette(void) {
void Image::copyPalette(void) const {
tonccpy((currentScreen ? BG_PALETTE : BG_PALETTE_SUB) + _paletteStart, _palette.data(), _palette.size() * 2);
}
void Image::draw(int x, int y, float scaleX, float scaleY, bool skipAlpha) {
void Image::draw(int x, int y, float scaleX, float scaleY, bool skipAlpha) const {
SCALE_3DS(x);
SCALE_3DS(y);
char s[64];
// _bitmap[0] = 39;
snprintf(s, sizeof(s), "%lu, %lu, %u, %u, 0x%X", _width, _height, _bitmap.size(), _bitmap[0], 0);
// If the scale is 1 use faster integer math
if(scaleX == 1.0f && scaleY == 1.0f) {
if(skipAlpha) {
for(u32 i = 0; i < _height; i++) {
u8 *src = _bitmap.data() + i * _width;
const u8 *src = _bitmap.data() + i * _width;
u8 *dst = (u8 *)bgGetGfxPtr(currentScreen ? 3 : 7) + (y + i) * 256 + x;
for(u32 j = 0; j < _width; j++) {
if(_palette[src[j] - _paletteStart] & 0x8000)
@@ -178,7 +175,7 @@ void Image::draw(int x, int y, float scaleX, float scaleY, bool skipAlpha) {
}
}
void Image::drawSegment(int x, int y, int imageX, int imageY, int w, int h, float scaleX, float scaleY, bool skipAlpha) {
void Image::drawSegment(int x, int y, int imageX, int imageY, int w, int h, float scaleX, float scaleY, bool skipAlpha) const {
SCALE_3DS(x);
SCALE_3DS(y);
@@ -186,7 +183,7 @@ void Image::drawSegment(int x, int y, int imageX, int imageY, int w, int h, floa
if(scaleX == 1.0f && scaleY == 1.0f) {
if(skipAlpha) {
for(int i = 0; i < h; i++) {
u8 *src = _bitmap.data() + i * _width;
const u8 *src = _bitmap.data() + i * _width;
u8 *dst = (u8 *)bgGetGfxPtr(currentScreen ? 3 : 7) + (y + i) * 256 + x;
for(int j = 0; j < w; j++) {
if(_palette[src[j] - _paletteStart] & 0x8000)
@@ -210,3 +207,7 @@ void Image::drawSegment(int x, int y, int imageX, int imageY, int w, int h, floa
}
}
}
u16 Image::operator[](int index) const {
return _palette[_bitmap[index] - _paletteStart];
}
+10 -27
View File
@@ -180,36 +180,22 @@ void Sprite::visibility(bool show) {
}
}
void Sprite::clear(void) { toncset16(_gfx, 0, (_size & 0xFF) << 5); }
void Sprite::clear(void) const { toncset16(_gfx, 0, (_size & 0xFF) << 5); }
void Sprite::fillColor(u16 color) { toncset16(_gfx, color, (_size & 0xFF) << 5); }
void Sprite::fillColor(u16 color) const { toncset16(_gfx, color, (_size & 0xFF) << 5); }
void Sprite::drawOutline(int x, int y, int w, int h, u16 color) {
h += y;
if(y >= 0 && y <= _height)
toncset16(_gfx + y * _width + std::max(x, 0), color, std::min(w, _width - x));
for(y++; y < (h - 1); y++) {
if(y >= 0 && y <= _height && x >= 0)
_gfx[y * _width + x] = color;
if(y >= 0 && y <= _height && x + w <= _width)
_gfx[y * _width + x + w - 1] = color;
}
if(y >= 0 && y <= _height)
toncset16(_gfx + y * _width + std::max(x, 0), color, std::min(w, _width - x));
}
void Sprite::drawRectangle(int x, int y, int w, int h, u16 color1, u16 color2) {
void Sprite::drawRectangle(int x, int y, int w, int h, u16 color) const {
for(int i = 0; i < h; i++) {
toncset16(_gfx + ((y + i) * _width + x), ((i % 2) ? color1 : color2), w);
toncset16(_gfx + ((y + i) * _width + x), color, w);
}
}
void Sprite::drawImage(int x, int y, const Image &image, float scaleX, float scaleY) {
void Sprite::drawImage(int x, int y, const Image &image, float scaleX, float scaleY) const {
// If the scale is 1 use faster integer math
if(scaleX == 1.0f && scaleY == 1.0f) {
for(int i = 0; i < image.height(); i++) {
for(int j = 0; j < image.width(); j++) {
u16 px = image.palette()[image.bitmap()[i * image.width() + j] - image.paletteStart()];
u16 px = image[i * image.width() + j];
if(px & 0x8000)
_gfx[(y + i) * _height + x + j] = px;
}
@@ -217,8 +203,7 @@ void Sprite::drawImage(int x, int y, const Image &image, float scaleX, float sca
} else {
for(int i = 0; i < image.height() * scaleY; i++) {
for(int j = 0; j < image.width() * scaleX; j++) {
u16 px =
image.palette()[image.bitmap()[int(i / scaleY) * image.width() + int(j / scaleX)] - image.paletteStart()];
u16 px = image[int(i / scaleY) * image.width() + int(j / scaleX)];
if(px & 0x8000)
_gfx[(y + i) * _height + j + x] = px;
}
@@ -227,12 +212,12 @@ void Sprite::drawImage(int x, int y, const Image &image, float scaleX, float sca
}
void Sprite::drawImageSegment(int x, int y, int imageX, int imageY, int w, int h, const Image &image, float scaleX,
float scaleY) {
float scaleY) const {
// If the scale is 1 use faster integer math
if(scaleX == 1.0f && scaleY == 1.0f) {
for(int i = 0; i < h; i++) {
for(int j = 0; j < w; j++) {
u16 px = image.palette()[image.bitmap()[(imageY + i) * image.width() + imageX + j] - image.paletteStart()];
u16 px = image[(imageY + i) * image.width() + imageX + j];
if(px & 0x8000)
_gfx[(y + i) * _height + x + j] = px;
}
@@ -240,9 +225,7 @@ void Sprite::drawImageSegment(int x, int y, int imageX, int imageY, int w, int h
} else {
for(int i = 0; i < h * scaleY; i++) {
for(int j = 0; j < w * scaleX; j++) {
u16 px = image.palette()[image.bitmap()[(imageY + int(i / scaleY)) * image.width() + imageX +
int(j / scaleX)] -
image.paletteStart()];
u16 px = image[imageY + int(i / scaleY) * image.width() + imageX + int(j / scaleX)];
if(px & 0x8000)
_gfx[(y + i) * _height + x + j] = px;
}
+1 -1
View File
@@ -83,7 +83,7 @@ Spritesheet::~Spritesheet(void) {
}
}
Image &Spritesheet::operator[](size_t index) {
Image &Spritesheet::operator[](size_t index) const {
if(_images[index] && index < _images.size())
return *_images[index];
else