-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathex7.c
391 lines (308 loc) · 6.68 KB
/
ex7.c
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
/*=========================================================================
GFX EXAMPLE CODE - #7
"starship game, a recap of all things"
Copyright (C) 2004 Rafael de Oliveira Jannone
This example's source code is Public Domain.
WARNING: The author makes no guarantees and holds no responsibility for
any damage, injury or loss that may result from the use of this source
code. USE IT AT YOUR OWN RISK.
Contact the author:
by e-mail : rafael AT jannone DOT org
homepage : http://jannone.org/gfxlib
ICQ UIN : 10115284
=========================================================================*/
// note: this is my personal favorite demo :) - Rafael
#include "gfx.h"
// shapes and attributes
extern u_char grass1[8];
extern u_char grass1_attr[8];
extern u_char grass2[8];
extern u_char grass2_attr[8];
extern u_char water1[8];
extern u_char water2[8];
extern u_char water3[8];
extern u_char spaceship[32];
extern u_char fire[32];
// terrain definitions
#define MAP_WIDTH 32
#define MAP_HEIGHT 24
#define MAP_SIZE MAP_WIDTH * MAP_HEIGHT
// helper
#define KEEP_RANGE(var, min, max) \
if (var < min) var = min; else if (var > max) var = max
// terrain functions
void show_map(u_char *map, int line_start) {
int y, addr;
u_char *l;
l = map + (line_start * MAP_WIDTH);
addr = 6144;
for (y = line_start; y < MAP_HEIGHT; y++) {
vwrite(l, addr, MAP_WIDTH);
addr += MAP_WIDTH;
l += MAP_WIDTH;
}
l = map;
for (y = 0; y < line_start; y++) {
vwrite(l, addr, MAP_WIDTH);
addr += MAP_WIDTH;
l += MAP_WIDTH;
}
}
void map_fill_line(u_char* line, int river_width, int river_offs) {
int river_left;
u_char *last;
river_left = (MAP_WIDTH - river_width) / 2 + river_offs;
last = line + MAP_WIDTH;
while (river_left--)
*line++ = (get_rnd() & 1) + '%';
while (river_width--)
*line++ = (get_rnd() % 3) + '"';
while (line < last)
*line++ = (get_rnd() & 1) + '%';
}
void randomize_river(int *width, int *offs) {
*width += (get_rnd() & 2) - 1;
*offs += (get_rnd() & 2) - 1;
KEEP_RANGE(*width, 8, 16);
KEEP_RANGE(*offs, -4, 4);
}
// player data
typedef struct {
int x;
int y;
int vx, vy;
bool trig;
} player_t;
// rocket data
typedef struct {
int x;
int y;
u_char sp;
bool active;
} rocket_t;
// maximum number of rockets at a time
#define MAX_ROCKET 3
// rocket functions
rocket_t* alloc_rocket(rocket_t *array) {
int c;
for (c = 0; c < MAX_ROCKET; c++) {
if (!array->active) {
array->active = true;
array->sp = 10 + c;
return array;
}
++array;
}
return NULL;
}
void free_rocket(rocket_t *r) {
r->active = false;
put_sprite_16(r->sp, 0, 192, 0, 0);
}
void initialize_rockets(rocket_t *array) {
int c;
for (c = 0; c < MAX_ROCKET; c++)
free_rocket(array++);
}
void move_rockets(rocket_t *array) {
int c;
for (c = 0; c < MAX_ROCKET; c++) {
if (array->active) {
if ((array->y -= 7) < 0)
free_rocket(array);
else
put_sprite_16(array->sp, array->x, array->y, 1, 6);
}
++array;
}
}
// player functions
void move_player(player_t *player) {
u_char s;
char ax, ay;
s = st_dir[get_stick(0)];
ay = (s & st_up) ? -1 : ((s & st_down) ? 1 : 0);
ax = (s & st_left) ? -1 : ((s & st_right) ? 1 : 0);
player->vx += ax;
player->vy += ay;
KEEP_RANGE(player->vx, -3, 3);
KEEP_RANGE(player->vy, -3, 3);
player->x += player->vx;
player->y += player->vy;
put_sprite_16(0, player->x, player->y, 0, 15);
put_sprite_16(31, player->x + 6, player->y + 6, 0, 1);
}
// main :)
main() {
// allocating stuff
int c, river_width, river_offs;
player_t player;
rocket_t rockets[MAX_ROCKET], *rckt;
u_char *l;
u_char map[MAP_WIDTH * MAP_HEIGHT];
// set screen
set_color(15, 1, 1);
set_mangled_mode();
// set sprites
set_sprite_mode(sprite_large);
set_sprite_16(0, spaceship);
set_sprite_16(1, fire);
// set char shapes and colors
set_char('"', water1, NULL, 0x54, place_all);
set_char('#', water2, NULL, 0x54, place_all);
set_char('$', water3, NULL, 0x54, place_all);
set_char('%', grass1, grass1_attr, 0, place_all);
set_char('&', grass2, grass2_attr, 0, place_all);
// initializing
initialize_rockets(rockets);
river_width = 16;
river_offs = 0;
l = map + MAP_SIZE - MAP_WIDTH;
for (c = 0; c < MAP_HEIGHT; c++) {
randomize_river(&river_width, &river_offs);
map_fill_line(l, river_width, river_offs);
l -= MAP_WIDTH;
}
c = 0;
player.x = 128;
player.y = 96;
player.vx = player.vy = player.trig = 0;
// main loop
for (;;) {
move_player(&player);
player.trig |= get_trigger(0);
if ((c & 2) && player.trig && (rckt = alloc_rocket(rockets))) {
rckt->x = player.x + 4;
rckt->y = player.y - 8;
player.trig = false;
}
move_rockets(rockets);
if (--c < 0)
c += MAP_HEIGHT;
l = map + c * MAP_WIDTH;
randomize_river(&river_width, &river_offs);
map_fill_line(l, river_width, river_offs);
show_map(map, c);
}
}
// binary data (shapes, attributes, whatever...)
#asm
psect data
global _grass1
_grass1:
defb 00000000B
defb 00111000B
defb 00000000B
defb 10000011B
defb 00000000B
defb 00110000B
defb 00000100B
defb 00011010B
global _grass1_attr
_grass1_attr:
defb 032h ;00000000B
defb 032h ;00111000B
defb 032h ;00000000B
defb 0B2h ;10000011B
defb 032h ;00000000B
defb 032h ;00110000B
defb 0B2h ;00000100B
defb 0A2h ;00011010B
global _grass2
_grass2:
defb 01011000B
defb 00000000B
defb 00000000B
defb 11100000B
defb 00000000B
defb 00000111B
defb 00000000B
defb 00100000B
global _grass2_attr
_grass2_attr:
defb 0A2h
defb 032h
defb 032h
defb 0B2h
defb 032h
defb 032h
defb 032h
defb 0B2h
global _water1
_water1:
defb 00000000B
defb 11111000B
defb 00000000B
defb 00000000B
defb 00111111B
defb 00000000B
defb 00000000B
defb 00000000B
global _water2
_water2:
defb 00000000B
defb 00000000B
defb 00000000B
defb 00000000B
defb 11111111B
defb 00000000B
defb 00000111B
defb 00000000B
global _water3
_water3:
defb 0,0,0,0,0,0,0,0
global _spaceship
_spaceship:
defb 00000011B
defb 00000011B
defb 00000011B
defb 00000111B
defb 00000111B
defb 11000111B
defb 11000111B
defb 11100111B
defb 11101110B
defb 11101110B
defb 11111110B
defb 11111111B
defb 11100111B
defb 11000011B
defb 11110000B
defb 10010000B
defb 11000000B
defb 11000000B
defb 11000000B
defb 11100000B
defb 11100000B
defb 11100011B
defb 11100011B
defb 11100111B
defb 01110111B
defb 01110111B
defb 01111111B
defb 11111111B
defb 11100111B
defb 11000011B
defb 00001111B
defb 00001001B
global _fire
_fire:
defb 00011000B
defb 00111100B
defb 00111100B
defb 01111110B
defb 01111110B
defb 01111110B
defb 01110110B
defb 01010100B
defb 00010000B
defb 00010000B
defb 00010000B
defb 00000000B
defb 00000000B
defb 00000000B
defb 00000000B
defb 00000000B
defb 0,0,0,0,0,0,0,0
defb 0,0,0,0,0,0,0,0
#endasm