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