Added a Font _font variable to the engine instance. Fixed font colour handling by replacing the appropriate colours before drawing. Added Font::setColour() method for changing the current font colour. Added include guards to draci/font.h. Moved kFontBig and kFontSmall constants to draci/font.cpp to prevent redefinition errors.

svn-id: r41524
This commit is contained in:
Denis Kasak 2009-06-14 18:59:31 +00:00
parent 149b45f7a5
commit 718f84fb97
4 changed files with 88 additions and 12 deletions

View File

@ -49,7 +49,7 @@ DraciEngine::DraciEngine(OSystem *syst, const ADGameDescription *gameDesc)
// However this is the place to specify all default directories
//Common::File::addDefaultDirectory(_gameDataPath + "sound/");
// Here is the right place to set up the engine specific debug levels
Common::addDebugChannel(kDraciGeneralDebugLevel, "general", "Draci general debug level");
Common::addDebugChannel(kDraciBytecodeDebugLevel, "bytecode", "GPL bytecode instructions");
@ -63,6 +63,9 @@ int DraciEngine::init() {
// Initialize graphics using following:
initGraphics(320, 200, false);
// Load default font
_font.setFont(kFontBig);
// Basic archive test
debugC(2, kDraciGeneralDebugLevel, "Running archive tests...");
Common::String path("INIT.DFW");
@ -132,21 +135,20 @@ int DraciEngine::go() {
_system->fillScreen(255);
// Draw big string
Font font(kFontBig);
Common::String testString = "Testing, testing, read all about it!";
Graphics::Surface *surf = _system->lockScreen();
font.drawString(surf, testString,
(320 - font.getStringWidth(testString, 1)) / 2, 130, 1);
_font.drawString(surf, testString,
(320 - _font.getStringWidth(testString, 1)) / 2, 130, 1);
// Draw small string
font.setFont(kFontSmall);
_font.setFont(kFontSmall);
testString = "I'm smaller than the font above me.";
font.drawString(surf, testString,
(320 - font.getStringWidth(testString, 1)) / 2, 150, 1);
_font.drawString(surf, testString,
(320 - _font.getStringWidth(testString, 1)) / 2, 150, 1);
// Overflow handling test
testString = "Checking overflooooooooooooooooooooooooow...";
font.drawString(surf, testString, 50, 170, 1);
_font.drawString(surf, testString, 50, 170, 1);
_system->unlockScreen();
_system->updateScreen();

View File

@ -30,6 +30,8 @@
#include "engines/engine.h"
#include "engines/advancedDetector.h"
#include "draci/font.h"
namespace Draci {
class DraciEngine : public Engine {
@ -43,6 +45,8 @@ public:
bool hasFeature(Engine::EngineFeature f) const;
Font _font;
private:
Common::RandomSource _rnd;
};

View File

@ -30,16 +30,40 @@
namespace Draci {
const Common::String kFontSmall("Small.fon");
const Common::String kFontBig("Big.fon");
Font::Font() :
_fontHeight(0), _maxCharWidth(0),
_charWidths(NULL), _charData(0) {
setFont(kFontBig);
_currentFontColour = _fontColour1;
}
Font::Font(const Common::String &filename) :
_fontHeight(0), _maxCharWidth(0),
_charWidths(NULL), _charData(0) {
setFont(filename);
_currentFontColour = _fontColour1;
}
Font::~Font() {
freeFont();
}
/**
* @brief Sets the varying font colour
* @param colour The new font colour
*/
void Font::setColour(uint8 colour) {
_currentFontColour = colour;
}
/**
* @brief Loads fonts from a file
* @param path Path to font file
@ -135,9 +159,31 @@ void Font::drawChar(Graphics::Surface *dst, uint8 chr, int tx, int ty) const {
for (int y = 0; y < yPixelsToDraw; ++y) {
for (int x = 0; x <= xPixelsToDraw; ++x) {
// Paint pixel
int curr = y * _maxCharWidth + x;
ptr[x] = _charData[charOffset + curr];
int colour = _charData[charOffset + curr];
// Replace colour with font colours
switch (colour) {
case 254:
colour = _currentFontColour;
break;
case 253:
colour = _fontColour2;
break;
case 252:
colour = _fontColour3;
break;
case 251:
colour = _fontColour4;
break;
}
// Paint pixel
ptr[x] = colour;
}
// Advance to next row

View File

@ -23,12 +23,15 @@
*
*/
#ifndef FONT_H
#define FONT_H
#include "graphics/font.h"
namespace Draci {
const Common::String kFontSmall("Small.fon");
const Common::String kFontBig("Big.fon");
extern const Common::String kFontSmall;
extern const Common::String kFontBig;
/**
* Represents the game's fonts. See docs for setFont() for font format details.
@ -37,8 +40,11 @@ const Common::String kFontBig("Big.fon");
class Font {
public:
Font();
Font(const Common::String &filename);
~Font();
bool setFont(const Common::String &filename);
uint8 getFontHeight() const { return _fontHeight; };
uint8 getMaxCharWidth() const { return _maxCharWidth; };
@ -47,6 +53,7 @@ public:
void drawString(Graphics::Surface *dst, Common::String &str,
int x, int y, int spacing = 0) const;
int getStringWidth(Common::String &str, int spacing = 0) const;
void setColour(uint8 colour);
private:
uint8 _fontHeight;
@ -66,8 +73,25 @@ private:
*/
static const unsigned int kCharIndexOffset = 32;
/** Default font colours. They all seem to remain constant except for the
* first one which varies depending on the character speaking.
* _overFontColour is set to transparent.
* TODO: Find out what _fontColour1 should actually be when the game starts
*/
static const uint8 _fontColour1 = 2;
static const uint8 _fontColour2 = 0;
static const uint8 _fontColour3 = 3;
static const uint8 _fontColour4 = 4;
static const uint8 _overFontColour = 255;
/** The varying font colour; initially set to _fontColour1 */
uint8 _currentFontColour;
/** Internal function for freeing fonts when destructing/loading another */
void freeFont();
};
} // End of namespace Draci
#endif // FONT_H