reorganized TextObject code to easier bitmap recreation
This commit is contained in:
parent
54185ac340
commit
51b5e54e34
8 changed files with 125 additions and 100 deletions
1
driver.h
1
driver.h
|
@ -74,6 +74,7 @@ public:
|
||||||
virtual void loadEmergFont() = 0;
|
virtual void loadEmergFont() = 0;
|
||||||
virtual TextObjectHandle *prepareToTextBitmap(uint8 *bitmap, int width, int height, const Color &fgColor) = 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 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 prepareSmushFrame(int width, int height, byte *bitmap) = 0;
|
||||||
virtual void drawSmushFrame(int offsetX, int offsetY) = 0;
|
virtual void drawSmushFrame(int offsetX, int offsetY) = 0;
|
||||||
|
|
|
@ -652,3 +652,8 @@ void DriverGL::drawTextBitmap(int x, int y, TextObjectHandle *handle) {
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glEnable(GL_LIGHTING);
|
glEnable(GL_LIGHTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DriverGL::destroyTextBitmap(TextObjectHandle *handle) {
|
||||||
|
glDeleteTextures(handle->numTex, (GLuint *)handle->texIds);
|
||||||
|
delete[] handle->texIds;
|
||||||
|
}
|
||||||
|
|
|
@ -65,6 +65,7 @@ public:
|
||||||
void loadEmergFont();
|
void loadEmergFont();
|
||||||
TextObjectHandle *prepareToTextBitmap(uint8 *bitmap, int width, int height, const Color &fgColor);
|
TextObjectHandle *prepareToTextBitmap(uint8 *bitmap, int width, int height, const Color &fgColor);
|
||||||
void drawTextBitmap(int x, int y, TextObjectHandle *handle);
|
void drawTextBitmap(int x, int y, TextObjectHandle *handle);
|
||||||
|
void destroyTextBitmap(TextObjectHandle *handle);
|
||||||
|
|
||||||
void prepareSmushFrame(int width, int height, byte *bitmap);
|
void prepareSmushFrame(int width, int height, byte *bitmap);
|
||||||
void drawSmushFrame(int offsetX, int offsetY);
|
void drawSmushFrame(int offsetX, int offsetY);
|
||||||
|
|
|
@ -441,3 +441,8 @@ void DriverTinyGL::drawTextBitmap(int x, int y, TextObjectHandle *handle) {
|
||||||
SDL_SetColorKey((SDL_Surface *)handle->surface, SDL_SRCCOLORKEY, 0xf81f);
|
SDL_SetColorKey((SDL_Surface *)handle->surface, SDL_SRCCOLORKEY, 0xf81f);
|
||||||
SDL_BlitSurface((SDL_Surface *)handle->surface, &srcRect, _screen, &dstRect);
|
SDL_BlitSurface((SDL_Surface *)handle->surface, &srcRect, _screen, &dstRect);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DriverTinyGL::destroyTextBitmap(TextObjectHandle *handle) {
|
||||||
|
delete handle->bitmapData;
|
||||||
|
SDL_FreeSurface((SDL_Surface *)handle->surface);
|
||||||
|
}
|
||||||
|
|
|
@ -68,6 +68,7 @@ public:
|
||||||
void loadEmergFont();
|
void loadEmergFont();
|
||||||
TextObjectHandle *prepareToTextBitmap(uint8 *bitmap, int width, int height, const Color &fgColor);
|
TextObjectHandle *prepareToTextBitmap(uint8 *bitmap, int width, int height, const Color &fgColor);
|
||||||
void drawTextBitmap(int x, int y, TextObjectHandle *handle);
|
void drawTextBitmap(int x, int y, TextObjectHandle *handle);
|
||||||
|
void destroyTextBitmap(TextObjectHandle *handle);
|
||||||
|
|
||||||
void prepareSmushFrame(int width, int height, byte *bitmap);
|
void prepareSmushFrame(int width, int height, byte *bitmap);
|
||||||
void drawSmushFrame(int offsetX, int offsetY);
|
void drawSmushFrame(int offsetX, int offsetY);
|
||||||
|
|
108
lua.cpp
108
lua.cpp
|
@ -1160,8 +1160,7 @@ void GetControlState() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void getTextObjectParams(lua_Object table_obj, Font **font, int &x, int &y, int &width,
|
void getTextObjectParams(TextObject *textObject, lua_Object table_obj) {
|
||||||
int &height, Color **fgColor, bool ¢er, bool &ljustify) {
|
|
||||||
char *key_text = NULL;
|
char *key_text = NULL;
|
||||||
lua_Object key;
|
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);
|
key_text = lua_getstring(key);
|
||||||
if (strstr(key_text, "x"))
|
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"))
|
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"))
|
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"))
|
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"))
|
else if (strstr(key_text, "font"))
|
||||||
*font = check_font(2);
|
textObject->setFont(check_font(2));
|
||||||
else if (strstr(key_text, "fgcolor"))
|
else if (strstr(key_text, "fgcolor"))
|
||||||
*fgColor = check_color(2);
|
textObject->setFGColor(check_color(2));
|
||||||
else if (strstr(key_text, "center"))
|
else if (strstr(key_text, "center"))
|
||||||
center = !lua_isnil(lua_getresult(2));
|
textObject->setJustify(1);
|
||||||
else if (strstr(key_text, "ljustify"))
|
else if (strstr(key_text, "ljustify"))
|
||||||
ljustify = !lua_isnil(lua_getresult(2));
|
textObject->setJustify(2);
|
||||||
else
|
else
|
||||||
error("Unknown getTextObjectParams key %s\n", key_text);
|
error("Unknown getTextObjectParams key %s\n", key_text);
|
||||||
}
|
}
|
||||||
|
@ -1206,23 +1205,27 @@ static void MakeTextObject() {
|
||||||
Font *font = NULL;
|
Font *font = NULL;
|
||||||
|
|
||||||
char *line = lua_getstring(lua_getparam(1));
|
char *line = lua_getstring(lua_getparam(1));
|
||||||
lua_Object table_obj = lua_getparam(2);
|
lua_Object tableObj = lua_getparam(2);
|
||||||
if (lua_istable(table_obj))
|
|
||||||
getTextObjectParams(table_obj, &font, x, y, width, height, &fgColor, center, ljustify);
|
|
||||||
|
|
||||||
// Note: The debug func display_setup_name in _sets.LUA creates an empty TextObject,
|
TextObject *textObject = new TextObject();
|
||||||
// and fills it with ChangeTextObject
|
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_pushusertag(textObject, MKID('TEXT'));
|
||||||
|
lua_pushnumber(textObject->getBitmapWidth());
|
||||||
|
lua_pushnumber(textObject->getBitmapHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void KillTextObject() {
|
static void KillTextObject() {
|
||||||
TextObject *textObjectParm;
|
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)");
|
error("KillTextObject(NULL)");
|
||||||
//g_engine->killTextObjects();
|
|
||||||
return;
|
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() {
|
static void ChangeTextObject() {
|
||||||
return;
|
TextObject *textObject = check_textobject(1);
|
||||||
char *textID = lua_getstring(lua_getparam(1));
|
|
||||||
lua_Object tableObj = lua_getparam(2);
|
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++) {
|
for (Engine::TextListType::const_iterator i = g_engine->textsBegin(); i != g_engine->textsEnd(); i++) {
|
||||||
TextObject *textO = *i;
|
TextObject *textO = *i;
|
||||||
|
|
||||||
if (strstr(textO->name(), textID)) {
|
if (strstr(textO->name(), textObject->name())) {
|
||||||
modifyObject = textO;
|
modifyObject = textO;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!modifyObject)
|
if (!modifyObject)
|
||||||
error("ChangeTextObject(%s): Cannot find active text object", textID);
|
error("ChangeTextObject(): Cannot find active text object");
|
||||||
|
|
||||||
if (!lua_istable(tableObj))
|
modifyObject->destroyBitmap();
|
||||||
return;
|
|
||||||
|
|
||||||
ChangeTextObject_Real("object", modifyObject);
|
if (lua_istable(tableObj))
|
||||||
lua_pushobject(tableObj);
|
getTextObjectParams(modifyObject, tableObj);
|
||||||
lua_pushcfunction(ChangeTextObject_CB); // Callback handler
|
|
||||||
lua_call("foreach");
|
modifyObject->createBitmap();
|
||||||
|
|
||||||
|
lua_pushnumber(modifyObject->getBitmapWidth());
|
||||||
|
lua_pushnumber(modifyObject->getBitmapHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void GetTextObjectDimensions() {
|
static void GetTextObjectDimensions() {
|
||||||
warning("STUB: GetTextObjectDimensions()");
|
TextObject *textObjectParam = check_textobject(1);
|
||||||
lua_pushnumber(100); // Dummy X
|
lua_pushnumber(textObjectParam->getBitmapWidth());
|
||||||
lua_pushnumber(100); // Dummy Y
|
lua_pushnumber(textObjectParam->getBitmapHeight());
|
||||||
}
|
}
|
||||||
|
|
||||||
static void SetSpeechMode() {
|
static void SetSpeechMode() {
|
||||||
|
|
|
@ -23,20 +23,48 @@
|
||||||
|
|
||||||
std::string parseMsgText(const char *msg, char *msgId);
|
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) :
|
TextObject::TextObject() :
|
||||||
_fgColor(fgColor), _x(x), _y(y) {
|
_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];
|
char msgId[32];
|
||||||
std::string msg = parseMsgText(_textID, msgId);
|
std::string msg = parseMsgText(_textID, msgId);
|
||||||
|
|
||||||
// Calculate bitmap dimensions
|
_bitmapWidth = 0;
|
||||||
_bitmapHeight = _bitmapWidth = 0;
|
_bitmapHeight = 0;
|
||||||
|
|
||||||
for (int i = 0; msg[i] != '\0'; ++i) {
|
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)
|
if (h > _bitmapHeight)
|
||||||
_bitmapHeight = h;
|
_bitmapHeight = h;
|
||||||
}
|
}
|
||||||
|
@ -50,14 +78,14 @@ TextObject::TextObject(const char *text, const int x, const int y, /*const*/ Fon
|
||||||
int offset = 0;
|
int offset = 0;
|
||||||
for (int line = 0; line < _bitmapHeight; ++line) {
|
for (int line = 0; line < _bitmapHeight; ++line) {
|
||||||
for (int c = 0; msg[c] != '\0'; ++c) {
|
for (int c = 0; msg[c] != '\0'; ++c) {
|
||||||
uint32 charWidth = font->getCharWidth(msg[c]);
|
uint32 charWidth = _font->getCharWidth(msg[c]);
|
||||||
uint32 charLogicalWidth = font->getCharLogicalWidth(msg[c]);
|
uint32 charLogicalWidth = _font->getCharLogicalWidth(msg[c]);
|
||||||
uint8 startingCol = font->getCharStartingCol(msg[c]);
|
uint8 startingCol = _font->getCharStartingCol(msg[c]);
|
||||||
uint8 startingLine = font->getCharStartingLine(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,
|
memcpy(_textBitmap + offset + startingCol,
|
||||||
font->getCharData(msg[c]) + charWidth * (line - startingLine), charWidth);
|
_font->getCharData(msg[c]) + charWidth * (line - startingLine), charWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
offset += charLogicalWidth + startingCol;
|
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);
|
_textObjectHandle = g_driver->prepareToTextBitmap(_textBitmap, _bitmapWidth, _bitmapHeight, _fgColor);
|
||||||
|
|
||||||
|
_created = true;
|
||||||
g_engine->registerTextObject(this);
|
g_engine->registerTextObject(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
TextObject::~TextObject() {
|
void TextObject::destroyBitmap() {
|
||||||
|
if (_textObjectHandle) {
|
||||||
delete[] _textBitmap;
|
delete[] _textBitmap;
|
||||||
if (_textObjectHandle->bitmapData)
|
g_driver->destroyTextBitmap(_textObjectHandle);
|
||||||
delete _textObjectHandle->bitmapData;
|
|
||||||
if (_textObjectHandle->surface)
|
|
||||||
SDL_FreeSurface((SDL_Surface *)_textObjectHandle->surface);
|
|
||||||
if (_textObjectHandle->texIds)
|
|
||||||
delete _textObjectHandle->texIds;
|
|
||||||
|
|
||||||
delete _textObjectHandle;
|
delete _textObjectHandle;
|
||||||
|
_textObjectHandle = NULL;
|
||||||
|
}
|
||||||
|
_created = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextObject::draw() {
|
void TextObject::draw() {
|
||||||
|
|
31
textobject.h
31
textobject.h
|
@ -28,19 +28,40 @@
|
||||||
|
|
||||||
class TextObject {
|
class TextObject {
|
||||||
public:
|
public:
|
||||||
TextObject(const char *text, const int x, const int y, /*const*/ Font *font, const Color& fgColor);
|
TextObject();
|
||||||
~TextObject();
|
~TextObject();
|
||||||
void setX(int x) {_x = x; }
|
void createBitmap();
|
||||||
void setY(int y) {_y = y; }
|
void destroyBitmap();
|
||||||
void setColor(Color *newColor) { _fgColor = newColor; }
|
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; }
|
const char *name() const { return _textID; }
|
||||||
void draw();
|
void draw();
|
||||||
|
|
||||||
|
enum Justify {
|
||||||
|
NONE,
|
||||||
|
CENTER,
|
||||||
|
LJUSTIFY
|
||||||
|
};
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
bool _created;
|
||||||
Color _fgColor;
|
Color _fgColor;
|
||||||
int _x, _y;
|
int _x, _y;
|
||||||
char _textID[10];
|
int _width, _height;
|
||||||
|
int _justify;
|
||||||
|
Font *_font;
|
||||||
|
char *_text;
|
||||||
|
char _textID[32];
|
||||||
uint8 *_textBitmap;
|
uint8 *_textBitmap;
|
||||||
int _bitmapHeight, _bitmapWidth;
|
int _bitmapHeight, _bitmapWidth;
|
||||||
Driver::TextObjectHandle *_textObjectHandle;
|
Driver::TextObjectHandle *_textObjectHandle;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue