Skip to content

Commit

Permalink
SAGA: replace Font "::*alloc" & "::free" with Common::Array
Browse files Browse the repository at this point in the history
svn-id: r53656
  • Loading branch information
ajax16384 committed Oct 20, 2010
1 parent e797fdc commit 1bd1a25
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 47 deletions.
61 changes: 20 additions & 41 deletions engines/saga/font.cpp
Expand Up @@ -41,35 +41,20 @@ Font::Font(SagaEngine *vm) : _vm(vm) {

assert(_vm->getFontsCount() > 0);

_fonts = (FontData **)calloc(_vm->getFontsCount(), sizeof(*_fonts));
_loadedFonts = 0;

_fonts.resize(_vm->getFontsCount());
for (i = 0; i < _vm->getFontsCount(); i++) {
loadFont(_vm->getFontDescription(i)->fontResourceId);
loadFont(&_fonts[i], _vm->getFontDescription(i)->fontResourceId);
}

_fontMapping = 0;
}

Font::~Font() {
debug(8, "Font::~Font(): Freeing fonts.");
int i;

for (i = 0 ; i < _loadedFonts ; i++) {
if (_fonts[i] != NULL) {
free(_fonts[i]->normal.font);
free(_fonts[i]->outline.font);
}

free(_fonts[i]);
}

free(_fonts);
}


