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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* 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.
*
* $URL$
* $Id$
*
*/
#include "cine/cine.h"
#include "cine/bg.h"
#include "cine/bg_list.h"
#include "cine/various.h"
#include "cine/pal.h"
#include "common/endian.h"
#include "common/system.h"
#include "graphics/cursorman.h"
namespace Cine {
byte *collisionPage;
FWRenderer *renderer = NULL;
static const byte mouseCursorNormal[] = {
0x00, 0x00, 0x40, 0x00, 0x60, 0x00, 0x70, 0x00,
0x78, 0x00, 0x7C, 0x00, 0x7E, 0x00, 0x7F, 0x00,
0x7F, 0x80, 0x7C, 0x00, 0x6C, 0x00, 0x46, 0x00,
0x06, 0x00, 0x03, 0x00, 0x03, 0x00, 0x00, 0x00,
0xC0, 0x00, 0xE0, 0x00, 0xF0, 0x00, 0xF8, 0x00,
0xFC, 0x00, 0xFE, 0x00, 0xFF, 0x00, 0xFF, 0x80,
0xFF, 0xC0, 0xFF, 0xC0, 0xFE, 0x00, 0xFF, 0x00,
0xCF, 0x00, 0x07, 0x80, 0x07, 0x80, 0x03, 0x80
};
static const byte mouseCursorDisk[] = {
0x7F, 0xFC, 0x9F, 0x12, 0x9F, 0x12, 0x9F, 0x12,
0x9F, 0x12, 0x9F, 0xE2, 0x80, 0x02, 0x9F, 0xF2,
0xA0, 0x0A, 0xA0, 0x0A, 0xA0, 0x0A, 0xA0, 0x0A,
0xA0, 0x0A, 0xA0, 0x0A, 0x7F, 0xFC, 0x00, 0x00,
0x7F, 0xFC, 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE,
0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE,
0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE, 0xFF, 0xFE,
0xFF, 0xFE, 0xFF, 0xFE, 0x7F, 0xFC, 0x00, 0x00
};
static const byte mouseCursorCross[] = {
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x7C, 0x7C,
0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x01, 0x00,
0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80,
0x03, 0x80, 0x03, 0x80, 0xFF, 0xFE, 0xFE, 0xFE,
0xFF, 0xFE, 0x03, 0x80, 0x03, 0x80, 0x03, 0x80,
0x03, 0x80, 0x03, 0x80, 0x03, 0x80, 0x00, 0x00
};
static const struct MouseCursor {
int hotspotX;
int hotspotY;
const byte *bitmap;
} mouseCursors[] = {
{ 1, 1, mouseCursorNormal },
{ 0, 0, mouseCursorDisk },
{ 7, 7, mouseCursorCross }
};
static const byte cursorPalette[] = {
0, 0, 0, 0xff,
0xff, 0xff, 0xff, 0xff
};
/*! \brief Initialize renderer
*/
FWRenderer::FWRenderer() : _background(NULL), _palette(NULL), _cmd(""),
_cmdY(0), _messageBg(0), _backBuffer(new byte[_screenSize]),
_activeLowPal(NULL), _changePal(0) {
assert(_backBuffer);
memset(_backBuffer, 0, _screenSize);
memset(_bgName, 0, sizeof (_bgName));
}
/* \brief Destroy renderer
*/
FWRenderer::~FWRenderer() {
delete[] _background;
delete[] _palette;
delete[] _backBuffer;
delete[] _activeLowPal;
}
/* \brief Reset renderer state
*/
void FWRenderer::clear() {
delete[] _background;
delete[] _palette;
delete[] _activeLowPal;
_background = NULL;
_palette = NULL;
_activeLowPal = NULL;
memset(_backBuffer, 0, _screenSize);
_cmd = "";
_cmdY = 0;
_messageBg = 0;
_changePal = 0;
}
/*! \brief Draw 1bpp sprite using selected color
* \param obj Object info
* \param fillColor Sprite color
*/
void FWRenderer::fillSprite(const objectStruct &obj, uint8 color) {
const byte *data = animDataTable[obj.frame].data();
int x, y, width, height;
x = obj.x;
y = obj.y;
width = animDataTable[obj.frame]._realWidth;
height = animDataTable[obj.frame]._height;
gfxFillSprite(data, width, height, _backBuffer, x, y, color);
}
/*! \brief Draw 1bpp sprite using selected color on background
* \param obj Object info
* \param fillColor Sprite color
*/
void FWRenderer::incrustMask(const objectStruct &obj, uint8 color) {
const byte *data = animDataTable[obj.frame].data();
int x, y, width, height;
x = obj.x;
y = obj.y;
width = animDataTable[obj.frame]._realWidth;
height = animDataTable[obj.frame]._height;
gfxFillSprite(data, width, height, _background, x, y, color);
}
/*! \brief Draw color sprite using with external mask
* \param obj Object info
* \param mask External mask
*/
void FWRenderer::drawMaskedSprite(const objectStruct &obj, const byte *mask) {
const byte *data = animDataTable[obj.frame].data();
int x, y, width, height;
x = obj.x;
y = obj.y;
width = animDataTable[obj.frame]._realWidth;
height = animDataTable[obj.frame]._height;
assert(mask);
drawSpriteRaw(data, mask, width, height, _backBuffer, x, y);
}
/*! \brief Draw color sprite
* \param obj Object info
*/
void FWRenderer::drawSprite(const objectStruct &obj) {
const byte *mask = animDataTable[obj.frame].mask();
drawMaskedSprite(obj, mask);
}
/*! \brief Draw color sprite on background
* \param obj Object info
*/
void FWRenderer::incrustSprite(const objectStruct &obj) {
const byte *data = animDataTable[obj.frame].data();
const byte *mask = animDataTable[obj.frame].mask();
int x, y, width, height;
x = obj.x;
y = obj.y;
width = animDataTable[obj.frame]._realWidth;
height = animDataTable[obj.frame]._height;
assert(mask);
drawSpriteRaw(data, mask, width, height, _background, x, y);
}
/*! \brief Draw command box on screen
*/
void FWRenderer::drawCommand() {
unsigned int i;
int x = 10, y = _cmdY;
drawPlainBox(x, y, 301, 11, 0);
drawBorder(x - 1, y - 1, 302, 12, 2);
x += 2;
y += 2;
for (i = 0; i < _cmd.size(); i++) {
x = drawChar(_cmd[i], x, y);
}
}
/*! \brief Draw message in a box
* \param str Message to draw
* \param x Top left message box corner coordinate
* \param y Top left message box corner coordinate
* \param width Message box width
* \param color Message box background color
*/
void FWRenderer::drawMessage(const char *str, int x, int y, int width, byte color) {
int i, tx, ty, tw;
int line = 0, words = 0, cw = 0;
int space = 0, extraSpace = 0;
drawPlainBox(x, y, width, 4, color);
tx = x + 4;
ty = str[0] ? y - 5 : y + 4;
tw = width - 8;
for (i = 0; str[i]; i++, line--) {
// Fit line of text into textbox
if (!line) {
while (str[i] == ' ') i++;
line = fitLine(str + i, tw, words, cw);
if ( str[i + line] != '\0' && str[i + line] != 0x7C && words) {
space = (tw - cw) / words;
extraSpace = (tw - cw) % words;
} else {
space = 5;
extraSpace = 0;
}
ty += 9;
drawPlainBox(x, ty, width, 9, color);
tx = x + 4;
}
// draw characters
if (str[i] == ' ') {
tx += space + extraSpace;
if (extraSpace) {
extraSpace = 0;
}
} else {
tx = drawChar(str[i], tx, ty);
}
}
ty += 9;
drawPlainBox(x, ty, width, 4, color);
drawDoubleBorder(x, y, width, ty - y + 4, 2);
}
/*! \brief Draw rectangle on screen
* \param x Top left corner coordinate
* \param y Top left corner coordinate
* \param width Rectangle width (Negative values draw the box horizontally flipped)
* \param height Rectangle height (Negative values draw the box vertically flipped)
* \param color Fill color
* \note Rectangle's drawn width is always at least one.
* \note Rectangle's drawn height is always at least one.
*/
void FWRenderer::drawPlainBox(int x, int y, int width, int height, byte color) {
int i;
// Handle horizontally flipped boxes
if (width < 0) {
x += width;
width = ABS(width);
}
// Handle vertically flipped boxes
if (height < 0) {
y += height;
height = ABS(height);
}
// Handle horizontal boundaries
if (x < 0) {
width += x; // Remove invisible columns
x = 0; // Start drawing at the screen's left border
} else if (x > 319) {
// Nothing left to draw as we're over the screen's right border
width = 0;
}
// Handle vertical boundaries
if (y < 0) {
height += y; // Remove invisible rows
y = 0; // Start drawing at the screen's top border
} else if (y > 199) {
// Nothing left to draw as we're below the screen's bottom border
height = 0;
}
// Make width and height at least one
// which forces this function to always draw something.
// This fixes at least the showing of the oxygen gauge meter in
// Operation Stealth's first arcade sequence where this function
// is called with a height of zero.
width = MAX(1, width);
height = MAX(1, height);
// Draw the filled rectangle
byte *dest = _backBuffer + y * 320 + x;
for (i = 0; i < height; i++) {
memset(dest + i * 320, color, width);
}
}
/*! \brief Draw empty rectangle
* \param x Top left corner coordinate
* \param y Top left corner coordinate
* \param width Rectangle width
* \param height Rectangle height
* \param color Line color
*/
void FWRenderer::drawBorder(int x, int y, int width, int height, byte color) {
drawLine(x, y, width, 1, color);
drawLine(x, y + height, width, 1, color);
drawLine(x, y, 1, height, color);
drawLine(x + width, y, 1, height + 1, color);
}
/*! \brief Draw empty 2 color rectangle (inner line color is black)
* \param x Top left corner coordinate
* \param y Top left corner coordinate
* \param width Rectangle width
* \param height Rectangle height
* \param color Outter line color
*/
void FWRenderer::drawDoubleBorder(int x, int y, int width, int height, byte color) {
drawBorder(x + 1, y + 1, width - 2, height - 2, 0);
drawBorder(x, y, width, height, color);
}
/*! \brief Draw text character on screen
* \param character Character to draw
* \param x Character coordinate
* \param y Character coordinate
*/
int FWRenderer::drawChar(char character, int x, int y) {
int width, idx;
if (character == ' ') {
x += 5;
} else if ((width = g_cine->_textHandler.fontParamTable[(unsigned char)character].characterWidth)) {
idx = g_cine->_textHandler.fontParamTable[(unsigned char)character].characterIdx;
drawSpriteRaw(g_cine->_textHandler.textTable[idx][0], g_cine->_textHandler.textTable[idx][1], 16, 8, _backBuffer, x, y);
x += width + 1;
}
return x;
}
/*! \brief Draw Line
* \param x Line end coordinate
* \param y Line end coordinate
* \param width Horizontal line length
* \param height Vertical line length
* \param color Line color
* \note Either width or height must be equal to 1
*/
void FWRenderer::drawLine(int x, int y, int width, int height, byte color) {
// this line is a special case of rectangle ;-)
drawPlainBox(x, y, width, height, color);
}
/*! \brief Hide invisible parts of the sprite
* \param[in,out] mask Mask to be updated
* \param it Overlay info from overlayList
*/
void FWRenderer::remaskSprite(byte *mask, Common::List<overlay>::iterator it) {
AnimData &sprite = animDataTable[objectTable[it->objIdx].frame];
int x, y, width, height, idx;
int mx, my, mw, mh;
x = objectTable[it->objIdx].x;
y = objectTable[it->objIdx].y;
width = sprite._realWidth;
height = sprite._height;
for (++it; it != overlayList.end(); ++it) {
if (it->type != 5) {
continue;
}
idx = ABS(objectTable[it->objIdx].frame);
mx = objectTable[it->objIdx].x;
my = objectTable[it->objIdx].y;
mw = animDataTable[idx]._realWidth;
mh = animDataTable[idx]._height;
gfxUpdateSpriteMask(mask, x, y, width, height, animDataTable[idx].data(), mx, my, mw, mh);
}
}
/*! \brief Draw background to backbuffer
*/
void FWRenderer::drawBackground() {
assert(_background);
memcpy(_backBuffer, _background, _screenSize);
}
/*! \brief Draw one overlay
* \param it Overlay info
*/
void FWRenderer::renderOverlay(const Common::List<overlay>::iterator &it) {
int idx, len, width;
objectStruct *obj;
AnimData *sprite;
byte *mask;
switch (it->type) {
// color sprite
case 0:
sprite = &animDataTable[objectTable[it->objIdx].frame];
len = sprite->_realWidth * sprite->_height;
mask = new byte[len];
memcpy(mask, sprite->mask(), len);
remaskSprite(mask, it);
drawMaskedSprite(objectTable[it->objIdx], mask);
delete[] mask;
break;
// game message
case 2:
if (it->objIdx >= messageTable.size()) {
return;
}
_messageLen += messageTable[it->objIdx].size();
drawMessage(messageTable[it->objIdx].c_str(), it->x, it->y, it->width, it->color);
waitForPlayerClick = 1;
break;
// action failure message
case 3:
idx = it->objIdx * 4 + g_cine->_rnd.getRandomNumber(3);
len = strlen(failureMessages[idx]);
_messageLen += len;
width = 6 * len + 20;
width = width > 300 ? 300 : width;
drawMessage(failureMessages[idx], (320 - width) / 2, 80, width, 4);
waitForPlayerClick = 1;
break;
// bitmap
case 4:
assert(it->objIdx < NUM_MAX_OBJECT);
obj = &objectTable[it->objIdx];
if (obj->frame < 0) {
return;
}
if (!animDataTable[obj->frame].data()) {
return;
}
fillSprite(*obj);
break;
}
}
/*! \brief Draw overlays
*/
void FWRenderer::drawOverlays() {
Common::List<overlay>::iterator it;
for (it = overlayList.begin(); it != overlayList.end(); ++it) {
renderOverlay(it);
}
}
/*! \brief Draw another frame
*/
void FWRenderer::drawFrame() {
drawBackground();
drawOverlays();
if (!_cmd.empty()) {
drawCommand();
}
if (_changePal) {
refreshPalette();
}
blit();
}
/*! \brief Update screen
*/
void FWRenderer::blit() {
g_system->copyRectToScreen(_backBuffer, 320, 0, 0, 320, 200);
}
/*! \brief Set player command string
* \param cmd New command string
*/
void FWRenderer::setCommand(const char *cmd) {
_cmd = cmd;
}
/*! \brief Refresh current palette
*/
void FWRenderer::refreshPalette() {
int i;
byte pal[16*4];
assert(_activeLowPal);
for (i = 0; i < 16; i++) {
// This seems to match the output from DOSbox.
pal[i * 4 + 2] = ((_activeLowPal[i] & 0x00f) >> 0) * 32;
pal[i * 4 + 1] = ((_activeLowPal[i] & 0x0f0) >> 4) * 32;
pal[i * 4 + 0] = ((_activeLowPal[i] & 0xf00) >> 8) * 32;
pal[i * 4 + 3] = 0;
}
g_system->setPalette(pal, 0, 16);
_changePal = 0;
}
/*! \brief Load palette of current background
*/
void FWRenderer::reloadPalette() {
assert(_palette);
if (!_activeLowPal) {
_activeLowPal = new uint16[_lowPalSize];
}
assert(_activeLowPal);
memcpy(_activeLowPal, _palette, _lowPalSize * sizeof (uint16));
_changePal = 1;
}
/*! \brief Load background into renderer
* \param bg Raw background data
*/
void FWRenderer::loadBg16(const byte *bg, const char *name) {
int i;
if (!_background) {
_background = new byte[_screenSize];
}
if (!_palette) {
_palette = new uint16[_lowPalSize];
}
assert(_background && _palette);
strcpy(_bgName, name);
for (i = 0; i < _lowPalSize; i++, bg += 2) {
_palette[i] = READ_BE_UINT16(bg);
}
gfxConvertSpriteToRaw(_background, bg, 160, 200);
}
/*! \brief Placeholder for Operation Stealth implementation
*/
void FWRenderer::loadBg16(const byte *bg, const char *name, unsigned int idx) {
error("Future Wars renderer doesn't support multiple backgrounds");
}
/*! \brief Placeholder for Operation Stealth implementation
*/
void FWRenderer::loadCt16(const byte *ct, const char *name) {
error("Future Wars renderer doesn't support multiple backgrounds");
}
/*! \brief Placeholder for Operation Stealth implementation
*/
void FWRenderer::loadBg256(const byte *bg, const char *name) {
error("Future Wars renderer doesn't support 256 color mode");
}
/*! \brief Placeholder for Operation Stealth implementation
*/
void FWRenderer::loadBg256(const byte *bg, const char *name, unsigned int idx) {
error("Future Wars renderer doesn't support multiple backgrounds");
}
/*! \brief Placeholder for Operation Stealth implementation
*/
void FWRenderer::loadCt256(const byte *ct, const char *name) {
error("Future Wars renderer doesn't support multiple backgrounds");
}
/*! \brief Placeholder for Operation Stealth implementation
*/
void FWRenderer::selectBg(unsigned int idx) {
error("Future Wars renderer doesn't support multiple backgrounds");
}
/*! \brief Placeholder for Operation Stealth implementation
*/
void FWRenderer::selectScrollBg(unsigned int idx) {
error("Future Wars renderer doesn't support multiple backgrounds");
}
/*! \brief Placeholder for Operation Stealth implementation
*/
void FWRenderer::setScroll(unsigned int shift) {
error("Future Wars renderer doesn't support multiple backgrounds");
}
/*! \brief Future Wars has no scrolling backgrounds so scroll value is always zero.
*/
uint FWRenderer::getScroll() const {
return 0;
}
/*! \brief Placeholder for Operation Stealth implementation
*/
void FWRenderer::removeBg(unsigned int idx) {
error("Future Wars renderer doesn't support multiple backgrounds");
}
void FWRenderer::saveBgNames(Common::OutSaveFile &fHandle) {
fHandle.write(_bgName, 13);
}
const char *FWRenderer::getBgName(uint idx) const {
assert(idx == 0);
return _bgName;
}
/*! \brief Restore active and backup palette from save
* \param fHandle Savefile open for reading
*/
void FWRenderer::restorePalette(Common::SeekableReadStream &fHandle) {
int i;
if (!_palette) {
_palette = new uint16[_lowPalSize];
}
if (!_activeLowPal) {
_activeLowPal = new uint16[_lowPalSize];
}
assert(_palette && _activeLowPal);
for (i = 0; i < _lowPalSize; i++) {
_activeLowPal[i] = fHandle.readUint16BE();
}
for (i = 0; i < _lowPalSize; i++) {
_palette[i] = fHandle.readUint16BE();
}
_changePal = 1;
}
/*! \brief Write active and backup palette to save
* \param fHandle Savefile open for writing
*/
void FWRenderer::savePalette(Common::OutSaveFile &fHandle) {
int i;
assert(_palette && _activeLowPal);
for (i = 0; i < _lowPalSize; i++) {
fHandle.writeUint16BE(_activeLowPal[i]);
}
for (i = 0; i < _lowPalSize; i++) {
fHandle.writeUint16BE(_palette[i]);
}
}
/*! \brief Write active and backup palette to save
* \param fHandle Savefile open for writing
*/
void OSRenderer::savePalette(Common::OutSaveFile &fHandle) {
int i;
assert(_activeHiPal);
// Write the active 256 color palette.
for (i = 0; i < _hiPalSize; i++) {
fHandle.writeByte(_activeHiPal[i]);
}
// Write the active 256 color palette a second time.
// FIXME: The backup 256 color palette should be saved here instead of the active one.
for (i = 0; i < _hiPalSize; i++) {
fHandle.writeByte(_activeHiPal[i]);
}
}
/*! \brief Restore active and backup palette from save
* \param fHandle Savefile open for reading
*/
void OSRenderer::restorePalette(Common::SeekableReadStream &fHandle) {
int i;
if (!_activeHiPal) {
_activeHiPal = new byte[_hiPalSize];
}
assert(_activeHiPal);
for (i = 0; i < _hiPalSize; i++) {
_activeHiPal[i] = fHandle.readByte();
}
// Jump over the backup 256 color palette.
// FIXME: Load the backup 256 color palette and use it properly.
fHandle.seek(_hiPalSize, SEEK_CUR);
_changePal = 1;
}
/*! \brief Rotate active palette
* \param a First color to rotate
* \param b Last color to rotate
* \param c Possibly rotation step, must be equal to 1 at the moment
*/
void FWRenderer::rotatePalette(int a, int b, int c) {
palRotate(_activeLowPal, a, b, c);
refreshPalette();
}
/*! \brief Copy part of backup palette to active palette and transform
* \param first First color to transform
* \param last Last color to transform
* \param r Red channel transformation
* \param g Green channel transformation
* \param b Blue channel transformation
*/
void FWRenderer::transformPalette(int first, int last, int r, int g, int b) {
if (!_activeLowPal) {
_activeLowPal = new uint16[_lowPalSize];
memset(_activeLowPal, 0, _lowPalSize * sizeof (uint16));
}
transformPaletteRange(_activeLowPal, _palette, first, last, r, g, b);
refreshPalette();
}
/*! \brief Draw menu box, one item per line with possible highlight
* \param items Menu items
* \param height Item count
* \param x Top left menu corner coordinate
* \param y Top left menu corner coordinate
* \param width Menu box width
* \param selected Index of highlighted item (no highlight if less than 0)
*/
void FWRenderer::drawMenu(const CommandeType *items, unsigned int height, int x, int y, int width, int selected) {
int tx, ty, th = height * 9 + 10;
unsigned int i, j;
if (x + width > 319) {
x = 319 - width;
}
if (y + th > 199) {
y = 199 - th;
}
drawPlainBox(x, y, width, 4, _messageBg);
ty = y + 4;
for (i = 0; i < height; i++, ty += 9) {
drawPlainBox(x, ty, width, 9, (int)i == selected ? 0 : _messageBg);
tx = x + 4;
for (j = 0; items[i][j]; j++) {
tx = drawChar(items[i][j], tx, ty);
}
}
drawPlainBox(x, ty, width, 4, _messageBg);
drawDoubleBorder(x, y, width, ty - y + 4, 2);
}
/*! \brief Draw text input box
* \param info Input box message
* \param input Text entered in the input area
* \param cursor Cursor position in the input area
* \param x Top left input box corner coordinate
* \param y Top left input box corner coordinate
* \param width Input box width
*/
void FWRenderer::drawInputBox(const char *info, const char *input, int cursor, int x, int y, int width) {
int i, tx, ty, tw;
int line = 0, words = 0, cw = 0;
int space = 0, extraSpace = 0;
drawPlainBox(x, y, width, 4, _messageBg);
tx = x + 4;
ty = info[0] ? y - 5 : y + 4;
tw = width - 8;
// input box info message
for (i = 0; info[i]; i++, line--) {
// fit line of text
if (!line) {
line = fitLine(info + i, tw, words, cw);
if ( info[i + line] != '\0' && words) {
space = (tw - cw) / words;
extraSpace = (tw - cw) % words;
} else {
space = 5;
extraSpace = 0;
}
ty += 9;
drawPlainBox(x, ty, width, 9, _messageBg);
tx = x + 4;
}
// draw characters
if (info[i] == ' ') {
tx += space + extraSpace;
if (extraSpace) {
extraSpace = 0;
}
} else {
tx = drawChar(info[i], tx, ty);
}
}
// input area background
ty += 9;
drawPlainBox(x, ty, width, 9, _messageBg);
drawPlainBox(x + 16, ty - 1, width - 32, 9, 0);
tx = x + 20;
// text in input area
for (i = 0; input[i]; i++) {
tx = drawChar(input[i], tx, ty);
if (cursor == i + 2) {
drawLine(tx, ty - 1, 1, 9, 2);
}
}
if (!input[0] || cursor == 1) {
drawLine(x + 20, ty - 1, 1, 9, 2);
}
ty += 9;
drawPlainBox(x, ty, width, 4, _messageBg);
drawDoubleBorder(x, y, width, ty - y + 4, 2);
}
/*! \brief Fade to black
*/
void FWRenderer::fadeToBlack() {
// FIXME: _activeLowPal is invalid when starting Operation Stealth
// Adding this sanity check fixes a crash when the game
// starts, but I'm not sure if this is the best place to check it
if (!_activeLowPal) {
warning("_activeLowPal is invalid");
return;
}
assert(_activeLowPal);
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 16; j++) {
_activeLowPal[j] = transformColor(_activeLowPal[j], -1, -1, -1);
}
refreshPalette();
g_system->updateScreen();
g_system->delayMillis(50);
}
}
/*! \brief Initialize Operation Stealth renderer
*/
OSRenderer::OSRenderer() : _activeHiPal(NULL), _currentBg(0), _scrollBg(0),
_bgShift(0) {
int i;
for (i = 0; i < 9; i++) {
_bgTable[i].bg = NULL;
_bgTable[i].lowPal = NULL;
_bgTable[i].hiPal = NULL;
memset(_bgTable[i].name, 0, sizeof (_bgTable[i].name));
}
}
/*! \brief Destroy Operation Stealth renderer
*/
OSRenderer::~OSRenderer() {
delete[] _activeHiPal;
for (int i = 0; i < 9; i++) {
delete[] _bgTable[i].bg;
delete[] _bgTable[i].lowPal;
delete[] _bgTable[i].hiPal;
}
}
/*! \brief Reset Operation Stealth renderer state
*/
void OSRenderer::clear() {
delete[] _activeHiPal;
_activeHiPal = NULL;
for (int i = 0; i < 9; i++) {
delete[] _bgTable[i].bg;
delete[] _bgTable[i].lowPal;
delete[] _bgTable[i].hiPal;
_bgTable[i].bg = NULL;
_bgTable[i].lowPal = NULL;
_bgTable[i].hiPal = NULL;
memset(_bgTable[i].name, 0, sizeof (_bgTable[i].name));
}
_currentBg = 0;
_scrollBg = 0;
_bgShift = 0;
FWRenderer::clear();
}
/*! \brief Draw 1bpp sprite using selected color on backgrounds
* \param obj Object info
* \param fillColor Sprite color
*/
void OSRenderer::incrustMask(const objectStruct &obj, uint8 color) {
const byte *data = animDataTable[obj.frame].data();
int x, y, width, height, i;
x = obj.x;
y = obj.y;
width = animDataTable[obj.frame]._realWidth;
height = animDataTable[obj.frame]._height;
for (i = 0; i < 8; i++) {
if (!_bgTable[i].bg) {
continue;
}
gfxFillSprite(data, width, height, _bgTable[i].bg, x, y, color);
}
}
/*! \brief Draw color sprite
* \param obj Object info
*/
void OSRenderer::drawSprite(const objectStruct &obj) {
const byte *data = animDataTable[obj.frame].data();
int x, y, width, height, transColor;
x = obj.x;
y = obj.y;
transColor = obj.part;
width = animDataTable[obj.frame]._realWidth;
height = animDataTable[obj.frame]._height;
drawSpriteRaw2(data, transColor, width, height, _backBuffer, x, y);
}
/*! \brief Draw color sprite
* \param obj Object info
*/
void OSRenderer::incrustSprite(const objectStruct &obj) {
const byte *data = animDataTable[obj.frame].data();
int x, y, width, height, transColor, i;
x = obj.x;
y = obj.y;
transColor = obj.part;
width = animDataTable[obj.frame]._realWidth;
height = animDataTable[obj.frame]._height;
for (i = 0; i < 8; i++) {
if (!_bgTable[i].bg) {
continue;
}
drawSpriteRaw2(data, transColor, width, height, _bgTable[i].bg, x, y);
}
}
/*! \brief Draw text character on screen
* \param character Character to draw
* \param x Character coordinate
* \param y Character coordinate
*/
int OSRenderer::drawChar(char character, int x, int y) {
int width, idx;
if (character == ' ') {
x += 5;
} else if ((width = g_cine->_textHandler.fontParamTable[(unsigned char)character].characterWidth)) {
idx = g_cine->_textHandler.fontParamTable[(unsigned char)character].characterIdx;
drawSpriteRaw2(g_cine->_textHandler.textTable[idx][0], 0, 16, 8, _backBuffer, x, y);
x += width + 1;
}
return x;
}
/*! \brief Draw background to backbuffer
*/
void OSRenderer::drawBackground() {
byte *main;
main = _bgTable[_currentBg].bg;
assert(main);
if (!_bgShift) {
memcpy(_backBuffer, main, _screenSize);
} else {
byte *scroll = _bgTable[_scrollBg].bg;
int mainShift = _bgShift * _screenWidth;
int mainSize = _screenSize - mainShift;
assert(scroll);
if (mainSize > 0) { // Just a precaution
memcpy(_backBuffer, main + mainShift, mainSize);
}
if (mainShift > 0) { // Just a precaution
memcpy(_backBuffer + mainSize, scroll, mainShift);
}
}
}
/*! \brief Draw one overlay
* \param it Overlay info
* \todo Add handling of type 22 overlays
*/
void OSRenderer::renderOverlay(const Common::List<overlay>::iterator &it) {
int len;
objectStruct *obj;
AnimData *sprite;
byte *mask;
switch (it->type) {
// color sprite
case 0:
if (objectTable[it->objIdx].frame < 0) {
break;
}
sprite = &animDataTable[objectTable[it->objIdx].frame];
len = sprite->_realWidth * sprite->_height;
mask = new byte[len];
generateMask(sprite->data(), mask, len, objectTable[it->objIdx].part);
remaskSprite(mask, it);
drawMaskedSprite(objectTable[it->objIdx], mask);
delete[] mask;
break;
// game message
case 2:
if (it->objIdx >= messageTable.size()) {
return;
}
_messageLen += messageTable[it->objIdx].size();
drawMessage(messageTable[it->objIdx].c_str(), it->x, it->y, it->width, it->color);
if (it->color >= 0) { // This test isn't in Future Wars's implementation
waitForPlayerClick = 1;
}
break;
// bitmap
case 4:
if (objectTable[it->objIdx].frame >= 0) {
FWRenderer::renderOverlay(it);
}
break;
// masked background
case 20:
assert(it->objIdx < NUM_MAX_OBJECT);
var5 = it->x; // A global variable updated here!
obj = &objectTable[it->objIdx];
sprite = &animDataTable[obj->frame];
if (obj->frame < 0 || it->x < 0 || it->x > 8 || !_bgTable[it->x].bg || sprite->_bpp != 1) {
break;
}
maskBgOverlay(_bgTable[it->x].bg, sprite->data(), sprite->_realWidth, sprite->_height, _backBuffer, obj->x, obj->y);
break;
// FIXME: Looking at Operation Stealth's disassembly I can't find any codepath that
// will draw a type 21 overlay. But looking at the first arcade sequence's scripts
// it looks like the oxygen gauge meter is implemented using a type 21 overlay.
// So for the time being I'm simply drawing type 21 overlays as type 22 overlays
// and hoping for the best.
// TODO: Check how the original game looks under DOSBox to see if the oxygen gauge works in it
case 21:
// A filled rectangle:
case 22: {
// TODO: Check it this implementation really works correctly (Some things might be wrong, needs testing).
assert(it->objIdx < NUM_MAX_OBJECT);
obj = &objectTable[it->objIdx];
byte color = obj->part & 0x0F;
int width = obj->frame;
int height = obj->costume;
drawPlainBox(obj->x, obj->y, width, height, color);
debug(5, "renderOverlay: type=%d, x=%d, y=%d, width=%d, height=%d, color=%d",
it->type, obj->x, obj->y, width, height, color);
break;
}
// something else
default:
FWRenderer::renderOverlay(it);
break;
}
}
/*! \brief Refresh current palette
*/
void OSRenderer::refreshPalette() {
if (!_activeHiPal) {
FWRenderer::refreshPalette();
return;
}
int i;
byte pal[256*4];
for (i = 0; i < 256; i++) {
pal[i * 4 + 0] = _activeHiPal[i * 3 + 0];
pal[i * 4 + 1] = _activeHiPal[i * 3 + 1];
pal[i * 4 + 2] = _activeHiPal[i * 3 + 2];
pal[i * 4 + 3] = 0;
}
g_system->setPalette(pal, 0, 256);
_changePal = 0;
}
/*! \brief Load palette of current background
*/
void OSRenderer::reloadPalette() {
// selected background in plane takeoff scene has swapped colors 12
// and 14, shift background has it right
palBg *bg = _bgShift ? &_bgTable[_scrollBg] : &_bgTable[_currentBg];
assert(bg->lowPal || bg->hiPal);
if (bg->lowPal) {
if (!_activeLowPal) {
_activeLowPal = new uint16[_lowPalSize];
}
assert(_activeLowPal);
delete[] _activeHiPal;
_activeHiPal = NULL;
memcpy(_activeLowPal, bg->lowPal, _lowPalSize * sizeof (uint16));
} else {
if (!_activeHiPal) {
_activeHiPal = new byte[_hiPalSize];
}
assert(_activeHiPal);
delete[] _activeLowPal;
_activeLowPal = NULL;
memcpy(_activeHiPal, bg->hiPal, _hiPalSize);
}
_changePal = 1;
}
/*! \brief Rotate active palette
* \param a First color to rotate
* \param b Last color to rotate
* \param c Possibly rotation step, must be equal to 1 at the moment
*/
void OSRenderer::rotatePalette(int a, int b, int c) {
if (_activeLowPal) {
FWRenderer::rotatePalette(a, b, c);
return;
}
palRotate(_activeHiPal, a, b, c);
refreshPalette();
}
/*! \brief Copy part of backup palette to active palette and transform
* \param first First color to transform
* \param last Last color to transform
* \param r Red channel transformation
* \param g Green channel transformation
* \param b Blue channel transformation
*/
void OSRenderer::transformPalette(int first, int last, int r, int g, int b) {
palBg *bg = _bgShift ? &_bgTable[_scrollBg] : &_bgTable[_currentBg];
if (!bg->lowPal) {
if (!_activeHiPal) {
_activeHiPal = new byte[_hiPalSize];
memset(_activeHiPal, 0, _hiPalSize);
}
delete[] _activeLowPal;
_activeLowPal = NULL;
transformPaletteRange(_activeHiPal, bg->hiPal, first, last, r, g, b);
} else {
if (!_activeLowPal) {
_activeLowPal = new uint16[_lowPalSize];
memset(_activeLowPal, 0, _lowPalSize * sizeof (uint16));
}
delete[] _activeHiPal;
_activeHiPal = NULL;
transformPaletteRange(_activeLowPal, bg->lowPal, first, last, r, g, b);
}
refreshPalette();
}
/*! \brief Load 16 color background into renderer
* \param bg Raw background data
* \param name Background filename
*/
void OSRenderer::loadBg16(const byte *bg, const char *name) {
loadBg16(bg, name, 0);
}
/*! \brief Load 16 color background into renderer
* \param bg Raw background data
* \param name Background filename
* \param pos Background index
*/
void OSRenderer::loadBg16(const byte *bg, const char *name, unsigned int idx) {
int i;
assert(idx < 9);
if (!_bgTable[idx].bg) {
_bgTable[idx].bg = new byte[_screenSize];
}
if (!_bgTable[idx].lowPal) {
_bgTable[idx].lowPal = new uint16[_lowPalSize];
}
assert(_bgTable[idx].bg && _bgTable[idx].lowPal);
delete[] _bgTable[idx].hiPal;
_bgTable[idx].hiPal = NULL;
strcpy(_bgTable[idx].name, name);
for (i = 0; i < _lowPalSize; i++, bg += 2) {
_bgTable[idx].lowPal[i] = READ_BE_UINT16(bg);
}
gfxConvertSpriteToRaw(_bgTable[idx].bg, bg, 160, 200);
}
/*! \brief Load 16 color CT data as background into renderer
* \param ct Raw CT data
* \param name Background filename
*/
void OSRenderer::loadCt16(const byte *ct, const char *name) {
loadBg16(ct, name, 8);
}
/*! \brief Load 256 color background into renderer
* \param bg Raw background data
* \param name Background filename
*/
void OSRenderer::loadBg256(const byte *bg, const char *name) {
loadBg256(bg, name, 0);
}
/*! \brief Load 256 color background into renderer
* \param bg Raw background data
* \param name Background filename
* \param pos Background index
*/
void OSRenderer::loadBg256(const byte *bg, const char *name, unsigned int idx) {
assert(idx < 9);
if (!_bgTable[idx].bg) {
_bgTable[idx].bg = new byte[_screenSize];
}
if (!_bgTable[idx].hiPal) {
_bgTable[idx].hiPal = new byte[_hiPalSize];
}
assert(_bgTable[idx].bg && _bgTable[idx].hiPal);
delete[] _bgTable[idx].lowPal;
_bgTable[idx].lowPal = NULL;
strcpy(_bgTable[idx].name, name);
memcpy(_bgTable[idx].hiPal, bg, _hiPalSize);
memcpy(_bgTable[idx].bg, bg + _hiPalSize, _screenSize);
}
/*! \brief Load 256 color CT data as background into renderer
* \param ct Raw CT data
* \param name Background filename
*/
void OSRenderer::loadCt256(const byte *ct, const char *name) {
loadBg256(ct, name, 8);
}
/*! \brief Select active background and load its palette
* \param idx Background index
*/
void OSRenderer::selectBg(unsigned int idx) {
assert(idx < 9 && _bgTable[idx].bg);
assert(_bgTable[idx].lowPal || _bgTable[idx].hiPal);
_currentBg = idx;
reloadPalette();
}
/*! \brief Select scroll background
* \param idx Scroll background index
*/
void OSRenderer::selectScrollBg(unsigned int idx) {
assert(idx < 9);
if (_bgTable[idx].bg) {
_scrollBg = idx;
}
reloadPalette();
}
/*! \brief Set background scroll
* \param shift Background scroll in pixels
*/
void OSRenderer::setScroll(unsigned int shift) {
assert(shift <= 200);
_bgShift = shift;
}
/*! \brief Get background scroll
* \return Background scroll in pixels
*/
uint OSRenderer::getScroll() const {
return _bgShift;
}
/*! \brief Unload background from renderer
* \param idx Background to unload
*/
void OSRenderer::removeBg(unsigned int idx) {
assert(idx > 0 && idx < 9);
if (_currentBg == idx) {
_currentBg = 0;
}
if (_scrollBg == idx) {
_scrollBg = 0;
}
delete[] _bgTable[idx].bg;
delete[] _bgTable[idx].lowPal;
delete[] _bgTable[idx].hiPal;
_bgTable[idx].bg = NULL;
_bgTable[idx].lowPal = NULL;
_bgTable[idx].hiPal = NULL;
memset(_bgTable[idx].name, 0, sizeof (_bgTable[idx].name));
}
void OSRenderer::saveBgNames(Common::OutSaveFile &fHandle) {
for (int i = 0; i < 8; i++) {
fHandle.write(_bgTable[i].name, 13);
}
}
const char *OSRenderer::getBgName(uint idx) const {
assert(idx < 9);
return _bgTable[idx].name;
}
/*! \brief Fade to black
* \bug Operation Stealth sometimes seems to fade to black using
* transformPalette resulting in double fadeout
*/
void OSRenderer::fadeToBlack() {
if (!_activeHiPal) {
FWRenderer::fadeToBlack();
return;
}
for (int i = 0; i < 8; i++) {
for (int j = 0; j < _hiPalSize; j++) {
_activeHiPal[j] = CLIP(_activeHiPal[j] - 32, 0, 255);
}
refreshPalette();
g_system->updateScreen();
g_system->delayMillis(50);
}
}
void setMouseCursor(int cursor) {
static int currentMouseCursor = -1;
assert(cursor >= 0 && cursor < 3);
if (currentMouseCursor != cursor) {
byte mouseCursor[16 * 16];
const MouseCursor *mc = &mouseCursors[cursor];
const byte *src = mc->bitmap;
for (int i = 0; i < 32; ++i) {
int offs = i * 8;
for (byte mask = 0x80; mask != 0; mask >>= 1) {
if (src[0] & mask) {
mouseCursor[offs] = 1;
} else if (src[32] & mask) {
mouseCursor[offs] = 0;
} else {
mouseCursor[offs] = 0xFF;
}
++offs;
}
++src;
}
CursorMan.replaceCursor(mouseCursor, 16, 16, mc->hotspotX, mc->hotspotY);
CursorMan.replaceCursorPalette(cursorPalette, 0, 2);
currentMouseCursor = cursor;
}
}
void gfxFillSprite(const byte *spritePtr, uint16 width, uint16 height, byte *page, int16 x, int16 y, uint8 fillColor) {
int16 i, j;
for (i = 0; i < height; i++) {
byte *destPtr = page + x + y * 320;
destPtr += i * 320;
for (j = 0; j < width; j++) {
if (x + j >= 0 && x + j < 320 && i + y >= 0 && i + y < 200 && !*spritePtr) {
*destPtr = fillColor;
}
destPtr++;
spritePtr++;
}
}
}
void gfxDrawMaskedSprite(const byte *spritePtr, const byte *maskPtr, uint16 width, uint16 height, byte *page, int16 x, int16 y) {
int16 i, j;
for (i = 0; i < height; i++) {
byte *destPtr = page + x + y * 320;
destPtr += i * 320;
for (j = 0; j < width; j++) {
if (x + j >= 0 && x + j < 320 && i + y >= 0 && i + y < 200 && *maskPtr == 0) {
*destPtr = *spritePtr;
}
++destPtr;
++spritePtr;
++maskPtr;
}
}
}
void gfxUpdateSpriteMask(byte *destMask, int16 x, int16 y, int16 width, int16 height, const byte *srcMask, int16 xm, int16 ym, int16 maskWidth, int16 maskHeight) {
int16 i, j, d, spritePitch, maskPitch;
spritePitch = width;
maskPitch = maskWidth;
// crop update area to overlapping parts of masks
if (y > ym) {
d = y - ym;
srcMask += d * maskPitch;
maskHeight -= d;
} else if (y < ym) {
d = ym - y;
destMask += d * spritePitch;
height -= d;
}
if (x > xm) {
d = x - xm;
srcMask += d;
maskWidth -= d;
} else if (x < xm) {
d = xm - x;
destMask += d;
width -= d;
}
// update mask
for (j = 0; j < MIN(maskHeight, height); ++j) {
for (i = 0; i < MIN(maskWidth, width); ++i) {
destMask[i] |= srcMask[i] ^ 1;
}
destMask += spritePitch;
srcMask += maskPitch;
}
}
void gfxUpdateIncrustMask(byte *destMask, int16 x, int16 y, int16 width, int16 height, const byte *srcMask, int16 xm, int16 ym, int16 maskWidth, int16 maskHeight) {
int16 i, j, d, spritePitch, maskPitch;
spritePitch = width;
maskPitch = maskWidth;
// crop update area to overlapping parts of masks
if (y > ym) {
d = y - ym;
srcMask += d * maskPitch;
maskHeight -= d;
} else if (y < ym) {
d = ym - y > height ? height : ym - y;
memset(destMask, 1, d * spritePitch);
destMask += d * spritePitch;
height -= d;
}
if (x > xm) {
d = x - xm;
xm = x;
srcMask += d;
maskWidth -= d;
}
d = xm - x;
maskWidth += d;
// update mask
for (j = 0; j < MIN(maskHeight, height); ++j) {
for (i = 0; i < width; ++i) {
destMask[i] |= i < d || i >= maskWidth ? 1 : srcMask[i - d];
}
destMask += spritePitch;
srcMask += maskPitch;
}
if (j < height) {
memset(destMask, 1, (height - j) * spritePitch);
}
}
void gfxDrawLine(int16 x1, int16 y1, int16 x2, int16 y2, byte color, byte *page) {
if (x1 == x2) {
if (y1 > y2) {
SWAP(y1, y2);
}
while (y1 <= y2) {
*(page + (y1 * 320 + x1)) = color;
y1++;
}
} else {
if (x1 > x2) {
SWAP(x1, x2);
}
while (x1 <= x2) {
*(page + (y1 * 320 + x1)) = color;
x1++;
}
}
}
void gfxDrawPlainBoxRaw(int16 x1, int16 y1, int16 x2, int16 y2, byte color, byte *page) {
int16 t;
if (x1 > x2) {
SWAP(x1, x2);
}
if (y1 > y2) {
SWAP(y1, y2);
}
t = x1;
while (y1 <= y2) {
x1 = t;
while (x1 <= x2) {
*(page + y1 * 320 + x1) = color;
x1++;
}
y1++;
}
}
int16 gfxGetBit(int16 x, int16 y, const byte *ptr, int16 width) {
const byte *ptrToData = (ptr) + y * width + x;
if (x > width) {
return 0;
}
if (*ptrToData) {
return 0;
}
return 1;
}
void gfxResetRawPage(byte *pageRaw) {
memset(pageRaw, 0, 320 * 200);
}
void gfxConvertSpriteToRaw(byte *dst, const byte *src, uint16 w, uint16 h) {
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w / 8; ++x) {
for (int bit = 0; bit < 16; ++bit) {
uint8 color = 0;
for (int p = 0; p < 4; ++p) {
if (READ_BE_UINT16(src + p * 2) & (1 << (15 - bit))) {
color |= 1 << p;
}
}
*dst++ = color;
}
src += 8;
}
}
}
void drawSpriteRaw(const byte *spritePtr, const byte *maskPtr, int16 width, int16 height, byte *page, int16 x, int16 y) {
int16 i, j;
// FIXME: Is it a bug if maskPtr == NULL?
if (!maskPtr)
warning("drawSpriteRaw: maskPtr == NULL");
for (i = 0; i < height; i++) {
byte *destPtr = page + x + y * 320;
destPtr += i * 320;
for (j = 0; j < width; j++) {
if ((!maskPtr || !(*maskPtr)) && x + j >= 0 && x + j < 320 && i + y >= 0 && i + y < 200) {
*(destPtr++) = *(spritePtr++);
} else {
destPtr++;
spritePtr++;
}
if (maskPtr)
maskPtr++;
}
}
}
void drawSpriteRaw2(const byte *spritePtr, byte transColor, int16 width, int16 height, byte *page, int16 x, int16 y) {
int16 i, j;
for (i = 0; i < height; i++) {
byte *destPtr = page + x + y * 320;
destPtr += i * 320;
for (j = 0; j < width; j++) {
if ((*spritePtr != transColor) && (x + j >= 0 && x + j < 320 && i + y >= 0 && i + y < 200)) {
*destPtr = *spritePtr;
}
destPtr++;
spritePtr++;
}
}
}
void maskBgOverlay(const byte *bgPtr, const byte *maskPtr, int16 width, int16 height,
byte *page, int16 x, int16 y) {
int16 i, j, tmpWidth, tmpHeight;
Common::List<BGIncrust>::iterator it;
byte *mask;
const byte *backup = maskPtr;
// background pass
for (i = 0; i < height; i++) {
byte *destPtr = page + x + y * 320;
const byte *srcPtr = bgPtr + x + y * 320;
destPtr += i * 320;
srcPtr += i * 320;
for (j = 0; j < width; j++) {
if ((!maskPtr || !(*maskPtr)) && (x + j >= 0
&& x + j < 320 && i + y >= 0 && i + y < 200)) {
*destPtr = *srcPtr;
}
destPtr++;
srcPtr++;
maskPtr++;
}
}
maskPtr = backup;
// incrust pass
for (it = bgIncrustList.begin(); it != bgIncrustList.end(); ++it) {
tmpWidth = animDataTable[it->frame]._realWidth;
tmpHeight = animDataTable[it->frame]._height;
mask = (byte*)malloc(tmpWidth * tmpHeight);
if (it->param == 0) {
generateMask(animDataTable[it->frame].data(), mask, tmpWidth * tmpHeight, it->part);
gfxUpdateIncrustMask(mask, it->x, it->y, tmpWidth, tmpHeight, maskPtr, x, y, width, height);
gfxDrawMaskedSprite(animDataTable[it->frame].data(), mask, tmpWidth, tmpHeight, page, it->x, it->y);
} else {
memcpy(mask, animDataTable[it->frame].data(), tmpWidth * tmpHeight);
gfxUpdateIncrustMask(mask, it->x, it->y, tmpWidth, tmpHeight, maskPtr, x, y, width, height);
gfxFillSprite(mask, tmpWidth, tmpHeight, page, it->x, it->y);
}
free(mask);
}
}
} // End of namespace Cine
|