From 566da748e16f90218aab6bd1f7ea19adc57e098a Mon Sep 17 00:00:00 2001 From: Peter Kohaut Date: Thu, 6 Oct 2016 21:23:46 +0200 Subject: BLADERUNNER: added support for fonts --- engines/bladerunner/bladerunner.cpp | 30 +++++- engines/bladerunner/bladerunner.h | 2 + engines/bladerunner/font.cpp | 203 ++++++++++++++++++++++++++++++++++++ engines/bladerunner/font.h | 88 ++++++++++++++++ engines/bladerunner/module.mk | 1 + engines/bladerunner/shape.cpp | 2 +- engines/bladerunner/shape.h | 2 +- 7 files changed, 321 insertions(+), 7 deletions(-) create mode 100644 engines/bladerunner/font.cpp create mode 100644 engines/bladerunner/font.h diff --git a/engines/bladerunner/bladerunner.cpp b/engines/bladerunner/bladerunner.cpp index 804bfbd81c..fb8751c758 100644 --- a/engines/bladerunner/bladerunner.cpp +++ b/engines/bladerunner/bladerunner.cpp @@ -30,6 +30,7 @@ #include "bladerunner/chapters.h" #include "bladerunner/combat.h" #include "bladerunner/crimes_database.h" +#include "bladerunner/font.h" #include "bladerunner/gameflags.h" #include "bladerunner/gameinfo.h" #include "bladerunner/image.h" @@ -278,9 +279,9 @@ bool BladeRunnerEngine::startup(bool hasSavegames) { // TODO: Scores - // TODO: Font - - // TODO: KIA6PT.FON + _mainFont = new Font(this); + _mainFont->open("KIA6PT.FON", 640, 480, -1, 0, 0x252D); + _mainFont->setSpacing(1, 0); for (int i = 0; i != 43; ++i) { Shape *shape = new Shape(this); @@ -422,7 +423,11 @@ void BladeRunnerEngine::shutdown() { if (isArchiveOpen("SPCHSFX.TLK")) closeArchive("SPCHSFX.TLK"); - // TODO: Delete KIA6PT.FON + if(_mainFont) { + _mainFont->close(); + delete _mainFont; + _mainFont = nullptr; + } delete _items; _items = nullptr; @@ -652,6 +657,19 @@ void BladeRunnerEngine::gameTick() { sceneObject->_sceneObjectId; drawBBox(a, b, _view, &_surface2, color); + + Vector3 pos = _view->calculateScreenPosition(0.5 * (a + b)); + switch(sceneObject->_sceneObjectType) { + case SceneObjectTypeActor: + _mainFont->drawColor(_textActorNames->getText(sceneObject->_sceneObjectId - SCENE_OBJECTS_ACTORS_OFFSET), _surface2, pos.x, pos.y, color); + break; + case SceneObjectTypeItem: + _mainFont->draw("item", _surface2, pos.x, pos.y); + break; + case SceneObjectTypeObject: + _mainFont->drawColor(_scene->objectGetName(sceneObject->_sceneObjectId - SCENE_OBJECTS_OBJECTS_OFFSET), _surface2, pos.x, pos.y, color); + break; + } } } @@ -673,9 +691,11 @@ void BladeRunnerEngine::gameTick() { for (int j = 0; j < walkbox->_vertexCount; j++) { Vector3 start = _view->calculateScreenPosition(walkbox->_vertices[j]); - Vector3 end = _view->calculateScreenPosition(walkbox->_vertices[(j+1) % walkbox->_vertexCount]); + Vector3 end = _view->calculateScreenPosition(walkbox->_vertices[(j + 1) % walkbox->_vertexCount]); //debug("walkbox[%i][%i] = x=%f y=%f x=%f y=%f", i, j, start.x, start.y, end.x, end.y); _surface2.drawLine(start.x, start.y, end.x, end.y, 0b111111111100000); + Vector3 pos = _view->calculateScreenPosition(0.5 * (start + end)); + _mainFont->drawColor(walkbox->_name, _surface2, pos.x, pos.y, 0b111111111100000); } } diff --git a/engines/bladerunner/bladerunner.h b/engines/bladerunner/bladerunner.h index 525aedfd6e..3546fa8fce 100644 --- a/engines/bladerunner/bladerunner.h +++ b/engines/bladerunner/bladerunner.h @@ -45,6 +45,7 @@ class AudioSpeech; class Chapters; class CrimesDatabase; class Combat; +class Font; class GameFlags; class GameInfo; class Items; @@ -80,6 +81,7 @@ public: GameInfo *_gameInfo; Items *_items; Lights *_lights; + Font *_mainFont; Mouse *_mouse; Obstacles *_obstacles; Scene *_scene; diff --git a/engines/bladerunner/font.cpp b/engines/bladerunner/font.cpp new file mode 100644 index 0000000000..2f15780719 --- /dev/null +++ b/engines/bladerunner/font.cpp @@ -0,0 +1,203 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * 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 "bladerunner/font.h" + +#include "bladerunner/bladerunner.h" + +#include "common/debug.h" + +namespace BladeRunner { + +Font::Font(BladeRunnerEngine *vm) : _vm(vm) { + reset(); +} + +Font::~Font() { + close(); +} + +bool Font::open(const Common::String &fileName, int screenWidth, int screenHeight, int spacing1, int spacing2, uint16 color) { + reset(); + + _screenWidth = screenWidth; + _screenHeight = screenHeight; + _spacing1 = spacing1; + _spacing2 = spacing2; + _color = color; + + Common::ScopedPtr stream(_vm->getResourceStream(fileName)); + if (!stream) { + debug("Font::open failed to open '%s'", fileName.c_str()); + return false; + } + + _characterCount = stream->readUint32LE(); + _maxWidth = stream->readUint32LE(); + _maxHeight = stream->readUint32LE(); + _dataSize = stream->readUint32LE(); + _data = new uint16[_dataSize]; + if (!_data) { + debug("Font::open failed to allocate font buffer"); + return false; + } + + stream->read(_characters, _characterCount * sizeof(FontCharacter)); + stream->read(_data, _dataSize * sizeof(uint16)); + return true; +} + +void Font::close() { + if (_data != nullptr) { + delete[] _data; + } + reset(); +} + +void Font::setSpacing(int spacing1, int spacing2) { + if (_data) { + _spacing1 = spacing1; + _spacing2 = spacing2; + } +} + +void Font::setColor(uint16 color) { + if (_data && _color != color) { + replaceColor(_color, color); + _color = color; + } +} + +void Font::draw(const Common::String &text, Graphics::Surface &surface, int x, int y) { + if (_data) { + if (x < 0) { + x = 0; + } + if (y < 0) { + y = 0; + } + + int textWidth = getTextWidth(text); + if (textWidth + x >= _screenWidth) { + x = _screenWidth - (textWidth + 1); + } + if (_maxHeight + y >= _screenHeight) { + y = _screenHeight - _maxHeight; + } + + const char *character = text.c_str(); + while (*character != 0) { + drawCharacter(*character, surface, x, y); + x += _spacing1 + _characters[*character + 1]._width; + character++; + } + } + +} + +void Font::drawColor(const Common::String &text, Graphics::Surface &surface, int x, int y, uint16 color) { + if (_color != color) { + setColor(color); + } + draw(text, surface, x, y); +} + +int Font::getTextWidth(const Common::String &text) { + const char *character = text.c_str(); + + if (!_data) { + return 0; + } + int totalWidth = 0; + if (*character == 0) { + return 0; + } + while (*character != 0) { + totalWidth = _spacing1 + _characters[*character + 1]._width; + character++; + } + return totalWidth - _spacing1; +} + +void Font::reset() { + _maxWidth = 0; + _maxHeight = 0; + _characterCount = 0; + _data = nullptr; + _dataSize = 0; + _screenWidth = 0; + _screenHeight = 0; + _spacing1 = 0; + _spacing2 = 0; + _color = 0x7FFF; + _intersperse = 0; + + memset(_characters, 0, 5120); +} + +void Font::replaceColor(uint16 oldColor, uint16 newColor) { + if (!_data || !_dataSize) { + return; + } + for (int i = 0; i < _dataSize; i++) { + if (_data[i] == oldColor) { + _data[i] = newColor; + } + } +} + +void Font::drawCharacter(const char character, Graphics::Surface &surface, int x, int y) { + if (x < 0 || x >= this->_screenWidth || y < 0 || y >= _screenHeight || !_data || character + 1 >= _characterCount) { + return; + } + + uint16 *dstPtr = (uint16*)surface.getBasePtr(x + _characters[character + 1]._x, y + _characters[character + 1]._y); + uint16 *srcPtr = &_data[_characters[character + 1]._dataOffset]; + int width = _characters[character + 1]._width; + int height = _characters[character + 1]._height; + if (_intersperse && y & 1) { + dstPtr += surface.w; + } + + int endY = height + y - 1; + int currentY = y; + while (currentY <= endY && currentY < _screenHeight) { + int currentX = x; + int endX = width + x - 1; + while (currentX <= endX && currentX < _screenWidth) { + if (!(*srcPtr & 0x8000)) { + *dstPtr = *srcPtr; + } + dstPtr++; + srcPtr++; + currentX++; + } + dstPtr += surface.w - width; + if (_intersperse) { + srcPtr += width; + dstPtr += surface.w; + currentY++; + } + currentY++; + } +} +} // End of namespace BladeRunner diff --git a/engines/bladerunner/font.h b/engines/bladerunner/font.h new file mode 100644 index 0000000000..329bc48abe --- /dev/null +++ b/engines/bladerunner/font.h @@ -0,0 +1,88 @@ +/* ScummVM - Graphic Adventure Engine + * + * ScummVM is the legal property of its developers, whose names + * are too numerous to list here. Please refer to the COPYRIGHT + * 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 BLADERUNNER_FONT_H +#define BLADERUNNER_FONT_H + +#include "common/str.h" + +namespace Graphics { +struct Surface; +} + +namespace BladeRunner { + +class BladeRunnerEngine; + +#include "common/pack-start.h" + +struct FontCharacter { + int _x; + int _y; + int _width; + int _height; + int _dataOffset; +} PACKED_STRUCT; + +#include "common/pack-end.h" + +class Font { + BladeRunnerEngine *_vm; + + int _characterCount; + int _maxWidth; + int _maxHeight; + FontCharacter _characters[256]; + int _dataSize; + uint16 *_data; + int _screenWidth; + int _screenHeight; + int _spacing1; + int _spacing2; + uint16 _color; + int _intersperse; + +public: + Font(BladeRunnerEngine *vm); + ~Font(); + + bool open(const Common::String &fileName, int screenWidth, int screenHeight, int spacing1, int spacing2, uint16 color); + void close(); + + void setSpacing(int spacing1, int spacing2); + void setColor(uint16 color); + + void draw(const Common::String &text, Graphics::Surface &surface, int x, int y); + void drawColor(const Common::String &text, Graphics::Surface &surface, int x, int y, uint16 color); + + int getTextWidth(const Common::String &text); + +private: + void reset(); + void replaceColor(uint16 oldColor, uint16 newColor); + + void drawCharacter(const char character, Graphics::Surface &surface, int x, int y); +}; + +} // End of namespace BladeRunner + +#endif diff --git a/engines/bladerunner/module.mk b/engines/bladerunner/module.mk index 2aa3c24437..85dd8d5388 100644 --- a/engines/bladerunner/module.mk +++ b/engines/bladerunner/module.mk @@ -21,6 +21,7 @@ MODULE_OBJS = \ decompress_lzo.o \ detection.o \ fog.o \ + font.o \ gameflags.o \ gameinfo.o \ image.o \ diff --git a/engines/bladerunner/shape.cpp b/engines/bladerunner/shape.cpp index 59fc103ccc..8e62aa4057 100644 --- a/engines/bladerunner/shape.cpp +++ b/engines/bladerunner/shape.cpp @@ -33,7 +33,7 @@ namespace BladeRunner { Shape::Shape(BladeRunnerEngine *vm) - : _vm(vm), _data(0) { + : _vm(vm), _data(nullptr) { } Shape::~Shape() { diff --git a/engines/bladerunner/shape.h b/engines/bladerunner/shape.h index 782d20f278..e2e77b1a76 100644 --- a/engines/bladerunner/shape.h +++ b/engines/bladerunner/shape.h @@ -23,7 +23,7 @@ #ifndef BLADERUNNER_SHAPE_H #define BLADERUNNER_SHAPE_H -#include "common/substream.h" +#include "common/str.h" namespace Graphics { struct Surface; -- cgit v1.2.3