void Font::loadFont(uint32 fontResourceId) {
FontData *font;
void Font::loadFont(FontData *font, uint32 fontResourceId) {
byte *fontResourcePointer;
size_t fontResourceLength;
int numBits;
Expand All @@ -92,9 +77,6 @@ void Font::loadFont(uint32 fontResourceId) {

MemoryReadStreamEndian readS(fontResourcePointer, fontResourceLength, fontContext->isBigEndian());

// Create new font structure
font = (FontData *)malloc(sizeof(*font));

// Read font header
font->normal.header.charHeight = readS.readUint16();
font->normal.header.charWidth = readS.readUint16();
Expand Down Expand Up @@ -126,17 +108,14 @@ void Font::loadFont(uint32 fontResourceId) {
error("Invalid font resource size");
}

font->normal.font = (byte*)malloc(fontResourceLength - FONT_DESCSIZE);
memcpy(font->normal.font, fontResourcePointer + FONT_DESCSIZE, fontResourceLength - FONT_DESCSIZE);
font->normal.font.resize(fontResourceLength - FONT_DESCSIZE);
memcpy(&font->normal.font.front(), fontResourcePointer + FONT_DESCSIZE, fontResourceLength - FONT_DESCSIZE);

free(fontResourcePointer);


// Create outline font style
createOutline(font);

// Set font data
_fonts[_loadedFonts++] = font;
}

void Font::createOutline(FontData *font) {
Expand All @@ -145,12 +124,12 @@ void Font::createOutline(FontData *font) {
int newByteWidth;
int newRowLength = 0;
int currentByte;
unsigned char *basePointer;
unsigned char *srcPointer;
unsigned char *destPointer1;
unsigned char *destPointer2;
unsigned char *destPointer3;
unsigned char charRep;
byte *basePointer;
byte *srcPointer;
byte *destPointer1;
byte *destPointer2;
byte *destPointer3;
byte charRep;

// Populate new font style character data
for (i = 0; i < FONT_CHARCOUNT; i++) {
Expand All @@ -177,28 +156,28 @@ void Font::createOutline(FontData *font) {
font->outline.header.rowLength = newRowLength;

// Allocate new font representation storage
font->outline.font = (unsigned char *)calloc(newRowLength, font->outline.header.charHeight);
font->outline.font.resize(newRowLength * font->outline.header.charHeight);


// Generate outline font representation
for (i = 0; i < FONT_CHARCOUNT; i++) {
for (row = 0; row < font->normal.header.charHeight; row++) {
for (currentByte = 0; currentByte < font->outline.fontCharEntry[i].byteWidth; currentByte++) {
basePointer = font->outline.font + font->outline.fontCharEntry[i].index + currentByte;
basePointer = &font->outline.font[font->outline.fontCharEntry[i].index + currentByte];
destPointer1 = basePointer + newRowLength * row;
destPointer2 = basePointer + newRowLength * (row + 1);
destPointer3 = basePointer + newRowLength * (row + 2);
if (currentByte > 0) {
// Get last two columns from previous byte
srcPointer = font->normal.font + font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + (currentByte - 1);
srcPointer = &font->normal.font[font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + (currentByte - 1)];
charRep = *srcPointer;
*destPointer1 |= ((charRep << 6) | (charRep << 7));
*destPointer2 |= ((charRep << 6) | (charRep << 7));
*destPointer3 |= ((charRep << 6) | (charRep << 7));
}

if (currentByte < font->normal.fontCharEntry[i].byteWidth) {
srcPointer = font->normal.font + font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + currentByte;
srcPointer = &font->normal.font[font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + currentByte];
charRep = *srcPointer;
*destPointer1 |= charRep | (charRep >> 1) | (charRep >> 2);
*destPointer2 |= charRep | (charRep >> 1) | (charRep >> 2);
Expand All @@ -210,15 +189,15 @@ void Font::createOutline(FontData *font) {
// "Hollow out" character to prevent overdraw
for (row = 0; row < font->normal.header.charHeight; row++) {
for (currentByte = 0; currentByte < font->outline.fontCharEntry[i].byteWidth; currentByte++) {
destPointer2 = font->outline.font + font->outline.header.rowLength * (row + 1) + font->outline.fontCharEntry[i].index + currentByte;
destPointer2 = &font->outline.font[font->outline.header.rowLength * (row + 1) + font->outline.fontCharEntry[i].index + currentByte];
if (currentByte > 0) {
// Get last two columns from previous byte
srcPointer = font->normal.font + font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + (currentByte - 1);
srcPointer = &font->normal.font[font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + (currentByte - 1)];
*destPointer2 &= ((*srcPointer << 7) ^ 0xFFU);
}

if (currentByte < font->normal.fontCharEntry[i].byteWidth) {
srcPointer = font->normal.font + font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + currentByte;
srcPointer = &font->normal.font[font->normal.header.rowLength * row + font->normal.fontCharEntry[i].index + currentByte];
*destPointer2 &= ((*srcPointer >> 1) ^ 0xFFU);
}
}
Expand Down Expand Up @@ -289,7 +268,7 @@ void Font::draw(FontId fontId, const char *text, size_t count, const Common::Poi

void Font::outFont(const FontStyle &drawFont, const char *text, size_t count, const Common::Point &point, int color, FontEffectFlags flags) {
const byte *textPointer;
byte *c_dataPointer;
const byte *c_dataPointer;
int c_code;
int charRow = 0;
Point textPoint(point);
Expand Down Expand Up @@ -384,7 +363,7 @@ void Font::outFont(const FontStyle &drawFont, const char *text, size_t count, co
break;
}

c_dataPointer = drawFont.font + charRow * drawFont.header.rowLength + drawFont.fontCharEntry[c_code].index;
c_dataPointer = &drawFont.font[charRow * drawFont.header.rowLength + drawFont.fontCharEntry[c_code].index];

for (c_byte = 0; c_byte < c_byte_len; c_byte++, c_dataPointer++) {
// Check each bit, draw pixel if bit is set
Expand Down
11 changes: 5 additions & 6 deletions engines/saga/font.h
Expand Up @@ -120,7 +120,7 @@ struct FontCharEntry {
struct FontStyle {
FontHeader header;
FontCharEntry fontCharEntry[256];
byte *font;
Common::Array<byte> font;
};

struct FontData {
Expand Down Expand Up @@ -170,14 +170,14 @@ class Font {
void textDrawRect(FontId fontId, const char *text, const Common::Rect &rect, int color, int effectColor, FontEffectFlags flags);
void textDraw(FontId fontId, const char *string, const Common::Point &point, int color, int effectColor, FontEffectFlags flags);

void loadFont(uint32 fontResourceId);
void loadFont(FontData *font, uint32 fontResourceId);
void createOutline(FontData *font);
void draw(FontId fontId, const char *text, size_t count, const Common::Point &point, int color, int effectColor, FontEffectFlags flags);
void outFont(const FontStyle &drawFont, const char *text, size_t count, const Common::Point &point, int color, FontEffectFlags flags);

FontData *getFont(FontId fontId) {
validate(fontId);
return _fonts[fontId];
return &_fonts[fontId];
}

int getHeight(FontId fontId) {
Expand All @@ -190,7 +190,7 @@ class Font {
}
}
bool valid(FontId fontId) {
return (fontId < _loadedFonts);
return (uint(fontId) < _fonts.size());
}
int getByteLen(int numBits) const {
int byteLength = numBits / 8;
Expand All @@ -207,8 +207,7 @@ class Font {

int _fontMapping;

int _loadedFonts;
FontData **_fonts;
Common::Array<FontData> _fonts;
};

} // End of namespace Saga
Expand Down

0 comments on commit 1bd1a25

Please sign in to comment.