]> 4ch.mooo.com Git - 16.git/blobdiff - src/tesuto.c
having the palette in the game code may help. also i added wolf3d's palette for exama...
[16.git] / src / tesuto.c
index 82965a8193d9a37f9fd60e741291dca6376e6a1a..ab7e2db64924be9a4ca49e3aa1edcedfedf1267e 100755 (executable)
@@ -1,75 +1,15 @@
+#include <hw/cpu/cpu.h>\r
+#include <hw/dos/dos.h>\r
+#include <hw/vga/vga.h>\r
+#include <hw/vga/vrl.h>\r
+\r
 #include "src/tesuto.h"\r
-/*\r
-#include <stdio.h>\r
-#include <conio.h> // this is where Open Watcom hides the outp() etc. functions\r
-#include <ctype.h>\r
-#include <stdlib.h>\r
-#include <unistd.h>\r
-#include <assert.h>\r
-#include <fcntl.h>\r
-#include <math.h>\r
-#include <dos.h>\r
-*/\r
-\r
-#pragma pack(push,1)\r
-struct vrl_header {\r
-       uint8_t                 vrl_sig[4];             // +0x00  "VRL1"\r
-       uint8_t                 fmt_sig[4];             // +0x04  "VGAX"\r
-       uint16_t                height;                 // +0x08  Sprite height\r
-       uint16_t                width;                  // +0x0A  Sprite width\r
-       int16_t                 hotspot_x;              // +0x0C  Hotspot offset (X) for programmer's reference\r
-       int16_t                 hotspot_y;              // +0x0E  Hotspot offset (Y) for programmer's reference\r
-};                                                     // =0x10\r
-#pragma pack(pop)\r
 \r
 static unsigned char palette[768];\r
 \r
