2004-04-12 21:40:49 +00:00
|
|
|
/* ScummVM - Scumm Interpreter
|
|
|
|
* Copyright (C) 2004 The ScummVM project
|
|
|
|
*
|
|
|
|
* The ReInherit Engine is (C)2000-2003 by Daniel Balsom.
|
|
|
|
*
|
|
|
|
* 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*
|
|
|
|
* $Header$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Background animation management module
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/saga.h"
|
|
|
|
#include "saga/gfx.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/cvar_mod.h"
|
|
|
|
#include "saga/console_mod.h"
|
|
|
|
#include "saga/game_mod.h"
|
|
|
|
#include "saga/events_mod.h"
|
|
|
|
#include "saga/render.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-02 16:20:35 +00:00
|
|
|
#include "saga/animation.h"
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
namespace Saga {
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
static void CF_anim_info(int argc, char *argv[], void *refCon);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::reg() {
|
2004-07-31 23:33:14 +00:00
|
|
|
CVAR_RegisterFunc(CF_anim_info, "anim_info", NULL, R_CVAR_NONE, 0, 0, this);
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-08-01 13:34:20 +00:00
|
|
|
Anim::Anim(SagaEngine *vm) : _vm(vm) {
|
2004-08-01 07:23:36 +00:00
|
|
|
int i;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
_anim_limit = R_MAX_ANIMATIONS;
|
|
|
|
_anim_count = 0;
|
2004-08-01 07:23:36 +00:00
|
|
|
|
|
|
|
for (i = 0; i < R_MAX_ANIMATIONS; i++)
|
|
|
|
_anim_tbl[i] = NULL;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
_initialized = true;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
Anim::~Anim(void) {
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 i;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-01 10:48:11 +00:00
|
|
|
for (i = 0; i < R_MAX_ANIMATIONS; i++)
|
|
|
|
free(_anim_tbl[i]);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
_initialized = false;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::load(const byte *anim_resdata, size_t anim_resdata_len, uint16 *anim_id_p) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_ANIMATION *new_anim;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 anim_id = 0;
|
|
|
|
uint16 i;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
if (!_initialized) {
|
2004-08-01 01:56:22 +00:00
|
|
|
warning("Anim::load not initialised");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Find an unused animation slot
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < R_MAX_ANIMATIONS; i++) {
|
2004-07-31 23:33:14 +00:00
|
|
|
if (_anim_tbl[i] == NULL) {
|
2004-04-12 21:40:49 +00:00
|
|
|
anim_id = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i == R_MAX_ANIMATIONS) {
|
2004-08-01 01:56:22 +00:00
|
|
|
warning("Anim::load could not find unused animation slot");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_anim = (R_ANIMATION *)malloc(sizeof *new_anim);
|
|
|
|
if (new_anim == NULL) {
|
2004-08-01 01:56:22 +00:00
|
|
|
warning("Anim::load Allocation failure");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
new_anim->resdata = anim_resdata;
|
|
|
|
new_anim->resdata_len = anim_resdata_len;
|
|
|
|
|
|
|
|
if (GAME_GetGameType() == R_GAMETYPE_ITE) {
|
2004-07-31 13:34:43 +00:00
|
|
|
if (getNumFrames(anim_resdata, anim_resdata_len, &new_anim->n_frames) != R_SUCCESS) {
|
2004-08-01 01:56:22 +00:00
|
|
|
warning("Anim::load Couldn't get animation frame count");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Cache frame offsets
|
|
|
|
new_anim->frame_offsets = (size_t *)malloc(new_anim->n_frames * sizeof *new_anim->frame_offsets);
|
2004-04-12 21:40:49 +00:00
|
|
|
if (new_anim->frame_offsets == NULL) {
|
2004-08-01 01:56:22 +00:00
|
|
|
warning("Anim::load Allocation failure");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_MEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < new_anim->n_frames; i++) {
|
2004-07-31 13:34:43 +00:00
|
|
|
getFrameOffset(anim_resdata, anim_resdata_len, i + 1, &new_anim->frame_offsets[i]);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
new_anim->cur_frame_p = anim_resdata + SAGA_FRAME_HEADER_LEN;
|
2004-05-01 06:16:57 +00:00
|
|
|
new_anim->cur_frame_len = anim_resdata_len - SAGA_FRAME_HEADER_LEN;
|
2004-07-31 13:34:43 +00:00
|
|
|
getNumFrames(anim_resdata, anim_resdata_len, &new_anim->n_frames);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Set animation data
|
2004-04-12 21:40:49 +00:00
|
|
|
new_anim->current_frame = 1;
|
|
|
|
new_anim->end_frame = new_anim->n_frames;
|
|
|
|
new_anim->stop_frame = new_anim->end_frame;
|
|
|
|
|
|
|
|
new_anim->frame_time = R_DEFAULT_FRAME_TIME;
|
|
|
|
new_anim->flags = 0;
|
|
|
|
new_anim->play_flag = 0;
|
|
|
|
new_anim->link_flag = 0;
|
|
|
|
new_anim->link_id = 0;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
_anim_tbl[anim_id] = new_anim;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
*anim_id_p = anim_id;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
_anim_count++;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::link(uint16 anim_id1, uint16 anim_id2) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_ANIMATION *anim1;
|
|
|
|
R_ANIMATION *anim2;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
if ((anim_id1 >= _anim_count) || (anim_id2 >= _anim_count)) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
anim1 = _anim_tbl[anim_id1];
|
|
|
|
anim2 = _anim_tbl[anim_id2];
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
if ((anim1 == NULL) || (anim2 == NULL)) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
anim1->link_id = anim_id2;
|
|
|
|
anim1->link_flag = 1;
|
|
|
|
|
|
|
|
anim2->frame_time = anim1->frame_time;
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::play(uint16 anim_id, int vector_time) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_EVENT event;
|
|
|
|
R_ANIMATION *anim;
|
|
|
|
R_ANIMATION *link_anim;
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 link_anim_id;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
R_BUFFER_INFO buf_info;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
byte *display_buf;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
const byte *nextf_p;
|
2004-04-12 21:40:49 +00:00
|
|
|
size_t nextf_len;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 frame;
|
2004-04-12 21:40:49 +00:00
|
|
|
int result;
|
|
|
|
|
|
|
|
R_GAME_DISPLAYINFO disp_info;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
if (anim_id >= _anim_count) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GAME_GetDisplayInfo(&disp_info);
|
|
|
|
|
2004-07-31 23:00:48 +00:00
|
|
|
_vm->_render->getBufferInfo(&buf_info);
|
2004-04-12 21:40:49 +00:00
|
|
|
display_buf = buf_info.r_bg_buf;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
anim = _anim_tbl[anim_id];
|
2004-04-12 21:40:49 +00:00
|
|
|
if (anim == NULL) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (anim->play_flag) {
|
|
|
|
frame = anim->current_frame;
|
|
|
|
if (GAME_GetGameType() == R_GAMETYPE_ITE) {
|
2004-05-04 03:33:03 +00:00
|
|
|
result = ITE_DecodeFrame(anim->resdata, anim->resdata_len, anim->frame_offsets[frame - 1], display_buf,
|
2004-05-01 06:16:57 +00:00
|
|
|
disp_info.logical_w * disp_info.logical_h);
|
2004-04-12 21:40:49 +00:00
|
|
|
if (result != R_SUCCESS) {
|
2004-07-31 13:34:43 +00:00
|
|
|
warning("ANIM::play: Error decoding frame %u", anim->current_frame);
|
2004-04-12 21:40:49 +00:00
|
|
|
anim->play_flag = 0;
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (anim->cur_frame_p == NULL) {
|
2004-07-31 13:34:43 +00:00
|
|
|
warning("ANIM::play: Frames exhausted");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
result = IHNM_DecodeFrame(display_buf, disp_info.logical_w * disp_info.logical_h,
|
|
|
|
anim->cur_frame_p, anim->cur_frame_len, &nextf_p, &nextf_len);
|
2004-04-12 21:40:49 +00:00
|
|
|
if (result != R_SUCCESS) {
|
2004-07-31 13:34:43 +00:00
|
|
|
warning("ANIM::play: Error decoding frame %u", anim->current_frame);
|
2004-04-12 21:40:49 +00:00
|
|
|
anim->play_flag = 0;
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
anim->cur_frame_p = nextf_p;
|
|
|
|
anim->cur_frame_len = nextf_len;
|
|
|
|
}
|
|
|
|
anim->current_frame++;
|
|
|
|
}
|
|
|
|
|
|
|
|
anim->play_flag = 1;
|
|
|
|
|
|
|
|
if (anim->current_frame > anim->n_frames) {
|
2004-05-01 06:16:57 +00:00
|
|
|
// Animation done playing
|
2004-04-12 21:40:49 +00:00
|
|
|
if (anim->link_flag) {
|
2004-05-01 06:16:57 +00:00
|
|
|
// If this animation has a link, follow it
|
2004-04-12 21:40:49 +00:00
|
|
|
anim->play_flag = 0;
|
|
|
|
anim->current_frame = 1;
|
|
|
|
|
|
|
|
link_anim_id = anim->link_id;
|
2004-07-31 23:33:14 +00:00
|
|
|
link_anim = _anim_tbl[link_anim_id];
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
if (link_anim != NULL) {
|
|
|
|
link_anim->current_frame = 1;
|
|
|
|
link_anim->play_flag = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
anim_id = link_anim_id;
|
|
|
|
} else if (anim->flags & ANIM_LOOP) {
|
2004-05-01 06:16:57 +00:00
|
|
|
// Loop animation
|
2004-04-12 21:40:49 +00:00
|
|
|
anim->current_frame = 1;
|
2004-05-01 06:16:57 +00:00
|
|
|
anim->cur_frame_p = anim->resdata + SAGA_FRAME_HEADER_LEN;
|
|
|
|
anim->cur_frame_len = anim->resdata_len - SAGA_FRAME_HEADER_LEN;
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
2004-05-01 06:16:57 +00:00
|
|
|
// No link, stop playing
|
2004-04-12 21:40:49 +00:00
|
|
|
anim->current_frame = anim->n_frames;
|
|
|
|
anim->play_flag = 0;
|
|
|
|
|
|
|
|
if (anim->flags & ANIM_ENDSCENE) {
|
2004-05-01 06:16:57 +00:00
|
|
|
// This animation ends the scene
|
2004-04-12 21:40:49 +00:00
|
|
|
event.type = R_ONESHOT_EVENT;
|
|
|
|
event.code = R_SCENE_EVENT;
|
|
|
|
event.op = EVENT_END;
|
|
|
|
event.time = anim->frame_time + vector_time;
|
|
|
|
EVENT_Queue(&event);
|
|
|
|
}
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
event.type = R_ONESHOT_EVENT;
|
|
|
|
event.code = R_ANIM_EVENT;
|
|
|
|
event.op = EVENT_FRAME;
|
|
|
|
event.param = anim_id;
|
|
|
|
event.time = anim->frame_time + vector_time;
|
|
|
|
|
|
|
|
EVENT_Queue(&event);
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::reset() {
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 i;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
for (i = 0; i < R_MAX_ANIMATIONS; i++) {
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
freeId(i);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
_anim_count = 0;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::setFlag(uint16 anim_id, uint16 flag) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_ANIMATION *anim;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
if (anim_id > _anim_count) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
anim = _anim_tbl[anim_id];
|
2004-04-12 21:40:49 +00:00
|
|
|
if (anim == NULL) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
anim->flags |= flag;
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::setFrameTime(uint16 anim_id, int time) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_ANIMATION *anim;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
if (anim_id > _anim_count) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
anim = _anim_tbl[anim_id];
|
2004-04-12 21:40:49 +00:00
|
|
|
if (anim == NULL) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
anim->frame_time = time;
|
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::freeId(uint16 anim_id) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_ANIMATION *anim;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
if (anim_id > _anim_count) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
anim = _anim_tbl[anim_id];
|
2004-04-12 21:40:49 +00:00
|
|
|
if (anim == NULL) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GAME_GetGameType() == R_GAMETYPE_ITE) {
|
|
|
|
free(anim->frame_offsets);
|
|
|
|
anim->frame_offsets = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(anim);
|
2004-07-31 23:33:14 +00:00
|
|
|
_anim_tbl[anim_id] = NULL;
|
|
|
|
_anim_count--;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// The actual number of frames present in an animation resource is
|
|
|
|
// sometimes less than number present in the .nframes member of the
|
|
|
|
// animation header. For this reason, the function attempts to find
|
|
|
|
// the last valid frame number, which it returns via 'n_frames'
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::getNumFrames(const byte *anim_resource, size_t anim_resource_len, uint16 *n_frames) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_ANIMATION_HEADER ah;
|
|
|
|
|
|
|
|
size_t offset;
|
|
|
|
int magic;
|
|
|
|
|
|
|
|
int x;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
if (!_initialized) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
MemoryReadStream readS(anim_resource, anim_resource_len);
|
2004-05-04 03:33:03 +00:00
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
ah.magic = readS.readUint16LE();
|
|
|
|
ah.screen_w = readS.readUint16LE();
|
|
|
|
ah.screen_h = readS.readUint16LE();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
ah.unknown06 = readS.readByte();
|
|
|
|
ah.unknown07 = readS.readByte();
|
|
|
|
ah.nframes = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
if (GAME_GetGameType() == R_GAMETYPE_IHNM) {
|
|
|
|
*n_frames = ah.nframes;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ah.magic == 68) {
|
|
|
|
for (x = ah.nframes; x > 0; x--) {
|
2004-07-31 13:34:43 +00:00
|
|
|
if (getFrameOffset(anim_resource, anim_resource_len, x, &offset) != R_SUCCESS) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
magic = *(anim_resource + offset);
|
|
|
|
if (magic == SAGA_FRAME_HEADER_MAGIC) {
|
|
|
|
*n_frames = x;
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::ITE_DecodeFrame(const byte *resdata, size_t resdata_len, size_t frame_offset, byte *buf, size_t buf_len) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_ANIMATION_HEADER ah;
|
|
|
|
R_FRAME_HEADER fh;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
byte *write_p;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 magic;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 x_start;
|
|
|
|
uint16 y_start;
|
|
|
|
uint32 screen_w;
|
|
|
|
uint32 screen_h;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
int mark_byte;
|
2004-04-30 23:02:23 +00:00
|
|
|
byte data_byte;
|
2004-04-12 21:40:49 +00:00
|
|
|
int new_row;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 control_ch;
|
|
|
|
uint16 param_ch;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 runcount;
|
2004-04-12 21:40:49 +00:00
|
|
|
int x_vector;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 i;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
if (!_initialized) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
MemoryReadStream headerReadS(resdata, resdata_len);
|
2004-05-01 06:16:57 +00:00
|
|
|
// Read animation header
|
2004-08-01 10:02:41 +00:00
|
|
|
ah.magic = headerReadS.readUint16LE();
|
|
|
|
ah.screen_w = headerReadS.readUint16LE();
|
|
|
|
ah.screen_h = headerReadS.readUint16LE();
|
|
|
|
ah.unknown06 = headerReadS.readByte();
|
|
|
|
ah.unknown07 = headerReadS.readByte();
|
|
|
|
ah.nframes = headerReadS.readByte();
|
|
|
|
ah.flags = headerReadS.readByte();
|
|
|
|
ah.unknown10 = headerReadS.readByte();
|
|
|
|
ah.unknown11 = headerReadS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
screen_w = ah.screen_w;
|
|
|
|
screen_h = ah.screen_h;
|
|
|
|
|
|
|
|
if ((screen_w * screen_h) > buf_len) {
|
2004-05-01 06:16:57 +00:00
|
|
|
// Buffer argument is too small to hold decoded frame, abort.
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("ITE_DecodeFrame: Buffer size inadequate");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Read frame header
|
2004-08-01 10:02:41 +00:00
|
|
|
MemoryReadStream readS(resdata + frame_offset, resdata_len - frame_offset);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Check for frame magic byte
|
2004-08-01 10:02:41 +00:00
|
|
|
magic = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (magic != SAGA_FRAME_HEADER_MAGIC) {
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("ITE_DecodeFrame: Invalid frame offset");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// For some strange reason, the animation header is in little
|
|
|
|
// endian format, but the actual RLE encoded frame data,
|
|
|
|
// including the frame header, is in big endian format.
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
fh.x_start = readS.readUint16BE();
|
|
|
|
fh.y_start = readS.readByte();
|
|
|
|
readS.readByte(); /* Skip pad byte */
|
|
|
|
fh.x_pos = readS.readUint16BE();
|
|
|
|
fh.y_pos = readS.readUint16BE();
|
|
|
|
fh.width = readS.readUint16BE();
|
|
|
|
fh.height = readS.readUint16BE();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
x_start = fh.x_start;
|
|
|
|
y_start = fh.y_start;
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Setup write pointer to the draw origin
|
2004-04-12 21:40:49 +00:00
|
|
|
write_p = (buf + (y_start * screen_w) + x_start);
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Begin RLE decompression to output buffer
|
2004-04-12 21:40:49 +00:00
|
|
|
do {
|
2004-08-01 10:02:41 +00:00
|
|
|
mark_byte = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
switch (mark_byte) {
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x10: // Long Unencoded Run
|
2004-08-01 10:02:41 +00:00
|
|
|
runcount = readS.readSint16BE();
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < runcount; i++) {
|
2004-08-01 10:02:41 +00:00
|
|
|
data_byte = readS.readByte();
|
2004-05-04 03:33:03 +00:00
|
|
|
if (data_byte != 0) {
|
|
|
|
*write_p = data_byte;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
write_p++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x20: // Long encoded run
|
2004-08-01 10:02:41 +00:00
|
|
|
runcount = readS.readSint16BE();
|
|
|
|
data_byte = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < runcount; i++) {
|
|
|
|
*write_p++ = data_byte;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x2F: // End of row
|
2004-08-01 10:02:41 +00:00
|
|
|
x_vector = readS.readSint16BE();
|
|
|
|
new_row = readS.readByte();
|
2004-05-01 06:16:57 +00:00
|
|
|
// Set write pointer to the new draw origin
|
|
|
|
write_p = buf + ((y_start + new_row) * screen_w) + x_start + x_vector;
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x30: // Reposition command
|
2004-08-01 10:02:41 +00:00
|
|
|
x_vector = readS.readSint16BE();
|
2004-04-12 21:40:49 +00:00
|
|
|
write_p += x_vector;
|
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x3F: // End of frame marker
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_SUCCESS;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Mask all but two high order control bits
|
2004-04-12 21:40:49 +00:00
|
|
|
control_ch = mark_byte & 0xC0U;
|
|
|
|
param_ch = mark_byte & 0x3FU;
|
|
|
|
switch (control_ch) {
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0xC0: // 1100 0000
|
|
|
|
// Run of empty pixels
|
2004-04-12 21:40:49 +00:00
|
|
|
runcount = param_ch + 1;
|
|
|
|
write_p += runcount;
|
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x80: // 1000 0000
|
|
|
|
// Run of compressed data
|
2004-04-12 21:40:49 +00:00
|
|
|
runcount = param_ch + 1;
|
2004-08-01 10:02:41 +00:00
|
|
|
data_byte = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
for (i = 0; i < runcount; i++) {
|
|
|
|
*write_p++ = data_byte;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x40: // 0100 0000
|
|
|
|
// Uncompressed run
|
2004-04-12 21:40:49 +00:00
|
|
|
runcount = param_ch + 1;
|
|
|
|
for (i = 0; i < runcount; i++) {
|
2004-08-01 10:02:41 +00:00
|
|
|
data_byte = readS.readByte();
|
2004-05-04 03:33:03 +00:00
|
|
|
if (data_byte != 0) {
|
|
|
|
*write_p = data_byte;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
write_p++;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
default:
|
2004-05-01 06:16:57 +00:00
|
|
|
// Unknown marker found - abort
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("ITE_DecodeFrame: Invalid RLE marker encountered");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
break;
|
|
|
|
}
|
2004-05-01 06:16:57 +00:00
|
|
|
} while (mark_byte != 63); // end of frame marker
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::IHNM_DecodeFrame(byte *decode_buf, size_t decode_buf_len, const byte *thisf_p,
|
2004-05-01 06:16:57 +00:00
|
|
|
size_t thisf_len, const byte **nextf_p, size_t *nextf_len) {
|
2004-04-12 21:40:49 +00:00
|
|
|
int in_ch;
|
|
|
|
int decoded_data = 0;
|
|
|
|
int cont_flag = 1;
|
|
|
|
int control_ch;
|
|
|
|
int param_ch;
|
2004-04-30 23:02:23 +00:00
|
|
|
byte data_pixel;
|
2004-04-12 21:40:49 +00:00
|
|
|
int x_origin = 0;
|
|
|
|
int y_origin = 0;
|
|
|
|
int x_vector;
|
|
|
|
int new_row;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 runcount;
|
|
|
|
uint16 c;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
size_t in_ch_offset;
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
MemoryReadStream readS(thisf_p, thisf_len);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
byte *outbuf_p = decode_buf;
|
|
|
|
byte *outbuf_endp = (decode_buf + decode_buf_len) - 1;
|
2004-04-12 21:40:49 +00:00
|
|
|
size_t outbuf_remain = decode_buf_len;
|
|
|
|
|
|
|
|
R_GAME_DISPLAYINFO di;
|
|
|
|
|
|
|
|
GAME_GetDisplayInfo(&di);
|
|
|
|
|
|
|
|
*nextf_p = NULL;
|
|
|
|
|
|
|
|
for (; cont_flag; decoded_data = 1) {
|
2004-08-01 10:02:41 +00:00
|
|
|
in_ch_offset = readS.pos();
|
|
|
|
in_ch = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
switch (in_ch) {
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x0F: // 15: Frame header
|
2004-04-12 21:40:49 +00:00
|
|
|
{
|
|
|
|
int param1;
|
|
|
|
int param2;
|
|
|
|
int param3;
|
|
|
|
int param4;
|
|
|
|
int param5;
|
|
|
|
int param6;
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
if (thisf_len - readS.pos() < 13) {
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("0x%02X: Input buffer underrun", in_ch);
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
param1 = readS.readUint16BE();
|
|
|
|
param2 = readS.readUint16BE();
|
|
|
|
readS.readByte(); // skip 1?
|
|
|
|
param3 = readS.readUint16BE();
|
|
|
|
param4 = readS.readUint16BE();
|
|
|
|
param5 = readS.readUint16BE();
|
|
|
|
param6 = readS.readUint16BE();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
x_origin = param1;
|
|
|
|
y_origin = param2;
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
outbuf_p = decode_buf + x_origin + (y_origin * di.logical_w);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
if (outbuf_p > outbuf_endp) {
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("0x%02X: (0x%X) Invalid output position. (x: %d, y: %d)",
|
2004-05-01 06:16:57 +00:00
|
|
|
in_ch, in_ch_offset, x_origin, y_origin);
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbuf_remain = (outbuf_endp - outbuf_p) + 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x10: // Long Unencoded Run
|
2004-08-01 10:02:41 +00:00
|
|
|
runcount = readS.readSint16BE();
|
|
|
|
if (thisf_len - readS.pos() < runcount) {
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("0x%02X: Input buffer underrun", in_ch);
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
if (outbuf_remain < runcount) {
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("0x%02X: Output buffer overrun", in_ch);
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (c = 0; c < runcount; c++) {
|
2004-08-01 10:02:41 +00:00
|
|
|
data_pixel = readS.readByte();
|
2004-05-04 03:33:03 +00:00
|
|
|
if (data_pixel != 0) {
|
|
|
|
*outbuf_p = data_pixel;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
outbuf_p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbuf_remain -= runcount;
|
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x1F: // 31: Unusued?
|
2004-08-01 10:02:41 +00:00
|
|
|
if (thisf_len - readS.pos() < 3) {
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("0x%02X: Input buffer underrun", in_ch);
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
readS.readByte();
|
|
|
|
readS.readByte();
|
|
|
|
readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x20: // Long compressed run
|
2004-08-01 10:02:41 +00:00
|
|
|
if (thisf_len - readS.pos() <= 3) {
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("0x%02X: Input buffer underrun", in_ch);
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
runcount = readS.readSint16BE();
|
|
|
|
data_pixel = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
for (c = 0; c < runcount; c++) {
|
|
|
|
*outbuf_p++ = data_pixel;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbuf_remain -= runcount;
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x2F: // End of row
|
2004-08-01 10:02:41 +00:00
|
|
|
if (thisf_len - readS.pos() <= 4) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
x_vector = readS.readSint16BE();
|
|
|
|
new_row = readS.readSint16BE();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
outbuf_p = decode_buf + ((y_origin + new_row) * di.logical_w) + x_origin + x_vector;
|
2004-04-12 21:40:49 +00:00
|
|
|
outbuf_remain = (outbuf_endp - outbuf_p) + 1;
|
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x30: // Reposition command
|
2004-08-01 10:02:41 +00:00
|
|
|
if (thisf_len - readS.pos() < 2) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
x_vector = readS.readSint16BE();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
if (((x_vector > 0) && ((size_t) x_vector > outbuf_remain)) || (-x_vector > outbuf_p - decode_buf)) {
|
2004-05-05 13:05:45 +00:00
|
|
|
warning("0x30: Invalid x_vector");
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbuf_p += x_vector;
|
|
|
|
outbuf_remain -= x_vector;
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x3F: // 68: Frame end marker
|
2004-05-05 13:05:45 +00:00
|
|
|
debug(1, "0x3F: Frame end marker");
|
2004-08-01 10:02:41 +00:00
|
|
|
if (decoded_data && (thisf_len - readS.pos() > 0)) {
|
|
|
|
*nextf_p = thisf_p + readS.pos();
|
|
|
|
*nextf_len = thisf_len - readS.pos();
|
2004-04-12 21:40:49 +00:00
|
|
|
} else {
|
|
|
|
*nextf_p = NULL;
|
|
|
|
*nextf_len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
cont_flag = 0;
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
control_ch = in_ch & 0xC0;
|
|
|
|
param_ch = in_ch & 0x3f;
|
|
|
|
switch (control_ch) {
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0xC0: // Run of empty pixels
|
2004-04-12 21:40:49 +00:00
|
|
|
runcount = param_ch + 1;
|
|
|
|
if (outbuf_remain < runcount) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbuf_p += runcount;
|
|
|
|
outbuf_remain -= runcount;
|
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x80: // Run of compressed data
|
2004-04-12 21:40:49 +00:00
|
|
|
runcount = param_ch + 1;
|
2004-08-01 10:02:41 +00:00
|
|
|
if ((outbuf_remain < runcount) || (thisf_len - readS.pos() <= 1)) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
data_pixel = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
for (c = 0; c < runcount; c++) {
|
|
|
|
*outbuf_p++ = data_pixel;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbuf_remain -= runcount;
|
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x40: // Uncompressed run
|
2004-04-12 21:40:49 +00:00
|
|
|
runcount = param_ch + 1;
|
2004-08-01 10:02:41 +00:00
|
|
|
if ((outbuf_remain < runcount) || (thisf_len - readS.pos() < runcount)) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (c = 0; c < runcount; c++) {
|
2004-08-01 10:02:41 +00:00
|
|
|
data_pixel = readS.readByte();
|
2004-05-04 03:33:03 +00:00
|
|
|
if (data_pixel != 0) {
|
|
|
|
*outbuf_p = data_pixel;
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
outbuf_p++;
|
|
|
|
}
|
|
|
|
|
|
|
|
outbuf_remain -= runcount;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
}
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-31 13:34:43 +00:00
|
|
|
int Anim::getFrameOffset(const byte *resdata, size_t resdata_len, uint16 find_frame, size_t *frame_offset_p) {
|
2004-04-12 21:40:49 +00:00
|
|
|
R_ANIMATION_HEADER ah;
|
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 num_frames;
|
|
|
|
uint16 current_frame;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-04-30 23:02:23 +00:00
|
|
|
byte mark_byte;
|
|
|
|
uint16 control;
|
|
|
|
uint16 runcount;
|
|
|
|
uint16 magic;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-04 03:33:03 +00:00
|
|
|
int i;
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
if (!_initialized) {
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
MemoryReadStream readS(resdata, resdata_len);
|
2004-05-04 03:33:03 +00:00
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Read animation header
|
2004-08-01 10:02:41 +00:00
|
|
|
ah.magic = readS.readUint16LE();
|
|
|
|
ah.screen_w = readS.readUint16LE();
|
|
|
|
ah.screen_h = readS.readUint16LE();
|
|
|
|
ah.unknown06 = readS.readByte();
|
|
|
|
ah.unknown07 = readS.readByte();
|
|
|
|
ah.nframes = readS.readByte();
|
|
|
|
ah.flags = readS.readByte();
|
|
|
|
ah.unknown10 = readS.readByte();
|
|
|
|
ah.unknown11 = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
num_frames = ah.nframes;
|
|
|
|
|
|
|
|
if ((find_frame < 1) || (find_frame > num_frames)) {
|
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (current_frame = 1; current_frame < find_frame; current_frame++) {
|
2004-08-01 10:02:41 +00:00
|
|
|
magic = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
if (magic != SAGA_FRAME_HEADER_MAGIC) {
|
2004-05-01 06:16:57 +00:00
|
|
|
// Frame sync failure. Magic Number not found
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
}
|
|
|
|
|
2004-05-04 03:33:03 +00:00
|
|
|
// skip header
|
|
|
|
for (i = 0; i < SAGA_FRAME_HEADER_LEN; i++)
|
2004-08-01 10:02:41 +00:00
|
|
|
readS.readByte();
|
2004-05-04 03:33:03 +00:00
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// For some strange reason, the animation header is in little
|
|
|
|
// endian format, but the actual RLE encoded frame data,
|
|
|
|
// including the frame header, is in big endian format. */
|
2004-04-12 21:40:49 +00:00
|
|
|
do {
|
2004-08-01 10:02:41 +00:00
|
|
|
mark_byte = readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
switch (mark_byte) {
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x3F: // End of frame marker
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x30: // Reposition command
|
2004-08-01 10:02:41 +00:00
|
|
|
readS.readByte();
|
|
|
|
readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x2F: // End of row marker
|
2004-08-01 10:02:41 +00:00
|
|
|
readS.readByte();
|
|
|
|
readS.readByte();
|
|
|
|
readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x20: // Long compressed run marker
|
2004-08-01 10:02:41 +00:00
|
|
|
readS.readByte();
|
|
|
|
readS.readByte();
|
|
|
|
readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
case 0x10: // (16) 0001 0000
|
|
|
|
// Long Uncompressed Run
|
2004-08-01 10:02:41 +00:00
|
|
|
runcount = readS.readSint16BE();
|
2004-05-04 03:33:03 +00:00
|
|
|
for (i = 0; i < runcount; i++)
|
2004-08-01 10:02:41 +00:00
|
|
|
readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-05-01 06:16:57 +00:00
|
|
|
// Mask all but two high order (control) bits
|
2004-04-12 21:40:49 +00:00
|
|
|
control = mark_byte & 0xC0;
|
|
|
|
switch (control) {
|
|
|
|
case 0xC0:
|
2004-05-01 06:16:57 +00:00
|
|
|
// Run of empty pixels
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case 0x80:
|
2004-05-01 06:16:57 +00:00
|
|
|
// Run of compressed data
|
2004-08-01 10:02:41 +00:00
|
|
|
readS.readByte(); // Skip data byte
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
case 0x40:
|
2004-05-01 06:16:57 +00:00
|
|
|
// Uncompressed run
|
2004-04-12 21:40:49 +00:00
|
|
|
runcount = (mark_byte & 0x3f) + 1;
|
2004-05-04 03:33:03 +00:00
|
|
|
for (i = 0; i < runcount; i++)
|
2004-08-01 10:02:41 +00:00
|
|
|
readS.readByte();
|
2004-04-12 21:40:49 +00:00
|
|
|
continue;
|
|
|
|
break;
|
|
|
|
default:
|
2004-05-01 06:16:57 +00:00
|
|
|
// Encountered unknown RLE marker, abort
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_FAILURE;
|
|
|
|
break;
|
2004-05-01 06:16:57 +00:00
|
|
|
}
|
|
|
|
} while (mark_byte != 63);
|
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-08-01 10:02:41 +00:00
|
|
|
*frame_offset_p = readS.pos();
|
2004-04-12 21:40:49 +00:00
|
|
|
return R_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
void Anim::animInfo(int argc, char *argv[]) {
|
2004-04-30 23:02:23 +00:00
|
|
|
uint16 anim_ct;
|
|
|
|
uint16 i;
|
|
|
|
uint16 idx;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-05-04 03:33:03 +00:00
|
|
|
(void)(argc);
|
|
|
|
(void)(argv);
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
anim_ct = _anim_count;
|
2004-04-12 21:40:49 +00:00
|
|
|
|
|
|
|
CON_Print("There are %d animations loaded:", anim_ct);
|
|
|
|
|
|
|
|
for (idx = 0, i = 0; i < anim_ct; idx++, i++) {
|
2004-07-31 23:33:14 +00:00
|
|
|
while (_anim_tbl[idx] == NULL) {
|
2004-04-12 21:40:49 +00:00
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
CON_Print("%02d: Frames: %u Flags: %u", i, _anim_tbl[idx]->n_frames, _anim_tbl[idx]->flags);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
2004-07-31 23:33:14 +00:00
|
|
|
}
|
2004-04-12 21:40:49 +00:00
|
|
|
|
2004-07-31 23:33:14 +00:00
|
|
|
static void CF_anim_info(int argc, char *argv[], void *refCon) {
|
|
|
|
((Anim *)refCon)->animInfo(argc, argv);
|
2004-04-12 21:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Saga
|