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