Skip to content

Commit

Permalink
GRAPHICS: Add back support for simple texture fonts
Browse files Browse the repository at this point in the history
  • Loading branch information
DrMcCoy committed Jan 21, 2014
1 parent 3a3dc7a commit c22e555
Show file tree
Hide file tree
Showing 19 changed files with 1,251 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/engines/enginemanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "graphics/guiman.h"

#include "graphics/aurora/sceneman.h"
#include "graphics/aurora/fontman.h"

#include "events/events.h"
#include "events/requests.h"
Expand Down Expand Up @@ -231,6 +232,7 @@ void EngineManager::cleanup(GameInstance &game) const {
GUIMan.clear();
SceneMan.clear();

FontMan.clear();
CursorMan.clear();
TextureMan.clear();

Expand Down
2 changes: 2 additions & 0 deletions src/graphics/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ noinst_HEADERS = \
cursorman.h \
cameraman.h \
renderable.h \
font.h \
guiman.h \
$(EMPTY)

Expand All @@ -35,6 +36,7 @@ libgraphics_la_SOURCES = \
cursorman.cpp \
cameraman.cpp \
renderable.cpp \
font.cpp \
guiman.cpp \
$(EMPTY)

Expand Down
6 changes: 6 additions & 0 deletions src/graphics/aurora/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ noinst_HEADERS = \
model_nwn2.h \
model_kotor.h \
model_witcher.h \
texturefont.h \
fontman.h \
text.h \
$(EMPTY)

libaurora_la_SOURCES = \
Expand All @@ -23,4 +26,7 @@ libaurora_la_SOURCES = \
model_nwn2.cpp \
model_kotor.cpp \
model_witcher.cpp \
texturefont.cpp \
fontman.cpp \
text.cpp \
$(EMPTY)
2 changes: 2 additions & 0 deletions src/graphics/aurora/cube.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ void Cube::stopRotate() {

void Cube::setSelectable(bool selectable) {
_entity->setQueryFlags(selectable ? kSelectableCube : kSelectableNone);

Renderable::setSelectable(selectable);
}

} // End of namespace Aurora
Expand Down
194 changes: 194 additions & 0 deletions src/graphics/aurora/fontman.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names can be
* found in 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 3
* 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.
*
*
* The Infinity, Aurora, Odyssey, Eclipse and Lycium engines, Copyright (c) BioWare corp.
* The Electron engine, Copyright (c) Obsidian Entertainment and BioWare corp.
*/

/** @file graphics/aurora/fontman.cpp
* The Aurora font manager.
*/

#include "common/error.h"
#include "common/systemfonts.h"

#include "graphics/aurora/fontman.h"
#include "graphics/aurora/texturefont.h"

DECLARE_SINGLETON(Graphics::Aurora::FontManager)

namespace Graphics {

namespace Aurora {

const char *kSystemFontMono = "_xoreosSystemFontMono";

ManagedFont::ManagedFont(Font *f) {
referenceCount = 0;
font = f;
}

ManagedFont::~ManagedFont() {
delete font;
}


FontHandle::FontHandle() : empty(true) {
}

FontHandle::FontHandle(FontMap::iterator &i) : empty(false), it(i) {
}

FontHandle::FontHandle(const FontHandle &right) : empty(true) {
*this = right;
}

FontHandle::~FontHandle() {
FontMan.release(*this);
}

FontHandle &FontHandle::operator=(const FontHandle &right) {
if (this == &right)
return *this;

FontMan.release(*this);

empty = right.empty;
it = right.it;

if (!empty)
it->second->referenceCount++;

return *this;
}

void FontHandle::clear() {
empty = true;
}

const Common::UString &FontHandle::getFontName() const {
assert(!empty);

return it->first;
}

Font &FontHandle::getFont() const {
assert(!empty);

return *it->second->font;
}


FontManager::FontManager() : _format(kFontFormatUnknown) {
}

FontManager::~FontManager() {
clear();
}

void FontManager::clear() {
Common::StackLock lock(_mutex);

for (FontMap::iterator font = _fonts.begin(); font != _fonts.end(); ++font)
delete font->second;

_fonts.clear();

_format = kFontFormatUnknown;
_aliases.clear();
}

void FontManager::setFormat(FontFormat format) {
_format = format;
}

void FontManager::addAlias(const Common::UString &alias, const Common::UString &realName) {
_aliases[alias] = realName;
}

FontHandle FontManager::get(Common::UString name, int height) {
return get(_format, name, height);
}

FontHandle FontManager::get(FontFormat format, Common::UString name, int height) {
Common::StackLock lock(_mutex);

// Lock up the name in our alias map first
std::map<Common::UString, Common::UString>::iterator realName = _aliases.find(name);
if (realName != _aliases.end())
name = realName->second;

Common::UString indexName = name;

// If we have been given a size, index the font under that size
if (height > 0)
indexName = Common::UString::sprintf("%s-%d", name.c_str(), height);

// Look up the name in our font map
FontMap::iterator font = _fonts.find(indexName);
if (font == _fonts.end()) {
// If not found, load and add that font

std::pair<FontMap::iterator, bool> result;

ManagedFont *t = createFont(format, name, height);

result = _fonts.insert(std::make_pair(indexName, t));

font = result.first;
}

// Increase the reference count and return the font
font->second->referenceCount++;

return FontHandle(font);
}

void FontManager::release(FontHandle &handle) {
Common::StackLock lock(_mutex);

if (handle.empty)
return;

if (--handle.it->second->referenceCount == 0) {
delete handle.it->second;
_fonts.erase(handle.it);
}

handle.clear();
}

ManagedFont *FontManager::createFont(FontFormat format,
const Common::UString &name, int height) {

if (format == kFontFormatUnknown)
throw Common::Exception("Font format unknown (%s)", name.c_str());

if (format == kFontFormatTexture)
return new ManagedFont(new TextureFont(name));

throw Common::Exception("Invalid font format %d (%s)", format, name.c_str());
return 0;
}

} // End of namespace Aurora

} // End of namespace Graphics
126 changes: 126 additions & 0 deletions src/graphics/aurora/fontman.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/* xoreos - A reimplementation of BioWare's Aurora engine
*
* xoreos is the legal property of its developers, whose names can be
* found in 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 3
* 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.
*
*
* The Infinity, Aurora, Odyssey, Eclipse and Lycium engines, Copyright (c) BioWare corp.
* The Electron engine, Copyright (c) Obsidian Entertainment and BioWare corp.
*/

/** @file graphics/aurora/fontman.h
* The Aurora font manager.
*/

#ifndef GRAPHICS_AURORA_FONTMAN_H
#define GRAPHICS_AURORA_FONTMAN_H

#include <map>

#include "graphics/types.h"

#include "common/types.h"
#include "common/singleton.h"
#include "common/mutex.h"
#include "common/ustring.h"

namespace Graphics {

class Font;

namespace Aurora {

/** Identifier used for the monospaced system font. */
extern const char *kSystemFontMono;

/** The format of a font. */
enum FontFormat {
kFontFormatUnknown = 0, ///< Unknown font format.
kFontFormatTexture , ///< Textured font, used by NWN and KotOR/KotOR2
kFontFormatABC , ///< ABC/SBM font, used by Jade Empire.
kFontFormatTTF ///< TTF font, used by NWN2.
};

/** A managed font, storing how often it's referenced. */
struct ManagedFont {
Font *font;
uint32 referenceCount;

ManagedFont(Font *f);
~ManagedFont();
};

typedef std::map<Common::UString, ManagedFont *> FontMap;

/** A handle to a font. */
struct FontHandle {
bool empty;
FontMap::iterator it;

FontHandle();
FontHandle(FontMap::iterator &i);
FontHandle(const FontHandle &right);
~FontHandle();

FontHandle &operator=(const FontHandle &right);

void clear();

const Common::UString &getFontName() const;
Font &getFont() const;
};

/** The global Aurora font manager. */
class FontManager : public Common::Singleton<FontManager> {
public:
FontManager();
~FontManager();

void clear();

void setFormat(FontFormat format);

/** Add an alias for a specific font name. */
void addAlias(const Common::UString &alias, const Common::UString &realName);

FontHandle get(FontFormat format, Common::UString name, int height = 0);
FontHandle get(Common::UString name, int height = 0);

void release(FontHandle &handle);

private:
FontFormat _format;

std::map<Common::UString, Common::UString> _aliases;

FontMap _fonts;

Common::Mutex _mutex;

static ManagedFont *createFont(FontFormat format,
const Common::UString &name, int height);
};

} // End of namespace Aurora

} // End of namespace Graphics

/** Shortcut for accessing the font manager. */
#define FontMan Graphics::Aurora::FontManager::instance()

#endif // GRAPHICS_AURORA_FONTMAN_H
2 changes: 2 additions & 0 deletions src/graphics/aurora/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ void Model::setSelectable(bool selectable) {

for (EntityList::iterator e = _entities.begin(); e != _entities.end(); ++e)
(*e)->setQueryFlags(selectable ? kSelectableModel : kSelectableNone);

Renderable::setSelectable(selectable);
}

void Model::showBoundingBox(bool show) {
Expand Down

0 comments on commit c22e555

Please sign in to comment.