MADS: Added caching for font instances

This commit is contained in:
Paul Gilbert 2014-03-17 00:00:22 -04:00
parent acba8f9254
commit b831323c85
5 changed files with 43 additions and 27 deletions

View file

@ -106,7 +106,6 @@ TextDialog::TextDialog(MADSEngine *vm, const Common::String &fontName,
_fontName = fontName;
_position = pos;
_vm->_font->setFont(FONT_INTERFACE);
_vm->_font->setColors(TEXTDIALOG_BLACK, TEXTDIALOG_BLACK, TEXTDIALOG_BLACK, TEXTDIALOG_BLACK);
_innerWidth = (_vm->_font->maxWidth() + 1) * maxChars;

View file

@ -28,7 +28,9 @@
namespace MADS {
MADSEngine *Font::_vm = nullptr;
MADSEngine *Font::_vm;
Common::HashMap<Common::String, Font *> *Font::_fonts;
uint8 Font::_fontColors[4];
@ -38,24 +40,40 @@ void Font::init(MADSEngine *vm) {
_fontColors[1] = 0xF;
_fontColors[2] = 7;
_fontColors[3] = 8;
_fonts = new Common::HashMap<Common::String, Font *>();
}
void Font::deinit() {
Common::HashMap<Common::String, Font *>::iterator i;
for (i = _fonts->begin(); i != _fonts->end(); ++i)
delete (*i)._value;
delete _fonts;
}
Font *Font::getFont(const Common::String &fontName) {
Font *font = new Font();
font->setFont(fontName);
if (_fonts->contains(fontName)) {
return _fonts->getVal(fontName);
} else {
Font *font = new Font(fontName);
_fonts->setVal(fontName, font);
return font;
}
}
Font::Font() {
_sysFont = true;
setFont(FONT_INTERFACE);
}
Font::Font(const Common::String &filename) {
setFont(filename);
}
Font::~Font() {
if (!_sysFont) {
delete[] _charWidths;
delete[] _charOffs;
delete[] _charData;
}
}
void Font::setFont(const Common::String &filename) {
@ -63,7 +81,6 @@ void Font::setFont(const Common::String &filename) {
// Already using specified font, so don't bother reloading
return;
_sysFont = false;
_filename = filename;
MadsPack fontData(filename, _vm);
@ -95,13 +112,6 @@ void Font::setFont(const Common::String &filename) {
delete fontFile;
}
void Font::setColor(uint8 color) {
if (_sysFont)
_fontColors[1] = color;
else
_fontColors[3] = color;
}
void Font::setColors(uint8 v1, uint8 v2, uint8 v3, uint8 v4) {
_fontColors[0] = v1;
_fontColors[1] = v2;

View file

@ -24,8 +24,9 @@
#define MADS_FONT_H
#include "common/scummsys.h"
#include "common/util.h"
#include "common/hashmap.h"
#include "common/endian.h"
#include "common/util.h"
#include "mads/msurface.h"
namespace MADS {
@ -44,31 +45,37 @@ class Font {
private:
static uint8 _fontColors[4];
static MADSEngine *_vm;
static Common::HashMap<Common::String, Font *> *_fonts;
public:
/**
* Initialise the font system
*/
static void init(MADSEngine *vm);
/**
* Free up the resources used by the font
*/
static void deinit();
/**
* Returns a new Font instance using the specified font name
*/
static Font *getFont(const Common::String &fontName);
protected:
private:
uint8 _maxWidth, _maxHeight;
uint8 *_charWidths;
uint16 *_charOffs;
uint8 *_charData;
bool _sysFont;
Common::String _filename;
int getBpp(int charWidth);
public:
Font();
virtual ~Font();
void setFont(const Common::String &filename);
void setColor(uint8 color);
public:
Font();
Font(const Common::String &filename);
virtual ~Font();
void setColors(uint8 v1, uint8 v2, uint8 v3, uint8 v4);
void setColorMode(int mode);

View file

@ -59,6 +59,7 @@ MADSEngine::~MADSEngine() {
delete _dialogs;
delete _events;
delete _font;
Font::deinit();
delete _game;
delete _palette;
delete _resources;

View file

@ -40,7 +40,6 @@ KernelMessages::KernelMessages(MADSEngine *vm): _vm(vm) {
}
KernelMessages::~KernelMessages() {
delete _talkFont;
}
void KernelMessages::clear() {