/
yunlimited.c
412 lines (320 loc) · 9.59 KB
/
yunlimited.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
#include <exec/exec.h>
#include <dos/dos.h>
#include <intuition/intuition.h>
#include <graphics/gfx.h>
#include <hardware/custom.h>
#include <hardware/dmabits.h>
#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/graphics_protos.h>
#include <clib/intuition_protos.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "hardware.h"
#include "cop_all.h"
#include "global_defs.h"
#include "common.h"
#define EXTRAHEIGHT 32
#define BITMAPWIDTH SCREENWIDTH
#define BITMAPBYTESPERROW (BITMAPWIDTH / 8)
#define BITMAPHEIGHT ((SCREENHEIGHT + EXTRAHEIGHT) * 2)
#define HALFBITMAPHEIGHT (BITMAPHEIGHT / 2)
#define BLOCKSWIDTH 320
#define BLOCKSHEIGHT 200
#define BLOCKSBYTESPERROW (BLOCKSWIDTH / 8)
#define BLOCKSPERROW (BLOCKSWIDTH / BLOCKWIDTH)
#define BITMAPBLOCKSPERROW (BITMAPWIDTH / BLOCKWIDTH)
#define BITMAPBLOCKSPERCOL (BITMAPHEIGHT / BLOCKHEIGHT)
#define HALFBITMAPBLOCKSPERCOL (BITMAPBLOCKSPERCOL / 2)
#define VISIBLEBLOCKSX (SCREENWIDTH / BLOCKWIDTH)
#define VISIBLEBLOCKSY (SCREENHEIGHT / BLOCKHEIGHT)
#define BITMAPPLANELINES (BITMAPHEIGHT * BLOCKSDEPTH)
#define BLOCKPLANELINES (BLOCKHEIGHT * BLOCKSDEPTH)
#define BLOCKSFILESIZE (BLOCKSWIDTH * BLOCKSHEIGHT * BLOCKSPLANES / 8 + PALSIZE)
// calculate how many times (steps) y-scrolling needs to
// blit two blocks instead of one block to make sure a
// complete row is blitted after 16 pixels of y-scrolling
//
// x * 2 + (16 - x) = BITMAPBLOCKSPERROW
// 2x + 16 - x = BITMAPBLOCKSPERROW
// x = BITMAPBLOCKSPERROW - 16
#define TWOBLOCKS (BITMAPBLOCKSPERROW - NUMSTEPS)
#define TWOBLOCKSTEP (NUMSTEPS - TWOBLOCKS)
struct Screen *scr;
struct RastPort *ScreenRastPort;
struct BitMap *BlocksBitmap,*ScreenBitmap;
UBYTE *frontbuffer, *blocksbuffer;
WORD mapposy,videoposy;
WORD bitmapheight;
struct PrgOptions options;
struct LevelMap level_map;
UWORD colors[BLOCKSCOLORS];
char s[256];
struct FetchInfo fetchinfo [] =
{
{0x38,0xD0,0}, /* normal */
{0x38,0xC8,0}, /* BPL32 */
{0x38,0xC8,0}, /* BPAGEM */
{0x38,0xB8,0} /* BPL32 + BPAGEM */
};
/************* SETUP/CLEANUP ROUTINES ***************/
static void Cleanup (char *msg)
{
WORD rc;
if (msg) {
printf("Error: %s\n",msg);
rc = RETURN_WARN;
} else {
rc = RETURN_OK;
}
if (scr) CloseScreen(scr);
if (ScreenBitmap) {
WaitBlit();
FreeBitMap(ScreenBitmap);
}
if (BlocksBitmap) {
WaitBlit();
FreeBitMap(BlocksBitmap);
}
if (level_map.raw_map) FreeVec(level_map.raw_map);
exit(rc);
}
static void OpenDisplay(void)
{
DisplayInfoHandle dih;
struct DimensionInfo diminfo;
ULONG modeid;
LONG l;
bitmapheight = BITMAPHEIGHT + 3;
if (!(ScreenBitmap = AllocBitMap(BITMAPWIDTH, bitmapheight, BLOCKSDEPTH,
BMF_STANDARD | BMF_INTERLEAVED | BMF_CLEAR,0))) {
Cleanup("Can't alloc screen bitmap!");
}
frontbuffer = ScreenBitmap->Planes[0];
if (!(TypeOfMem(ScreenBitmap->Planes[0]) & MEMF_CHIP)) {
Cleanup("Screen bitmap is not in CHIP RAM!?? If you have a gfx card try disabling \"planes to fast\" or similiar options in your RTG system!");
}
l = GetBitMapAttr(ScreenBitmap,BMA_FLAGS);
if (!(GetBitMapAttr(ScreenBitmap,BMA_FLAGS) & BMF_INTERLEAVED)) {
Cleanup("Screen bitmap is not in interleaved format!??");
}
modeid = get_mode_id(options.how, options.ntsc);
if (!(scr = OpenScreenTags(0,SA_Width,BITMAPWIDTH,
SA_Height,bitmapheight,
SA_Depth,BLOCKSDEPTH,
SA_DisplayID,modeid,
SA_BitMap,ScreenBitmap,
options.how ? SA_Overscan : TAG_IGNORE,OSCAN_TEXT,
options.how ? SA_AutoScroll : TAG_IGNORE,TRUE,
SA_Quiet,TRUE,
TAG_DONE)))
{
Cleanup("Can't open screen!");
}
if (scr->RastPort.BitMap->Planes[0] != ScreenBitmap->Planes[0])
{
Cleanup("Screen was not created with the custom bitmap I supplied!??");
}
ScreenRastPort = &scr->RastPort;
LoadRGB4(&scr->ViewPort,colors,BLOCKSCOLORS);
}
static void InitCopperlist(void)
{
WORD *wp;
LONG l;
wait_vbl();
custom->dmacon = 0x7FFF;
custom->beamcon0 = options.ntsc ? 0 : DISPLAYPAL;
CopFETCHMODE[1] = options.fetchmode;
// bitplane control registers
CopBPLCON0[1] = ((BLOCKSDEPTH * BPL0_BPU0_F) & BPL0_BPUMASK) +
((BLOCKSDEPTH / 8) * BPL0_BPU3_F) +
BPL0_COLOR_F +
(options.speed ? 0 : BPL0_USEBPLCON3_F);
CopBPLCON1[1] = 0;
CopBPLCON3[1] = BPLCON3_BRDNBLNK;
// bitplane modulos
l = BITMAPBYTESPERROW * BLOCKSDEPTH -
SCREENBYTESPERROW - fetchinfo[options.fetchmode].modulooffset;
CopBPLMODA[1] = l;
CopBPLMODB[1] = l;
// display window start/stop
CopDIWSTART[1] = DIWSTART;
CopDIWSTOP[1] = DIWSTOP;
// display data fetch start/stop
CopDDFSTART[1] = fetchinfo[options.fetchmode].ddfstart;
CopDDFSTOP[1] = fetchinfo[options.fetchmode].ddfstop;
// plane pointers
wp = CopPLANE1H;
for (l = 0; l < BLOCKSDEPTH; l++) {
wp[1] = (WORD)(((ULONG)ScreenBitmap->Planes[l]) >> 16);
wp[3] = (WORD)(((ULONG)ScreenBitmap->Planes[l]) & 0xFFFF);
wp += 4;
}
custom->intena = 0x7FFF;
custom->dmacon = DMAF_SETCLR | DMAF_BLITTER | DMAF_COPPER | DMAF_RASTER | DMAF_MASTER;
custom->cop2lc = (ULONG)CopperList;
}
/******************* SCROLLING **********************/
static void DrawBlock(LONG x,LONG y,LONG mapx,LONG mapy)
{
UBYTE block;
// x = in pixels
// y = in "planelines" (1 realline = BLOCKSDEPTH planelines)
x = (x / 8) & 0xFFFE;
y = y * BITMAPBYTESPERROW;
block = level_map.data[mapy * level_map.width + mapx];
mapx = (block % BLOCKSPERROW) * (BLOCKWIDTH / 8);
mapy = (block / BLOCKSPERROW) * (BLOCKPLANELINES * BLOCKSBYTESPERROW);
if (options.how) OwnBlitter();
hard_wait_blit();
custom->bltcon0 = 0x9F0; // use A and D. Op: D = A
custom->bltcon1 = 0;
custom->bltafwm = 0xFFFF;
custom->bltalwm = 0xFFFF;
custom->bltamod = BLOCKSBYTESPERROW - (BLOCKWIDTH / 8);
custom->bltdmod = BITMAPBYTESPERROW - (BLOCKWIDTH / 8);
custom->bltapt = blocksbuffer + mapy + mapx;
custom->bltdpt = frontbuffer + y + x;
custom->bltsize = BLOCKPLANELINES * 64 + (BLOCKWIDTH / 16);
if (options.how) DisownBlitter();
}
static void FillScreen(void)
{
WORD a,b,x,y;
for (b = 0; b < HALFBITMAPBLOCKSPERCOL; b++) {
for (a = 0; a < BITMAPBLOCKSPERROW; a++) {
x = a * BLOCKWIDTH;
y = b * BLOCKPLANELINES;
DrawBlock(x,y,a,b);
DrawBlock(x,y + HALFBITMAPHEIGHT * BLOCKSDEPTH,a,b);
}
}
}
static void ScrollUp(void)
{
WORD mapx,mapy,x,y;
if (mapposy < 1) return;
mapposy--;
videoposy = mapposy % HALFBITMAPHEIGHT;
mapx = mapposy & (NUMSTEPS - 1);
mapy = mapposy / BLOCKHEIGHT;
y = ROUND2BLOCKHEIGHT(videoposy) * BLOCKSDEPTH;
if (mapx < TWOBLOCKSTEP) {
// blit only one block per half bitmap
x = mapx * BLOCKWIDTH;
DrawBlock(x,y,mapx,mapy);
DrawBlock(x,y + HALFBITMAPHEIGHT * BLOCKSDEPTH,mapx,mapy);
} else {
// blit two blocks per half bitmap
mapx = TWOBLOCKSTEP + (mapx - TWOBLOCKSTEP) * 2;
x = mapx * BLOCKWIDTH;
DrawBlock(x,y,mapx,mapy);
DrawBlock(x,y + HALFBITMAPHEIGHT * BLOCKSDEPTH,mapx,mapy);
DrawBlock(x + BLOCKWIDTH,y,mapx + 1,mapy);
DrawBlock(x + BLOCKWIDTH,y + HALFBITMAPHEIGHT * BLOCKSDEPTH,mapx + 1,mapy);
}
}
static void ScrollDown(void)
{
WORD mapx,mapy,x,y;
if (mapposy >= (level_map.height * BLOCKHEIGHT - SCREENHEIGHT - BLOCKHEIGHT)) return;
mapx = mapposy & (NUMSTEPS - 1);
mapy = HALFBITMAPBLOCKSPERCOL + mapposy / BLOCKHEIGHT;
y = ROUND2BLOCKHEIGHT(videoposy) * BLOCKSDEPTH;
if (mapx < TWOBLOCKSTEP) {
// blit only one block per half bitmap
x = mapx * BLOCKWIDTH;
DrawBlock(x,y,mapx,mapy);
DrawBlock(x,y + HALFBITMAPHEIGHT * BLOCKSDEPTH,mapx,mapy);
} else {
// blit two blocks per half bitmap
mapx = TWOBLOCKSTEP + (mapx - TWOBLOCKSTEP) * 2;
x = mapx * BLOCKWIDTH;
DrawBlock(x,y,mapx,mapy);
DrawBlock(x,y + HALFBITMAPHEIGHT * BLOCKSDEPTH,mapx,mapy);
DrawBlock(x + BLOCKWIDTH,y,mapx + 1,mapy);
DrawBlock(x + BLOCKWIDTH,y + HALFBITMAPHEIGHT * BLOCKSDEPTH,mapx + 1,mapy);
}
mapposy++;
videoposy = mapposy % HALFBITMAPHEIGHT;
}
static void CheckJoyScroll(void)
{
WORD i,count;
if (joy_fire()) count = 8; else count = 1;
if (joy_up()) {
for (i = 0; i < count; i++) {
ScrollUp();
}
}
if (joy_down()) {
for (i = 0; i < count; i++) {
ScrollDown();
}
}
}
static void UpdateCopperlist(void)
{
ULONG pl;
LONG planeadd;
WORD i;
WORD *wp;
planeadd = ((LONG)(videoposy + BLOCKHEIGHT)) * BITMAPBYTESPERROW * BLOCKSDEPTH;
// set plane pointers
wp = CopPLANE1H;
for (i = 0; i < BLOCKSDEPTH; i++) {
pl = ((ULONG)ScreenBitmap->Planes[i]) + planeadd;
wp[1] = (WORD)(pl >> 16);
wp[3] = (WORD)(pl & 0xFFFF);
wp += 4;
}
}
static void MainLoop(void)
{
if (!options.how) {
// activate copperlist
hard_wait_blit();
wait_vbl();
custom->copjmp2 = 0;
}
while (!lmb_down()) {
if (!options.how) {
wait_vbeam(199);
wait_vbeam(200);
} else {
Delay(1);
}
if (options.speed) *(WORD *)0xdff180 = 0xFF0;
CheckJoyScroll();
if (options.speed) *(WORD *)0xdff180 = 0xF00;
if (!options.how) {
UpdateCopperlist();
}
}
}
/********************* MAIN *************************/
int main(int argc, char **argv)
{
BOOL res = get_arguments(&options, s);
if (!res) Cleanup(s);
res = read_level_map(RACE_MAP_PATH, &level_map, s);
if (!res) Cleanup(s);
BlocksBitmap = read_blocks(RACE_BLOCKS_PATH, colors, s, BLOCKSWIDTH, BLOCKSHEIGHT);
if (!BlocksBitmap) Cleanup(s);
blocksbuffer = BlocksBitmap->Planes[0];
OpenDisplay();
if (!options.how) {
Delay(2*50);
kill_system();
InitCopperlist();
}
FillScreen();
MainLoop();
if (!options.how) {
activate_system();
}
Cleanup(0);
return 0;
}