font draw improvements

This commit is contained in:
MihailRis 2022-07-15 16:54:35 +03:00 committed by GitHub
parent 889b2a62b5
commit fa4538c124
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 32 deletions

View File

@ -43,35 +43,67 @@ 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) {
for (unsigned c : text){ int page = 0;
if (isPrintableChar(c)){ int next = 10000;
batch->texture(pages[c >> 8]); int init_x = x;
batch->sprite(x, y, RES, RES, 16, c, vec4(1.0f)); do {
for (unsigned c : text){
if (isPrintableChar(c)){
int charpage = c >> 8;
if (charpage == page){
Texture* texture = pages[charpage];
if (texture == nullptr){
texture = pages[0];
}
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);
} }
x += getGlyphWidth(c); page = next;
} next = 10000;
x = init_x;
} while (page < 10000);
} }
void Font::drawWithShadow(Batch2D* batch, std::wstring text, int x, int y) { void Font::drawWithOutline(Batch2D* batch, std::wstring text, int x, int y) {
for (unsigned c : text){ int page = 0;
if (isPrintableChar(c)){ int next = 10000;
Texture* texture = pages[c >> 8]; int init_x = x;
if (texture == nullptr){ do {
texture = pages[0]; for (unsigned c : text){
} if (isPrintableChar(c)){
batch->texture(texture); int charpage = c >> 8;
batch->sprite(x+1, y+1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f)); if (charpage == page){
batch->sprite(x+1, y-1, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f)); Texture* texture = pages[charpage];
batch->sprite(x-1, y, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f)); if (texture == nullptr){
batch->sprite(x+1, y, RES, RES, 16, c, vec4(0.0f, 0.0f, 0.0f, 1.0f)); texture = pages[0];
}
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+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));
}
else if (charpage > page && charpage < next){
next = charpage;
}
}
x += getGlyphWidth(c);
} }
x += getGlyphWidth(c); page = next;
} next = 10000;
x = init_x;
} while (page < 10000);
} }

View File

@ -16,7 +16,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 drawWithShadow(Batch2D* batch, std::wstring text, int x, int y); void drawWithOutline(Batch2D* batch, std::wstring text, int x, int y);
}; };
#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->drawWithShadow(batch, L"рандом chunks: "+std::to_wstring(chunks->chunksCount), 16, 16); font->drawWithOutline(batch, L"chunks: "+std::to_wstring(chunks->chunksCount), 16, 16);
font->drawWithShadow(batch, std::to_wstring((int)player->camera->position.x), 10, 30); font->drawWithOutline(batch, std::to_wstring((int)player->camera->position.x), 10, 30);
font->drawWithShadow(batch, std::to_wstring((int)player->camera->position.y), 50, 30); font->drawWithOutline(batch, std::to_wstring((int)player->camera->position.y), 50, 30);
font->drawWithShadow(batch, std::to_wstring((int)player->camera->position.z), 90, 30); font->drawWithOutline(batch, std::to_wstring((int)player->camera->position.z), 90, 30);
font->drawWithShadow(batch, L"fps:", 16, 42); font->drawWithOutline(batch, L"fps:", 16, 42);
font->drawWithShadow(batch, std::to_wstring(fps), 40, 42); font->drawWithOutline(batch, std::to_wstring(fps), 40, 42);
} }
batch->render(); batch->render();