-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
201 lines (159 loc) · 3.25 KB
/
Copy pathmain.c
File metadata and controls
201 lines (159 loc) · 3.25 KB
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
/* main.c */
asm(
".code16gcc\n"
"call main\n"
"call exit\n"
);
#include <gui.h>
#include <assembly.h>
#include <shapes.h>
void *heaptr, *heapsaved;
extern boolean videoinit;
void plot(int16 x, int16 y, int8 color) {
point *p;
save();
p = mkpoint(x, y, color);
if (p)
drawpoint(p);
load();
return;
}
void trail(int16 x0, int16 y0, int16 x1, int16 y1, int8 color) {
int16 t;
if (x0 == x1) {
if (y0 > y1) { t = y0; y0 = y1; y1 = t; }
for (; y0 <= y1; y0++)
plot(x0, y0, color);
}
else if (y0 == y1) {
if (x0 > x1) { t = x0; x0 = x1; x1 = t; }
for (; x0 <= x1; x0++)
plot(x0, y0, color);
}
return;
}
void box(int16 x1, int16 y1, int16 x2, int16 y2, int8 color) {
point *p1, *p2;
rectangle *rct;
int16 t;
if (x1 > x2) { t = x1; x1 = x2; x2 = t; }
if (y1 > y2) { t = y1; y1 = y2; y2 = t; }
if ((x1 == x2) || (y1 == y2))
return;
save();
p1 = mkpoint(x1, y1, color);
p2 = mkpoint(x2, y2, color);
if (p1 && p2) {
rct = mkrectangle(p1, p2, color, 0, 2, false);
if (rct)
drawrectangle(rct);
}
load();
return;
}
void main() {
int16 cx, cy;
int16 oldx, oldy;
int16 anchorx, anchory;
int16 step;
int16 maxx, maxy;
int8 clr;
int8 key;
boolean anchored;
videoinit = false;
freeall();
videomode(x640x480x16);
maxx = getmaxx();
maxy = getmaxy();
cx = 320;
cy = 240;
anchorx = 0;
anchory = 0;
step = 8;
clr = 2;
anchored = false;
plot(cx, cy, clr);
for (;;) {
key = getchar();
if (key == 'q')
break;
if (key == 'c') {
clr++;
if (clr > 0xf)
clr = 1;
continue;
}
else if (key == ' ') {
anchorx = cx;
anchory = cy;
anchored = true;
continue;
}
else if (key == 'r') {
if (anchored)
box(anchorx, anchory, cx, cy, clr);
anchored = false;
continue;
}
oldx = cx;
oldy = cy;
if (key == 'w') {
if (cy >= step)
cy -= step;
}
else if (key == 's') {
if ((cy + step) <= maxy)
cy += step;
}
else if (key == 'a') {
if (cx >= step)
cx -= step;
}
else if (key == 'd') {
if ((cx + step) <= maxx)
cx += step;
}
else
continue;
trail(oldx, oldy, cx, cy, clr);
}
freeall();
return;
}
int8 getchar(void) {
return xgetchar();
}
void putchar(int8 c) {
xputchar(c);
return;
}
void print(int8 *str) {
int8 *p;
for (p=str; *p; p++)
putchar(*p);
return;
}
void *alloc(int16 size) {
void *p;
int16 newsize;
if (!size)
return $v 0;
newsize = ((heaptr-heap1)+size);
if (newsize > heapsz)
return $v 0;
p = heaptr;
heaptr += size;
return p;
}
void freeall() {
heaptr = heap1;
return;
}
void save() {
heapsaved = heaptr;
return;
}
void load() {
heaptr = heapsaved;
return;
}