1 /* ----------------------------------------------------------------
2  * BIFROST*2 ENGINE DEMO - converted to z88dk C Compiler
3  *
4  * See README.md for information and compile instructions.
5  *-----------------------------------------------------------------
6  */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <input.h>
11 #include <arch/zx.h>
12 #include <arch/zx/bifrost2.h>
13 
14 #pragma printf = "%c"
15 
16 #pragma output CLIB_MALLOC_HEAP_SIZE = 0            // do not create a heap
17 #pragma output REGISTER_SP           = -1           // do not change sp
18 
19 #define printInk(k)          printf("\x10%c", '0'+k)
20 #define printPaper(k)        printf("\x11%c", '0'+k)
21 #define printAt(row, col)    printf("\x16%c%c", (col)+1, (row)+1)
22 
23 extern unsigned char ctiles[];
24 
pressAnyKey()25 void pressAnyKey() {
26     in_wait_nokey();
27 
28     printInk(7);
29     printAt(23, 4);
30     printf("PRESS ANY KEY!");
31 
32     in_wait_key();
33 
34     printAt(23, 4);
35     printf("              ");
36 }
37 
main()38 main()
39 {
40     int px, py, tile, attr, f, key;
41 
42     zx_border(0);
43     zx_cls(0);
44     printPaper(0);
45     printInk(4);
46     printAt(5, 22);
47     printf("BIFROST*2");
48     printAt(7, 23);
49     printf("ENGINE");
50     printAt(9, 22);
51     printf("BY EINAR");
52 
53     // Install BIFROST*2 ENGINE (first time only!)
54     BIFROST2_install();
55 
56     // Configure tile images address
57     BIFROST2_resetTileImages(_ctiles);
58 
59     // Clear tilemap area
60     for (f = 0; f < 110; ++f) {
61         BIFROST2_tilemap[f] = BIFROST2_DISABLED;
62     }
63 
64     // Start BIFROST*2 ENGINE
65     BIFROST2_start();
66 
67     while (1) {
68 
69         printInk(5);
70         printAt(16, 23);
71         printf("STATIC  ");
72         printAt(18, 23);
73         printf("TILES");
74 
75         // Fill tilemap with random static tiles
76         for (f = 0; f < 110; ++f) {
77             tile = rand() & 63;
78             if (tile < 32)
79                 BIFROST2_tilemap[f] = BIFROST2_STATIC + tile;
80         }
81 
82         pressAnyKey();
83 
84         printInk(5);
85         printAt(16, 23);
86         printf("ANIMATED");
87         printAt(20, 22);
88         printf("(4 FRAMES)");
89 
90         // Animate tiles
91         for (f = 0; f < 110; ++f) {
92             if (BIFROST2_tilemap[f] != BIFROST2_DISABLED)
93                 BIFROST2_tilemap[f] -= BIFROST2_STATIC;
94         }
95 
96         printInk(7);
97         printAt(23, 1);
98         printf("CHOOSE FRAMES 2-4 OR 0 TO EXIT");
99 
100         BIFROST2_resetAnim4Frames();
101 
102         in_wait_nokey();
103 
104         // Modify number of animation frames
105         printInk(5);
106         while ((key = in_inkey()) != '0') {
107             switch (key) {
108                 case '2':
109                     BIFROST2_resetAnim2Frames();
110                     printAt(20, 23);
111                     printf("2");
112                     break;
113                 case '4':
114                     BIFROST2_resetAnim4Frames();
115                     printAt(20, 23);
116                     printf("4");
117                     break;
118             }
119         }
120 
121         printAt(23, 1);
122         printf("                              ");
123 
124         printInk(5);
125         printAt(16, 23);
126         printf("PAINTING");
127         printAt(20, 22);
128         printf("          ");
129 
130         // Disable, paint and erase tiles
131         px = 0;
132         py = 0;
133         do {
134             attr = rand() & 127;
135             BIFROST2_setTile(px, py, BIFROST2_DISABLED);
136             BIFROST2_halt();
137             BIFROST2_fillTileAttrH(PX2LIN(px), PY2COL(py), attr);
138             BIFROST2_halt();
139             BIFROST2_fillTileAttrH(PX2LIN(px), PY2COL(py), 0);
140             py += 7;
141             if (py > 9) {
142                 py -= 10;
143                 px = (px+1) % 11;
144             }
145         } while (px || py);
146 
147         pressAnyKey();
148     }
149 }
150