Added 'style' font render argument

This commit is contained in:
MihailRis 2022-07-16 00:31:04 +03:00 committed by GitHub
parent fa4538c124
commit c0f5e5f4fc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 41 deletions

View File

@ -43,6 +43,10 @@ bool Font::isPrintableChar(int c) {
#define RES 16 #define RES 16
void Font::draw(Batch2D* batch, std::wstring text, int x, int y) { void Font::draw(Batch2D* batch, std::wstring text, int x, int y) {
draw(batch, text, x, y, STYLE_NONE);
}
void Font::draw(Batch2D* batch, std::wstring text, int x, int y, int style) {
int page = 0; int page = 0;
int next = 10000; int next = 10000;
int init_x = x; int init_x = x;
@ -56,43 +60,20 @@ void Font::draw(Batch2D* batch, std::wstring text, int x, int y) {
texture = pages[0]; texture = pages[0];
} }
batch->texture(pages[charpage]); batch->texture(pages[charpage]);
batch->sprite(x, y, RES, RES, 16, c, vec4(1.0f));
}
else if (charpage > page && charpage < next){
next = charpage;
}
}
x += getGlyphWidth(c);
}
page = next;
next = 10000;
x = init_x;
} while (page < 10000);
}
void Font::drawWithOutline(Batch2D* batch, std::wstring text, int x, int y) { switch (style){
int page = 0; case STYLE_SHADOW:
int next = 10000; batch->sprite(x+1, y+1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
int init_x = x; break;
do { case STYLE_OUTLINE:
for (unsigned c : text){ for (int oy = -1; oy <= 1; oy++){
if (isPrintableChar(c)){ for (int ox = -1; ox <= 1; ox++){
int charpage = c >> 8; if (ox || oy)
if (charpage == page){ batch->sprite(x+ox, y+oy, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
Texture* texture = pages[charpage]; }
if (texture == nullptr){ }
texture = pages[0]; break;
} }
batch->texture(pages[charpage]);
batch->sprite(x+1, y+1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x+1, y-1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x-1, y, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x+1, y, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x-1, y-1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x+1, y-1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x+1, y+1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x-1, y+1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f));
batch->sprite(x, y, RES, RES, 16, c, vec4(1.0f)); batch->sprite(x, y, RES, RES, 16, c, vec4(1.0f));
} }

View File

@ -7,6 +7,10 @@
class Texture; class Texture;
class Batch2D; class Batch2D;
#define STYLE_NONE 0
#define STYLE_SHADOW 1
#define STYLE_OUTLINE 2
class Font { class Font {
public: public:
std::vector<Texture*> pages; std::vector<Texture*> pages;
@ -16,7 +20,7 @@ public:
int getGlyphWidth(char c); int getGlyphWidth(char c);
bool isPrintableChar(int c); bool isPrintableChar(int c);
void draw(Batch2D* batch, std::wstring text, int x, int y); void draw(Batch2D* batch, std::wstring text, int x, int y);
void drawWithOutline(Batch2D* batch, std::wstring text, int x, int y); void draw(Batch2D* batch, std::wstring text, int x, int y, int style);
}; };
#endif /* GRAPHICS_FONT_H_ */ #endif /* GRAPHICS_FONT_H_ */

View File

@ -128,12 +128,12 @@ void draw_hud(World* world, Level* level, Assets* assets, bool devdata, int fps)
Font* font = assets->getFont("normal"); Font* font = assets->getFont("normal");
batch->begin(); batch->begin();
if (devdata){ if (devdata){
font->drawWithOutline(batch, L"chunks: "+std::to_wstring(chunks->chunksCount), 16, 16); font->draw(batch, L"chunks: "+std::to_wstring(chunks->chunksCount), 16, 16, STYLE_OUTLINE);
font->drawWithOutline(batch, std::to_wstring((int)player->camera->position.x), 10, 30); font->draw(batch, std::to_wstring((int)player->camera->position.x), 10, 30, STYLE_OUTLINE);
font->drawWithOutline(batch, std::to_wstring((int)player->camera->position.y), 50, 30); font->draw(batch, std::to_wstring((int)player->camera->position.y), 50, 30, STYLE_OUTLINE);
font->drawWithOutline(batch, std::to_wstring((int)player->camera->position.z), 90, 30); font->draw(batch, std::to_wstring((int)player->camera->position.z), 90, 30, STYLE_OUTLINE);
font->drawWithOutline(batch, L"fps:", 16, 42); font->draw(batch, L"fps:", 16, 42, STYLE_OUTLINE);
font->drawWithOutline(batch, std::to_wstring(fps), 40, 42); font->draw(batch, std::to_wstring(fps), 40, 42, STYLE_OUTLINE);
} }
batch->render(); batch->render();