|
1 | 1 | // zcc +zx -lndos -create-app -DAMALLOC -o breakout breakout.c |
| 2 | +// zcc +zx -lndos -DAMALLOC -o breakout.bin breakout.c |
2 | 3 | #include <conio.h> |
3 | 4 | #include <input.h> |
4 | 5 | #include <graphics.h> |
5 | 6 | #include <math.h> |
6 | 7 | #include <arch/zx/spectrum.h> |
7 | 8 |
|
8 | | -#define L 22 |
9 | | -#define WALL 2 |
10 | | -#define DARK_C INK_BLUE |
11 | | -#define LIGHT_C INK_CYAN |
12 | | - |
| 9 | +/** Generic functions */ |
13 | 10 | static unsigned char* attrMem = 22528; |
14 | | -static uint8_t* grid; |
| 11 | +static uint8_t ticks = 0; |
15 | 12 |
|
16 | | -static void plot_shade(uint8_t x, uint8_t y, uint8_t density) |
| 13 | +static void plot_shade(uint8_t x, uint8_t y, uint8_t density255) |
17 | 14 | { |
18 | | - if (density == 0) |
19 | | - return; |
20 | | - if (density >= 100) |
21 | | - { |
| 15 | + // Plot this pixel based on the density. |
| 16 | + if (density255 == 255 || (uint8_t)rand() < density255) |
22 | 17 | plot(x, y); |
23 | | - return; |
24 | | - } |
25 | | - |
26 | | - // Convert density to a range from 0 to 100 for comparison |
27 | | - uint8_t threshold = (uint8_t)((density * 255) / 100); |
28 | | - |
29 | | - // Use rand() to decide whether to plot this pixel based on the density |
30 | | - if ((rand() % 256) < threshold) { |
31 | | - plot(x, y); // Plot if within the density threshold |
32 | | - } |
33 | 18 | } |
34 | 19 |
|
35 | | -static void c_plot_shade(uint8_t x, uint8_t y, uint8_t density) |
| 20 | +static void c_plot_shade(uint8_t x, uint8_t y, uint8_t density255) |
36 | 21 | { |
37 | 22 | x <<= 2; |
38 | 23 | y <<= 2; |
39 | 24 |
|
40 | 25 | for (uint8_t i = 0; i < 4; ++i) |
41 | 26 | { |
| 27 | + const uint8_t py = y + i; |
42 | 28 | for (uint8_t j = 0; j < 4; ++j) |
43 | | - plot_shade(x + i, y + j, density); |
| 29 | + plot_shade(x + i, py, density255); |
44 | 30 | } |
45 | 31 | } |
46 | 32 |
|
47 | 33 | static void drawLogo(uint8_t x, uint8_t y) |
48 | 34 | { |
49 | | - const char* logo[23] = |
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 | | - const uint8_t c[] = { WHITE, LIGHTGRAY, LIGHTCYAN, LIGHTGREEN, LIGHTMAGENTA, LIGHTRED, RED, RED }; |
76 | | - for (uint8_t j = 0; j < 23 * 2; ++j) |
| 35 | + if (ticks > 30) |
| 36 | + return; |
| 37 | + |
| 38 | + if (ticks == 0) |
77 | 39 | { |
78 | | - textcolor(c[(j >> 1) % 8]); |
79 | | - for (uint8_t i = 0; i < 13; ++i) |
| 40 | + // Seed the pixels - Initially all black. |
| 41 | + const char* logo[23] = |
80 | 42 | { |
81 | | - switch (logo[j >> 1][i]) |
| 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 | + for (uint8_t j = 0; j < 23 * 2; ++j) |
| 69 | + { |
| 70 | + textcolor(BLACK); |
| 71 | + uint8_t* l = logo[j >> 1]; |
| 72 | + const uint8_t w = strlen((const char*)l); |
| 73 | + const uint8_t py = j + y; |
| 74 | + for (uint8_t px = x; px < x + w; ++px) |
82 | 75 | { |
83 | | - case '*': |
84 | | - c_plot(i + x, j + y); |
85 | | - break; |
86 | | - case '.': |
87 | | - c_plot_shade(i + x, j + y, 15); |
88 | | - break; |
| 76 | + switch (*l++) |
| 77 | + { |
| 78 | + case '*': |
| 79 | + c_plot(px, py); |
| 80 | + break; |
| 81 | + case '.': |
| 82 | + c_plot_shade(px, py, 40); |
| 83 | + break; |
| 84 | + } |
89 | 85 | } |
90 | 86 | } |
| 87 | + |
| 88 | + ++ticks; |
| 89 | + return; |
91 | 90 | } |
| 91 | + |
| 92 | + // Apply the color over time. |
| 93 | + y >>= 1; |
| 94 | + x >>= 1; |
| 95 | + |
| 96 | + static const uint8_t logoC[] = { INK_WHITE + BRIGHT, INK_WHITE, INK_CYAN, INK_GREEN, INK_MAGENTA, INK_MAGENTA, INK_RED, INK_RED }; |
| 97 | + uint8_t j = ticks, i = 0; |
| 98 | + while (j >= 0 && i < 7) |
| 99 | + { |
| 100 | + attrMem[((j + y) << 5) + x + i] = logoC[j & 0x07]; |
| 101 | + --j; ++i; |
| 102 | + } |
| 103 | + |
| 104 | + ++ticks; |
92 | 105 | } |
93 | 106 |
|
| 107 | +/** App-specific **/ |
| 108 | +#define L 22 |
| 109 | +#define WALL 2 |
| 110 | +#define DARK_C INK_BLUE |
| 111 | +#define LIGHT_C INK_CYAN |
| 112 | + |
| 113 | +static uint8_t* grid; |
| 114 | + |
94 | 115 | static unsigned int gridToScreen(uint8_t x, uint8_t y) |
95 | 116 | { |
96 | 117 | return ((y + 1) << 5) + 9 + x; |
@@ -198,13 +219,12 @@ void main() |
198 | 219 | textbackground(BLACK); |
199 | 220 | textcolor(WHITE); |
200 | 221 | clrscr(); |
| 222 | + ticks = 0; |
201 | 223 |
|
202 | 224 | // Footer. |
203 | 225 | gotoxy(40, 23); |
204 | 226 | printf("Breakout (@DeanTheCoder)"); |
205 | 227 |
|
206 | | - drawLogo(3, 1); |
207 | | - |
208 | 228 | // Init the grid. |
209 | 229 | grid = malloc(L * L); |
210 | 230 | memset(grid, 1, L * L); // Clear all. |
@@ -238,6 +258,9 @@ void main() |
238 | 258 | in_GetKeyReset(); |
239 | 259 | while (!in_GetKey()) |
240 | 260 | { |
| 261 | + // Refresh the logo. |
| 262 | + drawLogo(3, 1); |
| 263 | + |
241 | 264 | // Move ballz. |
242 | 265 | for (uint8_t phase = 0; phase < 4; ++phase) |
243 | 266 | { |
|
0 commit comments