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