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

stark/resources/lipsync.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/stark/resources/lipsync.h"
00024 
00025 #include "engines/stark/formats/xrc.h"
00026 
00027 #include "engines/stark/resources/anim.h"
00028 #include "engines/stark/resources/item.h"
00029 #include "engines/stark/resources/textureset.h"
00030 
00031 #include "engines/stark/services/global.h"
00032 #include "engines/stark/services/services.h"
00033 #include "engines/stark/services/stateprovider.h"
00034 
00035 #include "engines/stark/visual/visual.h"
00036 #include "engines/stark/visual/actor.h"
00037 
00038 namespace Stark {
00039 namespace Resources {
00040 
00041 LipSync::~LipSync() {
00042 }
00043 
00044 LipSync::LipSync(Object *parent, byte subType, uint16 index, const Common::String &name) :
00045         Object(parent, subType, index, name),
00046         _item(nullptr),
00047         _sceneItem(nullptr),
00048         _faceTexture(nullptr),
00049         _visual(nullptr),
00050         _enabled(false),
00051         _checkForNewVisual(false),
00052         _positionMs(0) {
00053     _type = TYPE;
00054 }
00055 
00056 void LipSync::readData(Formats::XRCReadStream *stream) {
00057     uint32 shapeCount = stream->readUint32LE();
00058     for (uint i = 0; i < shapeCount; i++) {
00059         uint32 shape = stream->readUint32LE();
00060         _shapes.push_back(shape);
00061 
00062         // The original does not use that data
00063         stream->skip(4);
00064     }
00065 
00066     // The original does not use that data
00067     uint32 unkCount = stream->readUint32LE();
00068     stream->skip(unkCount);
00069 }
00070 
00071 void LipSync::printData() {
00072     Object::printData();
00073 
00074     Common::String phrase;
00075     for (uint i = 0; i < _shapes.size(); i++) {
00076         phrase += _shapes[i];
00077     }
00078 
00079     debug("shapes: %s", phrase.c_str());
00080 }
00081 
00082 void LipSync::setItem(ItemVisual *item, bool playTalkAnim) {
00083     _item = item;
00084     _checkForNewVisual = !playTalkAnim;
00085 
00086     if (_item->getSubType() != Item::kItemModel) {
00087         return;
00088     }
00089 
00090     _sceneItem = Object::cast<ModelItem>(item);
00091     _faceTexture = _sceneItem->findTextureSet(TextureSet::kTextureFace);
00092 
00093     if (!_faceTexture) {
00094         return;
00095     }
00096 
00097     Anim *anim = _item->getAnim();
00098     _visual = nullptr;
00099 
00100     if (!anim || anim->getSubType() != Anim::kAnimSkeleton) {
00101         return;
00102     }
00103 
00104     AnimSkeleton *animSkeleton = Object::cast<AnimSkeleton>(anim);
00105     _visual = animSkeleton->getVisual()->get<VisualActor>();
00106 
00107     if (!_visual) {
00108         return;
00109     }
00110 
00111     _visual->setTextureFacial(_faceTexture->getTexture());
00112     _enabled = true;
00113     _positionMs = 0;
00114 }
00115 
00116 void LipSync::reset() {
00117     _enabled = false;
00118     _visual = nullptr;
00119     _positionMs = 0;
00120     _item = nullptr;
00121     _sceneItem = nullptr;
00122     _checkForNewVisual = false;
00123     _faceTexture = nullptr;
00124 }
00125 
00126 void LipSync::onGameLoop() {
00127     Object::onGameLoop();
00128 
00129     if (!_enabled) {
00130         return;
00131     }
00132 
00133     if (_checkForNewVisual && _sceneItem && _faceTexture) {
00134         Anim *anim = _sceneItem->getAnim();
00135         if (anim && anim->getSubType() == Anim::kAnimSkeleton) {
00136             AnimSkeleton *animSkeleton = Object::cast<AnimSkeleton>(anim);
00137             _visual = animSkeleton->getVisual()->get<VisualActor>();
00138 
00139             if (_visual) {
00140                 _visual->setTextureFacial(_faceTexture->getTexture());
00141             }
00142         }
00143     }
00144 
00145     if (_visual) {
00146         uint32 shapeIndex = (_positionMs + 100) / 100;
00147         if (shapeIndex < _shapes.size()) {
00148             _visual->setNewFace(_shapes[shapeIndex]);
00149         } else {
00150             reset();
00151         }
00152 
00153         _positionMs += StarkGlobal->getMillisecondsPerGameloop();
00154     }
00155 
00156     if (_enabled && !_visual) {
00157         reset();
00158     }
00159 }
00160 
00161 void LipSync::saveLoadCurrent(ResourceSerializer *serializer) {
00162     serializer->syncAsUint32LE(_enabled);
00163     if (_enabled) {
00164         serializer->syncAsResourceReference(&_item);
00165         serializer->syncAsUint32LE(_positionMs);
00166 
00167         if (serializer->isLoading()) {
00168             setItem(_item, false);
00169         }
00170     }
00171 }
00172 
00173 } // End of namespace Resources
00174 } // End of namespace Stark


Generated on Sat Mar 23 2019 05:01:46 for ResidualVM by doxygen 1.7.1
curved edge   curved edge