]> 4ch.mooo.com Git - 16.git/blob - src/drawvrl5.c
Merge remote-tracking branch 'upstream/master'
[16.git] / src / drawvrl5.c
1
2 #include <stdio.h>
3 #include <conio.h> /* this is where Open Watcom hides the outp() etc. functions */
4 #include <ctype.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <assert.h>
8 #include <fcntl.h>
9 #include <math.h>
10 #include <dos.h>
11
12 #include <hw/cpu/cpu.h>
13 #include <hw/dos/dos.h>
14 #include <hw/vga/vga.h>
15 #include <hw/vga/vrl.h>
16
17 static unsigned char palette[768];
18
19 int main(int argc,char **argv) {
20         struct vrl1_vgax_header *vrl_header;
21         vrl1_vgax_offset_t *vrl_lineoffs;
22         unsigned char *buffer;
23         unsigned int bufsz;
24         int fd;
25
26         if (argc < 3) {
27                 fprintf(stderr,"drawvrl <VRL file> <palette file>\n");
28                 return 1;
29         }
30
31         fd = open(argv[1],O_RDONLY|O_BINARY);
32         if (fd < 0) {
33                 fprintf(stderr,"Unable to open '%s'\n",argv[1]);
34                 return 1;
35         }
36         {
37                 unsigned long sz = lseek(fd,0,SEEK_END);
38                 if (sz < sizeof(*vrl_header)) return 1;
39                 if (sz >= 65535UL) return 1;
40
41                 bufsz = (unsigned int)sz;
42                 buffer = malloc(bufsz);
43                 if (buffer == NULL) return 1;
44
45                 lseek(fd,0,SEEK_SET);
46                 if ((unsigned int)read(fd,buffer,bufsz) < bufsz) return 1;
47
48                 vrl_header = (struct vrl1_vgax_header*)buffer;
49                 if (memcmp(vrl_header->vrl_sig,"VRL1",4) || memcmp(vrl_header->fmt_sig,"VGAX",4)) return 1;
50                 if (vrl_header->width == 0 || vrl_header->height == 0) return 1;
51         }
52         close(fd);
53
54         probe_dos();
55         if (!probe_vga()) {
56                 printf("VGA probe failed\n");
57                 return 1;
58         }
59         int10_setmode(19);
60         update_state_from_vga();
61         vga_enable_256color_modex(); // VGA mode X
62         vga_state.vga_width = 320; // VGA lib currently does not update this
63         vga_state.vga_height = 200; // VGA lib currently does not update this
64
65 #if 1 // 320x240 test mode: this is how Project 16 is using our code, enable for test case
66         {
67                 struct vga_mode_params cm;
68
69                 vga_read_crtc_mode(&cm);
70
71                 // 320x240 mode 60Hz
72                 cm.vertical_total = 525;
73                 cm.vertical_start_retrace = 0x1EA;
74                 cm.vertical_end_retrace = 0x1EC;
75                 cm.vertical_display_end = 480;
76                 cm.vertical_blank_start = 489;
77                 cm.vertical_blank_end = 517;
78
79                 vga_write_crtc_mode(&cm,0);
80         }
81         vga_state.vga_height = 240; // VGA lib currently does not update this
82 #endif
83
84         /* load color palette */
85         fd = open(argv[2],O_RDONLY|O_BINARY);
86         if (fd >= 0) {
87                 unsigned int i;
88
89                 read(fd,palette,768);
90                 close(fd);
91
92                 vga_palette_lseek(0);
93                 for (i=0;i < 256;i++) vga_palette_write(palette[(i*3)+0]>>2,palette[(i*3)+1]>>2,palette[(i*3)+2]>>2);
94         }
95
96         /* preprocess the sprite to generate line offsets */
97         vrl_lineoffs = vrl1_vgax_genlineoffsets(vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
98         if (vrl_lineoffs == NULL) return 1;
99
100         {
101                 unsigned int i,j,o;
102
103                 /* fill screen with a distinctive pattern */
104                 for (i=0;i < vga_state.vga_width;i++) {
105                         o = i >> 2;
106                         vga_write_sequencer(0x02/*map mask*/,1 << (i&3));
107                         for (j=0;j < vga_state.vga_height;j++,o += vga_state.vga_stride)
108                                 vga_state.vga_graphics_ram[o] = (i^j)&15; // VRL samples put all colors in first 15!
109                 }
110         }
111         while (getch() != 13);
112
113         /* make distinctive pattern offscreen, render sprite, copy onscreen */
114         {
115                 const unsigned int offscreen_ofs = (vga_state.vga_stride * vga_state.vga_height);
116                 unsigned int i,j,o,o2,x,y,rx,ry,w,h;
117                 unsigned int overdraw = 1;      // how many pixels to "overdraw" so that moving sprites with edge pixels don't leave streaks.
118                                                 // if the sprite's edge pixels are clear anyway, you can set this to 0.
119                 VGA_RAM_PTR omemptr;
120                 int xdir=1,ydir=1;
121
122                 /* starting coords. note: this technique is limited to x coordinates of multiple of 4 */
123                 x = 0;
124                 y = 0;
125
126                 /* do it */
127                 omemptr = vga_state.vga_graphics_ram; // save original mem ptr
128                 while (1) {
129                         /* stop animating if the user hits ENTER */
130                         if (kbhit()) {
131                                 if (getch() == 13) break;
132                         }
133
134                         /* render box bounds. y does not need modification, but x and width must be multiple of 4 */
135                         if (x >= overdraw) rx = (x - overdraw) & (~3);
136                         else rx = 0;
137                         if (y >= overdraw) ry = (y - overdraw);
138                         else ry = 0;
139                         h = vrl_header->height + overdraw + y - ry;
140                         w = (x + vrl_header->width + (overdraw*2) + 3/*round up*/ - rx) & (~3);
141                         if ((rx+w) > vga_state.vga_width) w = vga_state.vga_width-rx;
142                         if ((ry+h) > vga_state.vga_height) h = vga_state.vga_height-ry;
143
144                         /* replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0) */
145                         vga_state.vga_draw_stride_limit = (vga_state.vga_width + 3/*round up*/ - x) >> 2;
146                         vga_state.vga_draw_stride = w >> 2;
147                         vga_state.vga_graphics_ram = omemptr + offscreen_ofs;
148
149                         /* first draw pattern corresponding to that part of the screen. this COULD be optimized, obviously, but it's designed for study.
150                          * also note we don't have to use the same stride as the display! */
151                         for (i=rx;i < (rx+w);i++) {
152                                 o = (i-rx) >> 2;
153                                 vga_write_sequencer(0x02/*map mask*/,1 << (i&3));
154                                 for (j=ry;j < (ry+h);j++,o += vga_state.vga_draw_stride)
155                                         vga_state.vga_graphics_ram[o] = (i^j)&15; // VRL samples put all colors in first 15!
156                         }
157
158                         /* then the sprite. note modding ram ptr means we just draw to (x&3,0) */
159                         draw_vrl1_vgax_modex(x-rx,y-ry,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
160
161                         /* restore ptr */
162                         vga_state.vga_graphics_ram = omemptr;
163
164                         /* block copy to visible RAM from offscreen */
165                         vga_setup_wm1_block_copy();
166                         o = offscreen_ofs; // source offscreen
167                         o2 = (ry * vga_state.vga_stride) + (rx >> 2); // dest visible (original stride)
168                         for (i=0;i < h;i++,o += vga_state.vga_draw_stride,o2 += vga_state.vga_stride) vga_wm1_mem_block_copy(o2,o,w >> 2);
169                         /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
170                         vga_restore_rm0wm0();
171
172                         /* restore stride */
173                         vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = vga_state.vga_stride;
174
175                         /* step */
176                         x += xdir;
177                         y += ydir;
178                         if (x >= (vga_state.vga_width - 1) || x == 0)
179                                 xdir = -xdir;
180                         if (y >= (vga_state.vga_height - 1) || y == 0)
181                                 ydir = -ydir;
182                 }
183         }
184
185         /* make distinctive pattern offscreen, render sprite, copy onscreen.
186          * this time, we render the distinctive pattern to another offscreen location and just copy.
187          * note this version is much faster too! */
188         {
189                 const unsigned int offscreen_ofs = (vga_state.vga_stride * vga_state.vga_height);
190                 const unsigned int pattern_ofs = 0x10000UL - (vga_state.vga_stride * vga_state.vga_height);
191                 unsigned int i,j,o,o2,x,y,rx,ry,w,h;
192                 unsigned int overdraw = 1;      // how many pixels to "overdraw" so that moving sprites with edge pixels don't leave streaks.
193                                                 // if the sprite's edge pixels are clear anyway, you can set this to 0.
194                 VGA_RAM_PTR omemptr;
195                 int xdir=1,ydir=1;
196
197                 /* fill pattern offset with a distinctive pattern */
198                 for (i=0;i < vga_state.vga_width;i++) {
199                         o = (i >> 2) + pattern_ofs;
200                         vga_write_sequencer(0x02/*map mask*/,1 << (i&3));
201                         for (j=0;j < vga_state.vga_height;j++,o += vga_state.vga_stride)
202                                 vga_state.vga_graphics_ram[o] = (i^j)&15; // VRL samples put all colors in first 15!
203                 }
204
205                 /* starting coords. note: this technique is limited to x coordinates of multiple of 4 */
206                 x = 0;
207                 y = 0;
208
209                 /* do it */
210                 omemptr = vga_state.vga_graphics_ram; // save original mem ptr
211                 while (1) {
212                         /* stop animating if the user hits ENTER */
213                         if (kbhit()) {
214                                 if (getch() == 13) break;
215                         }
216
217                         /* render box bounds. y does not need modification, but x and width must be multiple of 4 */
218                         if (x >= overdraw) rx = (x - overdraw) & (~3);
219                         else rx = 0;
220                         if (y >= overdraw) ry = (y - overdraw);
221                         else ry = 0;
222                         h = vrl_header->height + overdraw + y - ry;
223                         w = (x + vrl_header->width + (overdraw*2) + 3/*round up*/ - rx) & (~3);
224                         if ((rx+w) > vga_state.vga_width) w = vga_state.vga_width-rx;
225                         if ((ry+h) > vga_state.vga_height) h = vga_state.vga_height-ry;
226
227                         /* block copy pattern to where we will draw the sprite */
228                         vga_setup_wm1_block_copy();
229                         o2 = offscreen_ofs;
230                         o = pattern_ofs + (ry * vga_state.vga_stride) + (rx >> 2); // source offscreen
231                         for (i=0;i < h;i++,o += vga_state.vga_stride,o2 += (w >> 2)) vga_wm1_mem_block_copy(o2,o,w >> 2);
232                         /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
233                         vga_restore_rm0wm0();
234
235                         /* replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0) */
236                         vga_state.vga_draw_stride_limit = (vga_state.vga_width + 3/*round up*/ - x) >> 2;
237                         vga_state.vga_draw_stride = w >> 2;
238                         vga_state.vga_graphics_ram = omemptr + offscreen_ofs;
239
240                         /* then the sprite. note modding ram ptr means we just draw to (x&3,0) */
241                         draw_vrl1_vgax_modex(x-rx,y-ry,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
242
243                         /* restore ptr */
244                         vga_state.vga_graphics_ram = omemptr;
245
246                         /* block copy to visible RAM from offscreen */
247                         vga_setup_wm1_block_copy();
248                         o = offscreen_ofs; // source offscreen
249                         o2 = (ry * vga_state.vga_stride) + (rx >> 2); // dest visible (original stride)
250                         for (i=0;i < h;i++,o += vga_state.vga_draw_stride,o2 += vga_state.vga_stride) vga_wm1_mem_block_copy(o2,o,w >> 2);
251                         /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
252                         vga_restore_rm0wm0();
253
254                         /* restore stride */
255                         vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = vga_state.vga_stride;
256
257                         /* step */
258                         x += xdir;
259                         y += ydir;
260                         if (x >= (vga_state.vga_width - 1) || x == 0)
261                                 xdir = -xdir;
262                         if (y >= (vga_state.vga_height - 1) || y == 0)
263                                 ydir = -ydir;
264                 }
265         }
266
267         /* another handy "demo" effect using VGA write mode 1.
268          * we can take what's on screen and vertically squash it like an old analog TV set turning off. */
269         {
270                 unsigned int blank_line_ofs = (vga_state.vga_stride * vga_state.vga_height * 2);
271                 unsigned int copy_ofs = (vga_state.vga_stride * vga_state.vga_height);
272                 unsigned int display_ofs = 0x0000;
273                 unsigned int i,y,soh,doh,dstart;
274                 unsigned int dh_blankfill = 8;
275                 unsigned int dh_step = 8;
276                 uint32_t sh,dh,yf,ystep;
277
278                 /* copy active display (0) to offscreen buffer (0x4000) */
279                 vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = vga_state.vga_stride;
280                 vga_setup_wm1_block_copy();
281                 vga_wm1_mem_block_copy(copy_ofs,display_ofs,vga_state.vga_stride * vga_state.vga_height);
282                 vga_restore_rm0wm0();
283
284                 /* need a blank line as well */
285                 for (i=0;i < vga_state.vga_stride;i++) vga_state.vga_graphics_ram[i+blank_line_ofs] = 0;
286
287                 sh = dh = vga_state.vga_height;
288                 while (dh >= dh_step) {
289                         /* stop animating if the user hits ENTER */
290                         if (kbhit()) {
291                                 if (getch() == 13) break;
292                         }
293
294                         /* wait for vsync end */
295                         vga_wait_for_vsync_end();
296
297                         /* what scalefactor to use for stretching? */
298                         ystep = (0x10000UL * sh) / dh;
299                         dstart = (vga_state.vga_height - dh) / 2; // center the squash effect on screen, otherwise it would squash to top of screen
300                         doh = display_ofs;
301                         soh = copy_ofs;
302                         yf = 0;
303                         y = 0;
304
305                         /* for performance, keep VGA in write mode 1 the entire render */
306                         vga_setup_wm1_block_copy();
307
308                         /* blank lines */
309                         if (dstart >= dh_blankfill) y = dstart - dh_blankfill;
310                         else y = 0;
311                         doh = vga_state.vga_stride * y;
312
313                         while (y < dstart) {
314                                 vga_wm1_mem_block_copy(doh,blank_line_ofs,vga_state.vga_stride);
315                                 doh += vga_state.vga_stride;
316                                 y++;
317                         }
318
319                         /* draw */
320                         while (y < (dh+dstart)) {
321                                 soh = copy_ofs + ((yf >> 16UL) * vga_state.vga_stride);
322                                 vga_wm1_mem_block_copy(doh,soh,vga_state.vga_stride);
323                                 doh += vga_state.vga_stride;
324                                 yf += ystep;
325                                 y++;
326                         }
327
328                         /* blank lines */
329                         while (y < vga_state.vga_height && y < (dh+dstart+dh_blankfill)) {
330                                 vga_wm1_mem_block_copy(doh,blank_line_ofs,vga_state.vga_stride);
331                                 doh += vga_state.vga_stride;
332                                 y++;
333                         }
334
335                         /* done */
336                         vga_restore_rm0wm0();
337
338                         /* wait for vsync */
339                         vga_wait_for_vsync();
340
341                         /* make it shrink */
342                         dh -= dh_step;
343                         if (dh < 40) dh_step = 1;
344                 }
345         }
346
347         int10_setmode(3);
348         free(vrl_lineoffs);
349         buffer = NULL;
350         free(buffer);
351         bufsz = 0;
352         return 0;
353 }
354