reorganized TextObject code to easier bitmap recreation

This commit is contained in:
Pawel Kolodziejski 2005-03-20 13:51:40 +00:00
parent 54185ac340
commit 51b5e54e34
8 changed files with 125 additions and 100 deletions

View file

@ -74,6 +74,7 @@ public:
virtual void loadEmergFont() = 0;
virtual TextObjectHandle *prepareToTextBitmap(uint8 *bitmap, int width, int height, const Color &fgColor) = 0;
virtual void drawTextBitmap(int x, int y, TextObjectHandle *handle) = 0;
virtual void destroyTextBitmap(TextObjectHandle *handle) = 0;
virtual void prepareSmushFrame(int width, int height, byte *bitmap) = 0;
virtual void drawSmushFrame(int offsetX, int offsetY) = 0;

View file

@ -652,3 +652,8 @@ void DriverGL::drawTextBitmap(int x, int y, TextObjectHandle *handle) {
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
}
void DriverGL::destroyTextBitmap(TextObjectHandle *handle) {
glDeleteTextures(handle->numTex, (GLuint *)handle->texIds);
delete[] handle->texIds;
}

View file

@ -65,6 +65,7 @@ public:
void loadEmergFont();
TextObjectHandle *prepareToTextBitmap(uint8 *bitmap, int width, int height, const Color &fgColor);
void drawTextBitmap(int x, int y, TextObjectHandle *handle);
void destroyTextBitmap(TextObjectHandle *handle);
void prepareSmushFrame(int width, int height, byte *bitmap);
void drawSmushFrame(int offsetX, int offsetY);

View file

@ -441,3 +441,8 @@ void DriverTinyGL::drawTextBitmap(int x, int y, TextObjectHandle *handle) {
SDL_SetColorKey((SDL_Surface *)handle->surface, SDL_SRCCOLORKEY, 0xf81f);
SDL_BlitSurface((SDL_Surface *)handle->surface, &srcRect, _screen, &dstRect);
}
void DriverTinyGL::destroyTextBitmap(TextObjectHandle *handle) {
delete handle->bitmapData;
SDL_FreeSurface((SDL_Surface *)handle->surface);
}

View file

@ -68,6 +68,7 @@ public:
void loadEmergFont();
TextObjectHandle *prepareToTextBitmap(uint8 *bitmap, int width, int height, const Color &fgColor);
void drawTextBitmap(int x, int y, TextObjectHandle *handle);
void destroyTextBitmap(TextObjectHandle *handle);
void prepareSmushFrame(int width, int height, byte *bitmap);
void drawSmushFrame(int offsetX, int offsetY);

108
lua.cpp
View file

