ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

node.cpp

Go to the documentation of this file.
00001 /* ResidualVM - A 3D game interpreter
00002  *
00003  * ResidualVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the AUTHORS
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "engines/myst3/database.h"
00024 #include "engines/myst3/effects.h"
00025 #include "engines/myst3/node.h"
00026 #include "engines/myst3/myst3.h"
00027 #include "engines/myst3/state.h"
00028 #include "engines/myst3/subtitles.h"
00029 
00030 #include "common/config-manager.h"
00031 #include "common/debug.h"
00032 #include "common/rect.h"
00033 
00034 namespace Myst3 {
00035 
00036 void Face::setTextureFromJPEG(const DirectorySubEntry *jpegDesc) {
00037     _bitmap = Myst3Engine::decodeJpeg(jpegDesc);
00038     _texture = _vm->_gfx->createTexture(_bitmap);
00039 
00040     // Set the whole texture as dirty
00041     addTextureDirtyRect(Common::Rect(_bitmap->w, _bitmap->h));
00042 }
00043 
00044 Face::Face(Myst3Engine *vm) :
00045         _vm(vm),
00046         _textureDirty(true),
00047         _texture(0),
00048         _bitmap(0),
00049         _finalBitmap(0) {
00050 }
00051 
00052 void Face::addTextureDirtyRect(const Common::Rect &rect) {
00053     if (!_textureDirty) {
00054         _textureDirtyRect = rect;
00055     } else {
00056         _textureDirtyRect.extend(rect);
00057     }
00058 
00059     _textureDirty = true;
00060 }
00061 
00062 void Face::uploadTexture() {
00063     if (_textureDirty) {
00064         if (_finalBitmap)
00065             _texture->updatePartial(_finalBitmap, _textureDirtyRect);
00066         else
00067             _texture->updatePartial(_bitmap, _textureDirtyRect);
00068 
00069         _textureDirty = false;
00070     }
00071 }
00072 
00073 Face::~Face() {
00074     _bitmap->free();
00075     delete _bitmap;
00076     _bitmap = 0;
00077 
00078     if (_finalBitmap) {
00079         _finalBitmap->free();
00080         delete _finalBitmap;
00081     }
00082 
00083     if (_texture) {
00084         _vm->_gfx->freeTexture(_texture);
00085     }
00086 }
00087 
00088 Node::Node(Myst3Engine *vm, uint16 id) :
00089         _vm(vm),
00090         _id(id),
00091         _subtitles(nullptr) {
00092     for (uint i = 0; i < ARRAYSIZE(_faces); i++)
00093         _faces[i] = nullptr;
00094 }
00095 
00096 void Node::initEffects() {
00097     resetEffects();
00098 
00099     if (_vm->_state->getViewType() == kMenu) {
00100         // The node init script does not clear the magnet effect state.
00101         // Here we ignore effects on menu nodes so we don't try to
00102         // to load the magnet effect when opening the main menu on Amateria.
00103         return;
00104     }
00105 
00106     if (_vm->_state->getWaterEffects()) {
00107         Effect *effect = WaterEffect::create(_vm, _id);
00108         if (effect) {
00109             _effects.push_back(effect);
00110             _vm->_state->setWaterEffectActive(true);
00111         }
00112     }
00113 
00114     Effect *effect = MagnetEffect::create(_vm, _id);
00115     if (effect) {
00116         _effects.push_back(effect);
00117         _vm->_state->setMagnetEffectActive(true);
00118     }
00119 
00120     effect = LavaEffect::create(_vm, _id);
00121     if (effect) {
00122         _effects.push_back(effect);
00123         _vm->_state->setLavaEffectActive(true);
00124     }
00125 
00126     effect = ShieldEffect::create(_vm, _id);
00127     if (effect) {
00128         _effects.push_back(effect);
00129         _vm->_state->setShieldEffectActive(true);
00130     }
00131 }
00132 
00133 void Node::resetEffects() {
00134     for (uint i = 0; i < _effects.size(); i++) {
00135         delete _effects[i];
00136     }
00137     _effects.clear();
00138 }
00139 
00140 Node::~Node() {
00141     for (uint i = 0; i < _spotItems.size(); i++) {
00142         delete _spotItems[i];
00143     }
00144     _spotItems.clear();
00145 
00146     resetEffects();
00147 
00148     _vm->_state->setWaterEffectActive(false);
00149     _vm->_state->setMagnetEffectActive(false);
00150     _vm->_state->setLavaEffectActive(false);
00151     _vm->_state->setShieldEffectActive(false);
00152 
00153     for (int i = 0; i < 6; i++) {
00154         delete _faces[i];
00155     }
00156 
00157     delete _subtitles;
00158 }
00159 
00160 void Node::loadSpotItem(uint16 id, int16 condition, bool fade) {
00161     SpotItem *spotItem = new SpotItem(_vm);
00162 
00163     spotItem->setCondition(condition);
00164     spotItem->setFade(fade);
00165     spotItem->setFadeVar(abs(condition));
00166 
00167     for (int i = 0; i < 6; i++) {
00168         DirectorySubEntryList spotItemImages = _vm->listFilesMatching("", id, i + 1, DirectorySubEntry::kLocalizedSpotItem);
00169 
00170         if (spotItemImages.empty())
00171             spotItemImages = _vm->listFilesMatching("", id, i + 1, DirectorySubEntry::kSpotItem);
00172 
00173         for (uint j = 0; j < spotItemImages.size(); j++) {
00174             const DirectorySubEntry *image = spotItemImages[j];
00175 
00176             SpotItemFace *spotItemFace = new SpotItemFace(
00177                     _faces[i],
00178                     image->getSpotItemData().u,
00179                     image->getSpotItemData().v);
00180 
00181             spotItemFace->loadData(image);
00182 
00183             // SpotItems with an always true conditions cannot be undrawn.
00184             // Draw them now to make sure the "non drawn backups" for other, potentially
00185             // overlapping SpotItems have them drawn.
00186             if (condition == 1) {
00187                 spotItemFace->draw();
00188             }
00189 
00190             spotItem->addFace(spotItemFace);
00191         }
00192     }
00193 
00194     _spotItems.push_back(spotItem);
00195 }
00196 
00197 SpotItemFace *Node::loadMenuSpotItem(int16 condition, const Common::Rect &rect) {
00198     SpotItem *spotItem = new SpotItem(_vm);
00199 
00200     spotItem->setCondition(condition);
00201     spotItem->setFade(false);
00202     spotItem->setFadeVar(abs(condition));
00203 
00204     SpotItemFace *spotItemFace = new SpotItemFace(_faces[0], rect.left, rect.top);
00205     spotItemFace->initBlack(rect.width(), rect.height());
00206 
00207     spotItem->addFace(spotItemFace);
00208 
00209     _spotItems.push_back(spotItem);
00210 
00211     return spotItemFace;
00212 }
00213 
00214 void Node::loadSubtitles(uint32 id) {
00215     _subtitles = Subtitles::create(_vm, id);
00216 }
00217 
00218 bool Node::hasSubtitlesToDraw() {
00219     if (!_subtitles || _vm->_state->getSpotSubtitle() <= 0)
00220         return false;
00221 
00222     if (!_vm->isTextLanguageEnglish() && _vm->_state->getLocationRoom() == kRoomNarayan) {
00223         // The words written on the walls in Narayan are always in English.
00224         // Show the subtitles regardless of the "subtitles" setting if the game language is not English.
00225         return true;
00226     }
00227 
00228     return ConfMan.getBool("subtitles");
00229 }
00230 
00231 void Node::drawOverlay() {
00232     if (hasSubtitlesToDraw()) {
00233         uint subId = _vm->_state->getSpotSubtitle();
00234         _subtitles->setFrame(15 * subId + 1);
00235         _vm->_gfx->renderWindowOverlay(_subtitles);
00236     }
00237 }
00238 
00239 void Node::update() {
00240     // First undraw ...
00241     for (uint i = 0; i < _spotItems.size(); i++) {
00242         _spotItems[i]->updateUndraw();
00243     }
00244 
00245     // ... then redraw
00246     for (uint i = 0; i < _spotItems.size(); i++) {
00247         _spotItems[i]->updateDraw();
00248     }
00249 
00250     bool needsUpdate = false;
00251     for (uint i = 0; i < _effects.size(); i++) {
00252         needsUpdate |= _effects[i]->update();
00253     }
00254 
00255     // Apply the effects for all the faces
00256     for (uint faceId = 0; faceId < 6; faceId++) {
00257         Face *face = _faces[faceId];
00258 
00259         if (face == 0)
00260             continue; // No such face in this node
00261 
00262         if (!isFaceVisible(faceId)) {
00263             continue; // This face is not currently visible
00264         }
00265 
00266         uint effectsForFace = 0;
00267         for (uint i = 0; i < _effects.size(); i++) {
00268             if (_effects[i]->hasFace(faceId))
00269                 effectsForFace++;
00270         }
00271 
00272         if (effectsForFace == 0)
00273             continue;
00274         if (!needsUpdate && !face->isTextureDirty())
00275             continue;
00276 
00277         // Alloc the target surface if necessary
00278         if (!face->_finalBitmap) {
00279             face->_finalBitmap = new Graphics::Surface();
00280         }
00281         face->_finalBitmap->copyFrom(*face->_bitmap);
00282 
00283         if (effectsForFace == 1) {
00284             _effects[0]->applyForFace(faceId, face->_bitmap, face->_finalBitmap);
00285 
00286             face->addTextureDirtyRect(_effects[0]->getUpdateRectForFace(faceId));
00287         } else if (effectsForFace == 2) {
00288             // TODO: Keep the same temp surface to avoid heap fragmentation ?
00289             Graphics::Surface *tmp = new Graphics::Surface();
00290             tmp->copyFrom(*face->_bitmap);
00291 
00292             _effects[0]->applyForFace(faceId, face->_bitmap, tmp);
00293             _effects[1]->applyForFace(faceId, tmp, face->_finalBitmap);
00294 
00295             tmp->free();
00296             delete tmp;
00297 
00298             face->addTextureDirtyRect(_effects[0]->getUpdateRectForFace(faceId));
00299             face->addTextureDirtyRect(_effects[1]->getUpdateRectForFace(faceId));
00300         } else {
00301             error("Unable to render more than 2 effects per faceId (%d)", effectsForFace);
00302         }
00303     }
00304 }
00305 
00306 SpotItem::SpotItem(Myst3Engine *vm) :
00307     _vm(vm) {
00308 }
00309 
00310 SpotItem::~SpotItem() {
00311     for (uint i = 0; i < _faces.size(); i++) {
00312         delete _faces[i];
00313     }
00314 }
00315 
00316 void SpotItem::updateUndraw() {
00317     for (uint i = 0; i < _faces.size(); i++) {
00318         if (!_vm->_state->evaluate(_condition) && _faces[i]->isDrawn()) {
00319             _faces[i]->undraw();
00320         }
00321     }
00322 }
00323 
00324 void SpotItem::updateDraw() {
00325     for (uint i = 0; i < _faces.size(); i++) {
00326         if (_enableFade) {
00327             uint16 newFadeValue = _vm->_state->getVar(_fadeVar);
00328 
00329             if (_faces[i]->getFadeValue() != newFadeValue) {
00330                 _faces[i]->setFadeValue(newFadeValue);
00331                 _faces[i]->setDrawn(false);
00332             }
00333         }
00334 
00335         if (_vm->_state->evaluate(_condition) && !_faces[i]->isDrawn()) {
00336             if (_enableFade)
00337                 _faces[i]->fadeDraw();
00338             else
00339                 _faces[i]->draw();
00340         }
00341     }
00342 }
00343 
00344 SpotItemFace::SpotItemFace(Face *face, uint16 posX, uint16 posY):
00345         _face(face),
00346         _posX(posX),
00347         _posY(posY),
00348         _drawn(false),
00349         _bitmap(0),
00350         _notDrawnBitmap(0),
00351         _fadeValue(0) {
00352 }
00353 
00354 SpotItemFace::~SpotItemFace() {
00355     if (_bitmap) {
00356         _bitmap->free();
00357         delete _bitmap;
00358         _bitmap = 0;
00359     }
00360 
00361     if (_notDrawnBitmap) {
00362         _notDrawnBitmap->free();
00363         delete _notDrawnBitmap;
00364         _notDrawnBitmap = 0;
00365     }
00366 }
00367 
00368 void SpotItemFace::initBlack(uint16 width, uint16 height) {
00369     if (_bitmap) {
00370         _bitmap->free();
00371     }
00372 
00373     _bitmap = new Graphics::Surface();
00374     _bitmap->create(width, height, Texture::getRGBAPixelFormat());
00375 
00376     initNotDrawn(width, height);
00377 
00378     _drawn = false;
00379 }
00380 
00381 void SpotItemFace::loadData(const DirectorySubEntry *jpegDesc) {
00382     // Convert active SpotItem image to raw data
00383     _bitmap = Myst3Engine::decodeJpeg(jpegDesc);
00384 
00385     initNotDrawn(_bitmap->w, _bitmap->h);
00386 }
00387 
00388 void SpotItemFace::updateData(const Graphics::Surface *surface) {
00389     assert(_bitmap && surface);
00390     assert(surface->format == Texture::getRGBAPixelFormat());
00391 
00392     _bitmap->free();
00393     _bitmap->copyFrom(*surface);
00394 
00395     _drawn = false;
00396 }
00397 
00398 void SpotItemFace::clear() {
00399     memset(_bitmap->getPixels(), 0, _bitmap->pitch * _bitmap->h);
00400 
00401     _drawn = false;
00402 }
00403 
00404 void SpotItemFace::initNotDrawn(uint16 width, uint16 height) {
00405     // Copy not drawn SpotItem image from face
00406     _notDrawnBitmap = new Graphics::Surface();
00407     _notDrawnBitmap->create(width, height, Texture::getRGBAPixelFormat());
00408 
00409     for (uint i = 0; i < height; i++) {
00410         memcpy(_notDrawnBitmap->getBasePtr(0, i),
00411                 _face->_bitmap->getBasePtr(_posX, _posY + i), width * 4);
00412     }
00413 }
00414 
00415 Common::Rect SpotItemFace::getFaceRect() const {
00416     assert(_bitmap);
00417 
00418     Common::Rect r = Common::Rect(_bitmap->w, _bitmap->h);
00419     r.translate(_posX, _posY);
00420     return r;
00421 }
00422 
00423 void SpotItemFace::draw() {
00424     for (uint i = 0; i < _bitmap->h; i++) {
00425         memcpy(_face->_bitmap->getBasePtr(_posX, _posY + i),
00426                 _bitmap->getBasePtr(0, i),
00427                 _bitmap->w * 4);
00428     }
00429 
00430     _drawn = true;
00431     _face->addTextureDirtyRect(getFaceRect());
00432 }
00433 
00434 void SpotItemFace::undraw() {
00435     for (uint i = 0; i < _notDrawnBitmap->h; i++) {
00436         memcpy(_face->_bitmap->getBasePtr(_posX, _posY + i),
00437                 _notDrawnBitmap->getBasePtr(0, i),
00438                 _notDrawnBitmap->w * 4);
00439     }
00440 
00441     _drawn = false;
00442     _face->addTextureDirtyRect(getFaceRect());
00443 }
00444 
00445 void SpotItemFace::fadeDraw() {
00446     uint16 fadeValue = CLIP<uint16>(_fadeValue, 0, 100);
00447 
00448     for (int i = 0; i < _bitmap->h; i++) {
00449         byte *ptrND = (byte *)_notDrawnBitmap->getBasePtr(0, i);
00450         byte *ptrD = (byte *)_bitmap->getBasePtr(0, i);
00451         byte *ptrDest = (byte *)_face->_bitmap->getBasePtr(_posX, _posY + i);
00452 
00453         for (int j = 0; j < _bitmap->w; j++) {
00454             byte rND = *ptrND++;
00455             byte gND = *ptrND++;
00456             byte bND = *ptrND++;
00457             ptrND++; // Alpha
00458             byte rD = *ptrD++;
00459             byte gD = *ptrD++;
00460             byte bD = *ptrD++;
00461             ptrD++; // Alpha
00462 
00463             // TODO: optimize ?
00464             *ptrDest++ = rND * (100 - fadeValue) / 100 + rD * fadeValue / 100;
00465             *ptrDest++ = gND * (100 - fadeValue) / 100 + gD * fadeValue / 100;
00466             *ptrDest++ = bND * (100 - fadeValue) / 100 + bD * fadeValue / 100;
00467             ptrDest++; // Alpha
00468         }
00469     }
00470 
00471     _drawn = true;
00472     _face->addTextureDirtyRect(getFaceRect());
00473 }
00474 
00475 } // end of namespace Myst3


Generated on Sat Aug 17 2019 05:00:39 for ResidualVM by doxygen 1.7.1
curved edge   curved edge