-void draw_vrl_modex(unsigned int x,unsigned int y,struct vrl_header *hdr,unsigned char *data,unsigned int datasz) {\r
-       unsigned int vram_offset = (y * vga_stride) + (x >> 2);\r
-       unsigned char *fence = data + datasz;\r
-       unsigned char vga_plane = (x & 3);\r
-       unsigned char run,skip,b;\r
-       unsigned char far *draw;\r
-\r
-       while (data < fence) {\r
-               /* start of another vertical strip */\r
-               draw = vga_graphics_ram + vram_offset;\r
-               vga_write_sequencer(0x02/*map mask*/,1 << vga_plane);\r
-\r
-               while (data < fence) {\r
-                       run = *data++;\r
-                       if (run == 0xFF) break;\r
-                       skip = *data++;\r
-                       draw += skip * vga_stride;\r
-                       if (run & 0x80) {\r
-                               b = *data++;\r
-                               while (run > 0x80) {\r
-                                       *draw = b;\r
-                                       draw += vga_stride;\r
-                                       run--;\r
-                               }\r
-                       }\r
-                       else {\r
-                               while (run > 0) {\r
-                                       *draw = *data++;\r
-                                       draw += vga_stride;\r
-                                       run--;\r
-                               }\r
-                       }\r
-               }\r
-\r
-               /* end of a vertical strip. next line? */\r
-               if ((++vga_plane) == 4) {\r
-                       vram_offset++;\r
-                       vga_plane = 0;\r
-               }\r
-       }\r
-\r
-       vga_write_sequencer(0x02/*map mask*/,0xF);\r
-}\r
-\r
 int main(int argc,char **argv) {\r
-       struct vrl_header *vrl_header;\r
+       struct vrl1_vgax_header *vrl_header;\r
+       vrl1_vgax_offset_t *vrl_lineoffs;\r
        unsigned char *buffer;\r
        unsigned int bufsz;\r
        int fd;\r
@@ -86,7 +26,7 @@ int main(int argc,char **argv) {
        }\r
        {\r
                unsigned long sz = lseek(fd,0,SEEK_END);\r
-               if (sz < sizeof(vrl_header)) return 1;\r
+               if (sz < sizeof(*vrl_header)) return 1;\r
                if (sz >= 65535UL) return 1;\r
 \r
                bufsz = (unsigned int)sz;\r
@@ -96,7 +36,7 @@ int main(int argc,char **argv) {
                lseek(fd,0,SEEK_SET);\r
                if ((unsigned int)read(fd,buffer,bufsz) < bufsz) return 1;\r
 \r
-               vrl_header = (struct vrl_header*)buffer;\r
+               vrl_header = (struct vrl1_vgax_header*)buffer;\r
                if (memcmp(vrl_header->vrl_sig,"VRL1",4) || memcmp(vrl_header->fmt_sig,"VGAX",4)) return 1;\r
                if (vrl_header->width == 0 || vrl_header->height == 0) return 1;\r
        }\r
@@ -110,6 +50,27 @@ int main(int argc,char **argv) {
        int10_setmode(19);\r
        update_state_from_vga();\r
        vga_enable_256color_modex(); // VGA mode X\r
+       vga_state.vga_width = 320; // VGA lib currently does not update this\r
+       vga_state.vga_height = 240; // VGA lib currently does not update this\r
+\r
+#if 1 // 320x240 test mode: this is how Project 16 is using our code, enable for test case\r
+       {\r
+               struct vga_mode_params cm;\r
+\r
+               vga_read_crtc_mode(&cm);\r
+\r
+               // 320x240 mode 60Hz\r
+               cm.vertical_total = 525;\r
+               cm.vertical_start_retrace = 0x1EA;\r
+               cm.vertical_end_retrace = 0x1EC;\r
+               cm.vertical_display_end = 480;\r
+               cm.vertical_blank_start = 489;\r
+               cm.vertical_blank_end = 517;\r
+\r
+               vga_write_crtc_mode(&cm,0);\r
+       }\r
+       vga_state.vga_height = 240; // VGA lib currently does not update this\r
+#endif\r
 \r
        /* load color palette */\r
        fd = open(argv[2],O_RDONLY|O_BINARY);\r
@@ -123,26 +84,259 @@ int main(int argc,char **argv) {
                for (i=0;i < 256;i++) vga_palette_write(palette[(i*3)+0]>>2,palette[(i*3)+1]>>2,palette[(i*3)+2]>>2);\r
        }\r
 \r
-       draw_vrl_modex(0,0,vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));\r
-       while (getch() != 13);\r
+       /* preprocess the sprite to generate line offsets */\r
+       vrl_lineoffs = vrl1_vgax_genlineoffsets(vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));\r
+       if (vrl_lineoffs == NULL) return 1;\r
 \r
        {\r
-               unsigned int i;\r
+               unsigned int i,j,o;\r
 \r
-               for (i=1;i < 320;i++)\r
-                       draw_vrl_modex(i,0,vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));\r
+               /* fill screen with a distinctive pattern */\r
+               for (i=0;i < vga_state.vga_width;i++) {\r
+                       o = i >> 2;\r
+                       vga_write_sequencer(0x02/*map mask*/,1 << (i&3));\r
+                       for (j=0;j < vga_state.vga_height;j++,o += vga_state.vga_stride)\r
+                               vga_state.vga_graphics_ram[o] = (i^j)&15; // VRL samples put all colors in first 15!\r
+               }\r
        }\r
-       while (getch() != 13);\r
+       //while (getch() != 13);\r
 \r