@ -1160,8 +1160,7 @@ void GetControlState() {
}
}
void getTextObjectParams(lua_Object table_obj, Font **font, int &x, int &y, int &width,
int &height, Color **fgColor, bool &center, bool &ljustify) {
void getTextObjectParams(TextObject *textObject, lua_Object table_obj) {
char *key_text = NULL;
lua_Object key;
@ -1179,21 +1178,21 @@ void getTextObjectParams(lua_Object table_obj, Font **font, int &x, int &y, int
key_text = lua_getstring(key);
if (strstr(key_text, "x"))
x = atoi(lua_getstring(lua_getresult(2)));
textObject->setX(atoi(lua_getstring(lua_getresult(2))));
else if (strstr(key_text, "y"))
y = atoi(lua_getstring(lua_getresult(2)));
textObject->setY(atoi(lua_getstring(lua_getresult(2))));
else if (strstr(key_text, "width"))
width = atoi(lua_getstring(lua_getresult(2)));
textObject->setWidth(atoi(lua_getstring(lua_getresult(2))));
else if (strstr(key_text, "height"))
height = atoi(lua_getstring(lua_getresult(2)));
textObject->setHeight(atoi(lua_getstring(lua_getresult(2))));
else if (strstr(key_text, "font"))
*font = check_font(2);
textObject->setFont(check_font(2));
else if (strstr(key_text, "fgcolor"))
*fgColor = check_color(2);
textObject->setFGColor(check_color(2));
else if (strstr(key_text, "center"))
center = !lua_isnil(lua_getresult(2));
textObject->setJustify(1);
else if (strstr(key_text, "ljustify"))
ljustify = !lua_isnil(lua_getresult(2));
textObject->setJustify(2);
else
error("Unknown getTextObjectParams key %s\n", key_text);
}
@ -1206,23 +1205,27 @@ static void MakeTextObject() {
Font *font = NULL;
char *line = lua_getstring(lua_getparam(1));
lua_Object table_obj = lua_getparam(2);
if (lua_istable(table_obj))
getTextObjectParams(table_obj, &font, x, y, width, height, &fgColor, center, ljustify);
lua_Object tableObj = lua_getparam(2);
// Note: The debug func display_setup_name in _sets.LUA creates an empty TextObject,
// and fills it with ChangeTextObject
TextObject *textObject = new TextObject();
textObject->setDefaultsTextObjectParams();
if (lua_istable(tableObj))
getTextObjectParams(textObject, tableObj);
textObject->setText(line);
textObject->createBitmap();
TextObject *textObject = new TextObject((const char *)line, x, y, font, *fgColor);
lua_pushusertag(textObject, MKID('TEXT'));
lua_pushnumber(textObject->getBitmapWidth());
lua_pushnumber(textObject->getBitmapHeight());
}
static void KillTextObject() {
TextObject *textObjectParm;
if (lua_isnil(lua_getparam(1))) { // FIXME: check this.. nil is kill all lines?
if (lua_isnil(lua_getparam(1))) {
error("KillTextObject(NULL)");
//g_engine->killTextObjects();
return;
}
@ -1239,77 +1242,38 @@ static void KillTextObject() {
}
}
// Called from both Callback and Main CTO functions. This routine is NOT
// thread safe.
static void ChangeTextObject_Real(char *keyName, void *data) {
static TextObject *modifyObject = NULL; // Set by main CTO call 'object'
lua_Object *keyValue = NULL;
if (strstr(keyName, "object")) {
modifyObject = (TextObject*)data;
return;
}
if (!modifyObject) // We *need* a modify object for remaining calls
return;
keyValue = (lua_Object*)data;
// FIXME: X/Y sets depend on GetTextObjectDimensions
if (strstr(keyName, "fgcolor")) {
modifyObject->setColor(check_color(2));
} else if (strstr(keyName, "x")) {
//modifyObject->setX(atoi(lua_getstring(keyValue)));
} else if (strstr(keyName, "y")) {
//modifyObject->setY(atoi(lua_getstring(keyValue)));
} else {
printf("ChangeTextObject() - Unknown key %s\n", keyName);
}
}
// Callback from table walk method in Main CTO function
static void ChangeTextObject_CB(void) {
char *keyName = NULL;
lua_Object keyValue;
keyName = lua_getstring(lua_getparam(1));
keyValue = lua_getresult(2);
ChangeTextObject_Real(keyName, &keyValue);
};
// Main CTO handler and LUA interface
static void ChangeTextObject() {
return;
char *textID = lua_getstring(lua_getparam(1));
TextObject *textObject = check_textobject(1);
lua_Object tableObj = lua_getparam(2);
TextObject *modifyObject = NULL;
TextObject *modifyObject = NULL;
for (Engine::TextListType::const_iterator i = g_engine->textsBegin(); i != g_engine->textsEnd(); i++) {
TextObject *textO = *i;
if (strstr(textO->name(), textID)) {
if (strstr(textO->name(), textObject->name())) {
modifyObject = textO;
break;
}
}
if (!modifyObject)
error("ChangeTextObject(%s): Cannot find active text object", textID);
error("ChangeTextObject(): Cannot find active text object");
if (!lua_istable(tableObj))
return;
modifyObject->destroyBitmap();
ChangeTextObject_Real("object", modifyObject);
lua_pushobject(tableObj);
lua_pushcfunction(ChangeTextObject_CB); // Callback handler
lua_call("foreach");
if (lua_istable(tableObj))
getTextObjectParams(modifyObject, tableObj);
modifyObject->createBitmap();
lua_pushnumber(modifyObject->getBitmapWidth());
lua_pushnumber(modifyObject->getBitmapHeight());
}
static void GetTextObjectDimensions() {
warning("STUB: GetTextObjectDimensions()");
lua_pushnumber(100); // Dummy X
lua_pushnumber(100); // Dummy Y
TextObject *textObjectParam = check_textobject(1);
lua_pushnumber(textObjectParam->getBitmapWidth());
lua_pushnumber(textObjectParam->getBitmapHeight());
}
static void SetSpeechMode() {

View file

@ -23,20 +23,48 @@
std::string parseMsgText(const char *msg, char *msgId);
TextObject::TextObject(const char *text, const int x, const int y, /*const*/ Font *font, const Color& fgColor) :
_fgColor(fgColor), _x(x), _y(y) {
TextObject::TextObject() :
_created(false), _x(0), _y(0), _width(0), _height(0), _textBitmap(NULL),
_bitmapWidth(0), _bitmapHeight(0), _textObjectHandle(NULL), _justify(0),
_font(NULL), _text(NULL) {
memset(_textID, 0, 10);
_fgColor._vals[0] = 0;
_fgColor._vals[1] = 0;
_fgColor._vals[2] = 0;
}
strcpy(_textID, text);
TextObject::~TextObject() {
destroyBitmap();
}
void TextObject::setDefaultsTextObjectParams() {
_x = 0;
_y = 0;
_width = 0;
_height = 0;
_font = NULL;
_fgColor._vals[0] = 0;
_fgColor._vals[1] = 0;
_fgColor._vals[2] = 0;
_justify = 0;
_text = NULL;
}
void TextObject::createBitmap() {
if (_created)
destroyBitmap();
strcpy(_textID, _text);
char msgId[32];
std::string msg = parseMsgText(_textID, msgId);
// Calculate bitmap dimensions
_bitmapHeight = _bitmapWidth = 0;
_bitmapWidth = 0;
_bitmapHeight = 0;
for (int i = 0; msg[i] != '\0'; ++i) {
_bitmapWidth += font->getCharLogicalWidth(msg[i]) + font->getCharStartingCol(msg[i]);
_bitmapWidth += _font->getCharLogicalWidth(msg[i]) + _font->getCharStartingCol(msg[i]);
int h = font->getCharHeight(msg[i]) + font->getCharStartingLine(msg[i]);
int h = _font->getCharHeight(msg[i]) + _font->getCharStartingLine(msg[i]);
if (h > _bitmapHeight)
_bitmapHeight = h;
}
@ -50,14 +78,14 @@ TextObject::TextObject(const char *text, const int x, const int y, /*const*/ Fon
int offset = 0;
for (int line = 0; line < _bitmapHeight; ++line) {
for (int c = 0; msg[c] != '\0'; ++c) {
uint32 charWidth = font->getCharWidth(msg[c]);
uint32 charLogicalWidth = font->getCharLogicalWidth(msg[c]);
uint8 startingCol = font->getCharStartingCol(msg[c]);
uint8 startingLine = font->getCharStartingLine(msg[c]);
uint32 charWidth = _font->getCharWidth(msg[c]);
uint32 charLogicalWidth = _font->getCharLogicalWidth(msg[c]);
uint8 startingCol = _font->getCharStartingCol(msg[c]);
uint8 startingLine = _font->getCharStartingLine(msg[c]);
if (startingLine < line + 1 && font->getCharHeight(msg[c]) + startingLine > line) {
if (startingLine < line + 1 && _font->getCharHeight(msg[c]) + startingLine > line) {
memcpy(_textBitmap + offset + startingCol,
font->getCharData(msg[c]) + charWidth * (line - startingLine), charWidth);
_font->getCharData(msg[c]) + charWidth * (line - startingLine), charWidth);
}
offset += charLogicalWidth + startingCol;
@ -66,19 +94,18 @@ TextObject::TextObject(const char *text, const int x, const int y, /*const*/ Fon
_textObjectHandle = g_driver->prepareToTextBitmap(_textBitmap, _bitmapWidth, _bitmapHeight, _fgColor);
_created = true;
g_engine->registerTextObject(this);
}
TextObject::~TextObject() {
delete[] _textBitmap;
if (_textObjectHandle->bitmapData)
delete _textObjectHandle->bitmapData;
if (_textObjectHandle->surface)
SDL_FreeSurface((SDL_Surface *)_textObjectHandle->surface);
if (_textObjectHandle->texIds)
delete _textObjectHandle->texIds;
delete _textObjectHandle;
void TextObject::destroyBitmap() {
if (_textObjectHandle) {
delete[] _textBitmap;
g_driver->destroyTextBitmap(_textObjectHandle);
delete _textObjectHandle;
_textObjectHandle = NULL;
}
_created = false;
}
void TextObject::draw() {

View file

@ -28,19 +28,40 @@
class TextObject {
public:
TextObject(const char *text, const int x, const int y, /*const*/ Font *font, const Color& fgColor);
TextObject();
~TextObject();
void setX(int x) {_x = x; }
void setY(int y) {_y = y; }
void setColor(Color *newColor) { _fgColor = newColor; }
void createBitmap();
void destroyBitmap();
void setDefaultsTextObjectParams();
void setText(char *text) { _text = text; }
void setX(int x) { _x = x; }
void setY(int y) { _y = y; }
void setWidth(int width) { _width = width; }
void setHeight(int height) { _height = height; }
void setFGColor(Color *fgColor) { _fgColor = fgColor; }
void setFont(Font *font) { _font = font; }
void setJustify(int justify) { _justify = justify; }
int getBitmapWidth() { return _bitmapWidth; }
int getBitmapHeight() { return _bitmapHeight; }
const char *name() const { return _textID; }
void draw();
enum Justify {
NONE,
CENTER,
LJUSTIFY
};
protected:
bool _created;
Color _fgColor;
int _x, _y;
char _textID[10];
int _width, _height;
int _justify;
Font *_font;
char *_text;
char _textID[32];
uint8 *_textBitmap;
int _bitmapHeight, _bitmapWidth;
Driver::TextObjectHandle *_textObjectHandle;