Add support for Unicode rendering (#47)

This uses utfcpp combined with a custom font, in the form of a PNG and text file. By default, the game acts exactly as it did before; custom fonts can be provided by third parties.
This commit is contained in:
leo60228
2020-01-31 13:25:37 -05:00
committed by GitHub
parent 98ac1fdb53
commit 6a17625727
10 changed files with 505 additions and 87 deletions

View File

@@ -3,6 +3,10 @@
#include "Entity.h"
#include "Map.h"
#include "Screen.h"
#include "FileSystemUtils.h"
#include <utf8/unchecked.h>
#include <stdio.h>
#include <stdlib.h>
void Graphics::init()
{
@@ -129,6 +133,22 @@ void Graphics::init()
showmousecursor = true;
}
int Graphics::font_idx(char32_t ch) {
if (font_positions.size() > 0) {
std::map<int, int>::iterator iter = font_positions.find(ch);
if (iter == font_positions.end()) {
iter = font_positions.find('?');
if (iter == font_positions.end()) {
puts("font.txt missing fallback character!");
exit(1);
}
}
return iter->second;
} else {
return ch;
}
}
Graphics::~Graphics()
{
@@ -146,7 +166,7 @@ void Graphics::drawspritesetcol(int x, int y, int t, int c, UtilityClass& help)
void Graphics::Makebfont()
{
for (int j = 0; j < 16; j++)
for (int j = 0; j < (grphx.im_bfont->h / 8); j++)
{
for (int i = 0; i < 16; i++)
{
@@ -154,25 +174,31 @@ void Graphics::Makebfont()
SDL_Surface* temp = GetSubSurface(grphx.im_bfont,i*8,j*8,8,8);
bfont.push_back(temp);
temp = GetSubSurface(grphx.im_bfont,i*8,j*8,8,8);
SDL_Surface* TempFlipped = FlipSurfaceVerticle(temp);
SDL_Surface* TempFlipped = FlipSurfaceVerticle(temp);
flipbfont.push_back(TempFlipped);
SDL_FreeSurface(temp);
}
}
//Ok, now we work out the lengths (this data string cortesy of a program I wrote!)
for (int i = 0; i < 256; i++)
{
bfontlen.push_back(6);
unsigned char* charmap = NULL;
size_t length;
FILESYSTEM_loadFileToMemory("graphics/font.txt", &charmap, &length);
if (charmap != NULL) {
unsigned char* current = charmap;
unsigned char* end = charmap + length;
int pos = 0;
while (current != end) {
int codepoint = utf8::unchecked::next(current);
font_positions[codepoint] = pos;
++pos;
}
}
}
for(int k = 0; k < 96; k++)
{
bfontlen[k + 32] = 8;// int(maprow[k]);
int Graphics::bfontlen(char32_t ch) {
if (ch < 32) {
return 6;
} else {
return 8;
}
}
@@ -250,13 +276,9 @@ void Graphics::Print( int _x, int _y, std::string _s, int r, int g, int b, bool
_x = ((160 ) - ((len(_s)) / 2));
int bfontpos = 0;
int curr;
for (unsigned int i = 0; i < _s.length(); i++)
{
curr = (_s.c_str())[i];
if (curr > 255 || curr < 0)
{
curr = '?';
}
std::string::iterator iter = _s.begin();
while (iter != _s.end()) {
curr = utf8::unchecked::next(iter);
point tpoint;
tpoint.x = _x + bfontpos;
tpoint.y = _y;
@@ -267,13 +289,13 @@ void Graphics::Print( int _x, int _y, std::string _s, int r, int g, int b, bool
if (flipmode)
{
BlitSurfaceColoured( flipbfont[curr], NULL, backBuffer, &fontRect , ct);
BlitSurfaceColoured( flipbfont[font_idx(curr)], NULL, backBuffer, &fontRect , ct);
}
else
{
BlitSurfaceColoured( bfont[curr], NULL, backBuffer, &fontRect , ct);
BlitSurfaceColoured( bfont[font_idx(curr)], NULL, backBuffer, &fontRect , ct);
}
bfontpos+=bfontlen[curr] ;
bfontpos+=bfontlen(curr) ;
}
}
@@ -293,13 +315,9 @@ void Graphics::bigprint( int _x, int _y, std::string _s, int r, int g, int b, b
int bfontpos = 0;
int curr;
for (unsigned int i = 0; i < _s.length(); i++)
{
curr = (_s.c_str())[i];
if (curr > 255 || curr < 0)
{
curr = '?';
}
std::string::iterator iter = _s.begin();
while (iter != _s.end()) {
curr = utf8::unchecked::next(iter);
/*
point tpoint;
@@ -313,29 +331,29 @@ void Graphics::bigprint( int _x, int _y, std::string _s, int r, int g, int b, b
if (flipmode)
{
SDL_Surface* tempPrint = ScaleSurfaceSlow(flipbfont[curr], bfont[curr]->w *sc,bfont[curr]->h *sc);
SDL_Surface* tempPrint = ScaleSurfaceSlow(flipbfont[font_idx(curr)], bfont[font_idx(curr)]->w *sc,bfont[font_idx(curr)]->h *sc);
SDL_Rect printrect = { Sint16((_x) + bfontpos), Sint16(_y) , Sint16(bfont_rect.w*sc), Sint16(bfont_rect.h * sc)};
BlitSurfaceColoured(tempPrint, NULL, backBuffer, &printrect, ct);
SDL_FreeSurface(tempPrint);
}
else
{
SDL_Surface* tempPrint = ScaleSurfaceSlow(bfont[curr], bfont[curr]->w *sc,bfont[curr]->h *sc);
SDL_Surface* tempPrint = ScaleSurfaceSlow(bfont[font_idx(curr)], bfont[font_idx(curr)]->w *sc,bfont[font_idx(curr)]->h *sc);
SDL_Rect printrect = { static_cast<Sint16>((_x) + bfontpos), static_cast<Sint16>(_y) , static_cast<Sint16>((bfont_rect.w*sc)+1), static_cast<Sint16>((bfont_rect.h * sc)+1)};
BlitSurfaceColoured(tempPrint, NULL, backBuffer, &printrect, ct);
SDL_FreeSurface(tempPrint);
}
bfontpos+=bfontlen[curr] *sc;
bfontpos+=bfontlen(curr) *sc;
}
}
int Graphics::len(std::string t)
{
int bfontpos = 0;
for (unsigned int i = 0; i < t.length(); i++)
{
int cur = (t.c_str())[i];
bfontpos+= bfontlen[cur] ;
std::string::iterator iter = t.begin();
while (iter != t.end()) {
int cur = utf8::unchecked::next(iter);
bfontpos += bfontlen(cur);
}
return bfontpos;
}
@@ -351,14 +369,9 @@ void Graphics::PrintOff( int _x, int _y, std::string _s, int r, int g, int b, bo
if (cen)
_x = ((160) - (len(_s) / 2))+_x;
int bfontpos = 0;
int curr;
for (unsigned int i = 0; i < _s.length(); i++)
{
curr = (_s.c_str())[i];
if (curr > 255 || curr < 0)
{
curr = '?';
}
std::string::iterator iter = _s.begin();
while (iter != _s.end()) {
int curr = utf8::unchecked::next(iter);
point tpoint;
tpoint.x = _x + bfontpos;
tpoint.y = _y;
@@ -369,16 +382,16 @@ void Graphics::PrintOff( int _x, int _y, std::string _s, int r, int g, int b, bo
if (flipmode)
{
//flipbfont[cur].colorTransform(bfont_rect, ct);
BlitSurfaceColoured( bfont[curr], NULL, backBuffer, &fontRect , ct);
//flipbfont[font_idx(cur)].colorTransform(bfont_rect, ct);
BlitSurfaceColoured( bfont[font_idx(curr)], NULL, backBuffer, &fontRect , ct);
}
else
{
//bfont[cur].colorTransform(bfont_rect, ct);
//backBuffer.copyPixels(bfont[cur], bfont_rect, tpoint);
BlitSurfaceColoured( bfont[curr], NULL, backBuffer, &fontRect , ct);
//bfont[font_idx(cur)].colorTransform(bfont_rect, ct);
//backBuffer.copyPixels(bfont[font_idx(cur)], bfont_rect, tpoint);
BlitSurfaceColoured( bfont[font_idx(curr)], NULL, backBuffer, &fontRect , ct);
}
bfontpos+=bfontlen[curr] ;
bfontpos+=bfontlen(curr) ;
}
}
@@ -417,13 +430,9 @@ void Graphics::RPrint( int _x, int _y, std::string _s, int r, int g, int b, bool
_x = ((308) - (_s.length() / 2));
int bfontpos = 0;
int curr;
for (unsigned int i = 0; i < _s.length(); i++)
{
curr = (_s.c_str())[i];
if (curr > 255 || curr < 0)
{
curr = '?';
}
std::string::iterator iter = _s.begin();
while (iter != _s.end()) {
curr = utf8::unchecked::next(iter);
point tpoint;
tpoint.x = _x + bfontpos;
tpoint.y = _y;
@@ -434,16 +443,16 @@ void Graphics::RPrint( int _x, int _y, std::string _s, int r, int g, int b, bool
if (flipmode)
{
//flipbfont[cur].colorTransform(bfont_rect, ct);
BlitSurfaceColoured( flipbfont[curr], NULL, backBuffer, &fontRect , ct);
//flipbfont[font_idx(cur)].colorTransform(bfont_rect, ct);
BlitSurfaceColoured( flipbfont[font_idx(curr)], NULL, backBuffer, &fontRect , ct);
}
else
{
//bfont[cur].colorTransform(bfont_rect, ct);
//backBuffer.copyPixels(bfont[cur], bfont_rect, tpoint);
BlitSurfaceColoured( bfont[curr], NULL, backBuffer, &fontRect , ct);
//bfont[font_idx(cur)].colorTransform(bfont_rect, ct);
//backBuffer.copyPixels(bfont[font_idx(cur)], bfont_rect, tpoint);
BlitSurfaceColoured( bfont[font_idx(curr)], NULL, backBuffer, &fontRect , ct);
}
bfontpos+=bfontlen[curr] ;
bfontpos+=bfontlen(curr) ;
}
}
@@ -1035,7 +1044,8 @@ void Graphics::createtextbox( std::string t, int xp, int yp, int r/*= 255*/, int
textbox[m].clear();
textbox[m].line[0] = t;
textbox[m].xp = xp;
if (xp == -1) textbox[m].xp = 160 - (((t.length() / 2) + 1) * 8);
int length = utf8::unchecked::distance(t.begin(), t.end());
if (xp == -1) textbox[m].xp = 160 - (((length / 2) + 1) * 8);
textbox[m].yp = yp;
textbox[m].initcol(r, g, b);
textbox[m].resize();
@@ -3100,28 +3110,24 @@ void Graphics::bigrprint(int x, int y, std::string& t, int r, int g, int b, bool
int bfontpos = 0;
int cur;
for (size_t i = 0; i < t.length(); i++)
{
cur = (t.c_str())[i];
if (cur > 255 || cur < 0)
{
cur = '?';
}
std::string::iterator iter = t.begin();
while (iter != t.end()) {
cur = utf8::unchecked::next(iter);
if (flipmode)
{
SDL_Surface* tempPrint = ScaleSurfaceSlow(flipbfont[cur], bfont[cur]->w *sc,bfont[cur]->h *sc);
SDL_Surface* tempPrint = ScaleSurfaceSlow(flipbfont[font_idx(cur)], bfont[font_idx(cur)]->w *sc,bfont[font_idx(cur)]->h *sc);
SDL_Rect printrect = { Sint16(x + bfontpos), Sint16(y) , Sint16(bfont_rect.w*sc), Sint16(bfont_rect.h * sc)};
BlitSurfaceColoured(tempPrint, NULL, backBuffer, &printrect ,ct);
SDL_FreeSurface(tempPrint);
}
else
{
SDL_Surface* tempPrint = ScaleSurfaceSlow(bfont[cur], bfont[cur]->w *sc,bfont[cur]->h *sc);
SDL_Surface* tempPrint = ScaleSurfaceSlow(bfont[font_idx(cur)], bfont[font_idx(cur)]->w *sc,bfont[font_idx(cur)]->h *sc);
SDL_Rect printrect = { Sint16((x) + bfontpos), Sint16(y) , Sint16(bfont_rect.w*sc), Sint16(bfont_rect.h * sc)};
BlitSurfaceColoured(tempPrint, NULL, backBuffer, &printrect, ct);
SDL_FreeSurface(tempPrint);
}
bfontpos+=bfontlen[cur]* sc;
bfontpos+=bfontlen(cur)* sc;
}
}