forked from kilobyte/lastbeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmodeasm.c
575 lines (461 loc) · 12.5 KB
/
xmodeasm.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/* Allegro port Copyright (C) 2014 Gavin Smith. */
/*
;---------------------------------------------------------;
; ;
; ;
; This module contains GRAPHICS PRIMITIVES for ;
; VGA undocumented 320x240 256 color mode. ;
; ;
; written by Dany Schoch for Alpha-Helix ;
; Parts of this code first appeard in Dr.Dobb's ;
; September 1991 issue (during my military service). ;
; ;
*/
#include "xmode.h"
#include "baller.h"
extern volatile int subtick;
int run_in_fullscreen = 0;
void toggle_fullscreen (void)
{
run_in_fullscreen = !run_in_fullscreen;
setxmode ();
setstandardpalette ();
}
void setxmode (void)
{
set_color_depth (8);
request_refresh_rate (60);
set_gfx_mode (run_in_fullscreen ? GFX_AUTODETECT_FULLSCREEN
: GFX_AUTODETECT_WINDOWED,
DISPLAY_SCALE * 320,
DISPLAY_SCALE * 240, 0, 0);
}
#if 0
int
smooth_move (int difference, int subtick)
{
int ret = 0;
int sign = 1;
if (difference < 0)
{
sign = -1;
difference *= -1;
}
ret += difference / RESOLUTION;
if (subtick < difference % RESOLUTION)
{
if (difference > 0)
ret++;
if (difference < 0)
ret--;
}
return ret * sign;
}
#endif
#if 1
int
smooth_move (int difference, int subtick)
{
int ret = 0;
int sign = 1;
int leftover;
if (difference < 0)
{
sign = -1;
difference *= -1;
}
ret += difference / RESOLUTION;
leftover = difference % RESOLUTION;
if (leftover > 0 && leftover <= RESOLUTION/2)
{
/* Divide the leftover units to move over RESOLUTION frames. */
int interval = RESOLUTION / leftover;
if (subtick % interval == 0)
ret++;
}
else if (leftover > 0)
{
/* Divide the frames NOT to move over RESOLUTION frames. */
int interval = RESOLUTION / (RESOLUTION - leftover);
if (subtick % interval != 0)
ret++;
}
return ret * sign;
}
#endif
/* Scroll the star field */
void gostarfield(void)
{
unsigned long write_address;
int i;
_sfield.go = TRUE;
page ^= 1; /* Flip page so to draw to hidden page */
bmp_select(pages[page]);
for (i = 0; i < _sfield.n; i++) {
unsigned short int x, y, speed;
x = starstrc_member (_sfield.star, i, STAR_X);
y = starstrc_member (_sfield.star, i, STAR_Y);
speed = starstrc_member (_sfield.star, i, STAR_SPEED);
y += speed;
if (y >= windowy1) {
y -= windowy1;
}
write_address = bmp_write_line(pages[page], y);
bmp_write8 (write_address + x, backgrndcolor); /* Clear pixel */
}
bmp_unwrite_line (pages[page]);
/* Back to original page */
page ^= 1;
}
void stopstarfield(void)
{
int i;
unsigned long write_address;
_sfield.go = FALSE;
page ^= 1;
for (i = 0; i < _sfield.n; i++) {
int x, y;
int speed;
x = starstrc_member (_sfield.star, i, STAR_X);
y = starstrc_member (_sfield.star, i, STAR_Y);
speed = starstrc_member (_sfield.star, i, STAR_SPEED);
/* FIXME: Broken for RESOLUTION == 1 (no mod 1 arith) */
y -= smooth_move (speed, (subtick + RESOLUTION - 1) % RESOLUTION);
if (y < 0)
y += windowy1;
write_address = bmp_write_line(pages[page], y);
bmp_write8 (write_address + x, backgrndcolor); /* Clear pixel */
}
page ^= 1;
}
/* Draw the starfield */
void starfield (void)
{
unsigned long write_address;
int i;
if (!_sfield.active) return;
bmp_select(pages[page]);
if (_sfield.go == TRUE) {
for (i = 0; i < _sfield.n; i++) {
short int x, y, speed, colour;
x = starstrc_member (_sfield.star, i, STAR_X);
y = starstrc_member (_sfield.star, i, STAR_Y);
speed = starstrc_member (_sfield.star, i, STAR_SPEED);
colour = starstrc_member (_sfield.star, i, STAR_COLOR);
/* FIXME: Broken for RESOLUTION == 1 (no mod 1 arith) */
y -= smooth_move (speed, (subtick + RESOLUTION - 1) % RESOLUTION);
if (y < 0)
y += windowy1;
/* Clear pixel drawm on this page two frames ago. */
write_address = bmp_write_line(pages[page], y);
bmp_write8 (write_address + x, backgrndcolor);
y = starstrc_member (_sfield.star, i, STAR_Y);
y += smooth_move (speed, subtick);
if (y >= windowy1)
y -= windowy1;
write_address = bmp_write_line(pages[page], y);
bmp_write8 (write_address + x, colour); /* Set pixel */
/* This saves where the star was drawn in this frame. */
set_starstrc_member (_sfield.star, i, STAR_Y, y);
}
} else {
for (i = 0; i < _sfield.n; i++) {
unsigned short int x, y, speed, colour;
x = starstrc_member (_sfield.star, i, STAR_X);
y = starstrc_member (_sfield.star, i, STAR_Y);
speed = starstrc_member (_sfield.star, i, STAR_SPEED);
colour = starstrc_member (_sfield.star, i, STAR_COLOR);
write_address = bmp_write_line(pages[page], y);
bmp_write8 (write_address + x, colour); /* Set pixel */
}
}
bmp_unwrite_line (pages[page]);
}
/*
defobject.
This sub defines an animated object.
args: A HANDLE of a sprite and the xstart and ystart coords.
ret : HANDLE if free slot found or else ... have a guess.
*/
int defobject(int sprite, int x, int y, int flags)
{
struct objstrc *s;
int nobjs;
int dir;
int halves_tested = 0;
start:
if (halves_tested == 2)
return -1;
/* Depending on the priority bit, we either start in the middle and look
up for an empty slot, or start in the middle and look down. */
s = (struct objstrc *) _obj + (MAXOBJS/2);
dir = -1;
if (flags & OBJ_HIGH) {
dir = 1;
}
nobjs = MAXOBJS/2;
while (nobjs--) {
if (s->active == FALSE) {
s->sprite = sprite;
struct lowspr *ls = &_sprite[s->sprite];
s->xs = ls->xs;
s->ys = ls->ys;
s->maxn = ls->maxn;
s->nadd = ls->nadd;
s->n = 0;
s->x = x;
s->xa = x;
s->xb = x;
s->y = y;
s->ya = y;
s->yb = y;
s->cycle = (flags & OBJ_ONECYCLE);
s->flags = 0;
s->destroy = 0;
s->active = TRUE;
return s - _obj;
}
s += dir;
}
flags ^= OBJ_HIGH; /* Invert priority bit. */
halves_tested++;
goto start;
}
/* Moves the OBJECT 'obj' to the given location.
RETURNS: 0 if sprite cycled, non-zero otherwise. (what does that
mean??) */
int moveobject(int obj, int x, int y)
{
_obj [obj].x = x;
_obj [obj].y = y;
}
/* Moves the OBJECT 'obj' relative by the given values. */
int moveobjectdelta(int obj, int deltax, int deltay)
{
_obj [obj].x += deltax;
_obj [obj].y += deltay;
}
/* Set the object's flash bit. This will paint it
in the current flashcolor for one frame. */
void flash(int obj)
{
_obj[obj].flags |= O_FLASH;
}
/* Change the object's look. */
int changesprite(short obj, short sprite)
{
_obj[obj].sprite = sprite;
_obj[obj].maxn = _sprite[sprite].maxn;
_obj[obj].n = 0;
_obj[obj].xs = _sprite[sprite].xs;
_obj[obj].ys = _sprite[sprite].ys;
/* Probably not used. */
return _sprite[sprite].xs;
}
/* Kills object and removes it from both screen pages during next
two calls to 'updatescreen'. */
void abandonobject(int obj)
{
_obj[obj].destroy = 2;
}
/* Kills object and imediately removes it from both screen pages. */
void killobject(int obj)
{
removesprite (_obj[obj].sprite, _obj[obj].x, _obj[obj].y);
page ^= 1;
removesprite (_obj[obj].sprite, _obj[obj].x, _obj[obj].y);
page ^= 1;
_obj[obj].active = FALSE;
}
void killallobjects(void)
{
struct objstrc *s = _obj;
int ctr = MAXOBJS;
while (ctr--) {
s->active = FALSE;
s++;
}
}
/* This procy tests whether the given objects 'obj1' and 'obj2'
are overlapping each other. If so 1 will be returned, otherwise 0. */
int crashtest(int obj1, int obj2)
{
struct objstrc *o1, *o2;
o1 = &_obj[obj1]; /* was si */
o2 = &_obj[obj2]; /* was di */
/* Check x coords. */
if ( o1->x + o1->xs > o2->x
&& o1->x < o2->x + o2->xs
&& o1->y + o1->ys > o2->y
&& o1->y < o2->y + o2->ys)
return 1; /* H I T !!!!! */
return 0;
}
/* Checks whether the object is out of the currently defined
action window. */
int outofwindow(int obj)
{
if ( _obj[obj].x < windowx0
|| _obj[obj].x > windowx1
|| _obj[obj].y < windowy0
|| _obj[obj].y > windowy1)
return 1;
return 0;
}
/* Display one of the two pages (p==0 or p==1) */
void showpage(int p)
{
//show_video_bitmap (pages[p]);
stretch_blit (full_pages[p], screen,
0, 0, full_pages[p]->w, full_pages[p]->h,
0, 0, screen->w, screen->h);
}
/* Draw spr at (xp, yp). n may be a frame number. */
void putspritedirect (struct sprstrc *spr, int xp, int yp, int n)
{
int width, height, ofs;
int x,y,p;
unsigned char *data;
unsigned char *datap;
uintptr_t write_address;
/* Point at struct sprstrc representing the frame. */
width = spr->xs;
height = spr->ys;
data = spr->data + n * (width * height);
for (p = 0; p <= 1; p++) {
datap = data;
bmp_select(full_pages[p]);
for (y = 0; y < height; y++) {
write_address = bmp_write_line(full_pages[p], yp + y);
for (x = 0; x < width; x++) {
bmp_write8 (write_address + xp + x, *datap);
datap++;
}
}
bmp_unwrite_line (full_pages[p]);
}
}
void putsprite(int sprite, int x, int y, int n)
{
BITMAP *s = _sprite[sprite].bmp[n];
masked_blit(s, pages[page], 0, 0, x, y, s->w, s->h);
}
static void putflash (int handle, int x, int y, int n)
{
BITMAP *s = _sprite[handle].bmp_silhouette[n];
masked_blit(s, pages[page], 0, 0, x, y, s->w, s->h);
}
void removesprite(int sprite, int x, int y)
{
BITMAP *s = _sprite[sprite].bmp[0];
rectfill (pages[page],
x, y,
x + s->w - 1, y + s->h - 1, backgrndcolor);
}
/* updatescreen.
updatescreen removes the old objects from the backscreen and
redraws them at the latest positions. */
void updatescreen(void)
{
page ^= 1;
starfield();
struct objstrc *obj = _obj; /* was si */
int nleft = MAXOBJS; /* was bp */
/* Remove old objects from backscreen */
do {
if (obj->active) {
int nextn;
if (subtick == 0) {
nextn = obj->n + obj->nadd;
if (nextn >= obj->maxn) {
if (obj->cycle != 0) {
obj->destroy = 2;
obj->cycle = 2;
}
nextn = 0;
}
obj->n = nextn;
}
removesprite (obj->sprite, obj->xb, obj->yb);
if (obj->destroy != 0) {
obj->destroy--;
if (obj->destroy == 0) {
obj->active = FALSE;
}
}
}
obj++;
} while (--nleft);
obj = _obj;
nleft = MAXOBJS;
do {
if (obj->active) {
obj->xb = obj->xa;
obj->yb = obj->ya;
obj->xa = obj->x;
obj->ya = obj->y;
if (obj->destroy == 0) {
if (obj->flags & O_FLASH) {
copycolor (FLASH_COLOUR, objflashcolor);
putflash (obj->sprite, obj->x, obj->y, obj->n/2);
obj->flags &= ~O_FLASH; /* Clear flag */
} else {
putsprite (obj->sprite, obj->x, obj->y, obj->n/2);
}
}
}
obj++;
} while (--nleft);
showpage (page);
}
/* copypage: Copies a screen page to another.
scr: source page (0, or 1)
dst: destination page (0 or 1) */
void copypage(int src, int dst)
{
if (src == dst) return;
blit (full_pages[src], full_pages[dst], 0, 0, 0, 0,
full_pages[dst]->w, full_pages[dst]->h);
return;
}
PALETTE tmp_palette;
/* Slowly fades out the screen. */
void glowout()
{
int i, c;
showpage(page);
for (i = 0; i <= 63; i += 6) {
for (c = 0; c <= 255; c++) {
RGB rgb;
rgb.r = MAX(palette[c].r - i, 0);
rgb.g = MAX(palette[c].g - i, 0);
rgb.b = MAX(palette[c].b - i, 0);
tmp_palette[c] = rgb;
}
set_palette (tmp_palette);
}
}
void glowin (int dir)
{
int i, c;
for (c = 0; c <= 255; c++) {
RGB rgb;
rgb.r = 0;
rgb.g = 0;
rgb.b = 0;
tmp_palette[c] = rgb;
}
set_palette(tmp_palette);
showpage(page);
for (i = 62; i >= 0; i -= 6) {
for (c = 0; c <= 255; c++) {
RGB rgb;
rgb.r = MAX(palette[c].r - i, 0);
rgb.g = MAX(palette[c].g - i, 0);
rgb.b = MAX(palette[c].b - i, 0);
tmp_palette[c] = rgb;
}
set_palette(tmp_palette);
}
set_palette(palette);
}