dosgame1/graphics/Font.h

79 lines
2.4 KiB
C++

#ifndef GAME_FONT_H
#define GAME_FONT_H
#include "Bitmap.h"
class Font {
public:
Font() = default;
~Font() = default;
Font(Bitmap *bitmap, unsigned w, unsigned h, unsigned minCode = 32, unsigned maxCode = 127)
: Font() {
load(bitmap, w, h, minCode, maxCode);
}
void load(Bitmap *bitmap, unsigned w, unsigned h, unsigned minCode = 32, unsigned maxCode = 127) {
_font = bitmap;
_w = w;
_h = h;
_minCode = minCode;
_maxCode = maxCode;
_charsPerRow = _font->GetWidth() / _w;
}
void renderCharAlpha(int c, int x, int y, uint8_t color, uint8_t *fb, unsigned fbWidth, unsigned fbHeight) {
if (c < _minCode || c > _maxCode) return;
c -= _minCode;
auto bitmapX = (c % _charsPerRow) * _w;
auto bitmapY = (c / _charsPerRow) * _h;
for (auto charY = 0; charY < _h; charY++) {
if (y + charY < 0) continue;
if (y + charY >= fbHeight) break;
auto yOffset = (y + charY) * fbWidth;
for (auto charX = 0; charX < _w; charX++) {
if (x + charX < 0) continue;
if (x + charX >= fbWidth) break;
auto pv = _font->GetPixel(bitmapX + charX, bitmapY + charY);
if (pv & 0x80) continue;
fb[yOffset + x + charX] = color;
}
}
}
void renderChar(int c, int x, int y, uint8_t fgColor, uint8_t bgColor, uint8_t *fb, unsigned fbWidth, unsigned fbHeight) {
if (c < _minCode || c > _maxCode) return;
c -= _minCode;
auto bitmapX = (c % _charsPerRow) * _w;
auto bitmapY = (c / _charsPerRow) * _h;
for (auto charY = 0; charY < _h; charY++) {
if (y + charY < 0) continue;
if (y + charY >= fbHeight) break;
auto yOffset = (y + charY) * fbWidth;
for (auto charX = 0; charX < _w; charX++) {
if (x + charX < 0) continue;
if (x + charX >= fbWidth) break;
auto pv = _font->GetPixel(bitmapX + charX, bitmapY + charY) & 0x80 ? bgColor : fgColor;
fb[yOffset + x + charX] = pv;
}
}
}
private:
Bitmap *_font{nullptr};
unsigned _w{0}, _h{0};
unsigned _minCode{0};
unsigned _maxCode{0};
unsigned _charsPerRow{0};
};
#endif //GAME_FONT_H