Skip to content

Commit

Permalink
Add an auxiliary font reader class to reduce code repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
mniip committed Apr 30, 2018
1 parent ff27d69 commit 9d927cb
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 101 deletions.
2 changes: 2 additions & 0 deletions src/graphics/Font.cpp
@@ -0,0 +1,2 @@
#define INCLUDE_FONTDATA
#include "font.h"
43 changes: 43 additions & 0 deletions src/graphics/Font.h
@@ -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;
}
};
90 changes: 28 additions & 62 deletions src/graphics/Graphics.cpp
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/graphics/Graphics.h
Expand Up @@ -84,9 +84,9 @@ class VideoBuffer
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();
};

Expand Down
2 changes: 1 addition & 1 deletion src/graphics/OpenGLGraphics.cpp
@@ -1,5 +1,5 @@
#include "Graphics.h"
#include "font.h"
#include "Font.h"
#include "common/tpt-thread.h"
#ifdef OGLI

Expand Down
46 changes: 11 additions & 35 deletions src/graphics/RasterDrawMethods.inl
@@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 9d927cb

Please sign in to comment.