/
tatou.cpp
474 lines (361 loc) · 9.28 KB
/
tatou.cpp
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
/* 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.
*
*/
// seg 4
#include "fitd.h"
#include "osystem.h"
#include "resource.h"
#include "common.h"
#ifdef PCLIKE
#include "SDL.h"
#endif
namespace Fitd {
//////////// stuff to move
void clearScreenTatou(void) {
int i;
for(i = 0; i < 45120; i++) {
screen[i] = 0;
}
}
void blitScreenTatou(void) {
int i;
for(i = 0; i < 45120; i++) {
unkScreenVar[i] = screen[i];
}
}
void copyPalette(char *source, char *dest) {
int i;
for(i = 0; i < 768; i++) {
dest[i] = source[i];
}
}
void copyToScreen(char *source, char *dest) {
int i;
for(i = 0; i < 64000; i++) {
dest[i] = source[i];
}
}
void paletteFill(void *palette, unsigned char r, unsigned char g, unsigned b) {
unsigned char *paletteLocal = (unsigned char *) palette;
int offset = 0;
int i;
r <<= 1;
g <<= 1;
b <<= 1;
for(i = 0; i < 256; i++) {
paletteLocal[offset] = r;
paletteLocal[offset+1] = g;
paletteLocal[offset+2] = b;
offset += 3;
}
}
void fadeInSub1(char *palette) {
int i;
for(i = 0; i < 256; i++) {
/* palette[i*3] >>=2;
palette[i*3+1] >>=2;
palette[i*3+2] >>=2; */
}
}
void blitPalette(char *palettePtr, unsigned char startColor, unsigned char nbColor) {
int i;
char paletteRGBA[256*4];
char *outPtr = scaledScreen;
char *inPtr = unkScreenVar;
g_driver->getPalette(paletteRGBA);
for(i = startColor; i < startColor + nbColor; i++) {
paletteRGBA[i*4] = palettePtr[i*3];
paletteRGBA[i*4+1] = palettePtr[i*3+1];
paletteRGBA[i*4+2] = palettePtr[i*3+2];
paletteRGBA[i*4+3] = -1;
}
for(i = 0; i < 200; i++) {
int j;
char *copySource = outPtr;
for(j = 0; j < 320; j++) {
*(outPtr++) = *(inPtr);
*(outPtr++) = *(inPtr++);
}
// copy line
for(j = 0; j < 640; j++) {
*(outPtr++) = *(copySource++);
}
}
g_driver->setPalette((byte*)paletteRGBA);
g_driver->flip((unsigned char *)scaledScreen);
}
void flipOtherPalette(char *palettePtr) {
int i;
char paletteRGBA[256*4];
char *outPtr = scaledScreen;
char *inPtr = unkScreenVar;
g_driver->getPalette(paletteRGBA);
for(i = 0; i < 256; i++) {
paletteRGBA[i*4] = palettePtr[i*3];
paletteRGBA[i*4+1] = palettePtr[i*3+1];
paletteRGBA[i*4+2] = palettePtr[i*3+2];
paletteRGBA[i*4+3] = -1;
}
for(i = 0; i < 200; i++) {
int j;
char *copySource = outPtr;
for(j = 0; j < 320; j++) {
*(outPtr++) = *(inPtr);
*(outPtr++) = *(inPtr++);
}
// copy line
for(j = 0; j < 640; j++) {
*(outPtr++) = *(copySource++);
}
}
g_driver->setPalette((byte*)paletteRGBA);
g_driver->flip((unsigned char *)scaledScreen);
}
void computeProportionalPalette(unsigned char *inPalette, unsigned char *outPalette, int coef) {
int i;
for(i = 0; i < 256; i++) {
*(outPalette++) = ((*(inPalette++)) * coef) >> 8;
*(outPalette++) = ((*(inPalette++)) * coef) >> 8;
*(outPalette++) = ((*(inPalette++)) * coef) >> 8;
}
}
void make3dTatouUnk1(int var1, int var2) {
char localPalette[0x300];
int i;
freezeTime();
if(paletteVar == 2) { // only used for the ending ?
} else {
for(i = 0; i < 256; i += var1) {
computeProportionalPalette((unsigned char *)g_driver->_palette, (unsigned char *)localPalette, i);
fadeInSub1(localPalette);
flipOtherPalette(localPalette);
}
}
paletteVar = 1;
flip();
unfreezeTime();
}
void fadeOut(int var1, int var2) {
char localPalette[0x300];
int i;
freezeTime();
for(i = 256; i >= 0; i -= var1) {
computeProportionalPalette((unsigned char *)g_driver->_palette, (unsigned char *)localPalette, i);
fadeInSub1(localPalette);
flipOtherPalette(localPalette);
}
unfreezeTime();
}
void fadeIn(void *sourcePal) {
char localPalette[0x300];
copyPalette((char *)sourcePal, localPalette);
fadeInSub1(localPalette);
flipOtherPalette(localPalette);
}
void flip() {
char *outPtr = scaledScreen;
char *inPtr = unkScreenVar;
int i;
char paletteRGBA[256*4];
#ifdef USE_GL
g_driver->flip(NULL);
return;
#endif
for(i = 0; i < 256; i++) {
paletteRGBA[i*4] = g_driver->_palette[i*3];
paletteRGBA[i*4+1] = g_driver->_palette[i*3+1];
paletteRGBA[i*4+2] = g_driver->_palette[i*3+2];
paletteRGBA[i*4+3] = -1;
}
for(i = 0; i < 200; i++) {
int j;
char *copySource = outPtr;
for(j = 0; j < 320; j++) {
*(outPtr++) = *(inPtr);
*(outPtr++) = *(inPtr++);
}
// copy line
for(j = 0; j < 640; j++) {
*(outPtr++) = *(copySource++);
}
}
g_driver->setPalette((byte*)paletteRGBA);
g_driver->flip((unsigned char *)scaledScreen);
}
#ifdef PCLIKE
void process_events(void) {
/* Our SDL event placeholder. */
SDL_Event event;
/* Grab all the events off the queue. */
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_KEYDOWN:
/* Handle key presses. */
// handle_key_down( &event.key.keysym );
break;
case SDL_QUIT:
/* Handle quit requests (like Ctrl-c). */
exit(0);
break;
}
}
}
#else
void process_events(void) {
}
#endif
void startChrono(unsigned int *chrono) {
*chrono = g_fitd->getTimer();
}
int evalChrono(unsigned int *chrono) {
return(g_fitd->getTimer() - *chrono);
}
// bp = x, bx = y, cx = z
// out
// xOut = dx, yOut = ax
void makeRotationMtx(unsigned int x, unsigned int y, unsigned int z, int *xOut, int *yOut) {
if(x) {
int var1 = (((cosTable[(x+0x100)&0x3FF] * y) << 1) & 0xFFFF0000) - (((cosTable[x&0x3FF] * z) << 1) & 0xFFFF0000);
int var2 = (((cosTable[x&0x3FF] * y) << 1) & 0xFFFF0000) + (((cosTable[(x+0x100)&0x3FF] * z) << 1) & 0xFFFF0000);
*yOut = var1 >> 16;
*xOut = var2 >> 16;
} else {
*xOut = z;
*yOut = y;
}
}
void rotateModel(int x, int y, int z, int alpha, int beta, int gamma, int time) {
int x1;
int y1;
int x2;
int y2;
makeRotationMtx(alpha + 0x200, -time, 0, &x1, &y1);
makeRotationMtx(beta + 0x200, y1, 0, &x2, &y2);
setupSelfModifyingCode(x2 + x, -x1 + y, y2 + z);
setupPointTransformSM(alpha, beta, gamma);
}
void playSound(int num) {
int size = getPakSize("listsamp", num);
char *ptr = listSamp->get(num);
#ifndef NO_SOUND
g_driver->playSample(ptr, size);
#endif
}
////////////////////////
int make3dTatou(void) {
char *tatou2d;
char *tatou3d;
char *tatouPal;
int time;
int deltaTime;
int rotation;
int unk1;
char paletteBackup[768];
unsigned int localChrono;
tatou2d = g_resourceLoader->loadPakSafe("ITD_RESS", 2);
tatou3d = g_resourceLoader->loadPakSafe("ITD_RESS", 0);
tatouPal = g_resourceLoader->loadPakSafe("ITD_RESS", 1);
time = 8920;
deltaTime = 50;
rotation = 256;
unk1 = 8;
setupSMCode(160, 100, 128, 500, 490);
copyPalette(g_driver->_palette, paletteBackup);
paletteFill(g_driver->_palette, 0, 0, 0);
fadeIn(g_driver->_palette);
copyPalette(tatouPal, g_driver->_palette);
copyToScreen(tatou2d + 770, unkScreenVar);
copyToScreen(unkScreenVar, aux2);
#ifdef USE_GL
g_driver->CopyBlockPhys((unsigned char *)unkScreenVar, 0, 0, 320, 200);
flip();
#endif
make3dTatouUnk1(8, 0);
startChrono(&localChrono);
do {
process_events();
readKeyboard();
if(evalChrono(&localChrono) <= 180) { // avant eclair
#ifdef USE_GL
g_driver->startFrame();
#endif
if(input2 || input1) {
break;
}
#ifdef USE_GL
g_driver->stopFrame();
flip();
#endif
} else { // eclair
/* soundVar2 = -1;
soundVar1 = -1; */
playSound(CVars[getCVarsIdx(SAMPLE_TONNERRE)]);
/* soundVar2 = -1;
soundVar1 = -1;*/
paletteFill(g_driver->_palette, 63, 63, 63);
fadeIn(g_driver->_palette);
/* setClipSize(0,0,319,199);*/
clearScreenTatou();
rotateModel(0, 0, 0, unk1, rotation, 0, time);
renderModel(0, 0, 0, 0, 0, 0, tatou3d);
blitScreenTatou();
#ifdef USE_GL
g_driver->CopyBlockPhys((unsigned char *)unkScreenVar, 0, 0, 320, 200);
#endif
copyPalette(tatouPal, g_driver->_palette);
fadeIn(g_driver->_palette);
while(input2 == 0 && input1 == 0 && inputKey == 0) { // boucle de rotation du tatou
process_events();
time += deltaTime - 25;
if(time > 16000)
break;
rotation -= 8;
clearScreenTatou();
#ifdef USE_GL
g_driver->startFrame();
#endif
rotateModel(0, 0, 0, unk1, rotation, 0, time);
renderModel(0, 0, 0, 0, 0, 0, tatou3d);
blitScreenTatou();
#ifdef USE_GL
g_driver->stopFrame();
#endif
flip();
}
break;
}
} while(1);
free(tatouPal);
free(tatou3d);
free(tatou2d);
if(input2 || input1 || inputKey) {
while(input2) {
readKeyboard();
}
fadeOut(32, 0);
copyPalette(paletteBackup, g_driver->_palette);
return(1);
} else {
fadeOut(16, 0);
copyPalette(paletteBackup, g_driver->_palette);
return(0);
}
return(0);
}
} // end of namespace Fitd