1
0
mirror of https://github.com/The-Powder-Toy/The-Powder-Toy.git synced 2025-03-20 14:20:02 +01:00

Add an auxiliary font reader class to reduce code repetition

This commit is contained in:
mniip 2018-05-01 02:21:34 +03:00
parent ff27d69424
commit 9d927cbd6f
6 changed files with 88 additions and 101 deletions

2
src/graphics/Font.cpp Normal file

@ -0,0 +1,2 @@
#define INCLUDE_FONTDATA
#include "font.h"

43
src/graphics/Font.h Normal file

@ -0,0 +1,43 @@
#include <cstddef>
#include "common/String.h"
#include "font.h"
class FontReader
{
unsigned char *pointer;
int width;
int pixels;
int data;
inline FontReader(unsigned char *_pointer):
pointer(_pointer + 1),
width(*_pointer),
pixels(0),
data(0)
{}
public:
inline FontReader(String::value_type ch):
FontReader(ch <= 0xFF ? &font_data[font_ptrs[ch]] : &font_data[0])
{
}
inline int GetWidth() const
{
return width;
}
inline int NextPixel()
{
if(!pixels)
{
data = *(pointer++);
pixels = 4;
}
int old = data;
pixels--;
data >>= 2;
return old & 0x3;
}
};

@ -5,8 +5,7 @@
#include "Config.h"
#include "Misc.h"
#include "Graphics.h"
#define INCLUDE_FONTDATA
#include "font.h"
#include "Font.h"
#ifdef HIGH_QUALITY_RESAMPLE
#include "resampler/resampler.h"
#endif
@ -86,64 +85,31 @@ void VideoBuffer::Resize(int width, int height, bool resample, bool fixedRatio)
}
}
int VideoBuffer::SetCharacter(int x, int y, int c, int r, int g, int b, int a)
int VideoBuffer::SetCharacter(int x, int y, String::value_type c, int r, int g, int b, int a)
{
int i, j, w, bn = 0, ba = 0;
unsigned char *rp = font_data + font_ptrs[c];
w = *(rp++);
for (j=-2; j<FONT_H-2; j++)
for (i=0; i<w; i++)
{
if (!bn)
{
ba = *(rp++);
bn = 8;
}
SetPixel(x+i, y+j, r, g, b, ((ba&3)*a)/3);
ba >>= 2;
bn -= 2;
}
return x + w;
FontReader reader(c);
for (int j = -2; j < FONT_H - 2; j++)
for (int i = 0; i < reader.GetWidth(); i++)
SetPixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3);
return x + reader.GetWidth();
}
int VideoBuffer::BlendCharacter(int x, int y, int c, int r, int g, int b, int a)
int VideoBuffer::BlendCharacter(int x, int y, String::value_type c, int r, int g, int b, int a)
{
int i, j, w, bn = 0, ba = 0;
unsigned char *rp = font_data + font_ptrs[c];
w = *(rp++);
for (j=-2; j<FONT_H-2; j++)
for (i=0; i<w; i++)
{
if (!bn)
{
ba = *(rp++);
bn = 8;
}
BlendPixel(x+i, y+j, r, g, b, ((ba&3)*a)/3);
ba >>= 2;
bn -= 2;
}
return x + w;
FontReader reader(c);
for (int j = -2; j < FONT_H - 2; j++)
for (int i = 0; i < reader.GetWidth(); i++)
BlendPixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3);
return x + reader.GetWidth();
}
int VideoBuffer::AddCharacter(int x, int y, int c, int r, int g, int b, int a)
int VideoBuffer::AddCharacter(int x, int y, String::value_type c, int r, int g, int b, int a)
{
int i, j, w, bn = 0, ba = 0;
unsigned char *rp = font_data + font_ptrs[c];
w = *(rp++);
for (j=-2; j<FONT_H-2; j++)
for (i=0; i<w; i++)
{
if (!bn)
{
ba = *(rp++);
bn = 8;
}
AddPixel(x+i, y+j, r, g, b, ((ba&3)*a)/3);
ba >>= 2;
bn -= 2;
}
return x + w;
FontReader reader(c);
for (int j = -2; j < FONT_H - 2; j++)
for (int i = 0; i < reader.GetWidth(); i++)
AddPixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3);
return x + reader.GetWidth();
}
VideoBuffer::~VideoBuffer()
@ -582,14 +548,14 @@ int Graphics::textwidth(String str)
s+=3;
continue;
}
x += font_data[font_ptrs[*s]];
x += FontReader(*s).GetWidth();
}
return x-1;
}
int Graphics::CharWidth(String::value_type c)
{
return font_data[font_ptrs[(int)c]];
return FontReader(c).GetWidth();
}
int Graphics::textnwidth(String str, int n)
@ -610,7 +576,7 @@ int Graphics::textnwidth(String str, int n)
s+=3;
continue;
}
x += font_data[font_ptrs[*s]];
x += FontReader(*s).GetWidth();
n--;
}
return x-1;
@ -638,7 +604,7 @@ void Graphics::textnpos(String str, int n, int w, int *cx, int *cy)
if (!n) {
break;
}
x += font_data[font_ptrs[*s]];
x += FontReader(*s).GetWidth();
if (x>=w)
{
x = 0;
@ -668,7 +634,7 @@ int Graphics::textwidthx(String str, int w)
s+=3;
continue;
}
cw = font_data[font_ptrs[*s]];
cw = FontReader(*s).GetWidth();
if (x+(cw/2) >= w)
break;
x += cw;
@ -702,7 +668,7 @@ int Graphics::PositionAtCharIndex(String str, int charIndex, int & positionX, in
charIndex-=4;
continue;
}
x += font_data[font_ptrs[*s]];
x += FontReader(*s).GetWidth();
charIndex--;
}
positionX = x;
@ -732,7 +698,7 @@ int Graphics::CharIndexAtPosition(String str, int positionX, int positionY)
charIndex+=4;
continue;
}
cw = font_data[font_ptrs[*s]];
cw = FontReader(*s).GetWidth();
if ((x+(cw/2) >= positionX && y+FONT_H >= positionY) || y > positionY)
break;
x += cw;
@ -778,7 +744,7 @@ int Graphics::textwrapheight(String str, int width)
}
else
{
cw = font_data[font_ptrs[*s]];
cw = FontReader(*s).GetWidth();
if (x+cw>=width)
{
x = 0;
@ -821,7 +787,7 @@ void Graphics::textsize(String str, int & width, int & height)
}
else
{
cWidth += font_data[font_ptrs[*s]];
cWidth += FontReader(*s).GetWidth();
if(cWidth>lWidth)
lWidth = cWidth;
}

@ -84,9 +84,9 @@ public:
b = 255;
Buffer[y*(Width)+x] = PIXRGB(r,g,b);
}
int SetCharacter(int x, int y, int c, int r, int g, int b, int a);
int BlendCharacter(int x, int y, int c, int r, int g, int b, int a);
int AddCharacter(int x, int y, int c, int r, int g, int b, int a);
int SetCharacter(int x, int y, String::value_type c, int r, int g, int b, int a);
int BlendCharacter(int x, int y, String::value_type c, int r, int g, int b, int a);
int AddCharacter(int x, int y, String::value_type c, int r, int g, int b, int a);
~VideoBuffer();
};

