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