From 81235413d6a56ca849923c4d425e976f6759a799 Mon Sep 17 00:00:00 2001
From: Jonathan Campbell <jonathan@castus.tv>
Date: Fri, 18 Mar 2016 04:00:49 -0700
Subject: [PATCH] update tesuto.c to latest VRL demo code. sprite drawing just
 got a bit faster. also stretching is demonstrated. good luck Sparky.

---
 src/tesuto.c | 100 +++++++++++++++------------------------------------
 src/tesuto.h |  12 ++-----
 2 files changed, 30 insertions(+), 82 deletions(-)

diff --git a/src/tesuto.c b/src/tesuto.c
index 82965a81..f309c143 100755
--- a/src/tesuto.c
+++ b/src/tesuto.c
@@ -1,75 +1,10 @@
 #include "src/tesuto.h"
-/*
-#include <stdio.h>
-#include <conio.h> // this is where Open Watcom hides the outp() etc. functions
-#include <ctype.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <assert.h>
-#include <fcntl.h>
-#include <math.h>
-#include <dos.h>
-*/
-
-#pragma pack(push,1)
-struct vrl_header {
-	uint8_t			vrl_sig[4];		// +0x00  "VRL1"
-	uint8_t			fmt_sig[4];		// +0x04  "VGAX"
-	uint16_t		height;			// +0x08  Sprite height
-	uint16_t		width;			// +0x0A  Sprite width
-	int16_t			hotspot_x;		// +0x0C  Hotspot offset (X) for programmer's reference
-	int16_t			hotspot_y;		// +0x0E  Hotspot offset (Y) for programmer's reference
-};							// =0x10
-#pragma pack(pop)
 
 static unsigned char palette[768];
 
-void draw_vrl_modex(unsigned int x,unsigned int y,struct vrl_header *hdr,unsigned char *data,unsigned int datasz) {
-	unsigned int vram_offset = (y * vga_stride) + (x >> 2);
-	unsigned char *fence = data + datasz;
-	unsigned char vga_plane = (x & 3);
-	unsigned char run,skip,b;
-	unsigned char far *draw;
-
-	while (data < fence) {
-		/* start of another vertical strip */
-		draw = vga_graphics_ram + vram_offset;
-		vga_write_sequencer(0x02/*map mask*/,1 << vga_plane);
-
-		while (data < fence) {
-			run = *data++;
-			if (run == 0xFF) break;
-			skip = *data++;
-			draw += skip * vga_stride;
-			if (run & 0x80) {
-				b = *data++;
-				while (run > 0x80) {
-					*draw = b;
-					draw += vga_stride;
-					run--;
-				}
-			}
-			else {
-				while (run > 0) {
-					*draw = *data++;
-					draw += vga_stride;
-					run--;
-				}
-			}
-		}
-
-		/* end of a vertical strip. next line? */
-		if ((++vga_plane) == 4) {
-			vram_offset++;
-			vga_plane = 0;
-		}
-	}
-
-	vga_write_sequencer(0x02/*map mask*/,0xF);
-}
-
 int main(int argc,char **argv) {
-	struct vrl_header *vrl_header;
+	struct vrl1_vgax_header *vrl_header;
+	vrl1_vgax_offset_t *vrl_lineoffs;
 	unsigned char *buffer;
 	unsigned int bufsz;
 	int fd;
@@ -86,7 +21,7 @@ int main(int argc,char **argv) {
 	}
 	{
 		unsigned long sz = lseek(fd,0,SEEK_END);
-		if (sz < sizeof(vrl_header)) return 1;
+		if (sz < sizeof(*vrl_header)) return 1;
 		if (sz >= 65535UL) return 1;
 
 		bufsz = (unsigned int)sz;
@@ -96,7 +31,7 @@ int main(int argc,char **argv) {
 		lseek(fd,0,SEEK_SET);
 		if ((unsigned int)read(fd,buffer,bufsz) < bufsz) return 1;
 
-		vrl_header = (struct vrl_header*)buffer;
+		vrl_header = (struct vrl1_vgax_header*)buffer;
 		if (memcmp(vrl_header->vrl_sig,"VRL1",4) || memcmp(vrl_header->fmt_sig,"VGAX",4)) return 1;
 		if (vrl_header->width == 0 || vrl_header->height == 0) return 1;
 	}
@@ -123,14 +58,18 @@ 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);
 	}
 
-	draw_vrl_modex(0,0,vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
+	/* preprocess the sprite to generate line offsets */
+	vrl_lineoffs = vrl1_vgax_genlineoffsets(vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
+	if (vrl_lineoffs == NULL) return 1;
+
+	draw_vrl1_vgax_modex(0,0,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
 	while (getch() != 13);
 
 	{
 		unsigned int i;
 
 		for (i=1;i < 320;i++)
-			draw_vrl_modex(i,0,vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
+			draw_vrl1_vgax_modex(i,0,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
 	}
 	while (getch() != 13);
 
@@ -138,13 +77,30 @@ int main(int argc,char **argv) {
 		unsigned int i;
 
 		for (i=1;i < 200;i++)
-			draw_vrl_modex(i,i,vrl_header,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
+			draw_vrl1_vgax_modex(i,i,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
+	}
+	while (getch() != 13);
+
+	{
+		unsigned int i;
+
+		for (i=(2 << 6)/*200%*/;i >= (1 << 4)/*25%*/;i--)
+			draw_vrl1_vgax_modexstretch(0,0,i,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
+	}
+	while (getch() != 13);
+	{
+		unsigned int i;
+
+		for (i=(2 << 6)/*200%*/;i >= (1 << 4)/*25%*/;i--)
+			draw_vrl1_vgax_modexystretch(0,0,i,i,vrl_header,vrl_lineoffs,buffer+sizeof(*vrl_header),bufsz-sizeof(*vrl_header));
 	}
 	while (getch() != 13);
 
 	int10_setmode(3);
+	free(vrl_lineoffs);
 	buffer = NULL;
 	free(buffer);
 	bufsz = 0;
 	return 0;
 }
+
diff --git a/src/tesuto.h b/src/tesuto.h
index 8703081d..ffa16404 100755
--- a/src/tesuto.h
+++ b/src/tesuto.h
@@ -3,16 +3,8 @@
 
 #include "src/lib/16_head.h"
 #include "src/lib/doslib/hw/cpu/cpu.h"
-#include "src/lib/doslib/hw/vga/vga.h"
 #include "src/lib/doslib/hw/dos/dos.h"
+#include "src/lib/doslib/hw/vga/vga.h"
+#include "src/lib/doslib/hw/vga/vrl.h"
 
-
-//typedef unsigned char far *VGA_RAM_PTR;
-//VGA_RAM_PTR vga_graphics_ram = (VGA_RAM_PTR)MK_FP(0xA000,0x0000);
-//unsigned char vga_stride = 80; // 80 x 4 = 320 for 320-pixel wide modes
-
-/*static inline void vga_write_sequencer(unsigned char i,unsigned char c) {
-    outp(0x3C4,i);
-    outp(0x3C5,c);
-}*/
 #endif
-- 
2.39.5