@ -1,5 +1,5 @@
#include "Graphics.h"
#include "font.h"
#include "Font.h"
#include "common/tpt-thread.h"
#ifdef OGLI

@ -1,5 +1,5 @@
#include "font.h"
#include <cmath>
#include "Font.h"
int PIXELMETHODS_CLASS::drawtext_outline(int x, int y, String s, int r, int g, int b, int a)
{
@ -105,44 +105,20 @@ int PIXELMETHODS_CLASS::drawtext(int x, int y, String str, int r, int g, int b,
int PIXELMETHODS_CLASS::drawchar(int x, int y, String::value_type c, int r, int g, int b, int a)
{
int i, j, w, bn = 0, ba = 0;
unsigned char *rp = font_data + font_ptrs[c];
w = *(rp++);
for (j=-2; j<FONT_H-2; j++)
for (i=0; i<w; i++)
{
if (!bn)
{
ba = *(rp++);
bn = 8;
}
blendpixel(x+i, y+j, r, g, b, ((ba&3)*a)/3);
ba >>= 2;
bn -= 2;
}
return x + w;
FontReader reader(c);
for (int j = -2; j < FONT_H - 2; j++)
for (int i = 0; i < reader.GetWidth(); i++)
blendpixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3);
return x + reader.GetWidth();
}
int PIXELMETHODS_CLASS::addchar(int x, int y, String::value_type c, int r, int g, int b, int a)
{
int i, j, w, bn = 0, ba = 0;
unsigned char *rp = font_data + font_ptrs[c];
w = *(rp++);
for (j=-2; j<FONT_H-2; j++)
for (i=0; i<w; i++)
{
if (!bn)
{
ba = *(rp++);
bn = 8;
}
{
addpixel(x+i, y+j, r, g, b, ((ba&3)*a)/3);
}
ba >>= 2;
bn -= 2;
}
return x + w;
FontReader reader(c);
for (int j = -2; j < FONT_H - 2; j++)
for (int i = 0; i < reader.GetWidth(); i++)
addpixel(x + i, y + j, r, g, b, reader.NextPixel() * a / 3);
return x + reader.GetWidth();
}
TPT_INLINE void PIXELMETHODS_CLASS::xor_pixel(int x, int y)