1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
/* ResidualVM - A 3D game interpreter
*
* ResidualVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the AUTHORS
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "common/debug.h"
#include "engines/stark/formats/biffmesh.h"
#include "engines/stark/formats/tm.h"
#include "engines/stark/formats/xrc.h"
#include "engines/stark/gfx/driver.h"
#include "engines/stark/resources/anim.h"
#include "engines/stark/resources/animscript.h"
#include "engines/stark/resources/bonesmesh.h"
#include "engines/stark/resources/direction.h"
#include "engines/stark/resources/image.h"
#include "engines/stark/resources/item.h"
#include "engines/stark/resources/textureset.h"
#include "engines/stark/services/archiveloader.h"
#include "engines/stark/services/global.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/stateprovider.h"
#include "engines/stark/model/skeleton_anim.h"
#include "engines/stark/visual/actor.h"
#include "engines/stark/visual/prop.h"
#include "engines/stark/visual/smacker.h"
namespace Stark {
namespace Resources {
Object *Anim::construct(Object *parent, byte subType, uint16 index, const Common::String &name) {
switch (subType) {
case kAnimImages:
return new AnimImages(parent, subType, index, name);
case kAnimProp:
return new AnimProp(parent, subType, index, name);
case kAnimVideo:
return new AnimVideo(parent, subType, index, name);
case kAnimSkeleton:
return new AnimSkeleton(parent, subType, index, name);
default:
error("Unknown anim subtype %d", subType);
}
}
Anim::~Anim() {
}
Anim::Anim(Object *parent, byte subType, uint16 index, const Common::String &name) :
Object(parent, subType, index, name),
_usage(0),
_currentFrame(0),
_numFrames(0),
_refCount(0) {
_type = TYPE;
}
void Anim::readData(Formats::XRCReadStream *stream) {
_usage = stream->readUint32LE();
_numFrames = stream->readUint32LE();
}
void Anim::selectFrame(uint32 frameIndex) {
}
uint32 Anim::getUsage() const {
return _usage;
}
void Anim::applyToItem(Item *item) {
_refCount++;
}
void Anim::removeFromItem(Item *item) {
_refCount--;
}
bool Anim::isInUse() {
return _refCount > 0;
}
int Anim::getPointHotspotIndex(const Common::Point &point) const {
// Most anim types only have one hotspot
return 0;
}
uint32 Anim::getDuration() const {
// TODO: Implement for each anim type
warning("Anim::getDuration is not implemented");
return 0;
}
void Anim::playAsAction(ItemVisual *item) {
AnimScript *animScript = findChild<AnimScript>();
animScript->goToScriptItem(0);
}
bool Anim::isAtTime(uint32 time) const {
// TODO: Implement for each anim type
warning("Anim::isAtTime is not implemented");
return true;
}
uint32 Anim::getRemainingTime() const {
// TODO: Implement for each anim type
warning("Anim::getRemainingTime is not implemented");
return 0;
}
uint32 Anim::getMovementSpeed() const {
return 100;
}
uint32 Anim::getIdleActionFrequency() const {
return 1;
}
void Anim::printData() {
debug("usage: %d", _usage);
debug("numFrames: %d", _numFrames);
}
AnimImages::~AnimImages() {
}
AnimImages::AnimImages(Object *parent, byte subType, uint16 index, const Common::String &name) :
Anim(parent, subType, index, name),
_field_3C(0),
_currentDirection(0),
_currentFrameImage(nullptr) {
}
void AnimImages::readData(Formats::XRCReadStream *stream) {
Anim::readData(stream);
_field_3C = stream->readFloat();
}
void AnimImages::onAllLoaded() {
Anim::onAllLoaded();
_directions = listChildren<Direction>();
}
void AnimImages::selectFrame(uint32 frameIndex) {
if (frameIndex > _numFrames) {
// The original silently ignores this as well
warning("Request for frame %d for anim '%s' has been ignored, it is above max frame %d", frameIndex, getName().c_str(), _numFrames);
_currentFrame = 0;
}
_currentFrame = frameIndex;
}
Visual *AnimImages::getVisual() {
Direction *direction = _directions[_currentDirection];
_currentFrameImage = direction->findChildWithIndex<Image>(_currentFrame);
return _currentFrameImage->getVisual();
}
void AnimImages::printData() {
Anim::printData();
debug("field_3C: %f", _field_3C);
}
int AnimImages::getPointHotspotIndex(const Common::Point &point) const {
if (_currentFrameImage) {
return _currentFrameImage->indexForPoint(point);
}
return -1;
}
void AnimImages::saveLoad(ResourceSerializer *serializer) {
Anim::saveLoad(serializer);
serializer->syncAsUint32LE(_currentFrame);
if (serializer->isLoading()) {
selectFrame(_currentFrame);
}
}
AnimProp::~AnimProp() {
delete _visual;
}
AnimProp::AnimProp(Object *parent, byte subType, uint16 index, const Common::String &name) :
Anim(parent, subType, index, name),
_movementSpeed(100) {
_visual = StarkGfx->createPropRenderer();
}
Visual *AnimProp::getVisual() {
return _visual;
}
uint32 AnimProp::getMovementSpeed() const {
return _movementSpeed;
}
void AnimProp::readData(Formats::XRCReadStream *stream) {
Anim::readData(stream);
_field_3C = stream->readString();
uint32 meshCount = stream->readUint32LE();
for (uint i = 0; i < meshCount; i++) {
_meshFilenames.push_back(stream->readString());
}
_textureFilename = stream->readString();
_movementSpeed = stream->readUint32LE();
_archiveName = stream->getArchiveName();
}
void AnimProp::onPostRead() {
if (_meshFilenames.size() != 1) {
error("Unexpected mesh count in prop anim: '%d'", _meshFilenames.size());
}
ArchiveReadStream *stream = StarkArchiveLoader->getFile(_meshFilenames[0], _archiveName);
_visual->setModel(Formats::BiffMeshReader::read(stream));
delete stream;
stream = StarkArchiveLoader->getFile(_textureFilename, _archiveName);
_visual->setTexture(Formats::TextureSetReader::read(stream));
delete stream;
}
void AnimProp::printData() {
Anim::printData();
debug("field_3C: %s", _field_3C.c_str());
Common::String description;
for (uint32 i = 0; i < _meshFilenames.size(); i++) {
debug("meshFilename[%d]: %s", i, _meshFilenames[i].c_str());
}
debug("textureFilename: %s", _textureFilename.c_str());
debug("movementSpeed: %d", _movementSpeed);
}
AnimVideo::~AnimVideo() {
delete _smacker;
}
AnimVideo::AnimVideo(Object *parent, byte subType, uint16 index, const Common::String &name) :
Anim(parent, subType, index, name),
_width(0),
_height(0),
_smacker(nullptr),
_frameRateOverride(-1),
_preload(false),
_loop(false),
_actionItem(nullptr) {
}
void AnimVideo::readData(Formats::XRCReadStream *stream) {
Anim::readData(stream);
_smackerFile = stream->readString();
_width = stream->readUint32LE();
_height = stream->readUint32LE();
_positions.clear();
_sizes.clear();
uint32 size = stream->readUint32LE();
for (uint i = 0; i < size; i++) {
_positions.push_back(stream->readPoint());
_sizes.push_back(stream->readRect());
}
_loop = stream->readBool();
_frameRateOverride = stream->readUint32LE();
if (stream->isDataLeft()) {
_preload = stream->readBool();
}
_archiveName = stream->getArchiveName();
}
void AnimVideo::onAllLoaded() {
if (!_smacker) {
Common::SeekableReadStream *stream = StarkArchiveLoader->getExternalFile(_smackerFile, _archiveName);
_smacker = new VisualSmacker(StarkGfx);
_smacker->load(stream);
_smacker->overrideFrameRate(_frameRateOverride);
updateSmackerPosition();
}
}
void AnimVideo::onGameLoop() {
if (!_smacker || !isInUse()) {
return; // Animation not in use, no need to update the movie
}
if (_smacker->isDone()) {
// The last frame has been reached
if (!_loop && _actionItem) {
// Reset our item if needed
_actionItem->resetActionAnim();
_actionItem = nullptr;
}
if (_loop) {
_smacker->rewind();
}
}
if (!_smacker->isDone()) {
_smacker->update();
updateSmackerPosition();
}
}
void AnimVideo::onEnginePause(bool pause) {
Object::onEnginePause(pause);
if (_smacker && isInUse()) {
_smacker->pause(pause);
}
}
Visual *AnimVideo::getVisual() {
return _smacker;
}
void AnimVideo::updateSmackerPosition() {
int frame = _smacker->getFrameNumber();
if (frame != -1 && frame < (int) _positions.size()) {
_smacker->setPosition(_positions[frame]);
}
}
uint32 AnimVideo::getDuration() const {
return _smacker->getDuration();
}
void AnimVideo::playAsAction(ItemVisual *item) {
_actionItem = item;
if (!_loop) {
_smacker->rewind();
}
}
bool AnimVideo::isAtTime(uint32 time) const {
uint32 currentTime = _smacker->getCurrentTime();
return currentTime >= time;
}
void AnimVideo::saveLoadCurrent(ResourceSerializer *serializer) {
Anim::saveLoadCurrent(serializer);
int32 frameNumber = _smacker->getFrameNumber();
serializer->syncAsSint32LE(frameNumber);
serializer->syncAsSint32LE(_refCount);
// TODO: Seek to the saved frame number when loading
}
void AnimVideo::printData() {
Anim::printData();
debug("smackerFile: %s", _smackerFile.c_str());
debug("size: x %d, y %d", _width, _height);
Common::String description;
for (uint32 i = 0; i < _positions.size(); i++) {
description += Common::String::format("(x %d, y %d) ", _positions[i].x, _positions[i].y);
}
debug("positions: %s", description.c_str());
description.clear();
for (uint32 i = 0; i < _sizes.size(); i++) {
description += Common::String::format("(l %d, t %d, r %d, b %d) ",
_sizes[i].left, _sizes[i].top, _sizes[i].right, _sizes[i].bottom);
}
debug("sizes: %s", description.c_str());
debug("frameRateOverride: %d", _frameRateOverride);
debug("preload: %d", _preload);
debug("loop: %d", _loop);
}
AnimSkeleton::~AnimSkeleton() {
delete _visual;
delete _seletonAnim;
}
AnimSkeleton::AnimSkeleton(Object *parent, byte subType, uint16 index, const Common::String &name) :
Anim(parent, subType, index, name),
_castsShadow(true),
_loop(false),
_movementSpeed(100),
_idleActionFrequency(1),
_seletonAnim(nullptr),
_currentTime(0),
_totalTime(0),
_actionItem(nullptr) {
_visual = StarkGfx->createActorRenderer();
}
void AnimSkeleton::applyToItem(Item *item) {
Anim::applyToItem(item);
if (!_loop) {
_currentTime = 0;
}
if (_currentTime > _totalTime) {
_currentTime = 0;
}
ModelItem *modelItem = Object::cast<ModelItem>(item);
BonesMesh *mesh = modelItem->findBonesMesh();
TextureSet *texture = modelItem->findTextureSet(TextureSet::kTextureNormal);
_visual->setModel(mesh->getModel());
_visual->setAnimHandler(mesh->getAnimHandler());
_visual->setTexture(texture->getTexture());
_visual->setTextureFacial(nullptr);
_visual->setAnim(_seletonAnim);
_visual->setTime(_currentTime);
}
void AnimSkeleton::removeFromItem(Item *item) {
Anim::removeFromItem(item);
_actionItem = nullptr;
}
Visual *AnimSkeleton::getVisual() {
return _visual;
}
void AnimSkeleton::readData(Formats::XRCReadStream *stream) {
Anim::readData(stream);
_animFilename = stream->readString();
stream->readString(); // Skipped in the original
stream->readString(); // Skipped in the original
stream->readString(); // Skipped in the original
_loop = stream->readBool();
_movementSpeed = stream->readUint32LE();
if (_movementSpeed < 1) {
_movementSpeed = 100;
}
if (stream->isDataLeft()) {
_castsShadow = stream->readBool();
} else {
_castsShadow = true;
}
if (stream->isDataLeft()) {
_idleActionFrequency = stream->readUint32LE();
} else {
_idleActionFrequency = 1;
}
_archiveName = stream->getArchiveName();
}
void AnimSkeleton::onPostRead() {
ArchiveReadStream *stream = StarkArchiveLoader->getFile(_animFilename, _archiveName);
_seletonAnim = new SkeletonAnim();
_seletonAnim->createFromStream(stream);
delete stream;
}
void AnimSkeleton::onAllLoaded() {
Anim::onAllLoaded();
_totalTime = _seletonAnim->getLength();
_currentTime = 0;
}
void AnimSkeleton::onGameLoop() {
Anim::onGameLoop();
if (isInUse() && _totalTime) {
uint32 newTime = _currentTime + StarkGlobal->getMillisecondsPerGameloop();
if (!_loop && newTime >= _totalTime) {
if (_actionItem) {
_actionItem->resetActionAnim();
_actionItem = nullptr;
}
} else {
_currentTime = newTime % _totalTime;
_visual->setTime(_currentTime);
}
}
}
void AnimSkeleton::onExitLocation() {
Anim::onExitLocation();
_visual->resetBlending();
}
void AnimSkeleton::onPreDestroy() {
if (_actionItem) {
_actionItem->resetActionAnim();
_actionItem = nullptr;
}
Anim::onPreDestroy();
}
uint32 AnimSkeleton::getMovementSpeed() const {
return _movementSpeed;
}
uint32 AnimSkeleton::getDuration() const {
return _totalTime;
}
uint32 AnimSkeleton::getCurrentTime() const {
return _currentTime;
}
uint32 AnimSkeleton::getRemainingTime() const {
int32 remainingTime = _totalTime - _currentTime;
return CLIP<int32>(remainingTime, 0, _totalTime);
}
void AnimSkeleton::playAsAction(ItemVisual *item) {
_actionItem = item;
if (!_loop) {
_currentTime = 0;
}
}
bool AnimSkeleton::isAtTime(uint32 time) const {
return _currentTime >= time;
}
uint32 AnimSkeleton::getIdleActionFrequency() const {
return _idleActionFrequency;
}
void AnimSkeleton::printData() {
Anim::printData();
debug("filename: %s", _animFilename.c_str());
debug("castsShadow: %d", _castsShadow);
debug("loop: %d", _loop);
debug("movementSpeed: %d", _movementSpeed);
debug("idleActionFrequency: %d", _idleActionFrequency);
}
} // End of namespace Resources
} // End of namespace Stark
|