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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
|
/* ScummVM - Scumm Interpreter
* Copyright (C) 2003 The ScummVM project
*
* 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$
*
*/
#include "screen.h"
uint8 SkyScreen::_top16Colours[16*3] =
{
0, 0, 0,
38, 38, 38,
63, 63, 63,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
54, 54, 54,
45, 47, 49,
32, 31, 41,
29, 23, 37,
23, 18, 30,
49, 11, 11,
39, 5, 5,
29, 1, 1,
63, 63, 63
};
SkyScreen::SkyScreen(OSystem *pSystem, SkyDisk *pDisk) {
_system = pSystem;
_skyDisk = pDisk;
int i;
uint8 tmpPal[1024];
_system->init_size(FULL_SCREEN_WIDTH, FULL_SCREEN_HEIGHT);
_gameGrid = (uint8*)malloc(GRID_X * GRID_Y * 2);
_backScreen = (uint8*)malloc(GAME_SCREEN_WIDTH * GAME_SCREEN_HEIGHT);
forceRefresh();
_currentScreen = NULL;
_scrollScreen = NULL;
//blank the first 240 colors of the palette
memset(tmpPal, 0, GAME_COLOURS * 4);
//set the remaining colors
for (i = 0; i < (VGA_COLOURS-GAME_COLOURS); i++) {
tmpPal[4 * GAME_COLOURS + i * 4] = (_top16Colours[i * 3] << 2) + (_top16Colours[i * 3] & 3);
tmpPal[4 * GAME_COLOURS + i * 4 + 1] = (_top16Colours[i * 3 + 1] << 2) + (_top16Colours[i * 3 + 1] & 3);
tmpPal[4 * GAME_COLOURS + i * 4 + 2] = (_top16Colours[i * 3 + 2] << 2) + (_top16Colours[i * 3 + 2] & 3);
tmpPal[4 * GAME_COLOURS + i * 4 + 3] = 0x00;
}
//set the palette
_system->set_palette(tmpPal, 0, VGA_COLOURS);
_currentPalette = 0;
_seqInfo.framesLeft = 0;
_seqInfo.seqData = _seqInfo.seqDataPos = NULL;
_seqInfo.running = false;
}
SkyScreen::~SkyScreen(void) {
free(_gameGrid);
if (_currentScreen) free(_currentScreen);
if (_backScreen) free(_backScreen);
}
//set a new palette, pal is a pointer to dos vga rgb components 0..63
void SkyScreen::setPalette(uint8 *pal) {
convertPalette(pal, _palette);
_system->set_palette(_palette, 0, GAME_COLOURS);
_system->update_screen();
}
void SkyScreen::halvePalette(void) {
uint8 halfPalette[1024];
for (uint8 cnt = 0; cnt < GAME_COLOURS; cnt++) {
halfPalette[(cnt << 2) | 0] = _palette[(cnt << 2) | 0] >> 1;
halfPalette[(cnt << 2) | 1] = _palette[(cnt << 2) | 1] >> 1;
halfPalette[(cnt << 2) | 2] = _palette[(cnt << 2) | 2] >> 1;
halfPalette[(cnt << 2) | 3] = 0;
}
_system->set_palette(halfPalette, 0, GAME_COLOURS);
}
void SkyScreen::setPalette(uint16 fileNum) {
uint8 *tmpPal = _skyDisk->loadFile(fileNum, NULL);
if (tmpPal) {
setPalette(tmpPal);
free(tmpPal);
} else warning("SkyScreen::setPalette: can't load file nr. %d\n",fileNum);
}
void SkyScreen::showScreen(uint16 fileNum) {
if (_currentScreen) free(_currentScreen);
_currentScreen = _skyDisk->loadFile(fileNum, NULL);
if (_currentScreen) showScreen(_currentScreen);
else warning("SkyScreen::showScreen: can't load file nr. %d\n",fileNum);
}
void SkyScreen::showScreen(uint8 *pScreen) {
_system->copy_rect(pScreen, 320, 0, 0, GAME_SCREEN_WIDTH, GAME_SCREEN_HEIGHT);
_system->update_screen();
}
void SkyScreen::convertPalette(uint8 *inPal, uint8* outPal) { //convert 3 byte 0..63 rgb to 4byte 0..255 rgbx
int i;
for (i = 0; i < VGA_COLOURS; i++) {
outPal[4 * i] = (inPal[3 * i] << 2) + (inPal[3 * i] & 3);
outPal[4 * i + 1] = (inPal[3 * i + 1] << 2) + (inPal[3 * i + 1] & 3);
outPal[4 * i + 2] = (inPal[3 * i + 2] << 2) + (inPal[3 * i + 2] & 3);
outPal[4 * i + 3] = 0x00;
}
}
void SkyScreen::recreate(void) {
// check the game grid for changed blocks
if (!SkyLogic::_scriptVariables[LAYER_0_ID]) return ;
uint8 *gridPos = _gameGrid;
uint8 *screenData = (uint8*)SkyState::fetchItem(SkyLogic::_scriptVariables[LAYER_0_ID]);
if (!screenData) {
error("SkyScreen::recreate():\nSkyState::fetchItem(SkyLogic::_scriptVariables[LAYER_0_ID](%X)) returned NULL",SkyLogic::_scriptVariables[LAYER_0_ID]);
}
uint8 *screenPos = _backScreen;
for (uint8 cnty = 0; cnty < GRID_Y; cnty++) {
for (uint8 cntx = 0; cntx < GRID_X; cntx++) {
if (gridPos[0] & 0x80) {
gridPos[0] &= 0x7F; // reset recreate flag
gridPos[0] |= 1; // set bit for flip routine
uint8 *savedScreenY = screenPos;
for (uint8 gridCntY = 0; gridCntY < GRID_H; gridCntY++) {
memcpy(screenPos, screenData, GRID_W);
screenPos += GAME_SCREEN_WIDTH;
screenData += GRID_W;
}
screenPos = savedScreenY + GRID_W;
} else {
screenPos += GRID_W;
screenData += GRID_W * GRID_H;
}
gridPos++;
}
screenPos += (GRID_H - 1) * GAME_SCREEN_WIDTH;
}
}
void SkyScreen::flip(void) {
SkyState::_systemVars.mouseFlag |= MF_NO_UPDATE;
// drawMouseToBackScreen();
uint8 *screenPos = _currentScreen;
uint8 *backPos = _backScreen;
uint32 copyX, copyWidth;
copyWidth = 0;
for (uint8 cnty = 0; cnty < GRID_Y; cnty++) {
for (uint8 cntx = 0; cntx < GRID_X; cntx++) {
if (_gameGrid[cnty * GRID_X + cntx] & 1) {
_gameGrid[cnty * GRID_X + cntx] &= ~1;
if (!copyWidth) copyX = cntx * GRID_W;
copyWidth += GRID_W;
uint8 *copySrc = backPos;
uint8 *copyDest = screenPos;
for (uint8 gridLineCnt = 0; gridLineCnt < GRID_H; gridLineCnt++) {
memcpy(copyDest, copySrc, GRID_W);
copySrc += GAME_SCREEN_WIDTH;
copyDest += GAME_SCREEN_WIDTH;
}
} else if (copyWidth) {
_system->copy_rect(_currentScreen + cnty * GRID_H * GAME_SCREEN_WIDTH + copyX, GAME_SCREEN_WIDTH, copyX, cnty * GRID_H, copyWidth, GRID_H);
copyWidth = 0;
}
backPos += GRID_W;
screenPos += GRID_W;
}
if (copyWidth) {
_system->copy_rect(_currentScreen + cnty * GRID_H * GAME_SCREEN_WIDTH + copyX, GAME_SCREEN_WIDTH, copyX, cnty * GRID_H, copyWidth, GRID_H);
copyWidth = 0;
}
screenPos += (GRID_H - 1) * GAME_SCREEN_WIDTH;
backPos += (GRID_H - 1) * GAME_SCREEN_WIDTH;
}
SkyState::_systemVars.mouseFlag &= ~MF_NO_UPDATE;
// _skyMouse->restoreDataToBackScreen();
}
void SkyScreen::fnDrawScreen(uint32 palette, uint32 scroll) {
// set up the new screen
fnFadeDown(scroll);
forceRefresh();
recreate();
spriteEngine();
flip();
fnFadeUp(palette, scroll);
}
void SkyScreen::fnFadeDown(uint32 scroll) {
if (scroll && (!(SkyState::_systemVars.systemFlags & SF_NO_SCROLL))) {
// scrolling is performed by fnFadeUp. It's just prepared here
_scrollScreen = _currentScreen;
_currentScreen = (uint8*)malloc(FULL_SCREEN_WIDTH * FULL_SCREEN_HEIGHT);
// the game will draw the new room into _currentScreen which
// will be scrolled into the visible screen by fnFadeUp
// fnFadeUp also frees the _scrollScreen
} else {
for (uint8 cnt = 0; cnt < 32; cnt++) {
palette_fadedown_helper((uint32 *)_palette, GAME_COLOURS);
_system->set_palette(_palette, 0, GAME_COLOURS);
_system->update_screen();
_system->delay_msecs(20);
}
}
}
void SkyScreen::palette_fadedown_helper(uint32 *pal, uint num) {
byte *p = (byte *)pal;
do {
if (p[0] >= 8)
p[0] -= 8;
else
p[0] = 0;
if (p[1] >= 8)
p[1] -= 8;
else
p[1] = 0;
if (p[2] >= 8)
p[2] -= 8;
else
p[2] = 0;
p += sizeof(uint32);
} while (--num);
}
void SkyScreen::paletteFadeUp(uint16 fileNr) {
uint8 *pal = _skyDisk->loadFile(fileNr, NULL);
if (pal) {
paletteFadeUp(pal);
free(pal);
} else printf("SkyScreen::paletteFadeUp: Can't load palette #%d\n",fileNr);
}
void SkyScreen::paletteFadeUp(uint8 *pal) {
byte tmpPal[1024];
convertPalette(pal, tmpPal);
for (uint8 cnt = 1; cnt <= 32; cnt++) {
for (uint8 colCnt = 0; colCnt < GAME_COLOURS; colCnt++) {
_palette[(colCnt << 2) | 0] = (tmpPal[(colCnt << 2) | 0] * cnt) >> 5;
_palette[(colCnt << 2) | 1] = (tmpPal[(colCnt << 2) | 1] * cnt) >> 5;
_palette[(colCnt << 2) | 2] = (tmpPal[(colCnt << 2) | 2] * cnt) >> 5;
}
_system->set_palette(_palette, 0, GAME_COLOURS);
_system->update_screen();
_system->delay_msecs(20);
}
}
void SkyScreen::fnFadeUp(uint32 palNum, uint32 scroll) {
//_currentScreen points to new screen,
//_scrollScreen points to graphic showing old room
if ((scroll == 0) || (SkyState::_systemVars.systemFlags & SF_NO_SCROLL)) {
uint8 *palette = (uint8*)SkyState::fetchCompact(palNum);
if (palette == NULL)
error("SkyScreen::fnFadeUp: can't fetch compact %X.\n", palNum);
#ifdef SCUMM_BIG_ENDIAN
byte tmpPal[256 * 3];
for (uint16 cnt = 0; cnt < 256*3; cnt++)
tmpPal[cnt] = palette[cnt ^ 1];
paletteFadeUp(tmpPal);
#else
paletteFadeUp(palette);
#endif
} else if (scroll == 123) {
// scroll left (going right)
if (!_currentScreen) error("SkyScreen::fnFadeUp[Scroll L]: _currentScreen is NULL!\n");
if (!_scrollScreen) error("SkyScreen::fnFadeUp[Scroll L]: _scrollScreen is NULL!\n");
uint8 *scrNewPtr, *scrOldPtr;
for (uint8 scrollCnt = 0; scrollCnt < (GAME_SCREEN_WIDTH / SCROLL_JUMP) - 1; scrollCnt++) {
scrNewPtr = _currentScreen + scrollCnt * SCROLL_JUMP;
scrOldPtr = _scrollScreen;
for (uint8 lineCnt = 0; lineCnt < GAME_SCREEN_HEIGHT; lineCnt++) {
memmove(scrOldPtr, scrOldPtr + SCROLL_JUMP, GAME_SCREEN_WIDTH - SCROLL_JUMP);
memcpy(scrOldPtr + GAME_SCREEN_WIDTH - SCROLL_JUMP, scrNewPtr, SCROLL_JUMP);
scrNewPtr += GAME_SCREEN_WIDTH;
scrOldPtr += GAME_SCREEN_WIDTH;
}
showScreen(_scrollScreen);
waitForTimer();
}
showScreen(_currentScreen);
free(_scrollScreen);
} else if (scroll == 321) {
// scroll right (going left)
if (!_currentScreen) error("SkyScreen::fnFadeUp[Scroll R]: _currentScreen is NULL!\n");
if (!_scrollScreen) error("SkyScreen::fnFadeUp[Scroll R]: _scrollScreen is NULL!\n");
uint8 *scrNewPtr, *scrOldPtr;
for (uint8 scrollCnt = 0; scrollCnt < (GAME_SCREEN_WIDTH / SCROLL_JUMP) - 1; scrollCnt++) {
scrNewPtr = _currentScreen + GAME_SCREEN_WIDTH - (scrollCnt + 1) * SCROLL_JUMP;
scrOldPtr = _scrollScreen;
for (uint8 lineCnt = 0; lineCnt < GAME_SCREEN_HEIGHT; lineCnt++) {
memmove(scrOldPtr + SCROLL_JUMP, scrOldPtr, GAME_SCREEN_WIDTH - SCROLL_JUMP);
memcpy(scrOldPtr, scrNewPtr, SCROLL_JUMP);
scrNewPtr += GAME_SCREEN_WIDTH;
scrOldPtr += GAME_SCREEN_WIDTH;
}
showScreen(_scrollScreen);
waitForTimer();
}
showScreen(_currentScreen);
free(_scrollScreen);
} else error("Unknown scroll parameter: %d\n",scroll);
}
void SkyScreen::waitForTimer(void) {
_gotTick = false;
while (!_gotTick)
_system->delay_msecs(10);
}
void SkyScreen::handleTimer(void) {
_gotTick = true;
if (_seqInfo.running) processSequence();
}
void SkyScreen::startSequence(uint16 fileNum) {
_seqInfo.seqData = _skyDisk->loadFile(fileNum, NULL);
_seqInfo.framesLeft = _seqInfo.seqData[0];
_seqInfo.seqDataPos = _seqInfo.seqData + 1;
_seqInfo.delay = SEQ_DELAY;
_seqInfo.running = true;
}
void SkyScreen::stopSequence() {
_seqInfo.running = false;
waitForTimer();
waitForTimer();
_seqInfo.framesLeft = 0;
free(_seqInfo.seqData);
_seqInfo.seqData = _seqInfo.seqDataPos = NULL;
}
void SkyScreen::processSequence(void) {
uint32 screenPos = 0;
uint32 rectX, rectY, oldScreenPos;
_seqInfo.delay--;
if (_seqInfo.delay == 0) {
_seqInfo.delay = SEQ_DELAY;
uint8 nrToSkip, nrToDo, cnt;
do {
do {
nrToSkip = _seqInfo.seqDataPos[0];
_seqInfo.seqDataPos++;
screenPos += nrToSkip;
} while (nrToSkip == 0xFF);
do {
nrToDo = _seqInfo.seqDataPos[0];
_seqInfo.seqDataPos++;
rectX = screenPos % GAME_SCREEN_WIDTH;
rectY = screenPos / GAME_SCREEN_WIDTH;
oldScreenPos = screenPos;
for (cnt = 0; cnt < nrToDo; cnt++) {
_currentScreen[screenPos] = _seqInfo.seqDataPos[0];
_seqInfo.seqDataPos++;
screenPos++;
}
if (nrToDo > 0) {
if (rectX + nrToDo <= GAME_SCREEN_WIDTH)
_system->copy_rect(_currentScreen + oldScreenPos, GAME_SCREEN_WIDTH, rectX, rectY, nrToDo, 1);
else {
_system->copy_rect(_currentScreen + oldScreenPos, GAME_SCREEN_WIDTH, rectX, rectY, 320 - rectX, 1);
oldScreenPos += 320 - rectX;
_system->copy_rect(_currentScreen + oldScreenPos, GAME_SCREEN_WIDTH, 0, rectY + 1, nrToDo - (320 - rectX), 1);
}
}
} while (nrToDo == 0xFF);
} while (screenPos < (GAME_SCREEN_WIDTH * GAME_SCREEN_HEIGHT));
_system->update_screen();
_seqInfo.framesLeft--;
}
if (_seqInfo.framesLeft == 0) {
_seqInfo.running = false;
free(_seqInfo.seqData);
_seqInfo.seqData = _seqInfo.seqDataPos = NULL;
}
}
bool SkyScreen::sequenceRunning(void) {
return _seqInfo.running;
}
//- sprites.asm routines
void SkyScreen::spriteEngine(void) {
doSprites(BACK);
sortSprites();
doSprites(FORE);
}
void SkyScreen::sortSprites(void) {
StSortList sortList[30];
uint32 currDrawList = DRAW_LIST_NO;
uint32 loadDrawList;
bool nextDrawList = false;
while (SkyLogic::_scriptVariables[currDrawList]) {
// big_sort_loop
uint32 spriteCnt = 0;
loadDrawList = SkyLogic::_scriptVariables[currDrawList];
currDrawList++;
do { // a_new_draw_list:
uint16 *drawListData = (uint16*)SkyState::fetchCompact(loadDrawList);
nextDrawList = false;
while ((!nextDrawList) && (drawListData[0])) {
if (drawListData[0] == 0xFFFF) {
loadDrawList = drawListData[1];
nextDrawList = true;
} else {
// process_this_id:
Compact *spriteComp = SkyState::fetchCompact(drawListData[0]);
if ((spriteComp->status & 4) && // is it sortable playfield?(!?!)
(spriteComp->screen == SkyLogic::_scriptVariables[SCREEN])) { // on current screen
dataFileHeader *spriteData =
(dataFileHeader*)SkyState::fetchItem(spriteComp->frame >> 6);
if (!spriteData) {
printf("Missing file %d!\n",spriteComp->frame >> 6);
//getchar();
spriteComp->status = 0;
} else {
sortList[spriteCnt].yCood = spriteComp->ycood + (int16)FROM_LE_16(spriteData->s_offset_y) + (int16)FROM_LE_16(spriteData->s_height);
sortList[spriteCnt].compact = spriteComp;
sortList[spriteCnt].sprite = spriteData;
spriteCnt++;
}
}
drawListData++;
}
}
} while (nextDrawList);
// made_list:
if (spriteCnt > 1) { // bubble sort
for (uint32 cnt1 = 0; cnt1 < spriteCnt - 1; cnt1++)
for (uint32 cnt2 = cnt1 + 1; cnt2 < spriteCnt; cnt2++)
if (sortList[cnt1].yCood <= sortList[cnt2].yCood) {
StSortList tmp;
tmp.yCood = sortList[cnt1].yCood;
tmp.sprite = sortList[cnt1].sprite;
tmp.compact = sortList[cnt1].compact;
sortList[cnt1].yCood = sortList[cnt2].yCood;
sortList[cnt1].sprite = sortList[cnt2].sprite;
sortList[cnt1].compact = sortList[cnt2].compact;
sortList[cnt2].yCood = tmp.yCood;
sortList[cnt2].sprite = tmp.sprite;
sortList[cnt2].compact = tmp.compact;
}
}
for (uint32 cnt = 0; cnt < spriteCnt; cnt++) {
drawSprite((uint8*)sortList[cnt].sprite, sortList[cnt].compact);
if (sortList[cnt].compact->status & 8) vectorToGame(0x81);
else vectorToGame(1);
if (!(sortList[cnt].compact->status & 0x200)) verticalMask();
}
}
}
void SkyScreen::doSprites(uint8 layer) {
uint16 drawListNum = DRAW_LIST_NO;
uint32 idNum;
uint16* drawList;
while (SkyLogic::_scriptVariables[drawListNum]) { // std sp loop
idNum = SkyLogic::_scriptVariables[drawListNum];
drawListNum++;
drawList = (uint16*)SkyState::fetchCompact(idNum);
while(drawList[0]) {
// new_draw_list:
while ((drawList[0] != 0) && (drawList[0] != 0xFFFF)) {
// back_loop:
// not_new_list
Compact *spriteData = SkyState::fetchCompact(drawList[0]);
drawList++;
if ((spriteData->status & (1 << layer)) &&
(spriteData->screen == SkyLogic::_scriptVariables[SCREEN])) {
uint8 *toBeDrawn = (uint8*)SkyState::fetchItem(spriteData->frame >> 6);
if (!toBeDrawn) {
printf("Spritedata %d not loaded!\n",spriteData->frame >> 6);
getchar();
spriteData->status = 0;
} else {
drawSprite(toBeDrawn, spriteData);
if (layer == BACK) verticalMask();
if (spriteData->status & 8) vectorToGame(0x81);
else vectorToGame(1);
}
}
}
if (drawList[0] == 0xFFFF)
drawList = (uint16*)SkyState::fetchCompact(drawList[1]);
}
}
}
void SkyScreen::drawSprite(uint8 *spriteInfo, Compact *sprCompact) {
if (spriteInfo == NULL) {
warning("SkyScreen::drawSprite Can't draw sprite. Data %d was not loaded.\n", sprCompact->frame >> 6);
sprCompact->status = 0;
return ;
}
dataFileHeader *sprDataFile = (dataFileHeader *)spriteInfo;
_sprWidth = FROM_LE_16(sprDataFile->s_width);
_sprHeight = FROM_LE_16(sprDataFile->s_height);
_maskX1 = _maskX2 = 0;
uint8 *spriteData = spriteInfo + (sprCompact->frame & 0x3F) * FROM_LE_16(sprDataFile->s_sp_size);
spriteData += sizeof(dataFileHeader);
int32 spriteY = sprCompact->ycood + (int16)FROM_LE_16(sprDataFile->s_offset_y) - TOP_LEFT_Y;
if (spriteY < 0) {
spriteY = ~spriteY;
if (_sprHeight <= (uint32)spriteY) {
_sprWidth = 0;
return ;
}
_sprHeight -= spriteY;
spriteData += FROM_LE_16(sprDataFile->s_width) * spriteY;
spriteY = 0;
} else {
int32 botClip = GAME_SCREEN_HEIGHT - FROM_LE_16(sprDataFile->s_height) - spriteY;
if (botClip < 0) {
if (botClip + _sprHeight <= 0) {
_sprWidth = 0;
return ;
}
_sprHeight += botClip;
}
}
_sprY = (uint32)spriteY;
int32 spriteX = sprCompact->xcood + (int16)FROM_LE_16(sprDataFile->s_offset_x) - TOP_LEFT_X;
if (spriteX < 0) {
spriteX = ~spriteX;
if (_sprWidth <= (uint32)spriteX) {
_sprWidth = 0;
return ;
}
_sprWidth -= spriteX;
_maskX1 = spriteX;
spriteX = 0;
} else {
int32 rightClip = GAME_SCREEN_WIDTH - FROM_LE_16(sprDataFile->s_width) - spriteX;
if (rightClip < 0) {
rightClip = ~rightClip;
if (_sprWidth <= (uint32)rightClip) {
_sprWidth = 0;
return ;
}
_sprWidth -= rightClip;
_maskX2 = rightClip;
}
}
_sprX = (uint32)spriteX;
uint8 *screenPtr = _backScreen + _sprY * GAME_SCREEN_WIDTH + _sprX;
if ((_sprX + _sprWidth > 320) || (_sprY + _sprHeight > 192)) {
warning("SkyScreen::drawSprite fatal error: got x = %d, y = %d, w = %d, h = %d\n",_sprX, _sprY, _sprWidth, _sprHeight);
_sprWidth = 0;
return ;
}
for (uint8 cnty = 0; cnty < _sprHeight; cnty++) {
for (uint8 cntx = 0; cntx < _sprWidth; cntx++)
if (spriteData[cntx + _maskX1]) screenPtr[cntx] = spriteData[cntx + _maskX1];
spriteData += _sprWidth;
screenPtr += GAME_SCREEN_WIDTH;
}
// Convert the sprite coordinate/size values to blocks for vertical mask and/or vector to game
_sprWidth += _sprX + GRID_W-1;
_sprHeight += _sprY + GRID_H-1;
_sprX >>= GRID_W_SHIFT;
_sprWidth >>= GRID_W_SHIFT;
_sprY >>= GRID_H_SHIFT;
_sprHeight >>= GRID_H_SHIFT;
_sprWidth -= _sprX;
_sprHeight -= _sprY;
}
void SkyScreen::vectorToGame(uint8 gridVal) {
if (_sprWidth == 0) return ;
uint8 *trgGrid = _gameGrid + _sprY * GRID_X +_sprX;
for (uint32 cnty = 0; cnty < _sprHeight; cnty++) {
for (uint32 cntx = 0; cntx < _sprWidth; cntx++)
trgGrid[cntx] |= gridVal;
trgGrid += GRID_X;
}
}
void SkyScreen::vertMaskSub(uint16 *grid, uint32 gridOfs, uint8 *screenPtr, uint32 layerId) {
for (uint32 cntx = 0; cntx < _sprHeight; cntx++) { // start_x | block_loop
if (grid[gridOfs]) {
if (!(FROM_LE_16(grid[gridOfs]) & 0x8000)) {
uint32 gridVal = FROM_LE_16(grid[gridOfs]) - 1;
gridVal *= GRID_W * GRID_H;
uint8 *dataSrc = (uint8*)SkyState::fetchItem(SkyLogic::_scriptVariables[layerId]) + gridVal;
uint8 *dataTrg = screenPtr;
for (uint32 grdCntY = 0; grdCntY < GRID_H; grdCntY++) {
for (uint32 grdCntX = 0; grdCntX < GRID_W; grdCntX++)
if (dataSrc[grdCntX]) dataTrg[grdCntX] = dataSrc[grdCntX];
dataSrc += GRID_W;
dataTrg += GAME_SCREEN_WIDTH;
}
} // dummy_end:
screenPtr -= GRID_H * GAME_SCREEN_WIDTH;
gridOfs -= GRID_X;
} else return;
} // next_x
}
void SkyScreen::verticalMask(void) {
if (_sprWidth == 0) return ;
uint32 startGridOfs = (_sprY + _sprHeight - 1) * GRID_X + _sprX;
uint8 *startScreenPtr = (_sprY + _sprHeight - 1) * GRID_H * GAME_SCREEN_WIDTH + _sprX * GRID_W + _backScreen;
for (uint32 layerCnt = LAYER_1_ID; layerCnt <= LAYER_3_ID; layerCnt++) {
uint32 gridOfs = startGridOfs;
uint8 *screenPtr = startScreenPtr;
for (uint32 widCnt = 0; widCnt < _sprWidth; widCnt++) { // x_loop
uint32 nLayerCnt = layerCnt;
while (SkyLogic::_scriptVariables[nLayerCnt + 3]) {
uint16 *scrGrid;
scrGrid = (uint16*)SkyState::fetchItem(SkyLogic::_scriptVariables[layerCnt + 3]);
if (scrGrid[gridOfs]) {
vertMaskSub(scrGrid, gridOfs, screenPtr, layerCnt);
break;
} else nLayerCnt++;
}
// next_x:
screenPtr += GRID_W;
gridOfs++;
}
}
}
|