]> 4ch.mooo.com Git - 16.git/blob - src/tesuto.c
updated tesuto.exe
[16.git] / src / tesuto.c
1 #include "src/tesuto.h"
2
3 global_game_variables_t gvar;
4
5 static unsigned char palette[768];
6
7 int main(int argc,char **argv) {
8         struct vrl1_vgax_header *vrl_header;
9         vrl1_vgax_offset_t *vrl_lineoffs;
10         unsigned char *buffer;
11         unsigned int bufsz;
12         int fd;
13
14         if (argc < 3) {
15                 fprintf(stderr,"drawvrl <VRL file> <palette file>\n");
16                 return 1;
17         }
18
19         fd = open(argv[1],O_RDONLY|O_BINARY);
20         if (fd < 0) {
21                 fprintf(stderr,"Unable to open '%s'\n",argv[1]);
22                 return 1;
23         }
24         {
25                 unsigned long sz = lseek(fd,0,SEEK_END);
26                 if (sz < sizeof(*vrl_header)) return 1;
27                 if (sz >= 65535UL) return 1;
28
29                 bufsz = (unsigned int)sz;
30                 buffer = malloc(bufsz);
31                 if (buffer == NULL) return 1;
32
33                 lseek(fd,0,SEEK_SET);
34                 if ((unsigned int)read(fd,buffer,bufsz) < bufsz) return 1;
35
36                 vrl_header = (struct vrl1_vgax_header*)buffer;
37                 if (memcmp(vrl_header->vrl_sig,"VRL1",4) || memcmp(vrl_header->fmt_sig,"VGAX",4)) return 1;
38                 if (vrl_header->width == 0 || vrl_header->height == 0) return 1;
39         }
40         close(fd);
41
42         probe_dos();
43         if (!probe_vga()) {
44                 printf("VGA probe failed\n");
45                 return 1;
46         }
47         int10_setmode(19);
48         update_state_from_vga();
49         //vga_enable_256color_modex(); // VGA mode X
50         VGAmodeX(1, &gvar);
51
52         /* load color palette */
53         fd = open(argv[2],O_RDONLY|O_BINARY);
54         if (fd >= 0) {
55                 unsigned int i;
56
57                 read(fd,palette,768);
58                 close(fd);
59
60                 vga_palette_lseek(0);
61                 for (i=0;i < 256;i++) vga_palette_write(palette[(i*3)+0]>>2,palette[(i*3)+1]>>2,palette[(i*3)+2]>>2);
62         }
63
64         /* preprocess the sprite to generate line offsets */
65         vrl_lineoffs = vrl1_vgax_genlineoffsets(vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
66         if (vrl_lineoffs == NULL) return 1;
67
68         {
69                 unsigned int i,j,o;
70
71                 /* fill screen with a distinctive pattern */
72                 for (i=0;i < 320;i++) {
73                         o = i >> 2;
74                         vga_write_sequencer(0x02/*map mask*/,1 << (i&3));
75                         for (j=0;j < 240;j++,o += vga_state.vga_stride)
76                                 vga_state.vga_graphics_ram[o] = (i^j)&15; // VRL samples put all colors in first 15!
77                 }
78         }
79         while (getch() != 13);
80
81         /* make distinctive pattern offscreen, render sprite, copy onscreen */
82         {
83                 const unsigned int offscreen_ofs = 0x8000; /* middle of VGA RAM */
84                 unsigned int i,j,o,o2,x,y,rx,ry,w,h;
85                 unsigned int overdraw = 1;      // how many pixels to "overdraw" so that moving sprites with edge pixels don't leave streaks.
86                                                 // if the sprite's edge pixels are clear anyway, you can set this to 0.
87                 VGA_RAM_PTR omemptr;
88                 int xdir=1,ydir=1;
89
90                 /* starting coords. note: this technique is limited to x coordinates of multiple of 4 */
91                 x = 0;
92                 y = 0;
93
94                 /* do it */
95                 omemptr = vga_state.vga_graphics_ram; // save original mem ptr
96                 while (1) {
97                         /* stop animating if the user hits ENTER */
98                         if (kbhit()) {
99                                 if (getch() == 13) break;
100                         }
101
102                         /* render box bounds. y does not need modification, but x and width must be multiple of 4 */
103                         if (x >= overdraw) rx = (x - overdraw) & (~3);
104                         else rx = 0;
105                         if (y >= overdraw) ry = (y - overdraw);
106                         else ry = 0;
107                         h = vrl_header->height + overdraw + y - ry;
108                         w = (x + vrl_header->width + (overdraw*2) + 3 - rx) & (~3);
109                         if ((rx+w) > 320) w = 320-rx;
110
111                         /* replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0) */
112                         vga_state.vga_draw_stride = w >> 2;
113                         vga_state.vga_graphics_ram = omemptr + offscreen_ofs;
114
115                         /* first draw pattern corresponding to that part of the screen. this COULD be optimized, obviously, but it's designed for study.
116                          * also note we don't have to use the same stride as the display! */
117                         for (i=rx;i < (rx+w);i++) {
118                                 o = (i-rx) >> 2;
119                                 vga_write_sequencer(0x02/*map mask*/,1 << (i&3));
120                                 for (j=ry;j < (ry+h);j++,o += vga_state.vga_draw_stride)
121                                         vga_state.vga_graphics_ram[o] = (i^j)&15; // VRL samples put all colors in first 15!
122                         }
123
124                         /* then the sprite. note modding ram ptr means we just draw to (x&3,0) */
125                         draw_vrl1_vgax_modex(x-rx,y-ry,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
126
127                         /* restore ptr */
128                         vga_state.vga_graphics_ram = omemptr;
129
130                         /* block copy to visible RAM from offscreen */
131                         // TODO: Maybe it would be better for VGA state to have "display stride" vs "draw stride" to avoid saving/restoring like this?
132                         vga_setup_wm1_block_copy();
133                         o = offscreen_ofs; // source offscreen
134                         o2 = (ry * vga_state.vga_stride) + (rx >> 2); // dest visible (original stride)
135                         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);
136                         /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
137                         vga_restore_rm0wm0();
138
139                         /* restore stride */
140                         vga_state.vga_draw_stride = vga_state.vga_stride;
141
142                         /* step */
143                         x += xdir;
144                         y += ydir;
145                         if (x >= (319 - vrl_header->width) || x >= 319 || x == 0)
146                                 xdir = -xdir;
147                         if (y >= (239 - vrl_header->height) || y >= 239 || y == 0)
148                                 ydir = -ydir;
149                 }
150         }
151
152         /* make distinctive pattern offscreen, render sprite, copy onscreen.
153          * this time, we render the distinctive pattern to another offscreen location and just copy.
154          * note this version is much faster too! */
155         {
156                 const unsigned int offscreen_ofs = 0x8000; /* middle of VGA RAM */
157                 const unsigned int pattern_ofs = 0xC000;
158                 unsigned int i,j,o,o2,x,y,rx,ry,w,h;
159                 unsigned int overdraw = 1;      // how many pixels to "overdraw" so that moving sprites with edge pixels don't leave streaks.
160                                                 // if the sprite's edge pixels are clear anyway, you can set this to 0.
161                 VGA_RAM_PTR omemptr;
162                 int xdir=1,ydir=1;
163
164                 /* fill pattern offset with a distinctive pattern */
165                 for (i=0;i < 320;i++) {
166                         o = (i >> 2) + pattern_ofs;
167                         vga_write_sequencer(0x02/*map mask*/,1 << (i&3));
168                         for (j=0;j < 240;j++,o += vga_state.vga_stride)
169                                 vga_state.vga_graphics_ram[o] = (i^j)&15; // VRL samples put all colors in first 15!
170                 }
171
172                 /* starting coords. note: this technique is limited to x coordinates of multiple of 4 */
173                 x = 0;
174                 y = 0;
175
176                 /* do it */
177                 omemptr = vga_state.vga_graphics_ram; // save original mem ptr
178                 while (1) {
179                         /* stop animating if the user hits ENTER */
180                         if (kbhit()) {
181                                 if (getch() == 13) break;
182                         }
183
184                         /* render box bounds. y does not need modification, but x and width must be multiple of 4 */
185                         if (x >= overdraw) rx = (x - overdraw) & (~3);
186                         else rx = 0;
187                         if (y >= overdraw) ry = (y - overdraw);
188                         else ry = 0;
189                         h = vrl_header->height + overdraw + y - ry;
190                         w = (x + vrl_header->width + (overdraw*2) + 3 - rx) & (~3);
191                         if ((rx+w) > 320) w = 320-rx;
192
193                         /* block copy pattern to where we will draw the sprite */
194                         vga_setup_wm1_block_copy();
195                         o2 = offscreen_ofs;
196                         o = pattern_ofs + (ry * vga_state.vga_stride) + (rx >> 2); // source offscreen
197                         for (i=0;i < vrl_header->height;i++,o += vga_state.vga_stride,o2 += (w >> 2)) vga_wm1_mem_block_copy(o2,o,w >> 2);
198                         /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
199                         vga_restore_rm0wm0();
200
201                         /* replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0) */
202                         vga_state.vga_draw_stride = w >> 2;
203                         vga_state.vga_graphics_ram = omemptr + offscreen_ofs;
204
205                         /* then the sprite. note modding ram ptr means we just draw to (x&3,0) */
206                         draw_vrl1_vgax_modex(x-rx,y-ry,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
207
208                         /* restore ptr */
209                         vga_state.vga_graphics_ram = omemptr;
210
211                         /* block copy to visible RAM from offscreen */
212                         // TODO: Maybe it would be better for VGA state to have "display stride" vs "draw stride" to avoid saving/restoring like this?
213                         vga_setup_wm1_block_copy();
214                         o = offscreen_ofs; // source offscreen
215                         o2 = (ry * vga_state.vga_stride) + (rx >> 2); // dest visible (original stride)
216                         for (i=0;i < vrl_header->height;i++,o += vga_state.vga_draw_stride,o2 += vga_state.vga_stride) vga_wm1_mem_block_copy(o2,o,w >> 2);
217                         /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */
218                         vga_restore_rm0wm0();
219
220                         /* restore stride */
221                         vga_state.vga_draw_stride = vga_state.vga_stride;
222
223                         /* step */
224                         x += xdir;
225                         y += ydir;
226                         if (x >= (319 - vrl_header->width) || x >= 319 || x == 0)
227                                 xdir = -xdir;
228                         if (y >= (239 - vrl_header->height) || y >= 239 || y == 0)
229                                 ydir = -ydir;
230                 }
231         }
232
233         int10_setmode(3);
234         //VGAmodeX(0, &gvar);
235         free(vrl_lineoffs);
236         buffer = NULL;
237         free(buffer);
238         bufsz = 0;
239         return 0;
240 }