Made fonts be rendered by opengl.

This commit is contained in:
Joel Teichroeb 2011-05-22 20:43:28 -07:00
parent 1530048c74
commit 5fb97a647b
7 changed files with 165 additions and 14 deletions

View File

@ -78,6 +78,8 @@ Font::Font(const Common::String &filename, const char *data, int len) : Object()
error("Could not load font %s. Out of memory", _filename.c_str());
memcpy(_fontData, data, _dataSize);
g_driver->createFont(this);
}
Font::Font() :
@ -126,6 +128,14 @@ uint16 Font::getCharIndex(unsigned char c) {
return 0;
}
int Font::getStringLength(const Common::String &text) {
int result = 0;
for (uint32 i = 0; i < text.size(); ++i) {
result += getCharWidth(text[i]);
}
return result;
}
// Hardcoded default font for GUI, etc
const uint8 Font::emerFont[][13] = {
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},

View File

@ -45,8 +45,10 @@ public:
int32 getCharStartingLine(unsigned char c) { return _charHeaders[getCharIndex(c)].startingLine; }
const byte *getCharData(unsigned char c) { return _fontData + (_charHeaders[getCharIndex(c)].offset); }
int getStringLength(const Common::String &text);
static const uint8 emerFont[][13];
private:
//private:
uint16 getCharIndex(unsigned char c);
struct CharHeader {
@ -66,6 +68,8 @@ private:
CharHeader *_charHeaders;
byte *_fontData;
Common::String _filename;
void *_texIds;
void *_sizes;
};
} // end of namespace Grim

View File

