STARK: Implement TrueType font rendering

This commit is contained in:
Bastien Bouclet 2015-11-22 20:50:15 +01:00
parent 86c3e0d49e
commit c9540977eb
12 changed files with 215 additions and 11 deletions

View File

@ -24,9 +24,9 @@
#include "engines/stark/gfx/driver.h"
#include "engines/stark/services/gameinterface.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/staticprovider.h"
#include "engines/stark/services/gameinterface.h"
#include "engines/stark/resources/item.h"
@ -101,6 +101,7 @@ void Cursor::setMouseHint(const Common::String &hint) {
_mouseText = new VisualText(_gfx);
_mouseText->setText(hint);
_mouseText->setColor(0xFFFF0000);
_mouseText->setFont(FontProvider::kSmallFont);
_mouseText->setTargetWidth(96);
} else {
_mouseText = nullptr;

View File

@ -63,6 +63,7 @@ MODULE_OBJS := \
scene.o \
services/archiveloader.o \
services/dialogplayer.o \
services/fontprovider.o \
services/gameinterface.o \
services/global.o \
services/resourceprovider.o \

View File

@ -220,6 +220,7 @@ void ImageText::initVisual() {
text->setText(_text);
text->setColor(_color | 0xFF000000);
text->setTargetWidth(_size.x);
text->setFont(FontProvider::kCustomFont, _font);
_visual = text;
}

View File

@ -0,0 +1,103 @@
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "engines/stark/services/fontprovider.h"
#include "common/archive.h"
#include "graphics/font.h"
#include "graphics/fontman.h"
#include "graphics/fonts/ttf.h"
namespace Stark {
FontProvider::FontProvider() {
initFonts();
}
FontProvider::~FontProvider() {
}
void FontProvider::initFonts() {
// TODO: Use SystemFontMan instead when it gets merged
_ttfFileMap["Garamond"] = "Gara.ttf";
_ttfFileMap["Florentine Script"] = "flornt.TTF";
_ttfFileMap["Folkard"] = "folkard.ttf";
_ttfFileMap["Arial"] = "ARIAL.TTF";
_ttfFileMap["Bradley Hand ITC"] = "bradhitc.ttf";
// TODO: Read from gui.ini
_smallFont = FontHolder(this, "Garamond", 12);
_bigFont = FontHolder(this, "Florentine Script", 19);
_customFonts[0] = FontHolder(this, "Folkard", 20);
_customFonts[1] = FontHolder(this, "Folkard", 12);
_customFonts[2] = FontHolder(this, "Arial", 14);
_customFonts[3] = FontHolder(this, "Bradley Hand ITC", 16);
_customFonts[4] = FontHolder(this, "Bradley Hand ITC", 20);
_customFonts[5] = FontHolder(this, "Bradley Hand ITC", 16);
_customFonts[6] = FontHolder(this, "Bradley Hand ITC", 15);
_customFonts[7] = FontHolder(this, "Florentine Script", 13);
}
FontProvider::FontHolder::FontHolder(FontProvider *fontProvider, Common::String name, uint32 height, uint32 charset) {
_name = name;
_height = height;
_charset = charset;
// Fetch the font file name
Common::String ttfFileName = "fonts/" + fontProvider->_ttfFileMap[_name];
// Initialize the font
Common::SeekableReadStream *s = SearchMan.createReadStreamForMember(ttfFileName);
if (s) {
_font = Common::SharedPtr<Graphics::Font>(
Graphics::loadTTFFont(*s, _height, Graphics::kTTFSizeModeCell, 0, Graphics::kTTFRenderModeMonochrome)
);
delete s;
} else {
warning("Unable to load the font '%s'", ttfFileName.c_str());
}
}
const Graphics::Font *FontProvider::getFont(FontProvider::FontType type, int32 customFontIndex) {
// Get the font holder
FontHolder *holder;
if (type == kSmallFont) {
holder = &_smallFont;
} else if (type == kBigFont) {
holder = &_bigFont;
} else {
assert(customFontIndex >= 0 && customFontIndex < 8);
holder = &_customFonts[customFontIndex];
}
if (holder->_font) {
return holder->_font.get();
} else {
// Fallback to a default font
return FontMan.getFontByUsage(Graphics::FontManager::kBigGUIFont);
}
}
} // End of namespace Stark

View File

@ -0,0 +1,76 @@
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef STARK_SERVICES_FONT_PROVIDER_H
#define STARK_SERVICES_FONT_PROVIDER_H
#include "common/hash-str.h"
#include "common/ptr.h"
namespace Graphics {
class Font;
}
namespace Stark {
/**
* The font provider offers a set of predefined fonts for the game to use
*/
class FontProvider {
public:
FontProvider();
~FontProvider();
enum FontType {
kSmallFont,
kBigFont,
kCustomFont
};
/**
* Request a font matching the specified parameters
*/
const Graphics::Font *getFont(FontType type, int32 customFontIndex);
private:
struct FontHolder {
Common::String _name;
uint32 _height;
uint32 _charset;
Common::SharedPtr<Graphics::Font> _font;
FontHolder() : _height(0), _charset(0) {}
FontHolder(FontProvider *fontProvider, Common::String name, uint32 height, uint32 charset = 0);
};
void initFonts();
FontHolder _smallFont;
FontHolder _bigFont;
FontHolder _customFonts[8];
Common::StringMap _ttfFileMap;
};
} // End of namespace Stark
#endif // STARK_SERVICES_FONT_PROVIDER_H

View File

@ -38,11 +38,12 @@ class Driver;
class ArchiveLoader;
class DialogPlayer;
class FontProvider;
class GameInterface;
class Global;
class ResourceProvider;
class StaticProvider;
class Scene;
class GameInterface;
class UserInterface;
/**
@ -61,6 +62,7 @@ public:
staticProvider = nullptr;
gameInterface = nullptr;
userInterface = nullptr;
fontProvider = nullptr;
}
ArchiveLoader *archiveLoader;
@ -73,6 +75,7 @@ public:
StaticProvider *staticProvider;
GameInterface *gameInterface;
UserInterface *userInterface;
FontProvider *fontProvider;
};
/** Shortcuts for accessing the services. */
@ -86,6 +89,7 @@ public:
#define StarkStaticProvider StarkServices::instance().staticProvider
#define StarkGameInterface StarkServices::instance().gameInterface
#define StarkUserInterface StarkServices::instance().userInterface
#define StarkFontProvider StarkServices::instance().fontProvider
} // End of namespace Stark

