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

@@ -56,6 +56,7 @@ INCLUDE_DIRECTORIES(
../third_party/tinyxml
../third_party/physfs
../third_party/lodepng
../third_party/utfcpp/source
)
# Source Lists

View File

@@ -179,7 +179,7 @@ bool FILESYSTEM_loadTiXmlDocument(const char *name, TiXmlDocument *doc)
{
return false;
}
doc->Parse((const char*)mem);
doc->Parse((const char*)mem, NULL, TIXML_ENCODING_UTF8);
FILESYSTEM_freeMemory(&mem);
return true;
}

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;
}
}

View File

@@ -3,6 +3,7 @@
#include "GraphicsResources.h"
#include <vector>
#include <map>
@@ -28,6 +29,9 @@ public:
GraphicsResources grphx;
int bfontlen(char32_t ch);
int font_idx(char32_t ch);
void Makebfont();
void drawhuetile(int x, int y, int t, int c);
@@ -212,7 +216,6 @@ public:
std::vector <SDL_Surface*> bfontmask;
std::vector <SDL_Surface*> flipbfont;
std::vector <SDL_Surface*> flipbfontmask;
std::vector <int> bfontlen;
bool flipmode;
bool setflipmode;
@@ -280,6 +283,8 @@ public:
bool translucentroomname;
bool showmousecursor;
std::map<int, int> font_positions;
};
extern Graphics graphics;

View File

@@ -1,6 +1,7 @@
#include "KeyPoll.h"
#include <stdio.h>
#include <string.h>
#include <utf8/unchecked.h>
void KeyPoll::setSensitivity(int _value)
{
@@ -94,11 +95,12 @@ void KeyPoll::Poll()
if (textentrymode)
{
if (evt.key.keysym.sym == SDLK_BACKSPACE)
if (evt.key.keysym.sym == SDLK_BACKSPACE && !keybuffer.empty())
{
bool kbemptybefore = keybuffer.empty();
keybuffer = keybuffer.substr(0, keybuffer.length() - 1);
if (!kbemptybefore && keybuffer.empty())
std::string::iterator iter = keybuffer.end();
utf8::unchecked::prior(iter);
keybuffer = keybuffer.substr(0, iter - keybuffer.begin());
if (keybuffer.empty())
{
linealreadyemptykludge = true;
}

View File

@@ -1,4 +1,5 @@
#include "Textbox.h"
#include <utf8/unchecked.h>
textboxclass::textboxclass()
{
@@ -133,7 +134,8 @@ void textboxclass::resize()
max = 0;
for (int iter = 0; iter < numlines; iter++)
{
if (line[iter].length() > (unsigned int)max) max = line[iter].length();
unsigned int len = utf8::unchecked::distance(line[iter].begin(), line[iter].end());
if (len > (unsigned int)max) max = len;
}
lw = max;

View File

@@ -16,6 +16,7 @@
#include "FileSystemUtils.h"
#include <string>
#include <utf8/unchecked.h>
edlevelclass::edlevelclass()
{
@@ -2625,7 +2626,8 @@ void editorrender( KeyPoll& key, Graphics& dwgfx, Game& game, mapclass& map, ent
}
else
{
fillboxabs(dwgfx, (edentity[i].x*8)- (ed.levx*40*8),(edentity[i].y*8)- (ed.levy*30*8),edentity[i].scriptname.length()*8,8,dwgfx.getRGB(96,96,96));
int length = utf8::unchecked::distance(edentity[i].scriptname.begin(), edentity[i].scriptname.end());
fillboxabs(dwgfx, (edentity[i].x*8)- (ed.levx*40*8),(edentity[i].y*8)- (ed.levy*30*8),length*8,8,dwgfx.getRGB(96,96,96));
}
dwgfx.bprint((edentity[i].x*8)- (ed.levx*40*8),(edentity[i].y*8)- (ed.levy*30*8), edentity[i].scriptname, 196, 196, 255 - help.glow);
break;
@@ -3796,7 +3798,7 @@ void editorinput( KeyPoll& key, Graphics& dwgfx, Game& game, mapclass& map, enti
ed.sby--;
}
key.keybuffer=ed.sb[ed.pagey+ed.sby];
ed.sbx = ed.sb[ed.pagey+ed.sby].length();
ed.sbx = utf8::unchecked::distance(ed.sb[ed.pagey+ed.sby].begin(), ed.sb[ed.pagey+ed.sby].end());
}
if (key.isDown(27))
@@ -3881,7 +3883,7 @@ void editorinput( KeyPoll& key, Graphics& dwgfx, Game& game, mapclass& map, enti
}
ed.sb[ed.pagey+ed.sby]=key.keybuffer;
ed.sbx = ed.sb[ed.pagey+ed.sby].length();
ed.sbx = utf8::unchecked::distance(ed.sb[ed.pagey+ed.sby].begin(), ed.sb[ed.pagey+ed.sby].end());
if(!game.press_map && !key.isDown(27)) game.mapheld=false;
if (!game.mapheld)
@@ -3900,7 +3902,7 @@ void editorinput( KeyPoll& key, Graphics& dwgfx, Game& game, mapclass& map, enti
}
if(ed.sby+ed.pagey>=ed.sblength) ed.sblength=ed.sby+ed.pagey;
key.keybuffer=ed.sb[ed.pagey+ed.sby];
ed.sbx = ed.sb[ed.pagey+ed.sby].length();
ed.sbx = utf8::unchecked::distance(ed.sb[ed.pagey+ed.sby].begin(), ed.sb[ed.pagey+ed.sby].end());
}
else
{