-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgv.c
252 lines (217 loc) · 3.66 KB
/
gv.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
#include <unistd.h>
#include <fcntl.h>
#include <SDL.h>
#define XSIZE 1024
#define YSIZE 1024
int mousex,mousey;
SDL_Surface *thescreen;
void scrlock(void)
{
if ( SDL_LockSurface(thescreen) < 0 )
{
fprintf(stderr, "Couldn't lock display surface: %s\n",
SDL_GetError());
return;
}
}
void scrunlock(void)
{
SDL_UnlockSurface(thescreen);
}
void update(void)
{
SDL_UpdateRect(thescreen, 0, 0, 0, 0);
}
unsigned long maprgb(int r,int g,int b)
{
return SDL_MapRGB(thescreen->format,r,g,b);
}
void colordot(Uint32 x,Uint32 y,int c)
{
if(x<XSIZE && y<YSIZE)
*(Uint32 *)(thescreen->pixels+y*thescreen->pitch+(x<<2))=c;
}
void clear(int c)
{
int i;
for(i=0;i<XSIZE;++i)
colordot(i,0,c);
for(i=1;i<YSIZE;++i)
memcpy(thescreen->pixels + i*thescreen->pitch,
thescreen->pixels, XSIZE*4);
}
void colorrect(int x, int y, int w, int h, int c)
{
int i,j;
for(j=0;j<h;++j)
for(i=0;i<w;++i)
colordot(x+i, y+j, c);
}
// 0x10000 of gfx memoey
// 0x100 of color registers
// 0x20 of gfx registers
// 0x100 of vscroll registers
unsigned char gfx[0x10220], *gfxreg = gfx+0x10100;
int plane=0;
int gx = 128;
int gy = 128;
int cmap[64];
int anyword(int off)
{
off&=~1;
return gfx[off+1] | (gfx[off]<<8);
}
int gfxword(int off)
{
return anyword(off&0xfffe);
}
int hscroll(int n)
{
int hs;
n&=0x3ff;
hs = gfxreg[13]*0x400;
return gfxword(hs + n*2);
}
void puttile(int px, int py, int tile)
{
int pal;
int flipy = (tile&0x1000) ? 7 : 0;
int flipx = (tile&0x0800) ? 7 : 0;
int x,y;
int p;
pal = (tile&0x6000) >> 9;
tile &= 0x7ff;
tile<<=5;
for(y=0;y<8;++y)
{
for(x=0;x<8;++x)
{
p = gfx[tile];
if(x&1)
++tile;
else
p>>=4;
p=pal | (p&15);
if(p&15)
colordot(px + (x^flipx), py + (y^flipy), cmap[p]);
}
}
}
void iterate(void)
{
int x, y, t;
scrlock();
clear(cmap[gfxreg[7]&0x3f]);
t=0;
for(y=0;y<gy;++y)
{
for(x=0;x<gx;++x)
{
puttile(x*8, y*8, gfxword((plane<<11) + t));
t+=2;
}
}
if(0)
for(y=0;y<0x400;++y)
{
x = hscroll(y);
if(x>=0x8000)
x-=0x10000;
colordot(XSIZE/2 + x, y/2, ~0);
colordot(XSIZE/2 + x+1, y/2, 0);
colordot(XSIZE/2 + x-1, y/2, 0);
}
scrunlock();
update();
}
void handlekey(int code)
{
switch(code)
{
case SDLK_LEFT:
plane = (plane-1) & 0x1f;
break;
case SDLK_RIGHT:
plane = (plane+1) & 0x1f;
break;
case 'x':
gx <<=1;
if(gx==256) gx=32;
break;
case 'y':
gy <<=1;
if(gy==256) gy=32;
break;
}
iterate();
}
int main(int argc,char **argv)
{
int code;
SDL_Event event;
unsigned long videoflags;
int done=0;
int fd;
int i;
if(argc<2)
{
printf("specify the gfx file\n");
return -1;
}
fd=open(argv[1], O_RDONLY);
if(fd<0)
{
printf("Couldn't open %s\n", argv[1]);
return -2;
}
int res = read(fd, gfx, sizeof(gfx));res=res;
close(fd);
if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
{
fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
exit(1);
}
videoflags = 0;
thescreen = SDL_SetVideoMode(XSIZE, YSIZE, 32, videoflags);
if ( thescreen == NULL )
{
fprintf(stderr, "Couldn't set display mode: %s\n",
SDL_GetError());
exit(5);
}
for(i=0;i<64;++i)
{
int c = anyword(0x10000+i*2);
cmap[i] = maprgb((c&0x00e)<<4, (c&0x0e0), (c&0xe00)>>4);
}
iterate();
while(!done)
{
// iterate();
SDL_Delay(50);
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_MOUSEMOTION:
mousex=event.motion.x;
mousey=event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
mousex=event.button.x;
mousey=event.button.y;
break;
case SDL_KEYDOWN:
code=event.key.keysym.sym;
if(code==SDLK_ESCAPE) done=1;
handlekey(code);
break;
case SDL_QUIT:
done=1;
break;
}
}
}
SDL_Quit();
return 0;
}