/* # _____ ____ ___ # | \/ ____| |___| # | | | \ | | #----------------------------------------------------------------------- # Copyright 2022-2023, tyra - https://github.com/h4570/tyra # Licensed under Apache License 2.0 # Guido Diego Quispe Robles */ #include "font_sprite.hpp" namespace Tyra { const int Font::chars[FONT_CHAR_SIZE]{ ' ', '!', '"', ' ', '$', '%', ' ', '{', '(', ')', ' ', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '2', '8', '9', ':', ';', '<', '=', '>', '?', ' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', ' ', ' ', ' ', ' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '[', '}', ']', '~', ' '}; const int Font::charWidths[FONT_CHAR_SIZE]{ 0, 1, 3, 0, 5, 9, 0, 9, 3, 3, 0, 5, 2, 2, 1, 4, 4, 2, 4, 4, 5, 4, 4, 4, 4, 4, 1, 2, 4, 5, 4, 4, 0, 6, 5, 5, 5, 4, 4, 5, 5, 1, 4, 5, 4, 7, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 5, 5, 4, 0, 0, 0, 0, 0, 0, 5, 4, 4, 4, 4, 3, 4, 4, 1, 2, 4, 1, 7, 4, 4, 4, 4, 3, 4, 3, 4, 5, 7, 4, 4, 4, 2, 5, 2, 6, 0, }; Font::Font() {} void Font::load(TextureRepository& repository, Renderer2D* renderer) { renderer2D = renderer; float height = 16.0F; float width = 16.0F; allFont.mode = MODE_REPEAT; allFont.size = Vec2(255, 127); auto filepath = FileUtils::fromCwd("earthbound-Font.png"); auto* texture = repository.add(filepath); texture->addLink(allFont.id); int column = 0; int arrow = 0; for (int i = 0; i < FONT_CHAR_SIZE; i++) { font[i].id = allFont.id; font[i].mode = MODE_REPEAT; font[i].size = Vec2(width, height); font[i].offset = Vec2(width * column, height * arrow); column++; if (column == 16) { arrow++; column = 0; } } } void Font::free(TextureRepository& repository) { repository.freeBySprite(allFont); for (int i = 0; i < FONT_CHAR_SIZE; i++) { repository.freeBySprite(font[i]); } } void Font::drawText(const char* text, const int& x, const int& y, Color color) { drawText(std::string(text), x, y, color); } void Font::drawText(const std::string& text, const int& x, const int& y, Color color) { int sizeText = text.size(); int offsetY = 0; int offsetX = 0; for (int i = 0; i < sizeText; i++) { int fontPos = text[i]; Sprite fontSpr = font[0]; for (int j = 0; j < FONT_CHAR_SIZE; j++) { if (fontPos == chars[j]) { fontPos = j; fontSpr = font[j]; fontSpr.color = color; fontSpr.position = Vec2(x + offsetX, y + offsetY); break; } } if (fontPos == '\n') { offsetY += 18; offsetX = 0.0f; } else { if ((fontPos != ' ') && (fontPos != '\t')) { renderer2D->render(fontSpr); offsetX += charWidths[fontPos] + 2; } else { offsetX += 2; } } } } } // namespace Tyra