@ -32,13 +32,15 @@ struct Shadow;
class SaveGame;
class BitmapData;
class PrimitiveObject;
class Font;
class TextObject;
enum colorFormat {
BM_RGB565 = 1, // Grim Fandango
BM_RGB1555 = 2, // EMI-PS2
BM_RGBA = 3 // EMI-PC
};
class GfxBase {
public:
GfxBase() { ; }
@ -93,6 +95,9 @@ public:
virtual void drawBitmap(const Bitmap *bitmap) = 0;
virtual void destroyBitmap(BitmapData *bitmap) = 0;
virtual void createFont(Font *font) { }
virtual void drawText(int x, int y, const Common::String &text, Font *font, Color &color) { }
virtual Bitmap *getScreenshot(int w, int h) = 0;
virtual void storeDisplay() = 0;
virtual void copyStoredToDisplay() = 0;

View File

@ -769,6 +769,131 @@ void GfxOpenGL::destroyBitmap(BitmapData *bitmap) {
}
}
void GfxOpenGL::createFont(Font *font) {
byte *bitmapData = font->_fontData;
byte *texDataPtr = new byte[font->_dataSize *4];
byte *data = texDataPtr;
for (uint32 i = 0; i < font->_dataSize; i++, texDataPtr += 4, bitmapData++) {
byte pixel = *bitmapData;
if (pixel == 0x00) {
texDataPtr[0] = 0;
texDataPtr[1] = 0;
texDataPtr[2] = 0;
texDataPtr[3] = 0;
} else if (pixel == 0x80) {
texDataPtr[0] = 0;
texDataPtr[1] = 0;
texDataPtr[2] = 0;
texDataPtr[3] = 255;
} else if (pixel == 0xFF) {
texDataPtr[0] = 255;
texDataPtr[1] = 255;
texDataPtr[2] = 255;
texDataPtr[3] = 255;
}
}
byte *temp = new byte[64*64*4];
font->_texIds = new GLuint[256];
font->_sizes = new uint8[256];
GLuint *textures = (GLuint *)font->_texIds;
glGenTextures(256, textures);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
for (int i = 0; i < 256; ++i) {
//
uint8 size = 64;
int width = font->getCharDataWidth(i), height = font->getCharDataHeight(i);
int m = max(width, height);
if (m < 8)
size = 8;
if (m < 16)
size = 16;
else if (m < 32)
size = 32;
else if (m > 64)
error("too big");
int d = ((int)font->getCharData(i) - (int)font->getCharData(0));
// need to remove this
memset(temp, 0, 64*64*4);
for (int x = 0; x < height; ++x) {
int pos = x * size * 4;
memcpy(temp + pos, data + d * 4 + x * width * 4, width * 4);
}
glPixelStorei(GL_UNPACK_ROW_LENGTH, size);
glBindTexture(GL_TEXTURE_2D, textures[i]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size, size, 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, temp);
((uint8 *)font->_sizes)[i] = size;
}
glPixelStorei(GL_UNPACK_ROW_LENGTH, 0);
delete[] data;
}
void GfxOpenGL::drawText(int x, int y, const Common::String &text, Font *font, Color &color) {
if (text.size() == 0)
return;
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, _screenWidth, _screenHeight, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
// A lot more may need to be put there : disabling Alpha test, blending, ...
// For now, just keep this here :-)
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glColor3f(color.getRed()/255.f, color.getGreen()/255.f, color.getBlue()/255.f);
for (uint i = 0; i < text.size(); ++i) {
int w = y + font->getCharStartingLine(text[i]) + font->getBaseOffsetY();
uint8 size = ((uint8 *)font->_sizes)[text[i]];
GLuint *textures = (GLuint *)font->_texIds;
glBindTexture(GL_TEXTURE_2D, textures[text[i]]);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2i(x, w);
glTexCoord2f(1.0f, 0.0f);
glVertex2i(x + size, w);
glTexCoord2f(1.0f, 1.0f);
glVertex2i(x + size, w + size);
glTexCoord2f(0.0f, 1.0f);
glVertex2i(x, w + size);
glEnd();
x += font->getCharWidth(text[i]);
}
glColor3f(1, 1, 1);
glDisable(GL_SCISSOR_TEST);
glDisable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
}
void GfxOpenGL::createMaterial(Material *material, const char *data, const CMap *cmap) {
material->_textures = new GLuint[material->_numImages];
glGenTextures(material->_numImages, (GLuint *)material->_textures);

View File

@ -85,6 +85,9 @@ public:
void drawBitmap(const Bitmap *bitmap);
void destroyBitmap(BitmapData *bitmap);
void createFont(Font *font);
void drawText(int x, int y, const Common::String &text, Font *font, Color &color);
Bitmap *getScreenshot(int w, int h);
void storeDisplay();
void copyStoredToDisplay();

View File

@ -79,8 +79,8 @@ void L1_ChangeTextObject() {
lua_getstring(paramObj);
}
textObject->destroyBitmap();
textObject->createBitmap();
//textObject->destroyBitmap();
//textObject->createBitmap();
lua_pushnumber(textObject->getBitmapWidth());
lua_pushnumber(textObject->getBitmapHeight());
@ -121,8 +121,8 @@ void L1_MakeTextObject() {
setTextObjectParams(textObject, tableObj);
textObject->setText(text.c_str());
if (!(g_grim->getGameFlags() & ADGF_DEMO))
textObject->createBitmap();
//if (!(g_grim->getGameFlags() & ADGF_DEMO))
//textObject->createBitmap();
g_grim->registerTextObject(textObject);
lua_pushusertag(textObject->getId(), MKTAG('T', 'E', 'X', 'T'));
@ -177,7 +177,7 @@ void L1_BlastText() {
setTextObjectParams(textObject, tableObj);
textObject->setText(text.c_str());
textObject->createBitmap();
//textObject->createBitmap();
textObject->draw();
delete textObject;
}

View File

@ -133,7 +133,7 @@ void TextObject::setDefaults(TextObjectDefaults *defaults) {
}
int TextObject::getBitmapWidth() {
if (!_bitmapWidthPtr)
/*if (!_bitmapWidthPtr)
return 0;
int width = 0;
@ -142,7 +142,9 @@ int TextObject::getBitmapWidth() {
if (_bitmapWidthPtr[i] > width)
width = _bitmapWidthPtr[i];
}
return width;
return width;*/
Common::String msg = parseMsgText(_textID, NULL);
return _font->getStringLength(msg);
}
int TextObject::getBitmapHeight() {
@ -341,7 +343,7 @@ void TextObject::destroyBitmap() {
void TextObject::draw() {
int height = 0;
if (!_created || _disabled)
if (_disabled)
return;
// render multi-line (wrapped) text
for (int i = 0; i < _numberLines; i++) {
@ -365,20 +367,22 @@ void TextObject::draw() {
if (y < 0)
y = 0;
Common::String msg = parseMsgText(_textID, NULL);
if (_justify == LJUSTIFY || _justify == NONE)
g_driver->drawTextBitmap(_x, height + y, _textObjectHandle[i]);
g_driver->drawText(_x, height + y, msg, _font, *_fgColor);
else if (_justify == CENTER) {
int x = _x - (_bitmapWidthPtr[i] / 2);
int x = _x - (getBitmapWidth() / 2);
if (x < 0)
x = 0;
g_driver->drawTextBitmap(x, height + y, _textObjectHandle[i]);
g_driver->drawText(x, height + y, msg, _font, *_fgColor);
} else if (_justify == RJUSTIFY) {
int x = (_x - getBitmapWidth());
if (x < 0)
x = 0;
g_driver->drawTextBitmap(x, height + y, _textObjectHandle[i]);
g_driver->drawText(x, height + y, msg, _font, *_fgColor);
} else if (gDebugLevel == DEBUG_WARN || gDebugLevel == DEBUG_ALL)
warning("TextObject::draw: Unknown justification code (%d)", _justify);