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