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