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; _fontName = fontName;
_position = pos; _position = pos;
_vm->_font->setFont(FONT_INTERFACE);
_vm->_font->setColors(TEXTDIALOG_BLACK, TEXTDIALOG_BLACK, TEXTDIALOG_BLACK, TEXTDIALOG_BLACK); _vm->_font->setColors(TEXTDIALOG_BLACK, TEXTDIALOG_BLACK, TEXTDIALOG_BLACK, TEXTDIALOG_BLACK);
_innerWidth = (_vm->_font->maxWidth() + 1) * maxChars; _innerWidth = (_vm->_font->maxWidth() + 1) * maxChars;

View file

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

View file

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

View file

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

View file

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