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
/* 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"

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;
        }



        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   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                            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
        {
            gate_index_t x;
            gate_index_t y;
            gate_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()
            : x(0.0f), y(0.0f), z(1.5), direction(0.0f),
            moveVelocity(0.0f), strafeVelocity(0.0f), angularVelocity(0.0f),
            loadedWorld(createWorld())
        {
        }

        BlockLevelsGame::~BlockLevelsGame() noexcept
        {
        }

        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));
        }


        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] = p1.x; v[1] = p1.y; v[2] = p1.z;
            v[3] = p2.x; v[4] = p2.y; v[5] = p2.z;
            v[6] = p3.x; v[7] = p3.y; v[8] = p3.z;
            v[9] = p4.x; v[10] = p4.y; v[11] = p4.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)
        {
            gate_real32_t v[12];
            load_quad_vertexes(v, p1, p2, p3, p4);

            gl.setVertexPointer(3, v);	// 3 coords form one vertex
            gl.setColorByte(col->r, col->g, col->b, col->a);
            gl.drawArrays(OpenGL::DrawMode_TriangleFan, 0, 4);	// draw 4 vertices
        }
        void BlockLevelsGame::renderQuadBorders(GlApi& gl, Vertex const& p1, Vertex const& p2, Vertex const& p3, Vertex const& p4, Color const& col)
        {
            gate_real32_t v[12];
            load_quad_vertexes(v, p1, p2, p3, p4);

            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
        }
        void BlockLevelsGame::renderQuadTexture(GlApi& gl, Vertex const& p1, Vertex const& p2, Vertex const& p3, Vertex const& p4, void* texture)
        {
            gate_real32_t v[12];
            gate_real32_t t[8] = {
                0.0f, 0.0f,
                1.0f, 0.0f,
                1.0f, 1.0f,
                0.0f, 1.0f
            };

            load_quad_vertexes(v, p1, p2, p3, p4);

            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);
        }

        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 & 0x10) BlockLevelsGame::renderQuadArea(gl, ground[0], ground[1], ground[2], ground[3], cuboid.fill);
                if (cuboid.side_bits & 0x01) BlockLevelsGame::renderQuadArea(gl, s1[0], s1[1], s1[2], s1[3], cuboid.fill);
                if (cuboid.side_bits & 0x02) BlockLevelsGame::renderQuadArea(gl, s2[0], s2[1], s2[2], s2[3], cuboid.fill);
                if (cuboid.side_bits & 0x04) BlockLevelsGame::renderQuadArea(gl, s3[0], s3[1], s3[2], s3[3], cuboid.fill);
                if (cuboid.side_bits & 0x08) BlockLevelsGame::renderQuadArea(gl, s4[0], s4[1], s4[2], s4[3], cuboid.fill);
                if (cuboid.side_bits & 0x20) BlockLevelsGame::renderQuadArea(gl, top[0], top[1], top[2], top[3], 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::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_Dirt:
            {
                static BlockTextureType blockTextures[] = { BlockTexture_Dirt };
                this->renderBlock(gl, point, blockTextures, 1, side_bits);
                break;
            }
            case Block_OakPlanks:
            {
                static BlockTextureType blockTextures[] = { BlockTexture_OakPlanks };
                this->renderBlock(gl, point, blockTextures, 1, side_bits);
                break;
            }
            case Block_Oak:
            {
                static BlockTextureType blockTextures[] = { BlockTexture_OakSide, BlockTexture_OakTop };
                this->renderBlock(gl, point, blockTextures, 2, side_bits);
                break;
            }
            case Block_OakLeaves:
            {
                static BlockTextureType blockTextures[] = { BlockTexture_OakLeaves };
                this->renderBlock(gl, point, blockTextures, 1, side_bits);
                break;
            }
            case Block_Cobble:
            {
                static BlockTextureType blockTextures[] = { BlockTexture_Cobblestone };
                this->renderBlock(gl, point, blockTextures, 1, side_bits);
                break;
            }
            case Block_Grass:
            {
                static BlockTextureType blockTextures[] = { BlockTexture_GrassSide, BlockTexture_GrassTop, BlockTexture_Dirt };
                this->renderBlock(gl, point, blockTextures, 3, side_bits);
                break;
            }
            case Block_Brick:
            {
                static BlockTextureType blockTextures[] = { BlockTexture_Brick };
                this->renderBlock(gl, point, blockTextures, 1, side_bits);
                break;
            }
            case Block_Stone:
            {
                static BlockTextureType blockTextures[] = { BlockTexture_Stone };
                this->renderBlock(gl, point, blockTextures, 1, side_bits);
                break;
            }
            case Block_StoneBrick:
            {
                static BlockTextureType blockTextures[] = { BlockTexture_StoneBrick };
                this->renderBlock(gl, point, blockTextures, 1, side_bits);
                break;
            }
            case Block_Start1:
            {
                // nothing to do
                break;
            }
            case Block_Empty:
            {
                // nothing to do
                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();

            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 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::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;

            uint32_t width = 0, height = 0;
            surface.getSize(width, height);
            OpenGL& gl = surface.getGlApi();

            {	// reset renderer
                gl.setViewport(0, 0, width, height);
                gl.setShadeModel(OpenGL::ShadeModel_Flat);
                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);

                gl.enableCapability(OpenGL::Capability_CullFace);
                gl.setCullFace(OpenGL::CullFace_Front);

                gl.enableCapability(OpenGL::Capability_DepthTest);
                gl.setDepthFunc(OpenGL::Depth_Less);
                gl.setDepthMask(true);
                //gl.setHint(OpenGL::Hint_PerspectiveCorrection, OpenGL::HintMode_Fastest);
            }

            {	// setup projection
                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);
            }

            {	// setup world
                gl.setMatrixMode(OpenGL::MatrixMode_ModelView);

                gl.loadIdentity();
                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);

                gl.lookAt(eyeX, eyeY, eyeZ,
                    lookX, lookY, lookZ,
                    upX, upY, upZ);
            }

            {	// draw objects
                bool_t translucent = false;<--- Assignment 'translucent=false', assigned value is 0<--- Assignment 'translucent=false', assigned value is 0
                if (translucent)<--- Condition 'translucent' is always false
                {
                    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 (translucent)<--- Condition 'translucent' is always false
                {
                    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;
            }
            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;
            }
            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