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 "teenagent/actor.h"
24 #include "teenagent/objects.h"
25 #include "teenagent/resources.h"
26 #include "teenagent/teenagent.h"
27
28 #include "common/random.h"
29 #include "common/textconsole.h"
30
31 namespace TeenAgent {
32
Actor(TeenAgentEngine * vm)33 Actor::Actor(TeenAgentEngine *vm) : _vm(vm), headIndex(0), idleType(0) {}
34
renderIdle(Graphics::Surface * surface,const Common::Point & position,uint8 orientation,int deltaFrame,uint zoom,Common::RandomSource & rnd)35 Common::Rect Actor::renderIdle(Graphics::Surface *surface, const Common::Point &position, uint8 orientation, int deltaFrame, uint zoom, Common::RandomSource &rnd) {
36 if (index == 0) {
37 idleType = rnd.getRandomNumber(2);
38 debugC(kDebugActor, "switched to idle animation %u", idleType);
39 }
40
41 byte *framesIdle;
42 do {
43 framesIdle = _vm->res->dseg.ptr(_vm->res->dseg.get_word(dsAddr_idleAnimationListPtr + idleType * 2)) + index;
44 index += deltaFrame;
45 if (*framesIdle == 0) {
46 idleType = rnd.getRandomNumber(2);
47 debugC(kDebugActor, "switched to idle animation %u[loop]", idleType);
48 index = 3; //put 4th frame (base 1) if idle animation loops
49 }
50 } while (*framesIdle == 0);
51
52 bool mirror = orientation == kActorLeft;
53 Surface *s = frames + *framesIdle - 1;
54
55 //TODO: remove copy-paste here and below
56 int xp = position.x - s->w * zoom / 512 - s->x, yp = position.y - 62 * zoom / 256 - s->y; //hardcoded in original game
57 return s->render(surface, xp, yp, mirror, Common::Rect(), zoom);
58 }
59
render(Graphics::Surface * surface,const Common::Point & position,uint8 orientation,int deltaFrame,bool renderHead,uint zoom)60 Common::Rect Actor::render(Graphics::Surface *surface, const Common::Point &position, uint8 orientation, int deltaFrame, bool renderHead, uint zoom) {
61 const uint8 framesLeftRight[] = {0, 1, 2, 3, 4, 5, /* step */ 6, 7, 8, 9};
62 const uint8 framesUp[] = {18, 19, 20, 21, 22, 23, 24, 25, };
63 const uint8 framesDown[] = {10, 11, 12, 13, 14, 15, 16, 17, };
64
65 const uint8 framesHeadLeftRight[] = {
66 0x27, 0x1a, 0x1b,
67 0x27, 0x1c, 0x1d,
68 0x27, 0x1a,
69 0x27, 0x1e, 0x1f,
70 0x27, 0x1a, 0x1b,
71 0x27, 0x1c,
72 0x27, 0x1e,
73 0x27, 0x1a,
74 };
75
76 const uint8 framesHeadUp[] = {
77 0x29, 0x25, 0x29, 0x29,
78 0x26, 0x29, 0x26, 0x29,
79 0x29, 0x25, 0x29, 0x25,
80 0x29, 0x29, 0x29, 0x25,
81 0x25, 0x29, 0x29, 0x26
82 };
83 const uint8 framesHeadDown[] = {
84 0x20, 0x21, 0x22, 0x23,
85 0x28, 0x24, 0x28, 0x28,
86 0x24, 0x28, 0x20, 0x21,
87 0x20, 0x23, 0x28, 0x20,
88 0x28, 0x28, 0x20, 0x28
89 };
90
91 Surface *s = NULL, *head = NULL;
92
93 bool mirror = orientation == kActorLeft;
94 index += deltaFrame;
95
96 switch (orientation) {
97 case kActorLeft:
98 case kActorRight:
99 if (renderHead) {
100 if (headIndex >= ARRAYSIZE(framesHeadLeftRight))
101 headIndex = 0;
102 head = frames + framesHeadLeftRight[headIndex];
103 ++headIndex;
104 }
105
106 if (index >= ARRAYSIZE(framesLeftRight))
107 index = 1;
108 s = frames + framesLeftRight[index];
109 break;
110 case kActorUp:
111 if (renderHead) {
112 if (headIndex >= ARRAYSIZE(framesHeadUp))
113 headIndex = 0;
114 head = frames + framesHeadUp[headIndex];
115 ++headIndex;
116 }
117
118 if (index >= ARRAYSIZE(framesUp))
119 index = 1;
120 s = frames + framesUp[index];
121 break;
122 case kActorDown:
123 if (renderHead) {
124 if (headIndex >= ARRAYSIZE(framesHeadDown))
125 headIndex = 0;
126 head = frames + framesHeadDown[headIndex];
127 ++headIndex;
128 }
129
130 if (index >= ARRAYSIZE(framesDown))
131 index = 1;
132 s = frames + framesDown[index];
133 break;
134 default:
135 return Common::Rect();
136 }
137
138 Common::Rect dirty;
139 Common::Rect clip(0, 0, s->w, s->h);
140 if (head != NULL)
141 clip.top = head->h;
142
143 int xp = position.x - s->w * zoom / 512 - s->x, yp = position.y - s->h * zoom / 256 - s->y;
144 dirty = s->render(surface, xp, yp + clip.top * zoom / 256, mirror, clip, zoom);
145
146 if (head != NULL)
147 dirty.extend(head->render(surface, xp, yp, orientation == kActorLeft, Common::Rect(), zoom));
148
149 return dirty;
150 }
151
152 } // End of namespace TeenAgent
153