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 | /* GATE PROJECT LICENSE:
+----------------------------------------------------------------------------+
| Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> |
| All rights reserved. |
| |
| Redistribution and use in source and binary forms, with or without |
| modification, are permitted provided that the following conditions are met:|
| |
| 1. Redistributions of source code must retain the above copyright notice, |
| this list of conditions and the following disclaimer. |
| 2. Redistributions in binary form must reproduce the above copyright |
| notice, this list of conditions and the following disclaimer in the |
| documentation and/or other materials provided with the distribution. |
| |
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"|
| AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| THE POSSIBILITY OF SUCH DAMAGE. |
+----------------------------------------------------------------------------+
*/
#include "blocklevels.hpp"
#include "gate/graphics/gl_apis.hpp"
#include "gate/mathematics.hpp"
#include "gate/graphics/pixmapimages.hpp"
#include "textures/texture_block_cobblestone.xpm"
#include "textures/texture_block_grass_side.xpm"
#include "textures/texture_block_grass_top.xpm"
#include "textures/texture_block_dirt.xpm"
#include "textures/texture_block_oak_planks.xpm"
#include "textures/texture_block_oak_side.xpm"
#include "textures/texture_block_oak_top.xpm"
#include "textures/texture_block_oak_leaves.xpm"
#include "textures/texture_block_brick.xpm"
#include "textures/texture_block_stone.xpm"
#include "textures/texture_block_stonebrick.xpm"
#define GATEGAMES_SIMPLE_BLOCKS 1
namespace gate
{
namespace apps
{
struct Id2TextureMapping
{
BlockLevelsGame::BlockTextureType type;
char const* const* texture;
};
static Id2TextureMapping global_id_2_texture_mapping[] =
{
{ BlockLevelsGame::BlockTexture_Dirt, texture_block_dirt_xpm },
{ BlockLevelsGame::BlockTexture_Cobblestone, texture_block_cobblestone_xpm },
{ BlockLevelsGame::BlockTexture_GrassTop, texture_block_grass_top_xpm },
{ BlockLevelsGame::BlockTexture_GrassSide, texture_block_grass_side_xpm },
{ BlockLevelsGame::BlockTexture_OakPlanks, texture_block_oak_planks_xpm },
{ BlockLevelsGame::BlockTexture_OakSide, texture_block_oak_side_xpm },
{ BlockLevelsGame::BlockTexture_OakTop, texture_block_oak_top_xpm },
{ BlockLevelsGame::BlockTexture_OakLeaves, texture_block_oak_leaves_xpm },
{ BlockLevelsGame::BlockTexture_Stone, texture_block_stone_xpm },
{ BlockLevelsGame::BlockTexture_StoneBrick, texture_block_stonebrick_xpm },
{ BlockLevelsGame::BlockTexture_Brick, texture_block_brick_xpm }
};
struct CharToIdMapping
{
char character;
bool solid;
BlockLevelsGame::BlockType block;
};
static char const BlockId_Empty = ' ';
static char const BlockId_Dirt = 'D';
static char const BlockId_OakPlanks = 'P';
static char const BlockId_Oak = 'W';
static char const BlockId_OakLeaves = 'L';
static char const BlockId_Cobble = 'C';
static char const BlockId_Grass = 'G';
static char const BlockId_Brick = 'B';
static char const BlockId_Stone = 'S';
static char const BlockId_StoneBrick = 's';
static char const BlockId_Start1 = '1';
static struct CharToIdMapping char_to_block_mappings[] =
{
{ BlockId_Empty, false, BlockLevelsGame::Block_Empty },
{ BlockId_Dirt, true, BlockLevelsGame::Block_Dirt },
{ BlockId_OakPlanks, true, BlockLevelsGame::Block_OakPlanks },
{ BlockId_Oak, true, BlockLevelsGame::Block_Oak },
{ BlockId_OakLeaves, true, BlockLevelsGame::Block_OakLeaves },
{ BlockId_Cobble, true, BlockLevelsGame::Block_Cobble },
{ BlockId_Grass, true, BlockLevelsGame::Block_Grass },
{ BlockId_Brick, true, BlockLevelsGame::Block_Brick },
{ BlockId_Stone, true, BlockLevelsGame::Block_Stone },
{ BlockId_StoneBrick, true, BlockLevelsGame::Block_StoneBrick },
{ BlockId_Start1, false, BlockLevelsGame::Block_Start1 }
};
static size_t const char_to_block_mappings_count = sizeof(char_to_block_mappings) / sizeof(char_to_block_mappings[0]);
static bool isSolidBlockId(char blockId)
{
for (size_t ndx = 0; ndx != char_to_block_mappings_count; ++ndx)
{
if (char_to_block_mappings[ndx].character == blockId)
{
return char_to_block_mappings[ndx].solid;
}
}
return false;
}
struct GATE_API_LOCAL BlockLayout
{
typedef BlockLevelsGame::BlockType BlockType;
typedef BlockLevelsGame::BlockTextureType BlockTextureType;
BlockType type;
Color borderColor;
Color fillColor;
BlockTextureType textures[6];
};
static BlockLayout blockLayouts[BlockLevelsGame::Block_End];
static void initBlockLayout(
BlockLevelsGame::BlockType type, Color border, Color fill,
BlockLevelsGame::BlockTextureType tex1,
BlockLevelsGame::BlockTextureType tex2,
BlockLevelsGame::BlockTextureType tex3,
BlockLevelsGame::BlockTextureType tex4,
BlockLevelsGame::BlockTextureType texTop,
BlockLevelsGame::BlockTextureType texBottom)
{
BlockLayout& bl = blockLayouts[type];
bl.type = type;
bl.borderColor = border;
bl.fillColor = fill;
bl.textures[0] = tex1;
bl.textures[1] = tex2;
bl.textures[2] = tex3;
bl.textures[3] = tex4;
bl.textures[4] = texTop;
bl.textures[5] = texBottom;
}
static void initBlockLayout(
BlockLevelsGame::BlockType type, Color border, Color fill,
BlockLevelsGame::BlockTextureType texSide,
BlockLevelsGame::BlockTextureType texTop,
BlockLevelsGame::BlockTextureType texBottom)
{
initBlockLayout(type, border, fill, texSide, texSide, texSide, texSide, texTop, texBottom);
}
static void initBlockLayout(
BlockLevelsGame::BlockType type, Color border, Color fill,
BlockLevelsGame::BlockTextureType tex)
{
initBlockLayout(type, border, fill, tex, tex, tex, tex, tex, tex);
}
static void initBlockLayouts()
{
typedef BlockLevelsGame BLG;
initBlockLayout(BLG::Block_Dirt, Color::Black, Color::Brown, BLG::BlockTexture_Dirt);
initBlockLayout(BLG::Block_OakPlanks, Color::Black, Color(204,102, 51), BLG::BlockTexture_OakPlanks);
initBlockLayout(BLG::Block_Oak, Color::Black, Color(153, 51, 0), BLG::BlockTexture_OakSide, BLG::BlockTexture_OakTop, BLG::BlockTexture_OakTop);
initBlockLayout(BLG::Block_OakLeaves, Color::Black, Color::Green, BLG::BlockTexture_OakLeaves);
initBlockLayout(BLG::Block_Cobble, Color::Black, Color::DarkGray, BLG::BlockTexture_Cobblestone);
initBlockLayout(BLG::Block_Grass, Color::Black, Color::BrightGreen, BLG::BlockTexture_GrassSide, BLG::BlockTexture_GrassTop, BLG::BlockTexture_Dirt );
initBlockLayout(BLG::Block_Brick, Color::Black, Color(204, 51, 51), BLG::BlockTexture_Brick);
initBlockLayout(BLG::Block_Stone, Color::Black, Color(102,102,102), BLG::BlockTexture_Stone);
initBlockLayout(BLG::Block_StoneBrick, Color::Black, Color(153,153,153), BLG::BlockTexture_StoneBrick);
}
static BlockLayout const& getBlockLayout(BlockLevelsGame::BlockType type)
{
return blockLayouts[type];
}
static BlockLevelsGame::BlockType load_block_type(char chr)
{
for (size_t n = 0; n != char_to_block_mappings_count; ++n)
{
if (char_to_block_mappings[n].character == chr)
{
return char_to_block_mappings[n].block;
}
}
return BlockLevelsGame::Block_Empty;
}
static char const* const house0[] =
{
" ",
" CCCCC ",
" CPPPC ",
" CPPPC ",
" CPPPC ",
" CCCCC ",
" ",
NULL
};
static char const* const house1[] =
{
" ",
" sS Ss ",
" S S ",
" S S ",
" S S ",
" sSSSs ",
" ",
NULL
};
static char const* const house2[] =
{
" ",
" sS Ss ",
" S S ",
" ",
" S S ",
" sS Ss ",
" ",
NULL
};
static char const* const house3[] =
{
" ",
" sSSSs ",
" S S ",
" S S ",
" S S ",
" sSSSs ",
" ",
NULL
};
static char const* const house4[] =
{
"WWWWWWW",
"WWWWWWW",
"WW WW",
"WW WW",
"WW WW",
"WWWWWWW",
"WWWWWWW",
NULL
};
static char const* const house5[] =
{
" ",
" WWWWW ",
" WW WW ",
" WW WW ",
" WW WW ",
" WWWWW ",
" ",
NULL
};
static char const* const house6[] =
{
" ",
" ",
" WWW ",
" WWW ",
" WWW ",
" ",
" ",
NULL
};
static char const* const house7[] =
{
" ",
" ",
" ",
" W ",
" ",
" ",
" ",
NULL
};
static char const* const* const house_map[] =
{
house0,
house1,
house2,
house3,
house4,
house5,
house6,
NULL
};
static char const* const demo_ground0[] =
{
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCDDDDDDDDDDDDDDDDDDDDDDDDDDCC",
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
NULL
};
static char const* const demo_ground1[] =
{
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
"C C",
"C C",
"C WWWW DDD BBBBB PPPP C",
"C W D D D B P C",
"C W WWW DDDDD B PPP C",
"C W WW D D B P C",
"C WW W D D B PPPP C",
"C W D C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C PPPPPPP P C",
"C P P P P PPPPPPPPPPPPP C",
"C P PPP P P P P C",
"C P P P PPPPPPP P PP PPPPP C",
"C PPP P P P P P P P P C",
"C P P PPP P P PPPP P C",
"C PPPPPPPPP P PPPPP P P P P C",
"C C",
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
NULL
};
static char const* const demo_ground2[] =
{
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
"C C",
"C C",
"C WWWW GGG BBBBB PPPP C",
"C W G G B P C",
"C W WWW GGGGG B PPP C",
"C W WW G G B P C",
"C WW W G G B PPPP C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C 1 C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C C",
"C PPPPPPP P C",
"C P P P P PPPPPPPPPPPPP C",
"C P PPP P P P P C",
"C P P P PPPPPPP P PP PPPPP C",
"C PPP P P P P P P P P C",
"C P P PPP P P PPPP P C",
"C PPPPPPPPP P PPPPP P P P P C",
"C C",
"CCCCCCCCCCCCCCCCCCCCCCCCCCCCCC",
NULL
};
static char const* const* demo_ground_map[] =
{
demo_ground0,
demo_ground1,
demo_ground2,
NULL
};
static char const* const tree0[] =
{
" ",
" ",
" ",
" ",
" W ",
" ",
" ",
" ",
" ",
NULL
};
static char const* const tree1[] =
{
" ",
" ",
" ",
" ",
" W ",
" ",
" ",
" ",
" ",
NULL
};
static char const* const tree2[] =
{
" ",
" ",
" ",
" LLL ",
" LLWLL ",
" LLL ",
" ",
" ",
" ",
NULL
};
static char const* const tree3[] =
{
" LLLLL ",
" LLLLLLL ",
" LLLLLLL ",
"LLLLLLLLL",
"LLLLWLLLL",
"LLLLLLLLL",
" LLLLLLL ",
" LLLLLLL ",
" LLLLL ",
NULL
};
static char const* const tree4[] =
{
" LLLLL ",
" LLLLLLL ",
" LLLLLLL ",
"LLLLLLLLL",
"LLLLWLLLL",
"LLLLLLLLL",
" LLLLLLL ",
" LLLLLLL ",
" LLLLL ",
NULL
};
static char const* const tree5[] =
{
" LLL ",
" LLLLL ",
" LLLLLLL ",
" LLLLLLL ",
"LLLLWLLLL",
" LLLLLLL ",
" LLLLLLL ",
" LLLLL ",
" LLL ",
NULL
};
static char const* const tree6[] =
{
" ",
" L ",
" LLL ",
" LLLLL ",
" LLLWLLL ",
" LLLLL ",
" LLL ",
" L ",
" ",
NULL
};
static char const* const tree7[] =
{
" ",
" ",
" LLL ",
" LLLLL ",
" LLWLL ",
" LLLLL ",
" LLL ",
" ",
" ",
NULL
};
static char const* const tree8[] =
{
" ",
" ",
" ",
" LLL ",
" LLL ",
" LLL ",
" ",
" ",
" ",
NULL
};
static char const* const* tree_map[] =
{
tree0,
tree1,
tree2,
tree3,
tree4,
tree5,
tree6,
tree7,
tree8,
NULL
};
typedef struct object_map
{
index_t x;
index_t y;
index_t z;
char const* const* const* levels;
} object_map_t;
static object_map_t demo_ground = { 0, 0, 0, demo_ground_map };
static object_map_t houseA = { 20, 15, 1, house_map };
static object_map_t houseB = { 10, 20, 1, house_map };
static object_map_t houseC = { 0, 20, 1, house_map };
static object_map_t treeA = { 15, 5, 1, tree_map };
static object_map_t const* object_maps[] =
{
&demo_ground,
&houseA,
//&houseB,
//&houseC,
&treeA,
NULL
};
void BlockLevelsGame::loadObjectMapIntoWorld(BlockLevelsGame::World& world, index_t start_x, index_t start_y, index_t start_z, char const* const* const* objectLevels)
{
index_t z = start_z;
char const* const* ptrLevel;
while (NULL != (ptrLevel = *objectLevels))
{
index_t y = start_y;
char const* ptrLine;
while (NULL != (ptrLine = *ptrLevel))
{
index_t x = start_x;
char block;
while (0 != (block = *ptrLine))
{
if (block != ' ')
{
world.set(x, y, z, block);
}
++x;
++ptrLine;
}
++y;
++ptrLevel;
}
++z;
++objectLevels;
}
}
BlockLevelsGame::World::World(index_t x_length, index_t y_width, index_t z_height)
: length(x_length), width(y_width), height(z_height), buffer(static_cast<size_t>(x_length* y_width* z_height))
{
size_t totalLength = x_length * y_width * z_height;
this->buffer.add(' ', totalLength);
}
char* BlockLevelsGame::World::getBlockPtr(index_t x, index_t y, index_t z) const
{
if ((x < 0) || (y < 0) || (z < 0) || (x >= this->length) || (y >= this->width) || (z >= this->height))
{
return NULL;
}
else
{
size_t levelLength = static_cast<size_t>(this->width * this->length);
size_t bufferIndex = static_cast<size_t>(z * levelLength + y * this->length + x);
void* ptr = gate_arraylist_get(this->buffer.c_impl(), bufferIndex);
return static_cast<char*>(ptr);
}
}
void BlockLevelsGame::World::set(index_t x, index_t y, index_t z, char block)
{
char* ptr = this->getBlockPtr(x, y, z);
if (NULL != ptr)
{
*ptr = block;
}
}
char BlockLevelsGame::World::get(index_t x, index_t y, index_t z) const
{
char* ptr = this->getBlockPtr(x, y, z);
if (NULL == ptr)
{
return ' ';
}
else
{
return *ptr;
}
}
index_t BlockLevelsGame::World::Length() const
{
return this->length;
}
index_t BlockLevelsGame::World::Width() const
{
return this->width;
}
index_t BlockLevelsGame::World::Height() const
{
return this->height;
}
char BlockLevelsGame::World::operator()(index_t x, index_t y, index_t z) const
{
return this->get(x, y, z);
}
BlockLevelsGame::BlockLevelsGame()
: ptrHost(NULL), simpleBlocks(false),
translucentObjects(false), x(0.0f), y(0.0f), z(1.5), direction(0.0f),
moveVelocity(0.0f), strafeVelocity(0.0f), angularVelocity(0.0f),
loadedWorld(createWorld()),
shader_program((gate::uintptr_t)-1),
shader_loc_projection(-1),
shader_loc_view(-1),
shader_loc_model(-1),
shader_loc_position(-1),
shader_loc_color(-1),
shader_loc_texture(-1),
shader_loc_texcoord(-1),
shader_loc_usetex(-1),
vbo_positions((unsigned)-1),
vbo_colors((unsigned)-1),
vbo_usetex((unsigned)-1),
vbo_texture((unsigned)-1)
{
}
BlockLevelsGame::~BlockLevelsGame() noexcept
{
}
void BlockLevelsGame::init(GameHost& host)
{
this->ptrHost = &host;
}
static void convert_cartesian_to_gl(real32_t& x, real32_t& y, real32_t& z)
{
real32_t nx = y;
real32_t ny = z;
real32_t nz = x;
x = nx;
y = ny;
z = nz;
}
void BlockLevelsGame::addCuboid(Vertex point, Vertex dimension, Color borderColor, Color fillColor)
{
this->cuboids.add(Cuboid(point, dimension, borderColor, fillColor));
}
void BlockLevelsGame::addCuboid(Vertex point, Vertex dimension, void* const* texture_ids, size_t texture_count)
{
this->cuboids.add(Cuboid(point, dimension, texture_ids, texture_count));
}
#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
static void makePerspectiveMatrix(float fovY, float aspect, float zNear, float zFar, float* m)
{
float f = 1.0f / math::tan(fovY * (GATE_MATH_CONST_PI / 180.0f) / 2.0f);
m[0] = f / aspect;
m[1] = 0.0f;
m[2] = 0.0f;
m[3] = 0.0f;
m[4] = 0.0f;
m[5] = f;
m[6] = 0.0f;
m[7] = 0.0f;
m[8] = 0.0f;
m[9] = 0.0f;
m[10] = (zFar + zNear) / (zNear - zFar);
m[11] = -1.0f;
m[12] = 0.0f;
m[13] = 0.0f;
m[14] = (2.0f * zFar * zNear) / (zNear - zFar);
m[15] = 0.0f;
}
static void normalize(float v[3])
{
real32_t const len = math::sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
if (len > 0.0f)
{
v[0] /= len;
v[1] /= len;
v[2] /= len;
}
}
static void cross(const float a[3], const float b[3], float result[3])
{
result[0] = a[1]*b[2] - a[2]*b[1];
result[1] = a[2]*b[0] - a[0]*b[2];
result[2] = a[0]*b[1] - a[1]*b[0];
}
static void makeLookAtMatrix(float eyeX, float eyeY, float eyeZ,
float centerX, float centerY, float centerZ,
float upX, float upY, float upZ,
float* m)
{
float f[3] = { centerX - eyeX, centerY - eyeY, centerZ - eyeZ };
float up[3] = { upX, upY, upZ };
float s[3];
float u[3];
normalize(f);
normalize(up);
cross(f, up, s);
normalize(s);
cross(s, f, u);
m[0] = s[0];
m[1] = u[0];
m[2] = -f[0];
m[3] = 0.0f;
m[4] = s[1];
m[5] = u[1];
m[6] = -f[1];
m[7] = 0.0f;
m[8] = s[2];
m[9] = u[2];
m[10] = -f[2];
m[11] = 0.0f;
m[12] = -(s[0]*eyeX + s[1]*eyeY + s[2]*eyeZ);
m[13] = -(u[0]*eyeX + u[1]*eyeY + u[2]*eyeZ);
m[14] = (f[0]*eyeX + f[1]*eyeY + f[2]*eyeZ);
m[15] = 1.0f;
}
static uintptr_t load_shaders(GlApi& gl)
{
static char const* vertexShaderProjViewModelColor[] =
{
"attribute vec3 position;" GATE_STR_LF,
"attribute vec3 color;" GATE_STR_LF,
"attribute vec2 texcoord;" GATE_STR_LF,
"attribute float usetex;" GATE_STR_LF,
"varying float vUseTex;" GATE_STR_LF,
"varying vec3 vColor;" GATE_STR_LF,
"varying vec2 vTexCoord;" GATE_STR_LF,
"uniform mat4 uProjection;" GATE_STR_LF,
"uniform mat4 uView;" GATE_STR_LF,
"uniform mat4 uModel;" GATE_STR_LF,
"void main() {" GATE_STR_LF,
" gl_Position = uProjection * uView * uModel * vec4(position, 1.0);" GATE_STR_LF,
" vColor = color;" GATE_STR_LF,
" vTexCoord = texcoord;" GATE_STR_LF,
" vUseTex = usetex;" GATE_STR_LF,
"}" GATE_STR_LF,
};
static char const* fragmentShaderColor[] =
{
"precision mediump float;" GATE_STR_LF,
"uniform sampler2D uTexture;" GATE_STR_LF,
"varying float vUseTex;" GATE_STR_LF,
"varying vec3 vColor;" GATE_STR_LF,
"varying vec2 vTexCoord;" GATE_STR_LF,
"void main() {" GATE_STR_LF,
" if (vUseTex == 0.0f) {" GATE_STR_LF,
" gl_FragColor = vec4(vColor, 1.0);" GATE_STR_LF,
" } else {" GATE_STR_LF,
" gl_FragColor = texture2D(uTexture, vTexCoord);" GATE_STR_LF,
" }" GATE_STR_LF,
"}" GATE_STR_LF
};
intptr_t success = 0;
// color-vertex-shader
uintptr_t vertex_shader = gl.createShader(gate_gl2_api_shader_type_vertex);
gl.shaderSource(vertex_shader,
sizeof(vertexShaderProjViewModelColor) / sizeof(vertexShaderProjViewModelColor[0]),
vertexShaderProjViewModelColor, NULL);
gl.compileShader(vertex_shader);
gl.getShaderIv(vertex_shader, gate_gl2_api_shader_param_compile_status, &success);
if (!success)
{
char infoLog[1024];
gate_size_t infoLoglength = sizeof(infoLog) - 1;
gl.getShaderInfoLog(vertex_shader, infoLoglength, infoLoglength, infoLog);
infoLog[infoLoglength] = 0;
raiseException(results::Failed, infoLog, "glCompileShader(vertex)");
}
// color-fragment-shader
uintptr_t fragment_shader = gl.createShader(gate_gl2_api_shader_type_fragment);
gl.shaderSource(fragment_shader,
sizeof(fragmentShaderColor) / sizeof(fragmentShaderColor[0]),
fragmentShaderColor, NULL);
gl.compileShader(fragment_shader);
gl.getShaderIv(fragment_shader, gate_gl2_api_shader_param_compile_status, &success);
if (!success)
{
char infoLog[1024];
gate_size_t infoLoglength = sizeof(infoLog);<--- Assignment 'infoLoglength=sizeof(infoLog)', assigned value is 1024
gl.getShaderInfoLog(fragment_shader, infoLoglength, infoLoglength, infoLog);
infoLog[infoLoglength] = 0;<--- Array index out of bounds
raiseException(results::Failed, infoLog, "glCompileShader(fragment)");
}
uintptr_t program = gl.createProgram();
gl.attachShader(program, vertex_shader);
gl.attachShader(program, fragment_shader);
gl.linkProgram(program);
gl.getProgramIv(program,gate_gl2_api_program_param_link_status, &success);
if (!success)
{
char infoLog[1024];
gate_size_t infoLoglength = sizeof(infoLog) - 1;
gl.getProgramInfoLog(program, infoLoglength, &infoLoglength, infoLog);
infoLog[infoLoglength] = 0;
raiseException(results::Failed, infoLog, "glLinkProgram()");
}
gl.deleteShader(vertex_shader);
gl.deleteShader(fragment_shader);
return program;
}
#endif /* GATE_GRAPHICS_OPENGL2_SUPPORT */
static void load_quad_vertexes(real32_t* v,
BlockLevelsGame::Vertex const& p1, BlockLevelsGame::Vertex const& p2,
BlockLevelsGame::Vertex const& p3, BlockLevelsGame::Vertex const& p4)
{
v[0] = p2.x; v[1] = p2.y; v[2] = p2.z;
v[3] = p1.x; v[4] = p1.y; v[5] = p1.z;
v[6] = p4.x; v[7] = p4.y; v[8] = p4.z;
v[9] = p3.x; v[10] = p3.y; v[11] = p3.z;
convert_cartesian_to_gl(v[0], v[1], v[2]);
convert_cartesian_to_gl(v[3], v[4], v[5]);
convert_cartesian_to_gl(v[6], v[7], v[8]);
convert_cartesian_to_gl(v[9], v[10], v[11]);
}
void BlockLevelsGame::renderQuadArea(GlApi& gl, Vertex const& p1, Vertex const& p2, Vertex const& p3, Vertex const& p4, Color const& col)
{
real32_t v[12];
load_quad_vertexes(v, p1, p2, p3, p4);
#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
real32_t c[12];
real32_t const r = (real32_t)col->r / 255.0f;
real32_t const g = (real32_t)col->g / 255.0f;
real32_t const b = (real32_t)col->b / 255.0f;
for (size_t ndx = 0; ndx != 4; ++ndx)
{
c[ndx * 3 + 0] = r;
c[ndx * 3 + 1] = g;
c[ndx * 3 + 2] = b;
}
static real32_t const f[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
gl.bindBuffers(gate_gl2_api_buffer_type_array, this->vbo_usetex);
gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(f), f, gate_gl2_api_usage_type_static_draw);
gl.vertexAttribPointer(this->shader_loc_usetex, 1, gate_gl2_api_vertex_attrib_float, false, 0, NULL);
gl.bindBuffers(gate_gl2_api_buffer_type_array, this->vbo_positions);
gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(v), v, gate_gl2_api_usage_type_static_draw);
gl.vertexAttribPointer(this->shader_loc_position, 3, gate_gl2_api_vertex_attrib_float, false, 0, NULL);
gl.bindBuffers(gate_gl2_api_buffer_type_array, this->vbo_colors);
gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(c), c, gate_gl2_api_usage_type_static_draw);
gl.vertexAttribPointer(this->shader_loc_color, 3, gate_gl2_api_vertex_attrib_float, false, 0, NULL);
gl.drawArrays(GlApi::DrawMode_TriangleFan, 0, 4);
#else
gl.enableClientState(OpenGL::ClientState_VertexArray);
gl.setVertexPointer(3, v); // 3 coords for one vertex
gl.setColorByte(col->r, col->g, col->b, col->a);
gl.drawArrays(OpenGL::DrawMode_TriangleFan, 0, 4); // draw 4 vertices
gl.disableClientState(OpenGL::ClientState_VertexArray);
#endif
}
void BlockLevelsGame::renderQuadBorders(GlApi& gl, Vertex const& p1, Vertex const& p2, Vertex const& p3, Vertex const& p4, Color const& col)
{
real32_t v[12];
load_quad_vertexes(v, p1, p2, p3, p4);
#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
real32_t c[12];
real32_t const r = (real32_t)col->r / 255.0f;
real32_t const g = (real32_t)col->g / 255.0f;
real32_t const b = (real32_t)col->b / 255.0f;
for (size_t ndx = 0; ndx != 4; ++ndx)
{
c[ndx * 3 + 0] = r;
c[ndx * 3 + 1] = g;
c[ndx * 3 + 2] = b;
}
static real32_t const f[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
gl.bindBuffers(gate_gl2_api_buffer_type_array, this->vbo_usetex);
gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(f), f, gate_gl2_api_usage_type_static_draw);
gl.vertexAttribPointer(this->shader_loc_usetex, 1, gate_gl2_api_vertex_attrib_float, false, 0, NULL);
gl.bindBuffers(gate_gl2_api_buffer_type_array, this->vbo_positions);
gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(v), v, gate_gl2_api_usage_type_static_draw);
gl.vertexAttribPointer(this->shader_loc_position, 3, gate_gl2_api_vertex_attrib_float, false, 0, NULL);
gl.bindBuffers(gate_gl2_api_buffer_type_array, this->vbo_colors);
gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(c), c, gate_gl2_api_usage_type_static_draw);
gl.vertexAttribPointer(this->shader_loc_color, 3, gate_gl2_api_vertex_attrib_float, false, 0, NULL);
gl.drawArrays(OpenGL::DrawMode_LineStrip, 0, 4);
#else
gl.enableClientState(OpenGL::ClientState_VertexArray);
gl.setVertexPointer(3, v); // 3 coords form one vertex
gl.setColorByte(col->r, col->g, col->b, col->a);
gl.drawArrays(OpenGL::DrawMode_LineStrip, 0, 4); // draw 4 vertices
gl.disableClientState(OpenGL::ClientState_VertexArray);
#endif
}
void BlockLevelsGame::renderQuadTexture(GlApi& gl, Vertex const& p1, Vertex const& p2, Vertex const& p3, Vertex const& p4, void* texture)
{
real32_t v[12];
load_quad_vertexes(v, p1, p2, p3, p4);
#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
static real32_t const t[8] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
gl.enableClientState(OpenGL::ClientState_VertexArray);
gl.enableClientState(OpenGL::ClientState_TextureCoordArray);
gl.enableCapability(OpenGL::Capability_Texture2D);
static real32_t const f[4] = { 1.0f, 1.0f, 1.0f, 1.0f };
gl.bindBuffers(gate_gl2_api_buffer_type_array, this->vbo_usetex);
gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(f), f, gate_gl2_api_usage_type_static_draw);
gl.vertexAttribPointer(this->shader_loc_usetex, 1, gate_gl2_api_vertex_attrib_float, false, 0, NULL);
gl.bindBuffers(gate_gl2_api_buffer_type_array, this->vbo_positions);
gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(v), v, gate_gl2_api_usage_type_static_draw);
gl.vertexAttribPointer(this->shader_loc_position, 3, gate_gl2_api_vertex_attrib_float, false, 0, NULL);
gl.bindBuffers(gate_gl2_api_buffer_type_array, this->vbo_texture);
gl.bufferData(gate_gl2_api_buffer_type_array, sizeof(t), t, gate_gl2_api_usage_type_static_draw);
gl.vertexAttribPointer(this->shader_loc_texcoord, 2, gate_gl2_api_vertex_attrib_float, false, 0, NULL);
gl.activeTexture(0);
gl.bindTexture(texture);
gl.uniform1i(this->shader_loc_texture, 0);
gl.drawArrays(OpenGL::DrawMode_TriangleFan, 0, 4);
gl.disableCapability(OpenGL::Capability_Texture2D);
gl.disableClientState(OpenGL::ClientState_TextureCoordArray);
gl.disableClientState(OpenGL::ClientState_VertexArray);
#else
static real32_t const t[8] = {
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f
};
gl.enableClientState(OpenGL::ClientState_VertexArray);
gl.enableClientState(OpenGL::ClientState_TextureCoordArray);
gl.enableCapability(OpenGL::Capability_Texture2D);
gl.setVertexPointer(3, v); // 3 coords form one vertex
gl.bindTexture(texture);
gl.setTexCoordPointer(2, t);
gl.drawArrays(OpenGL::DrawMode_TriangleFan, 0, 4); // draw 4 vertices
gl.disableCapability(OpenGL::Capability_Texture2D);
gl.disableClientState(OpenGL::ClientState_TextureCoordArray);
gl.disableClientState(OpenGL::ClientState_VertexArray);
#endif
}
static uint8_t const bit_front = 0x01;
static uint8_t const bit_right = 0x02;
static uint8_t const bit_back = 0x04;
static uint8_t const bit_left = 0x08;
static uint8_t const bit_bottom = 0x10;
static uint8_t const bit_top = 0x20;
void BlockLevelsGame::renderCuboid(GlApi& gl, Cuboid const& cuboid)
{
//static real32_t patch = 0.001f;
Vertex ground[4];
ground[0] = ground[1] = ground[2] = ground[3] = cuboid.point;
ground[1].x += cuboid.dim.x;
ground[2].x += cuboid.dim.x;
ground[2].y += cuboid.dim.y;
ground[3].y += cuboid.dim.y;
Vertex top[4];
top[0] = ground[0];
top[1] = ground[1];
top[2] = ground[2];
top[3] = ground[3];
top[0].z += cuboid.dim.z;
top[1].z += cuboid.dim.z;
top[2].z += cuboid.dim.z;
top[3].z += cuboid.dim.z;
Vertex s1[4];
s1[0] = top[0];
s1[1] = top[1];
s1[2] = ground[1];
s1[3] = ground[0];
Vertex s2[4];
s2[0] = top[1];
s2[1] = top[2];
s2[2] = ground[2];
s2[3] = ground[1];
Vertex s3[4];
s3[0] = top[2];
s3[1] = top[3];
s3[2] = ground[3];
s3[3] = ground[2];
Vertex s4[4];
s4[0] = top[3];
s4[1] = top[0];
s4[2] = ground[0];
s4[3] = ground[3];
if ((cuboid.fill->a == 0) && (cuboid.border->a == 0))
{
if (cuboid.side_bits & bit_bottom) BlockLevelsGame::renderQuadTexture(gl, ground[0], ground[1], ground[2], ground[3], cuboid.textures[5]);
if (cuboid.side_bits & bit_front) BlockLevelsGame::renderQuadTexture(gl, s1[0], s1[1], s1[2], s1[3], cuboid.textures[0]);
if (cuboid.side_bits & bit_right) BlockLevelsGame::renderQuadTexture(gl, s2[0], s2[1], s2[2], s2[3], cuboid.textures[1]);
if (cuboid.side_bits & bit_back) BlockLevelsGame::renderQuadTexture(gl, s3[0], s3[1], s3[2], s3[3], cuboid.textures[2]);
if (cuboid.side_bits & bit_left) BlockLevelsGame::renderQuadTexture(gl, s4[0], s4[1], s4[2], s4[3], cuboid.textures[3]);
//BlockLevelsGame::renderQuadTexture(gl, top[0], top[1], top[2], top[3], cuboid.textures[4]);
if (cuboid.side_bits & bit_top) BlockLevelsGame::renderQuadTexture(gl, top[3], top[2], top[1], top[0], cuboid.textures[4]);
}
if (cuboid.fill->a != 0)
{
if (cuboid.side_bits & bit_bottom) BlockLevelsGame::renderQuadArea(gl, ground[0], ground[1], ground[2], ground[3], cuboid.fill);
if (cuboid.side_bits & bit_front) BlockLevelsGame::renderQuadArea(gl, s1[0], s1[1], s1[2], s1[3], cuboid.fill);
if (cuboid.side_bits & bit_right) BlockLevelsGame::renderQuadArea(gl, s2[0], s2[1], s2[2], s2[3], cuboid.fill);
if (cuboid.side_bits & bit_back) BlockLevelsGame::renderQuadArea(gl, s3[0], s3[1], s3[2], s3[3], cuboid.fill);
if (cuboid.side_bits & bit_left) BlockLevelsGame::renderQuadArea(gl, s4[0], s4[1], s4[2], s4[3], cuboid.fill);
if (cuboid.side_bits & bit_top) BlockLevelsGame::renderQuadArea(gl, top[3], top[2], top[1], top[0], cuboid.fill);
}
if (cuboid.border->a != 0)
{
//BlockLevelsGame::renderQuadBorders(gl, ground[0], ground[1], ground[2], ground[3], cuboid.border);
BlockLevelsGame::renderQuadBorders(gl, s1[0], s1[1], s1[2], s1[3], cuboid.border);
BlockLevelsGame::renderQuadBorders(gl, s2[0], s2[1], s2[2], s2[3], cuboid.border);
BlockLevelsGame::renderQuadBorders(gl, s3[0], s3[1], s3[2], s3[3], cuboid.border);
BlockLevelsGame::renderQuadBorders(gl, s4[0], s4[1], s4[2], s4[3], cuboid.border);
//BlockLevelsGame::renderQuadBorders(gl, top[0], top[1], top[2], top[3], cuboid.border);
}
}
void BlockLevelsGame::addBlock(BlockType type, Vertex const& point, uint8_t side_bits)
{
this->blocks.add(Block(type, point, side_bits));
}
void BlockLevelsGame::renderSimpleBlock(GlApi& gl, Vertex const& point, Color const& borderColor, Color const* colors, size_t colors_count, uint8_t side_bits)
{
static Vertex dim(1.0f, 1.0f, 1.0f);
Cuboid c(point, dim, borderColor, *colors);
c.side_bits = side_bits;
renderCuboid(gl, c);
}
void BlockLevelsGame::renderBlock(GlApi& gl, Vertex const& point, BlockTextureType const* textypes, size_t texcount, uint8_t side_bits)
{
static Vertex dim(1.0f, 1.0f, 1.0f);
void* texIds[6] = { 0 };
for (size_t ndx = 0; ndx != texcount; ++ndx)
{
TextureMap::iterator iter = this->textures.get(intptr_t(textypes[ndx]));
if (iter != this->textures.end())
{
texIds[ndx] = iter.value();
}
}
Cuboid c(point, dim, texIds, texcount);
c.side_bits = side_bits;
renderCuboid(gl, c);
}
char BlockLevelsGame::getBlockId(real32_t x, real32_t y, real32_t z)
{
char ret = BlockId_Empty;
index_t nx, ny, nz;
nx = static_cast<index_t>(x);
ny = static_cast<index_t>(y);
nz = static_cast<index_t>(z);
ret = this->loadedWorld(nx, this->loadedWorld.Width() - 1 - ny, nz);
return ret;
}
bool BlockLevelsGame::isSolidBlock(char blockId)
{
return isSolidBlockId(blockId);
}
bool BlockLevelsGame::isSolidBlock(real32_t x, real32_t y, real32_t z)
{
char blockId = this->getBlockId(x, y, z);
return this->isSolidBlock(blockId);
}
void BlockLevelsGame::renderBlock(GlApi& gl, BlockType type, Vertex point, uint8_t side_bits)
{
switch (type)
{
case Block_Start1:
{
// nothing to do
break;
}
case Block_Empty:
{
// nothing to do
break;
}
default:
{
BlockLayout const& layout = getBlockLayout(type);
if (this->simpleBlocks)
{
this->renderSimpleBlock(gl, point, layout.borderColor, &layout.fillColor, 1, side_bits);
}
else
{
this->renderBlock(gl, point, layout.textures, 6, side_bits);
}
break;
}
}
}
static void* loadTexture(GlApi& gl, char const* const* pixmap)
{
RasterImage image;
PixmapImages::parse(image, pixmap);
gate_color_t const* pixels = (gate_color_t const*)image.getPixelPtr(0, 0);
return gl.createTexture(GlApi::PixelFormat_Rgba, image.getWidth(), image.getHeight(), pixels);
}
BlockLevelsGame::World BlockLevelsGame::createWorld(size_t x_length, size_t y_width, size_t z_height)
{
return World(x_length, y_width, z_height);
}
void BlockLevelsGame::loadModule(GlSurface& surface)
{
GlApi& gl = surface.getGlApi();
initBlockLayouts();
size_t texCount = sizeof(global_id_2_texture_mapping) / sizeof(global_id_2_texture_mapping[0]);
for (size_t ndx = 0; ndx != texCount; ++ndx)
{
intptr_t const id = intptr_t(global_id_2_texture_mapping[ndx].type);
void* texId = loadTexture(gl, global_id_2_texture_mapping[ndx].texture);
this->textures.add(id, texId);
}
World world = createWorld(64, 64, 64);
object_map_t const* const* ptrMaps = object_maps;
while (*ptrMaps)
{
object_map_t const& omap = **ptrMaps;
loadObjectMapIntoWorld(world, omap.x, omap.y, omap.z, omap.levels);
++ptrMaps;
}
Vertex v;
const index_t max_z = world.Height();
const index_t max_y = world.Width();
const index_t max_x = world.Length();
for (index_t z = 0; z != max_z; ++z)
{
v.z = real32_t(z);
for (index_t y = 0; y != max_y; ++y)
{
v.y = real32_t(max_y - y - 1);
//v.y = real32_t(y);
for (index_t x = 0; x < max_x; ++x)
{
v.x = real32_t(x);
char block = world.get(x, y, z);
char block_left = world.get(x - 1, y, z);
char block_right = world.get(x + 1, y, z);
char block_back = world.get(x, y - 1, z);
char block_front = world.get(x, y + 1, z);
char block_bottom = world.get(x, y, z - 1);
char block_top = world.get(x, y, z + 1);
BlockType bt = load_block_type(block);
switch (bt)
{
case BlockLevelsGame::Block_Start1:
{
this->x = v.x + 0.5f;
this->y = v.y + 0.5f;
this->z = v.z + 0.5f;
break;
}
default:
{
uint8_t side_bits = 0;
if (block_left == ' ') side_bits |= bit_left;
if (block_right == ' ') side_bits |= bit_right;
if (block_front == ' ') side_bits |= bit_front;
if (block_back == ' ') side_bits |= bit_back;
if (block_top == ' ') side_bits |= bit_top;
if (block_bottom == ' ') side_bits |= bit_bottom;
this->addBlock(bt, v, side_bits);
break;
}
}
}
}
}
this->loadedWorld = world;
}
void BlockLevelsGame::unloadModule(GlSurface& surface)
{
GlApi& gl = surface.getGlApi();
this->cuboids.clear();
for (TextureMap::iterator iter(this->textures.begin()), end(this->textures.end()); iter != end; ++iter)
{
gl.deleteTexture(iter.value());
}
this->textures.clear();
}
void BlockLevelsGame::checkFloor()
{
if (this->isSolidBlock(this->x, this->y, this->z - 1))
{
//block below camera is solid, so we should try to step onto it
if (!this->isSolidBlock(this->x, this->y, this->z + 1) && !this->isSolidBlock(this->x, this->y, this->z))
{
// block above camera and of camera itself are non-solid, so we can step up.
this->z += 1.0f;
}
}
else
{
if (!this->isSolidBlock(this->x, this->y, this->z - 2))
{
// no floor below us, step down;
this->z -= 1.0f;
}
}
}
void BlockLevelsGame::move(real32_t addX, real32_t addY)
{
if (!this->isSolidBlock(this->x + addX, this->y + addY, this->z))
{
if (this->isSolidBlock(this->x + addX, this->y + addY, this->z - 1.0f))
{
if (this->isSolidBlock(this->x + addX, this->y + addY, this->z + 1.0f) || this->isSolidBlock(this->x, this->y, this->z + 1.0f))
{
// cannot move into 1-block hole
return;
}
}
if (!this->isSolidBlock(this->x + addX + math::signum(addX) * 0.5f,
this->y + addY,
this->z))
{
this->x += addX;
}
if (!this->isSolidBlock(this->x + addX,
this->y + addY + math::signum(addY) * 0.5f,
this->z))
{
this->y += addY;
}
}
}
void BlockLevelsGame::resize(GlSurface& surface, uint32_t width, uint32_t height)
{
//uint32_t width = 0, height = 0;
//surface.getSize(width, height);
OpenGL& gl = surface.getGlApi();
#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
if(this->shader_program != ((gate::uintptr_t)-1))
{
gl.disableVertexAttribArray(this->shader_loc_position);
gl.disableVertexAttribArray(this->shader_loc_color);
gl.disableVertexAttribArray(this->shader_loc_texcoord);
gl.deleteBuffers(1, &this->vbo_positions);
gl.deleteBuffers(1, &this->vbo_colors);
gl.deleteBuffers(1, &this->vbo_usetex);
gl.deleteBuffers(1, &this->vbo_texture);
gl.deleteProgram(this->shader_program);
this->shader_program = (gate::uintptr_t)-1;<--- Variable 'this->shader_program' is reassigned a value before the old one has been used.
}
this->shader_program = load_shaders(gl);<--- Variable 'this->shader_program' is reassigned a value before the old one has been used.
gl.useProgram(this->shader_program);
// create buffer and shader-attribute reference for "position" vertexes
gl.genBuffers(1, &this->vbo_positions);
this->shader_loc_position = gl.getAttribLocation(this->shader_program, "position");
gl.enableVertexAttribArray(this->shader_loc_position);
// create buffer and shader-attribute reference for "color" vertexes
gl.genBuffers(1, &this->vbo_colors);
this->shader_loc_color = gl.getAttribLocation(this->shader_program, "color");
gl.enableVertexAttribArray(this->shader_loc_color);
gl.genBuffers(1, &this->vbo_usetex);
this->shader_loc_usetex = gl.getAttribLocation(this->shader_program, "usetex");
gl.enableVertexAttribArray(this->shader_loc_usetex);
gl.genBuffers(1, &this->vbo_texture);
this->shader_loc_texcoord = gl.getAttribLocation(this->shader_program, "texcoord");
gl.enableVertexAttribArray(this->shader_loc_texcoord);
// get shader-matrix-references
this->shader_loc_projection = gl.getUniformLocation(this->shader_program, "uProjection");
this->shader_loc_view = gl.getUniformLocation(this->shader_program, "uView");
this->shader_loc_model = gl.getUniformLocation(this->shader_program, "uModel");
this->shader_loc_texture = gl.getUniformLocation(this->shader_program, "uTexture");
#endif
{ // reset renderer
gl.setViewport(0, 0, width, height);
gl.setShadeModel(OpenGL::ShadeModel_Flat);
gl.enableCapability(OpenGL::Capability_CullFace);
//gl.setCullFace(OpenGL::CullFace_Front);
gl.setCullFace(OpenGL::CullFace_Back);
gl.enableCapability(OpenGL::Capability_DepthTest);
gl.setDepthFunc(OpenGL::Depth_Less);
gl.setDepthMask(true);
//gl.setHint(OpenGL::Hint_PerspectiveCorrection, OpenGL::HintMode_Fastest);
}
{ // setup projection
#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
real32_t perspectiveAngle = 45.0f;
real32_t perspectiveNear = 0.25f;
real32_t perspectiveFar = 60.0f;
real32_t aspectRatio = real32_t(width) / real32_t(height);
real32_t projection_matrix[16];
makePerspectiveMatrix(perspectiveAngle, aspectRatio, perspectiveNear, perspectiveFar, projection_matrix);
gl.uniform_matrix4fv(this->shader_loc_projection, 1, false, projection_matrix);
#else
real32_t perspectiveAngle = 45.0f;
real32_t perspectiveNear = 0.25f;
real32_t perspectiveFar = 60.0f;
real32_t aspectRatio = real32_t(width) / real32_t(height);
gl.setMatrixMode(OpenGL::MatrixMode_Projection);
gl.loadIdentity();
gl.setPerspective(perspectiveAngle, aspectRatio, perspectiveNear, perspectiveFar);
#endif
}
{ // set model matrix
#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
const real32_t model_matrix[16] =
{
1,0,0,0,
0,1,0,0,
0,0,1,0,
0,0,0,1
};
gl.uniform_matrix4fv(this->shader_loc_model, 1, false, model_matrix);
#else
#endif
}
}
void BlockLevelsGame::render(GlSurface& surface)
{
TimeCounter now = TimeCounter::now();
time::Microseconds timeDiff = now - this->timer;
real32_t dir_x, dir_y;
real32_t add_x, add_y;
if (timeDiff.value() > 0)
{
real32_t timeSec = real32_t(timeDiff.value()) / 1000000.0f;
if (!math::isZero(this->angularVelocity))
{
this->direction += this->angularVelocity * timeSec;
}
this->checkFloor();
dir_x = math::cos(this->direction);
dir_y = math::sin(this->direction);
if (!math::isZero(this->moveVelocity))
{
real32_t length = this->moveVelocity * timeSec;
if (length > 1.0f) length = 1.0f;
add_x = length * dir_x;
add_y = length * dir_y;
this->move(add_x, add_y);
}
if (!math::isZero(this->strafeVelocity))
{
real32_t length = this->strafeVelocity * timeSec;
add_x = length * real32_t(math::cos(this->direction + math::PI * 0.5f));
add_y = length * real32_t(math::sin(this->direction + math::PI * 0.5f));
this->move(add_x, add_y);
}
}
else
{
dir_x = math::cos(this->direction);
dir_y = math::sin(this->direction);
}
timer = now;
OpenGL& gl = surface.getGlApi();
{ // setup world - view matrix
real32_t eyeX, eyeY, eyeZ;
eyeX = this->x - dir_x * 0.25f;
eyeY = this->y - dir_y * 0.25f;
eyeZ = this->z;
real32_t lookX, lookY, lookZ;
lookX = this->x + dir_x;
lookY = this->y + dir_y;
lookZ = this->z - 0.25f;
real32_t upX, upY, upZ;
upX = upY = 0.0f;
upZ = 1.0f;
convert_cartesian_to_gl(eyeX, eyeY, eyeZ);
convert_cartesian_to_gl(lookX, lookY, lookZ);
convert_cartesian_to_gl(upX, upY, upZ);
#if defined(GATE_GRAPHICS_OPENGL2_SUPPORT)
float view_matrix[16];
makeLookAtMatrix(eyeX, eyeY, eyeZ,
lookX, lookY, lookZ,
upX, upY, upZ,
view_matrix);
gl.uniform_matrix4fv(this->shader_loc_view, 1, false, view_matrix);
#else
gl.setMatrixMode(OpenGL::MatrixMode_ModelView);
gl.loadIdentity();
gl.lookAt(eyeX, eyeY, eyeZ,
lookX, lookY, lookZ,
upX, upY, upZ);
#endif
}
{ // clear screen
gl.setClearColor(0.8f, 0.8f, 1.0f, 1.0f);
gl.setClearDepth(1.0f);
gl.clear(OpenGL::ClearBit_ColorBuffer | OpenGL::ClearBit_DepthBuffer | OpenGL::ClearBit_StencilBuffer);
}
{ // draw objects
if (this->translucentObjects)
{
gl.enableCapability(OpenGL::Capability_Blend);
gl.setBlendFunc(OpenGL::Blend_SrcAlpha, OpenGL::Blend_OneMinusSrcAlpha);
}
for (CuboidList::iterator cube(this->cuboids.begin()), cubeEnd(this->cuboids.end()); cube != cubeEnd; ++cube)
{
renderCuboid(gl, *cube);
}
for (BlockList::iterator block(this->blocks.begin()), blockEnd(this->blocks.end()); block != blockEnd; ++block)
{
renderBlock(gl, block->type, block->point, block->side_bits);
}
//gl.disableClientState(OpenGL::ClientState_VertexArray);
if (this->translucentObjects)
{
gl.disableCapability(OpenGL::Capability_Blend);
}
}
{ // finish GL drawing and swap buffers
gl.flush();
surface.swapBuffers();
}
}
static real32_t const defaultMoveVelocity = 8.0f;
static real32_t const defaultAngularVelocity = 1.5f;
void BlockLevelsGame::processKeyDown(Keyboard::KeyEnum key, uint32_t state)
{
switch (key)
{
case Keyboard::Key_W:
case Keyboard::Key_Up:
{
this->moveVelocity = defaultMoveVelocity;
break;
}
case Keyboard::Key_A:
case Keyboard::Key_Left:
{
if (state & Keyboard::KeyState_Shift)
{
this->strafeVelocity = defaultMoveVelocity;
}
else
{
this->angularVelocity = defaultAngularVelocity;
}
break;
}
case Keyboard::Key_S:
case Keyboard::Key_Down:
{
this->moveVelocity = -defaultMoveVelocity;
break;
}
case Keyboard::Key_D:
case Keyboard::Key_Right:
{
if (state & Keyboard::KeyState_Shift)
{
this->strafeVelocity = -defaultMoveVelocity;
}
else
{
this->angularVelocity = -defaultAngularVelocity;
}
break;
}
case Keyboard::Key_PgUp:
{
this->z += 1.0f;
break;
}
case Keyboard::Key_PgDown:
{
this->z -= 1.0f;
break;
}
case Keyboard::Key_Escape:
case Keyboard::Key_F10:
{
this->ptrHost->suspend();
break;
}
default:
{
break;
}
}
}
void BlockLevelsGame::processKeyUp(Keyboard::KeyEnum key, uint32_t state)
{
switch (key)
{
case Keyboard::Key_W:
case Keyboard::Key_Up:
{
this->moveVelocity = 0.0f;
break;
}
case Keyboard::Key_A:
case Keyboard::Key_Left:
{
this->strafeVelocity = 0.0f;
this->angularVelocity = 0.0f;
break;
}
case Keyboard::Key_S:
case Keyboard::Key_Down:
{
this->moveVelocity = 0.0f;
break;
}
case Keyboard::Key_D:
case Keyboard::Key_Right:
{
this->strafeVelocity = 0.0f;
this->angularVelocity = 0.0f;
break;
}
case Keyboard::Key_T:
{
this->simpleBlocks = !this->simpleBlocks;
break;
}
default:
{
break;
}
}
}
void BlockLevelsGame::processPointerDown(real32_t x, real32_t y, real32_t xMax, real32_t yMax)
{
real32_t xCenter = xMax * 0.5f;
real32_t yCenter = yMax * 0.5f;
real32_t move = (y - yCenter) * -1.0f / yCenter;
real32_t direction = (x - xCenter) * -math::signum(move) / xCenter;
this->moveVelocity = move * defaultMoveVelocity;
this->angularVelocity = direction * defaultAngularVelocity;
}
void BlockLevelsGame::processPointerMove(real32_t x, real32_t y, real32_t xMax, real32_t yMax)
{
real32_t xCenter = xMax * 0.5f;
real32_t yCenter = yMax * 0.5f;
real32_t move = (y - yCenter) * -1.0f / yCenter;
real32_t direction = (x - xCenter) * -math::signum(move) / xCenter;
this->moveVelocity = move * defaultMoveVelocity;
this->angularVelocity = direction * defaultAngularVelocity;
}
void BlockLevelsGame::processPointerUp(real32_t x, real32_t y, real32_t xMax, real32_t yMax)
{
this->moveVelocity = 0.0f;
this->strafeVelocity = 0.0f;
this->angularVelocity = 0.0f;
}
} // end of namespace apps
} // end of namespace gate
|