1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 #include "kyra/engine/kyra_v2.h"
24 #include "kyra/resource/resource.h"
25 
26 #include "common/system.h"
27 
28 namespace Kyra {
29 
runAnimationScript(const char * filename,int allowSkip,int resetChar,int newShapes,int shapeUnload)30 void KyraEngine_v2::runAnimationScript(const char *filename, int allowSkip, int resetChar, int newShapes, int shapeUnload) {
31 	memset(&_animationScriptData, 0, sizeof(_animationScriptData));
32 	memset(&_animationScriptState, 0, sizeof(_animationScriptState));
33 
34 	if (!_emc->load(filename, &_animationScriptData, &_opcodesAnimation))
35 		error("Couldn't load temporary script '%s'", filename);
36 
37 	_emc->init(&_animationScriptState, &_animationScriptData);
38 	_emc->start(&_animationScriptState, 0);
39 
40 	_animResetFrame = -1;
41 
42 	if (_animShapeFiledata && newShapes) {
43 		uninitAnimationShapes(_animShapeCount, _animShapeFiledata);
44 		_animShapeFiledata = 0;
45 		_animShapeCount = 0;
46 	}
47 
48 	while (_emc->isValid(&_animationScriptState))
49 		_emc->run(&_animationScriptState);
50 
51 	uint8 *fileData = 0;
52 
53 	if (newShapes)
54 		_animShapeFiledata = _res->fileData(_animShapeFilename, 0);
55 
56 	fileData = _animShapeFiledata;
57 
58 	if (!fileData) {
59 		_emc->unload(&_animationScriptData);
60 		return;
61 	}
62 
63 	if (newShapes)
64 		_animShapeCount = initAnimationShapes(fileData);
65 
66 	processAnimationScript(allowSkip, resetChar);
67 
68 	if (shapeUnload) {
69 		uninitAnimationShapes(_animShapeCount, fileData);
70 		_animShapeCount = 0;
71 		_animShapeFiledata = 0;
72 	}
73 
74 	_emc->unload(&_animationScriptData);
75 }
76 
processAnimationScript(int allowSkip,int resetChar)77 void KyraEngine_v2::processAnimationScript(int allowSkip, int resetChar) {
78 	setCharacterAnimDim(_animShapeWidth, _animShapeHeight);
79 
80 	_emc->init(&_animationScriptState, &_animationScriptData);
81 	_emc->start(&_animationScriptState, 1);
82 
83 	resetSkipFlag();
84 
85 	while (_emc->isValid(&_animationScriptState)) {
86 		_animNeedUpdate = false;
87 		while (_emc->isValid(&_animationScriptState) && !_animNeedUpdate)
88 			_emc->run(&_animationScriptState);
89 
90 		if (_animNewFrame < 0)
91 			continue;
92 
93 		_mainCharacter.animFrame = _animNewFrame + _desc.animScriptFrameAdd;
94 		updateCharacterAnim(0);
95 		if (!_chatText.empty())
96 			updateWithText();
97 		else
98 			update();
99 
100 		uint32 delayEnd = _system->getMillis() + _animDelayTime * _tickLength;
101 
102 		while ((!skipFlag() || !allowSkip) && _system->getMillis() < delayEnd)
103 			delay(10, true);
104 
105 		if (skipFlag()) {
106 			if (!_kbEventSkip || _eventList.front().event.type != Common::EVENT_KEYDOWN)
107 				resetSkipFlag();
108 			if (allowSkip)
109 				break;
110 		}
111 	}
112 
113 	if (resetChar) {
114 		if (_animResetFrame >= 0) {
115 			_mainCharacter.animFrame = _animResetFrame + _desc.animScriptFrameAdd;
116 			updateCharacterAnim(0);
117 			if (!_chatText.empty())
118 				updateWithText();
119 			else
120 				update();
121 		}
122 
123 		_mainCharacter.animFrame = _desc.characterFrameTable[_mainCharacter.facing];
124 		updateCharacterAnim(0);
125 	}
126 
127 	_animResetFrame = -1;
128 	resetCharacterAnimDim();
129 }
130 
131 } // End of namespace Kyra
132