1 #include <hw/cpu/cpu.h>
2 #include <hw/dos/dos.h>
3 #include <hw/vga/vga.h>
4 #include <hw/vga/vrl.h>
6 #include "src/tesuto.h"
8 #define FILENAME_1 "data/aconita.vrl"
9 #define FILENAME_2 "data/aconita.pal"
11 static unsigned char palette[768];
12 global_game_variables_t gvar;
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;
21 char *bakapee1,*bakapee2;
24 bakapee2=malloc(1024);
27 IN_Default(0,&player,ctrl_Joystick);
28 IN_initplayer(&player, 0);
31 fprintf(stderr,"drawvrl <VRL file> <palette file>\n");
32 bakapee1 = FILENAME_1;//"data/aconita.vrl";
33 bakapee2 = FILENAME_2;//"data/aconita.pal";
36 if(argv[1]) bakapee1 = argv[1];
37 if(argv[2]) bakapee2 = argv[2];
40 fd = open(bakapee1,O_RDONLY|O_BINARY);
42 fprintf(stderr,"Unable to open '%s'\n", bakapee1);
46 unsigned long sz = lseek(fd,0,SEEK_END);
47 if (sz < sizeof(*vrl_header)) return 1;
48 if (sz >= 65535UL) return 1;
50 bufsz = (unsigned int)sz;
51 buffer = malloc(bufsz);
52 if (buffer == NULL) return 1;
55 if ((unsigned int)read(fd,buffer,bufsz) < bufsz) return 1;
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;
65 printf("VGA probe failed\n");
68 VGAmodeX(1, 1, &gvar);
70 /* load color palette */
71 fd = open(bakapee2,O_RDONLY|O_BINARY);
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);
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;
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]));
92 #define VMEMHEIGHT gvar.video.page[0].height+gvar.video.page[1].height
94 //4 this dose the screen
97 /* fill screen with a distinctive pattern */
98 for (i=0;i < gvar.video.page[0].width;i++) {
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!
106 //while (getch() != 13);
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! */
112 unsigned int i,j,o,o2;
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.
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!
128 /* starting coords. note: this technique is limited to x coordinates of multiple of 4 */
133 omemptr = vga_state.vga_graphics_ram; // save original mem ptr
135 while(!IN_KeyDown(sc_Escape))
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
142 //gvar.kurokku.fpscap=!gvar.kurokku.fpscap;
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;
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();
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;
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));
173 vga_state.vga_graphics_ram = omemptr;
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();
184 vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = gvar.video.page[0].stridew;
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))
190 if ((y + vrl_header->height) >= ((gvar.video.page[0].height + gvar.video.page[0].dy) - 1) || y == -(gvar.video.page[0].dy))
192 //printf("[x%u y%u] [rx%u ry%u] [w%u h%u]\n", x, y, rx, ry, w, h);
198 while(!IN_KeyDown(sc_Escape))
200 if(IN_KeyDown(2)) modexShowPage(&(gvar.video.page[0]));
201 if(IN_KeyDown(3)) modexShowPage(&(gvar.video.page[1]));
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. */
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;
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();
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;
225 sh = dh = gvar.video.page[0].height;
226 while (dh >= dh_step) {
227 /* stop animating if the user hits ENTER */
229 if (getch() == 13) break;
232 /* wait for vsync end */
233 vga_wait_for_vsync_end();
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
243 /* for performance, keep VGA in write mode 1 the entire render */
244 vga_setup_wm1_block_copy();
247 if (dstart >= dh_blankfill) y = dstart - dh_blankfill;
249 doh = gvar.video.page[0].stridew * y;
252 vga_wm1_mem_block_copy(doh,blank_line_ofs,gvar.video.page[0].stridew);
253 doh += gvar.video.page[0].stridew;
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;
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;
274 vga_restore_rm0wm0();
277 vga_wait_for_vsync();
281 if (dh < 40) dh_step = 1;
286 VGAmodeX(0, 1, &gvar);