Skip to content

Commit ea5fa29

Browse files
committed
Other: Refactor useful code out of 'breakout'.
1 parent c1bc781 commit ea5fa29

File tree

4 files changed

+122
-106
lines changed

4 files changed

+122
-106
lines changed

Experiments/BreakOut/breakout.c

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,6 @@
1-
// zcc +zx -lndos -create-app -DAMALLOC -o breakout breakout.c
2-
// zcc +zx -lndos -DAMALLOC -o breakout.bin breakout.c
3-
#include <conio.h>
4-
#include <input.h>
5-
#include <graphics.h>
6-
#include <math.h>
7-
#include <arch/zx/spectrum.h>
8-
9-
/** Generic functions */
10-
static unsigned char* attrMem = 22528;
11-
static uint8_t ticks = 0;
12-
13-
static void plot_shade(uint8_t x, uint8_t y, uint8_t density255)
14-
{
15-
// Plot this pixel based on the density.
16-
if (density255 == 255 || (uint8_t)rand() < density255)
17-
plot(x, y);
18-
}
19-
20-
static void c_plot_shade(uint8_t x, uint8_t y, uint8_t density255)
21-
{
22-
x <<= 2;
23-
y <<= 2;
24-
25-
for (uint8_t i = 0; i < 4; ++i)
26-
{
27-
const uint8_t py = y + i;
28-
for (uint8_t j = 0; j < 4; ++j)
29-
plot_shade(x + i, py, density255);
30-
}
31-
}
32-
33-
static void drawLogo(uint8_t x, uint8_t y)
34-
{
35-
if (ticks > 30)
36-
return;
37-
38-
if (ticks == 0)
39-
{
40-
// Seed the pixels - Initially all black.
41-
const char* logo[23] =
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-
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)
75-
{
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-
}
85-
}
86-
}
87-
88-
++ticks;
89-
return;
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;
105-
}
1+
// zcc +zx -lndos -create-app -DAMALLOC -o breakout ../utils.c breakout.c
2+
// zcc +zx -lndos -DAMALLOC -o breakout.bin ../utils.c breakout.c
3+
#include "../utils.h"
1064

1075
/** App-specific **/
1086
#define L 22
@@ -219,7 +117,6 @@ void main()
219117
textbackground(BLACK);
220118
textcolor(WHITE);
221119
clrscr();
222-
ticks = 0;
223120

224121
// Footer.
225122
gotoxy(40, 23);

Experiments/BreakOut/breakout.tap

-4 Bytes
Binary file not shown.

Experiments/utils.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#include "utils.h"
2+
3+
unsigned char* attrMem = 22528;
4+
static uint8_t ticks = 0;
5+
6+
void plot_shade(uint8_t x, uint8_t y, uint8_t density255)
7+
{
8+
// Plot this pixel based on the density.
9+
if (density255 == 255 || (uint8_t)rand() < density255)
10+
plot(x, y);
11+
}
12+
13+
void c_plot_shade(uint8_t x, uint8_t y, uint8_t density255)
14+
{
15+
x <<= 2;
16+
y <<= 2;
17+
18+
for (uint8_t i = 0; i < 4; ++i)
19+
{
20+
const uint8_t py = y + i;
21+
for (uint8_t j = 0; j < 4; ++j)
22+
plot_shade(x + i, py, density255);
23+
}
24+
}
25+
26+
void drawLogo(uint8_t x, uint8_t y)
27+
{
28+
if (ticks > 30)
29+
return;
30+
31+
if (ticks == 0)
32+
{
33+
// Seed the pixels - Initially all black.
34+
const char* logo[23] =
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+
for (uint8_t j = 0; j < 23 * 2; ++j)
62+
{
63+
textcolor(BLACK);
64+
uint8_t* l = logo[j >> 1];
65+
const uint8_t w = strlen((const char*)l);
66+
const uint8_t py = j + y;
67+
for (uint8_t px = x; px < x + w; ++px)
68+
{
69+
switch (*l++)
70+
{
71+
case '*':
72+
c_plot(px, py);
73+
break;
74+
case '.':
75+
c_plot_shade(px, py, 40);
76+
break;
77+
}
78+
}
79+
}
80+
81+
++ticks;
82+
return;
83+
}
84+
85+
// Apply the color over time.
86+
y >>= 1;
87+
x >>= 1;
88+
89+
static const uint8_t logoC[] = { INK_WHITE + BRIGHT, INK_WHITE, INK_CYAN, INK_GREEN, INK_MAGENTA, INK_MAGENTA, INK_RED, INK_RED };
90+
uint8_t j = ticks, i = 0;
91+
while (j >= 0 && i < 7)
92+
{
93+
attrMem[((j + y) << 5) + x + i] = logoC[j & 0x07];
94+
--j; ++i;
95+
}
96+
97+
++ticks;
98+
}

Experiments/utils.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef UTILS_H
2+
#define UTILS_H
3+
4+
#include <conio.h>
5+
#include <input.h>
6+
#include <graphics.h>
7+
#include <math.h>
8+
#include <arch/zx/spectrum.h>
9+
10+
extern unsigned char* attrMem;
11+
12+
/// @brief Plot a single pixel using a random dither.
13+
void plot_shade(uint8_t x, uint8_t y, uint8_t density255);
14+
15+
/// @brief Plot a single chunky pixel using a random dither.
16+
void c_plot_shade(uint8_t x, uint8_t y, uint8_t density255);
17+
18+
/// @brief Draw a frame from the DTC logo. (Call once per game loop.)
19+
void drawLogo(uint8_t x, uint8_t y);
20+
21+
#endif

0 commit comments

Comments
 (0)