View File

@ -31,12 +31,13 @@
#include "engines/stark/services/userinterface.h"
#include "engines/stark/services/archiveloader.h"
#include "engines/stark/services/dialogplayer.h"
#include "engines/stark/services/fontprovider.h"
#include "engines/stark/services/gameinterface.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/resourceprovider.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/stateprovider.h"
#include "engines/stark/services/staticprovider.h"
#include "engines/stark/services/gameinterface.h"
#include "engines/stark/gfx/driver.h"
#include "common/config-manager.h"
@ -65,6 +66,7 @@ StarkEngine::StarkEngine(OSystem *syst, const ADGameDescription *gameDesc) :
_randomSource(nullptr),
_dialogPlayer(nullptr),
_userInterface(nullptr),
_fontProvider(nullptr),
_lastClickTime(0) {
_mixer->setVolumeForSoundType(Audio::Mixer::kPlainSoundType, 127);
_mixer->setVolumeForSoundType(Audio::Mixer::kSFXSoundType, ConfMan.getInt("sfx_volume"));
@ -92,6 +94,7 @@ StarkEngine::~StarkEngine() {
delete _stateProvider;
delete _archiveLoader;
delete _userInterface;
delete _fontProvider;
StarkServices::destroy();
}
@ -109,6 +112,7 @@ Common::Error StarkEngine::run() {
_resourceProvider = new ResourceProvider(_archiveLoader, _stateProvider, _global);
_staticProvider = new StaticProvider(_archiveLoader, _global);
_randomSource = new Common::RandomSource("stark");
_fontProvider = new FontProvider();
_scene = new Scene(_gfx);
_dialogPlayer = new DialogPlayer();
_cursor = new Cursor(_gfx);
@ -127,6 +131,7 @@ Common::Error StarkEngine::run() {
services.staticProvider = _staticProvider;
services.gameInterface = _gameInterface;
services.userInterface = _userInterface;
services.fontProvider = _fontProvider;
// Load global resources
_resourceProvider->initGlobal();

View File

@ -44,6 +44,7 @@ class ArchiveLoader;
class Console;
class Cursor;
class DialogPlayer;
class FontProvider;
class Global;
class GameInterface;
class Scene;
@ -84,6 +85,7 @@ private:
StateProvider *_stateProvider;
StaticProvider *_staticProvider;
ResourceProvider *_resourceProvider;
FontProvider *_fontProvider;
Common::RandomSource *_randomSource;
const ADGameDescription *_gameDescription;

View File

@ -34,11 +34,13 @@ ClickText::ClickText(const Common::String &text, uint32 color) :
_visualPassive = new VisualText(StarkGfx);
_visualPassive->setText(_text);
_visualPassive->setColor(_color);
_visualPassive->setFont(FontProvider::kBigFont);
_visualPassive->setTargetWidth(600);
_visualActive = new VisualText(StarkGfx);
_visualActive->setText(_text);
_visualActive->setColor(0xFF00FF00);
_visualActive->setFont(FontProvider::kBigFont);
_visualActive->setTargetWidth(600);
_curVisual = _visualPassive;

View File

@ -111,6 +111,7 @@ void DialogPanel::updateSubtitleVisual() {
_subtitleVisual = new VisualText(_gfx);
_subtitleVisual->setText(_currentSpeech->getPhrase());
_subtitleVisual->setColor(_currentSpeech->characterIsApril() ? _aprilColor : _otherColor);
_subtitleVisual->setFont(FontProvider::kBigFont);
_subtitleVisual->setTargetWidth(600);
}

View File

@ -23,7 +23,6 @@
#include "engines/stark/visual/text.h"
#include "graphics/font.h"
#include "graphics/fontman.h"
#include "graphics/pixelformat.h"
#include "graphics/surface.h"
@ -40,7 +39,8 @@ VisualText::VisualText(Gfx::Driver *gfx) :
_gfx(gfx),
_texture(nullptr),
_color(0),
_targetWidth(600) {
_targetWidth(600),
_font(nullptr) {
}
VisualText::~VisualText() {
@ -71,22 +71,22 @@ void VisualText::setTargetWidth(uint32 width) {
}
void VisualText::createTexture() {
const Graphics::Font *font = FontMan.getFontByUsage(Graphics::FontManager::kBigGUIFont);
assert(_font);
Common::Array<Common::String> lines;
font->wordWrapText(_text, _targetWidth, lines);
_font->wordWrapText(_text, _targetWidth, lines);
int height = font->getFontHeight();
int height = _font->getFontHeight() + 1;
int width = 0;
for (uint i = 0; i < lines.size(); i++) {
width = MAX(width, font->getStringWidth(lines[i]));
width = MAX(width, _font->getStringWidth(lines[i]));
}
Graphics::Surface surface;
surface.create(width, height*lines.size(), _gfx->getScreenFormat());
surface.create(width, height * lines.size(), _gfx->getScreenFormat());
for (uint i = 0; i < lines.size(); i++) {
font->drawString(&surface, lines[i], 0, height*i, _targetWidth, _color);
_font->drawString(&surface, lines[i], 0, height * i, _targetWidth, _color);
}
_texture = _gfx->createTexture(&surface);
surface.free();
@ -105,4 +105,8 @@ void VisualText::render(const Common::Point &position) {
_gfx->drawSurface(_texture, position);
}
void VisualText::setFont(FontProvider::FontType type, int32 customFontIndex) {
_font = StarkFontProvider->getFont(type, customFontIndex);
}
} // End of namespace Stark

View File

@ -25,6 +25,8 @@
#include "engines/stark/visual/visual.h"
#include "engines/stark/services/fontprovider.h"
#include "common/rect.h"
namespace Stark {
@ -49,6 +51,7 @@ public:
void setText(const Common::String &text);
void setColor(uint32 color);
void setTargetWidth(uint32 width);
void setFont(FontProvider::FontType type, int32 customFontIndex = -1);
void render(const Common::Point &position);
@ -59,6 +62,7 @@ private:
Gfx::Driver *_gfx;
Common::String _text;
const Graphics::Font *_font;
uint32 _color;
uint32 _targetWidth;
Gfx::Texture *_texture;