+       /* make distinctive pattern offscreen, render sprite, copy onscreen */\r
        {\r
-               unsigned int i;\r
+               const unsigned int offscreen_ofs = (vga_state.vga_stride * vga_state.vga_height);\r
+               unsigned int i,j,o,o2,x,y,rx,ry,w,h;\r
+               unsigned int overdraw = 1;      // how many pixels to "overdraw" so that moving sprites with edge pixels don't leave streaks.\r
+                                               // if the sprite's edge pixels are clear anyway, you can set this to 0.\r
+               VGA_RAM_PTR omemptr;\r
+               int xdir=1,ydir=1;\r
+\r
+               /* starting coords. note: this technique is limited to x coordinates of multiple of 4 */\r
+               x = 0;\r
+               y = 0;\r
+\r
+               /* do it */\r
+               omemptr = vga_state.vga_graphics_ram; // save original mem ptr\r
+               while (1) {\r
+                       /* stop animating if the user hits ENTER */\r
+                       if (kbhit()) {\r
+                               if (getch() == 13) break;\r
+                       }\r
+\r
+                       /* render box bounds. y does not need modification, but x and width must be multiple of 4 */\r
+                       if (x >= overdraw) rx = (x - overdraw) & (~3);\r
+                       else rx = 0;\r
+                       if (y >= overdraw) ry = (y - overdraw);\r
+                       else ry = 0;\r
+                       h = vrl_header->height + overdraw + y - ry;\r
+                       w = (x + vrl_header->width + (overdraw*2) + 3/*round up*/ - rx) & (~3);\r
+                       if ((rx+w) > vga_state.vga_width) w = vga_state.vga_width-rx;\r
+                       if ((ry+h) > vga_state.vga_height) h = vga_state.vga_height-ry;\r
+\r
+                       /* replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0) */\r
+                       vga_state.vga_draw_stride_limit = (vga_state.vga_width + 3/*round up*/ - x) >> 2;\r
+                       vga_state.vga_draw_stride = w >> 2;\r
+                       vga_state.vga_graphics_ram = omemptr + offscreen_ofs;\r
+\r
+                       /* first draw pattern corresponding to that part of the screen. this COULD be optimized, obviously, but it's designed for study.\r
+                        * also note we don't have to use the same stride as the display! */\r
+                       for (i=rx;i < (rx+w);i++) {\r
+                               o = (i-rx) >> 2;\r
+                               vga_write_sequencer(0x02/*map mask*/,1 << (i&3));\r
+                               for (j=ry;j < (ry+h);j++,o += vga_state.vga_draw_stride)\r
+                                       vga_state.vga_graphics_ram[o] = (i^j)&15; // VRL samples put all colors in first 15!\r
+                       }\r
+\r
+                       /* then the sprite. note modding ram ptr means we just draw to (x&3,0) */\r
+                       draw_vrl1_vgax_modex(x-rx,y-ry,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));\r
+\r
+                       /* restore ptr */\r
+                       vga_state.vga_graphics_ram = omemptr;\r
+\r
+                       /* block copy to visible RAM from offscreen */\r
+                       vga_setup_wm1_block_copy();\r
+                       o = offscreen_ofs; // source offscreen\r
+                       o2 = (ry * vga_state.vga_stride) + (rx >> 2); // dest visible (original stride)\r
+                       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);\r
+                       /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */\r
+                       vga_restore_rm0wm0();\r
+\r
+                       /* restore stride */\r
+                       vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = vga_state.vga_stride;\r
+\r
+                       /* step */\r
+                       x += xdir;\r
+                       y += ydir;\r
+                       if (x >= (vga_state.vga_width - 1) || x == 0)\r
+                               xdir = -xdir;\r
+                       if (y >= (vga_state.vga_height - 1) || y == 0)\r
+                               ydir = -ydir;\r
+               }\r
+       }\r
+\r
+       /* make distinctive pattern offscreen, render sprite, copy onscreen.\r
+        * this time, we render the distinctive pattern to another offscreen location and just copy.\r
+        * note this version is much faster too! */\r
+       {\r
+               const unsigned int offscreen_ofs = (vga_state.vga_stride * vga_state.vga_height);\r
+               const unsigned int pattern_ofs = 0x10000UL - (vga_state.vga_stride * vga_state.vga_height);\r
+               unsigned int i,j,o,o2,x,y,rx,ry,w,h;\r
+               unsigned int overdraw = 1;      // how many pixels to "overdraw" so that moving sprites with edge pixels don't leave streaks.\r
+                                               // if the sprite's edge pixels are clear anyway, you can set this to 0.\r
+               VGA_RAM_PTR omemptr;\r
+               int xdir=1,ydir=1;\r
 \r
-               for (i=1;i < 200;i++)\r
-                       draw_vrl_modex(i,i,vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));\r
+               /* fill pattern offset with a distinctive pattern */\r
+               for (i=0;i < vga_state.vga_width;i++) {\r
+                       o = (i >> 2) + pattern_ofs;\r
+                       vga_write_sequencer(0x02/*map mask*/,1 << (i&3));\r
+                       for (j=0;j < vga_state.vga_height;j++,o += vga_state.vga_stride)\r
+                               vga_state.vga_graphics_ram[o] = (i^j)&15; // VRL samples put all colors in first 15!\r
+               }\r
+\r
+               /* starting coords. note: this technique is limited to x coordinates of multiple of 4 */\r
+               x = 0;\r
+               y = 0;\r
+\r
+               /* do it */\r
+               omemptr = vga_state.vga_graphics_ram; // save original mem ptr\r
+               while (1) {\r
+                       /* stop animating if the user hits ENTER */\r
+                       if (kbhit()) {\r
+                               if (getch() == 13) break;\r
+                       }\r
+\r
+                       /* render box bounds. y does not need modification, but x and width must be multiple of 4 */\r
+                       if (x >= overdraw) rx = (x - overdraw) & (~3);\r
+                       else rx = 0;\r
+                       if (y >= overdraw) ry = (y - overdraw);\r
+                       else ry = 0;\r
+                       h = vrl_header->height + overdraw + y - ry;\r
+                       w = (x + vrl_header->width + (overdraw*2) + 3/*round up*/ - rx) & (~3);\r
+                       if ((rx+w) > vga_state.vga_width) w = vga_state.vga_width-rx;\r
+                       if ((ry+h) > vga_state.vga_height) h = vga_state.vga_height-ry;\r
+\r
+                       /* block copy pattern to where we will draw the sprite */\r
+                       vga_setup_wm1_block_copy();\r
+                       o2 = offscreen_ofs;\r
+                       o = pattern_ofs + (ry * vga_state.vga_stride) + (rx >> 2); // source offscreen\r
+                       for (i=0;i < h;i++,o += vga_state.vga_stride,o2 += (w >> 2)) vga_wm1_mem_block_copy(o2,o,w >> 2);\r
+                       /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */\r
+                       vga_restore_rm0wm0();\r
+\r
+                       /* replace VGA stride with our own and mem ptr. then sprite rendering at this stage is just (0,0) */\r
+                       vga_state.vga_draw_stride_limit = (vga_state.vga_width + 3/*round up*/ - x) >> 2;\r
+                       vga_state.vga_draw_stride = w >> 2;\r
+                       vga_state.vga_graphics_ram = omemptr + offscreen_ofs;\r
+\r
+                       /* then the sprite. note modding ram ptr means we just draw to (x&3,0) */\r
+                       draw_vrl1_vgax_modex(x-rx,y-ry,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));\r
+\r
+                       /* restore ptr */\r
+                       vga_state.vga_graphics_ram = omemptr;\r
+\r
+                       /* block copy to visible RAM from offscreen */\r
+                       vga_setup_wm1_block_copy();\r
+                       o = offscreen_ofs; // source offscreen\r
+                       o2 = (ry * vga_state.vga_stride) + (rx >> 2); // dest visible (original stride)\r
+                       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);\r
+                       /* must restore Write Mode 0/Read Mode 0 for this code to continue drawing normally */\r
+                       vga_restore_rm0wm0();\r
+\r
+                       /* restore stride */\r
+                       vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = vga_state.vga_stride;\r
+\r
+                       /* step */\r
+                       x += xdir;\r
+                       y += ydir;\r
+                       if (x >= (vga_state.vga_width - 1) || x == 0)\r
+                               xdir = -xdir;\r
+                       if (y >= (vga_state.vga_height - 1) || y == 0)\r
+                               ydir = -ydir;\r
+               }\r
+       }\r
+\r
+       /* another handy "demo" effect using VGA write mode 1.\r
+        * we can take what's on screen and vertically squash it like an old analog TV set turning off. */\r
+       {\r
+               unsigned int blank_line_ofs = (vga_state.vga_stride * vga_state.vga_height * 2);\r
+               unsigned int copy_ofs = (vga_state.vga_stride * vga_state.vga_height);\r
+               unsigned int display_ofs = 0x0000;\r
+               unsigned int i,y,soh,doh,dstart;\r
+               unsigned int dh_blankfill = 8;\r
+               unsigned int dh_step = 8;\r
+               uint32_t sh,dh,yf,ystep;\r
+\r
+               /* copy active display (0) to offscreen buffer (0x4000) */\r
+               vga_state.vga_draw_stride_limit = vga_state.vga_draw_stride = vga_state.vga_stride;\r
+               vga_setup_wm1_block_copy();\r
+               vga_wm1_mem_block_copy(copy_ofs,display_ofs,vga_state.vga_stride * vga_state.vga_height);\r
+               vga_restore_rm0wm0();\r
+\r
+               /* need a blank line as well */\r
+               for (i=0;i < vga_state.vga_stride;i++) vga_state.vga_graphics_ram[i+blank_line_ofs] = 0;\r
+\r
+               sh = dh = vga_state.vga_height;\r
+               while (dh >= dh_step) {\r
+                       /* stop animating if the user hits ENTER */\r
+                       if (kbhit()) {\r
+                               if (getch() == 13) break;\r
+                       }\r
+\r
+                       /* wait for vsync end */\r
+                       vga_wait_for_vsync_end();\r
+\r
+                       /* what scalefactor to use for stretching? */\r
+                       ystep = (0x10000UL * sh) / dh;\r
+                       dstart = (vga_state.vga_height - dh) / 2; // center the squash effect on screen, otherwise it would squash to top of screen\r
+                       doh = display_ofs;\r
+                       soh = copy_ofs;\r
+                       yf = 0;\r
+                       y = 0;\r
+\r
+                       /* for performance, keep VGA in write mode 1 the entire render */\r
+                       vga_setup_wm1_block_copy();\r
+\r
+                       /* blank lines */\r
+                       if (dstart >= dh_blankfill) y = dstart - dh_blankfill;\r
+                       else y = 0;\r
+                       doh = vga_state.vga_stride * y;\r
+\r
+                       while (y < dstart) {\r
+                               vga_wm1_mem_block_copy(doh,blank_line_ofs,vga_state.vga_stride);\r
+                               doh += vga_state.vga_stride;\r
+                               y++;\r
+                       }\r
+\r
+                       /* draw */\r
+                       while (y < (dh+dstart)) {\r
+                               soh = copy_ofs + ((yf >> 16UL) * vga_state.vga_stride);\r
+                               vga_wm1_mem_block_copy(doh,soh,vga_state.vga_stride);\r
+                               doh += vga_state.vga_stride;\r
+                               yf += ystep;\r
+                               y++;\r
+                       }\r
+\r
+                       /* blank lines */\r
+                       while (y < vga_state.vga_height && y < (dh+dstart+dh_blankfill)) {\r
+                               vga_wm1_mem_block_copy(doh,blank_line_ofs,vga_state.vga_stride);\r
+                               doh += vga_state.vga_stride;\r
+                               y++;\r
+                       }\r
+\r
+                       /* done */\r
+                       vga_restore_rm0wm0();\r
+\r
+                       /* wait for vsync */\r
+                       vga_wait_for_vsync();\r
+\r
+                       /* make it shrink */\r
+                       dh -= dh_step;\r
+                       if (dh < 40) dh_step = 1;\r
+               }\r
        }\r
-       while (getch() != 13);\r
 \r
        int10_setmode(3);\r
+       free(vrl_lineoffs);\r
        buffer = NULL;\r
        free(buffer);\r
        bufsz = 0;\r