From 54e1478e8595e4596152321ce08f91bc50ebb4da Mon Sep 17 00:00:00 2001 From: Giulio Camuffo Date: Mon, 25 Jul 2011 23:42:07 +0200 Subject: [PATCH] GRIM: Calculate the bounding box of the meshes only if necessary. --- engines/grim/actor.cpp | 48 ++++++++++++++++++++++++------------- engines/grim/actor.h | 3 +-- engines/grim/costume.cpp | 14 +++++++---- engines/grim/costume.h | 3 ++- engines/grim/gfx_base.h | 2 +- engines/grim/gfx_opengl.cpp | 8 +++---- engines/grim/gfx_opengl.h | 2 +- engines/grim/gfx_tinygl.cpp | 8 +++---- engines/grim/gfx_tinygl.h | 2 +- engines/grim/model.cpp | 25 +++++++++---------- engines/grim/model.h | 4 ++-- engines/grim/textobject.cpp | 5 ++++ engines/grim/textobject.h | 1 + 13 files changed, 77 insertions(+), 48 deletions(-) diff --git a/engines/grim/actor.cpp b/engines/grim/actor.cpp index 72d46d88d05..6a6a5538b29 100644 --- a/engines/grim/actor.cpp +++ b/engines/grim/actor.cpp @@ -75,6 +75,7 @@ Actor::Actor(const Common::String &actorName) : _running = false; _scale = 1.f; _timeScale = 1.f; + _mustPlaceText = false; for (int i = 0; i < 5; i++) { _shadowArray[i].active = false; @@ -103,6 +104,7 @@ Actor::Actor() : _running = false; _scale = 1.f; _timeScale = 1.f; + _mustPlaceText = false; for (int i = 0; i < 5; i++) { _shadowArray[i].active = false; @@ -904,13 +906,7 @@ void Actor::sayLine(const char *msgId, bool background) { textObject->setX(640 / 2); textObject->setY(456); } else { - if (_winX1 == 1000 || _winX2 == -1000 || _winY2 == -1000) { - textObject->setX(640 / 2); - textObject->setY(463); - } else { - textObject->setX((_winX1 + _winX2) / 2); - textObject->setY(_winY1); - } + _mustPlaceText = true; } textObject->setText(msgId); g_grim->registerTextObject(textObject); @@ -1237,9 +1233,6 @@ void Actor::update() { } void Actor::draw() { - g_winX1 = g_winY1 = 1000; - g_winX2 = g_winY2 = -1000; - for (Common::List::iterator i = _costumeStack.begin(); i != _costumeStack.end(); ++i) { Costume *c = *i; c->setupTextures(); @@ -1255,6 +1248,20 @@ void Actor::draw() { } } + int x1, y1, x2, y2; + int *px1, *py1, *px2, *py2; + if (_mustPlaceText) { + px1 = &x1; + py1 = &y1; + px2 = &x2; + py2 = &y2; + + x1 = y1 = 1000; + x2 = y2 = -1000; + } else { + px1 = py1 = px2 = py2 = NULL; + } + if (!_costumeStack.empty()) { Costume *costume = _costumeStack.back(); if (!g_driver->isHardwareAccelerated()) { @@ -1271,12 +1278,12 @@ void Actor::draw() { } // normal draw actor g_driver->startActorDraw(_pos, _scale, _yaw, _pitch, _roll); - costume->draw(); + costume->draw(px1, py1, px2, py2); g_driver->finishActorDraw(); } else { // normal draw actor g_driver->startActorDraw(_pos, _scale, _yaw, _pitch, _roll); - costume->draw(); + costume->draw(px1, py1, px2, py2); g_driver->finishActorDraw(); for (int l = 0; l < 5; l++) { @@ -1293,10 +1300,19 @@ void Actor::draw() { } } } - _winX1 = g_winX1; - _winX2 = g_winX2; - _winY1 = g_winY1; - _winY2 = g_winY2; + + if (_mustPlaceText) { + TextObject *textObject = g_grim->getTextObject(_sayLineText); + if (x1 == 1000 || x2 == -1000 || y2 == -1000) { + textObject->setX(640 / 2); + textObject->setY(463); + } else { + textObject->setX((x1 + x2) / 2); + textObject->setY(y1); + } + textObject->reset(); + _mustPlaceText = false; + } } // "Undraw objects" (handle objects for actors that may not be on screen) diff --git a/engines/grim/actor.h b/engines/grim/actor.h index 2be0eacec4f..5d0f5d9eb18 100644 --- a/engines/grim/actor.h +++ b/engines/grim/actor.h @@ -43,8 +43,6 @@ struct Plane { typedef Common::List SectorListType; -extern int g_winX1, g_winY1, g_winX2, g_winY2; - struct Shadow { Common::String name; Graphics::Vector3d pos; @@ -240,6 +238,7 @@ private: static ObjectPtr _sayLineFont; int _sayLineText; + bool _mustPlaceText; // Validate a yaw angle then set it appropriately void setYaw(float yaw); diff --git a/engines/grim/costume.cpp b/engines/grim/costume.cpp index e4b85800d9d..64cd589508c 100644 --- a/engines/grim/costume.cpp +++ b/engines/grim/costume.cpp @@ -160,7 +160,7 @@ public: ModelNode *getHierarchy() { return _hier; } int getNumNodes() { return _obj->getNumNodes(); } Model *getModel() { return _obj; } - void draw(); + void draw(int *x1, int *y1, int *x2, int *y2); protected: struct AnimationEntry { @@ -551,7 +551,7 @@ void translateObject(ModelNode *node, bool reset) { } } -void ModelComponent::draw() { +void ModelComponent::draw(int *x1, int *y1, int *x2, int *y2) { // If the object was drawn by being a component // of it's parent then don't draw it @@ -562,7 +562,7 @@ void ModelComponent::draw() { if (_hier->_parent) translateObject(_hier->_parent, false); - _hier->draw(); + _hier->draw(x1, y1, x2, y2); // Need to un-translate when done if (_hier->_parent) @@ -1720,7 +1720,13 @@ void Costume::setupTextures() { void Costume::draw() { for (int i = 0; i < _numComponents; i++) if (_components[i]) - _components[i]->draw(); + _components[i]->draw(NULL, NULL, NULL, NULL); +} + +void Costume::draw(int *x1, int *y1, int *x2, int *y2) { + for (int i = 0; i < _numComponents; i++) + if (_components[i]) + _components[i]->draw(x1, y1, x2, y2); } void Costume::update() { diff --git a/engines/grim/costume.h b/engines/grim/costume.h index ff44631bebc..9b00c9703d3 100644 --- a/engines/grim/costume.h +++ b/engines/grim/costume.h @@ -76,6 +76,7 @@ public: void animate(); void setupTextures(); void draw(); + void draw(int *x1, int *y1, int *x2, int *y2); void setPosRotate(Graphics::Vector3d pos, float pitch, float yaw, float roll); Costume *getPreviousCostume() const; @@ -99,7 +100,7 @@ public: virtual void update() { } virtual void animate() { } virtual void setupTexture() { } - virtual void draw() { } + virtual void draw(int *x1, int *y1, int *x2, int *y2) { } virtual void reset() { } virtual void resetColormap() { } virtual void saveState(SaveGame *) { } diff --git a/engines/grim/gfx_base.h b/engines/grim/gfx_base.h index ab69f0fa977..f823cde6c54 100644 --- a/engines/grim/gfx_base.h +++ b/engines/grim/gfx_base.h @@ -79,7 +79,7 @@ public: virtual void translateViewpointStart(Graphics::Vector3d pos, float pitch, float yaw, float roll) = 0; virtual void translateViewpointFinish() = 0; - virtual void drawHierachyNode(const ModelNode *node) = 0; + virtual void drawHierachyNode(const ModelNode *node, int *x1, int *y1, int *x2, int *y2) = 0; virtual void drawModelFace(const MeshFace *face, float *vertices, float *vertNormals, float *textureVerts) = 0; virtual void drawSprite(const Sprite *sprite) = 0; diff --git a/engines/grim/gfx_opengl.cpp b/engines/grim/gfx_opengl.cpp index f0cfb25f0ac..24086d74e1c 100644 --- a/engines/grim/gfx_opengl.cpp +++ b/engines/grim/gfx_opengl.cpp @@ -494,7 +494,7 @@ void GfxOpenGL::translateViewpointFinish() { glPopMatrix(); } -void GfxOpenGL::drawHierachyNode(const ModelNode *node) { +void GfxOpenGL::drawHierachyNode(const ModelNode *node, int *x1, int *y1, int *x2, int *y2) { Graphics::Vector3d animPos = node->_pos + node->_animPos; float animPitch = node->_pitch + node->_animPitch; float animYaw = node->_yaw + node->_animYaw; @@ -513,21 +513,21 @@ void GfxOpenGL::drawHierachyNode(const ModelNode *node) { } if (node->_mesh && node->_meshVisible) { - node->_mesh->draw(); + node->_mesh->draw(x1, y1, x2, y2); } glMatrixMode(GL_MODELVIEW); glPopMatrix(); if (node->_child) { - node->_child->draw(); + node->_child->draw(x1, y1, x2, y2); glMatrixMode(GL_MODELVIEW); } } translateViewpointFinish(); if (node->_sibling) - node->_sibling->draw(); + node->_sibling->draw(x1, y1, x2, y2); } void GfxOpenGL::enableLights() { diff --git a/engines/grim/gfx_opengl.h b/engines/grim/gfx_opengl.h index c2300433a0d..411f168f0ab 100644 --- a/engines/grim/gfx_opengl.h +++ b/engines/grim/gfx_opengl.h @@ -75,7 +75,7 @@ public: void translateViewpointStart(Graphics::Vector3d pos, float pitch, float yaw, float roll); void translateViewpointFinish(); - void drawHierachyNode(const ModelNode *node); + void drawHierachyNode(const ModelNode *node, int *x1, int *y1, int *x2, int *y2); void drawModelFace(const MeshFace *face, float *vertices, float *vertNormals, float *textureVerts); void drawSprite(const Sprite *sprite); diff --git a/engines/grim/gfx_tinygl.cpp b/engines/grim/gfx_tinygl.cpp index e11455a16df..fb584e1f3f6 100644 --- a/engines/grim/gfx_tinygl.cpp +++ b/engines/grim/gfx_tinygl.cpp @@ -529,7 +529,7 @@ void GfxTinyGL::translateViewpointFinish() { tglPopMatrix(); } -void GfxTinyGL::drawHierachyNode(const ModelNode *node) { +void GfxTinyGL::drawHierachyNode(const ModelNode *node, int *x1, int *y1, int *x2, int *y2) { Graphics::Vector3d animPos = node->_pos + node->_animPos; float animPitch = node->_pitch + node->_animPitch; float animYaw = node->_yaw + node->_animYaw; @@ -548,21 +548,21 @@ void GfxTinyGL::drawHierachyNode(const ModelNode *node) { } if (node->_mesh && node->_meshVisible) { - node->_mesh->draw(); + node->_mesh->draw(x1, y1, x2, y2); } tglMatrixMode(TGL_MODELVIEW); tglPopMatrix(); if (node->_child) { - node->_child->draw(); + node->_child->draw(x1, y1, x2, y2); tglMatrixMode(TGL_MODELVIEW); } } translateViewpointFinish(); if (node->_sibling) - node->_sibling->draw(); + node->_sibling->draw(x1, y1, x2, y2); } void GfxTinyGL::enableLights() { diff --git a/engines/grim/gfx_tinygl.h b/engines/grim/gfx_tinygl.h index 22488dfb485..fd5bbb7e987 100644 --- a/engines/grim/gfx_tinygl.h +++ b/engines/grim/gfx_tinygl.h @@ -66,7 +66,7 @@ public: void translateViewpointStart(Graphics::Vector3d pos, float pitch, float yaw, float roll); void translateViewpointFinish(); - void drawHierachyNode(const ModelNode *node); + void drawHierachyNode(const ModelNode *node, int *x1, int *y1, int *x2, int *y2); void drawModelFace(const MeshFace *face, float *vertices, float *vertNormals, float *textureVerts); void drawSprite(const Sprite *sprite); diff --git a/engines/grim/model.cpp b/engines/grim/model.cpp index 69a8d80c2c4..f13d78ee2e0 100644 --- a/engines/grim/model.cpp +++ b/engines/grim/model.cpp @@ -207,7 +207,7 @@ void Model::loadText(TextSplitter *ts, CMap *cmap) { } void Model::draw() const { - _rootHierNode->draw(); + _rootHierNode->draw(NULL, NULL, NULL, NULL); } ModelNode *Model::copyHierarchy() { @@ -507,15 +507,16 @@ void Mesh::changeMaterials(Material *materials[]) { _faces[i].changeMaterial(materials[_materialid[i]]); } -extern int g_winX1, g_winY1, g_winX2, g_winY2; -void Mesh::draw() const { - int winX1, winY1, winX2, winY2; - g_driver->getBoundingBoxPos(this, &winX1, &winY1, &winX2, &winY2); - if (winX1 != -1 && winY1 != -1 && winX2 != -1 && winY2 != -1) { - g_winX1 = MIN(g_winX1, winX1); - g_winY1 = MIN(g_winY1, winY1); - g_winX2 = MAX(g_winX2, winX2); - g_winY2 = MAX(g_winY2, winY2); +void Mesh::draw(int *x1, int *y1, int *x2, int *y2) const { + if (x1) { + int winX1, winY1, winX2, winY2; + g_driver->getBoundingBoxPos(this, &winX1, &winY1, &winX2, &winY2); + if (winX1 != -1 && winY1 != -1 && winX2 != -1 && winY2 != -1) { + *x1 = MIN(*x1, winX1); + *y1 = MIN(*y1, winY1); + *x2 = MAX(*x2, winX2); + *y2 = MAX(*y2, winY2); + } } if (_lightingMode == 0) @@ -587,8 +588,8 @@ void ModelNode::loadBinary(const char *&data, ModelNode *hierNodes, const Model: _initialized = true; } -void ModelNode::draw() const { - g_driver->drawHierachyNode(this); +void ModelNode::draw(int *x1, int *y1, int *x2, int *y2) const { + g_driver->drawHierachyNode(this, x1, y1, x2, y2); } void ModelNode::addChild(ModelNode *child) { diff --git a/engines/grim/model.h b/engines/grim/model.h index adad12d5412..c2f094fd29a 100644 --- a/engines/grim/model.h +++ b/engines/grim/model.h @@ -112,7 +112,7 @@ public: void loadBinary(const char *&data, Material *materials[]); void loadText(TextSplitter *ts, Material *materials[]); void changeMaterials(Material *materials[]); - void draw() const; + void draw(int *x1, int *y1, int *x2, int *y2) const; void update(); Mesh() : _numFaces(0) { } ~Mesh(); @@ -140,7 +140,7 @@ public: ModelNode() : _initialized(false) { } ~ModelNode(); void loadBinary(const char *&data, ModelNode *hierNodes, const Model::Geoset *g); - void draw() const; + void draw(int *x1, int *y1, int *x2, int *y2) const; void addChild(ModelNode *child); void removeChild(ModelNode *child); void setMatrix(Graphics::Matrix4 matrix); diff --git a/engines/grim/textobject.cpp b/engines/grim/textobject.cpp index cc096e4a5c0..821553465b6 100644 --- a/engines/grim/textobject.cpp +++ b/engines/grim/textobject.cpp @@ -61,6 +61,11 @@ void TextObject::setText(const Common::String &text) { setupText(); } +void TextObject::reset() { + destroy(); + setupText(); +} + void TextObject::saveState(SaveGame *state) const { state->writeLEUint32(_fgColor->getId()); diff --git a/engines/grim/textobject.h b/engines/grim/textobject.h index 3791827e3f2..c7524e42205 100644 --- a/engines/grim/textobject.h +++ b/engines/grim/textobject.h @@ -83,6 +83,7 @@ public: void setDefaults(TextObjectDefaults *defaults); void setText(const Common::String &text); + void reset(); int getBitmapWidth(); int getBitmapHeight();