From e08355e15caf3345f9dc41411b3d0733175a93bb Mon Sep 17 00:00:00 2001
From: sparky4 <sparky4@cock.li>
Date: Mon, 13 Mar 2017 13:38:43 -0500
Subject: [PATCH] __seguse.txt added to show _seg usage also OpenVGMFile needs
 to be ported to 16_snd.c so we can use CA MM and PM ][ ca needs huge amounts
 of work and i should remember what needs to be done soon also i should
 reverify 16_in.c to make sure it can work with other code of id engine] fixed
 the glitch] i need the sprite stuff to use the memory manager

---
 src/lib/16_map.c   | 12 ++++++------
 src/lib/16_spri.c  | 22 +++++++++++-----------
 src/lib/16_tdef.h  | 30 ++++++++++++++++++++++++++++--
 src/lib/16_vrs.c   | 22 +++++++++-------------
 src/lib/16_vrs.h   |  6 +++---
 src/lib/scroll16.c |  4 ++--
 src/vrstest.c      |  2 --
 7 files changed, 59 insertions(+), 39 deletions(-)

diff --git a/src/lib/16_map.c b/src/lib/16_map.c
index de168acf..c20ab03b 100755
--- a/src/lib/16_map.c
+++ b/src/lib/16_map.c
@@ -203,9 +203,9 @@ int newloadmap(char *mn, map_t *map) {
 	else if(status != tokcount) { printf("Warning: used %d tok\n", status);}
 	extract_map(js, tok, tokcount, map);
 
-	free(js);
-	free(tok);
-	fclose(fh);
+	free(js);	//TODO: USE MM_ CA_ AND PM_
+	free(tok);	//TODO: USE MM_ CA_ AND PM_
+	fclose(fh);	//TODO: USE MM_ CA_ AND PM_
 
 	return 0;
 }
@@ -230,7 +230,7 @@ int CA_loadmap(char *mn, map_t *map, global_game_variables_t *gvar)
 	file_s = filesize(fh);
 	CA_LoadFile(mn, &MAPBUFINLM, gvar);
 	tokcount = jsmn_parse(&p, MAPBUFINLM, file_s, NULL, 0);
-	tok = malloc(tokcount*sizeof(jsmntok_t));
+	tok = malloc(tokcount*sizeof(jsmntok_t));//TODO: USE MM_ CA_ AND PM_
 //	printf("Allocated %d tokens", tokcount);
 	jsmn_init(&p);
 	if((status = jsmn_parse(&p, MAPBUFINLM, file_s, tok, tokcount)) < 0)
@@ -241,8 +241,8 @@ int CA_loadmap(char *mn, map_t *map, global_game_variables_t *gvar)
 	else if(status != tokcount) { printf("Warning: used %d tok\n", status);}
 	extract_map(MAPBUFINLM, tok, tokcount, map);
 
-	free(tok);
-	fclose(fh);
+	free(tok);	//TODO: USE MM_ CA_ AND PM_
+	fclose(fh);	//TODO: USE MM_ CA_ AND PM_
 
 	return 0;
 }
diff --git a/src/lib/16_spri.c b/src/lib/16_spri.c
index f4c79537..cde64ac6 100755
--- a/src/lib/16_spri.c
+++ b/src/lib/16_spri.c
@@ -25,7 +25,7 @@
 char* get_curr_anim_name(struct sprite *spri)
 {
 	// Retrive animation name list
-	struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;
+	struct vrs_header far *vrs = spri->spritesheet.vrs_hdr;
 	uint32_t far *anim_names_offsets = 	(uint32_t far *)
 						((byte far *)vrs +
 						 vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_NAME_LIST]);
@@ -35,7 +35,7 @@ char* get_curr_anim_name(struct sprite *spri)
 
 void init_anim(struct sprite *spri, int anim_index)
 {
-	struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;
+	struct vrs_header far *vrs = spri->spritesheet.vrs_hdr;
 	uint32_t far *anim_lists_offsets = 	(uint32_t far *)
 						((byte far *)vrs +
 						 vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_LIST]);
@@ -56,7 +56,7 @@ int set_anim_by_id(struct sprite *spri, int anim_id)
 {
 	int new_anim_index = 0;
 	int iter_id;
-	struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;
+	struct vrs_header far *vrs = spri->spritesheet.vrs_hdr;
 	// Retruve animation ids list
         uint16_t far *anim_ids =	(uint16_t far *)
 					((byte far *)vrs +
@@ -80,7 +80,7 @@ void print_anim_ids(struct sprite *spri)
 {
 	int new_anim_index = 0;
 	int iter_id;
-	struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;
+	struct vrs_header far *vrs = spri->spritesheet.vrs_hdr;
 	// Retruve animation ids list
         uint16_t far *anim_ids =	(uint16_t far *)
 					((byte far *)vrs +
@@ -148,7 +148,7 @@ void animate_spri(entity_t *enti, video_t *video)
 
 
 	// Draw sprite
-	j = get_vrl_by_id(enti->spri.spritesheet, enti->spri.curr_spri_id, enti->spri.sprite_vrl_cont);
+	j = get_vrl_by_id(&enti->spri.spritesheet, enti->spri.curr_spri_id, &enti->spri.sprite_vrl_cont);
 	if(j < 0)
 	{
 		//Quit (gv, "Error retriving required sprite");
@@ -162,8 +162,8 @@ void animate_spri(entity_t *enti, video_t *video)
 			else rx = -(video->page[0].dx);
 		if (y >= enti->overdrawh) ry = (y - enti->overdrawh);
 			else ry = -(video->page[0].dy);
-		h = enti->spri.sprite_vrl_cont->vrl_header->height + enti->overdrawh + y - ry;
-		w = (x + enti->spri.sprite_vrl_cont->vrl_header->width + (enti->overdraww*2) + 3 - rx) & (~3) - enti->overdraww;//round up
+		h = enti->spri.sprite_vrl_cont.vrl_header->height + enti->overdrawh + y - ry;
+		w = (x + enti->spri.sprite_vrl_cont.vrl_header->width + (enti->overdraww*2) + 3 - rx) & (~3) - enti->overdraww;//round up
 		if ((rx+w) > video->page[0].width) w = video->page[0].width-rx;
 		if ((ry+h) > video->page[0].height) h = video->page[0].height-ry;
 
@@ -190,10 +190,10 @@ void animate_spri(entity_t *enti, video_t *video)
 	draw_vrl1_vgax_modex(
 		x-rx,
 		y-ry,
-		enti->spri.sprite_vrl_cont->vrl_header,
-		enti->spri.sprite_vrl_cont->line_offsets,
-		enti->spri.sprite_vrl_cont->buffer + sizeof(struct vrl1_vgax_header),
-		enti->spri.sprite_vrl_cont->data_size
+		enti->spri.sprite_vrl_cont.vrl_header,
+		enti->spri.sprite_vrl_cont.line_offsets,
+		enti->spri.sprite_vrl_cont.buffer + sizeof(struct vrl1_vgax_header),
+		enti->spri.sprite_vrl_cont.data_size
 	);
 #endif
 	if(!video->rss)
diff --git a/src/lib/16_tdef.h b/src/lib/16_tdef.h
index e059fedf..2d027f61 100755
--- a/src/lib/16_tdef.h
+++ b/src/lib/16_tdef.h
@@ -66,12 +66,38 @@ typedef struct {
 
 //from 16_sprit.h
 #ifdef	__WATCOMC__
+#include <hw/vga/vrl.h>
+
+typedef struct vrs_container{
+	// Size of a .vrs blob in memory
+	// minus header
+	dword data_size;
+	union{
+		byte far *buffer;
+		struct vrs_header far *vrs_hdr;
+	};
+	// Array of corresponding vrl line offsets
+	vrl1_vgax_offset_t **vrl_line_offsets;
+} vrs_container_t;
+
+typedef struct vrl_container{
+	// Size of a .vrl blob in memory
+	// minus header
+	dword data_size;
+	union{
+		byte far *buffer;
+		struct vrl1_vgax_header far *vrl_header;
+	};
+	// Pointer to a corresponding vrl line offsets struct
+	vrl1_vgax_offset_t *line_offsets;
+} vrl_container_t;
+
 typedef struct sprite
 {
 	// VRS container from which we will extract animation and image data
-	struct vrs_container *spritesheet;
+	vrs_container_t	spritesheet;
 	// Container for a vrl sprite
-	struct vrl_container *sprite_vrl_cont;
+	vrl_container_t	sprite_vrl_cont;
 	// Current sprite id
 	int curr_spri_id;
 	// Index of a current sprite in an animation sequence
diff --git a/src/lib/16_vrs.c b/src/lib/16_vrs.c
index efb62ba8..07c3bd5d 100755
--- a/src/lib/16_vrs.c
+++ b/src/lib/16_vrs.c
@@ -25,8 +25,6 @@ void VRS_ReadVRS(char *filename, entity_t *enti, global_game_variables_t *gvar){
 void VRS_LoadVRS(char *filename, entity_t *enti, global_game_variables_t *gvar){	VRS_OpenVRS(filename, enti, 0, gvar);	}
 void VRS_OpenVRS(char *filename, entity_t *enti, boolean rlsw, global_game_variables_t *gvar)
 {
-	//memptr	big buffer;
-	static struct vrs_container vrs;
 	vrl1_vgax_offset_t **vrl_line_offsets;
 	uint16_t far *vrl_id_iter;
 	uint32_t far *vrl_headers_offsets;
@@ -46,21 +44,21 @@ void VRS_OpenVRS(char *filename, entity_t *enti, boolean rlsw, global_game_varia
 	}
 
 	// Insert sanity cheks later
-	vrs.buffer = gvar->ca.spribuff;
-	vrs.data_size = size - sizeof(struct vrl1_vgax_header);
+	enti->spri.spritesheet.buffer = gvar->ca.spribuff;
+	enti->spri.spritesheet.data_size = size - sizeof(struct vrl1_vgax_header);
 	num_of_vrl = 0;
-	vrl_id_iter = (uint16_t far *)(vrs.buffer + vrs.vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
+	vrl_id_iter = (uint16_t far *)(enti->spri.spritesheet.buffer + enti->spri.spritesheet.vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]);
 	while(vrl_id_iter[num_of_vrl]){
 		num_of_vrl++;
 	}
 
 	// Allocate memory for vrl line offsets table
-	vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t *)*num_of_vrl);
+	vrl_line_offsets = malloc(sizeof(vrl1_vgax_offset_t *)*num_of_vrl);//TODO: USE MM_ CA_ AND PM_
 
-	vrl_headers_offsets = (uint32_t far *)(vrs.buffer + vrs.vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
+	vrl_headers_offsets = (uint32_t far *)(enti->spri.spritesheet.buffer + enti->spri.spritesheet.vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]);
 	// Calculate line offsets for each vrl
 	for(i = 0; i < num_of_vrl; i++){
-		curr_vrl = (struct vrl1_vgax_header far *)(vrs.buffer + vrl_headers_offsets[i]);
+		curr_vrl = (struct vrl1_vgax_header far *)(enti->spri.spritesheet.buffer + vrl_headers_offsets[i]);
 
 		// Calc. vrl size as (next_offset - curr_offset)
 		if (i != num_of_vrl - 1){
@@ -68,14 +66,12 @@ void VRS_OpenVRS(char *filename, entity_t *enti, boolean rlsw, global_game_varia
 		}
 		// If it's the last vrl, size is (next_vrs_struct_offset - curr_offset)
 		else{
-			vrl_size = vrs.vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);
+			vrl_size = enti->spri.spritesheet.vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i] - sizeof(struct vrl1_vgax_header);
 		}
 		vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(struct vrl1_vgax_header), vrl_size);
 	}
-	vrs.vrl_line_offsets = vrl_line_offsets;
-
-	enti->spri.spritesheet = &vrs;
-	enti->spri.sprite_vrl_cont = malloc(sizeof(struct vrl_container));
+	enti->spri.spritesheet.vrl_line_offsets = vrl_line_offsets;
+	free(vrl_line_offsets);//TODO: USE MM_ CA_ AND PM_
 }
 
 // Seek and return a specified .vrl blob from .vrs blob in far memory
diff --git a/src/lib/16_vrs.h b/src/lib/16_vrs.h
index 9a089f47..07e75321 100755
--- a/src/lib/16_vrs.h
+++ b/src/lib/16_vrs.h
@@ -28,7 +28,7 @@
 //#include <hw/dos/dos.h>
 #include <hw/vga/vrl.h>
 #include "src/lib/16_ca.h"
-
+/*
 struct vrs_container{
 	// Size of a .vrs lob in memory
 	// minus header
@@ -40,7 +40,7 @@ struct vrs_container{
 	// Array of corresponding vrl line offsets
 	vrl1_vgax_offset_t **vrl_line_offsets;
 };
-
+*//*
 struct vrl_container{
 	// Size of a .vrl blob in memory
 	// minus header
@@ -52,7 +52,7 @@ struct vrl_container{
 	// Pointer to a corresponding vrl line offsets struct
 	vrl1_vgax_offset_t *line_offsets;
 };
-
+*/
 /* Read .vrs file into memory
 * In:
 * + char *filename - name of the file to load
diff --git a/src/lib/scroll16.c b/src/lib/scroll16.c
index be058acb..0e73d83b 100755
--- a/src/lib/scroll16.c
+++ b/src/lib/scroll16.c
@@ -576,8 +576,8 @@ mapDrawTile(tiles_t *t, word i, page_t *page, word x, word y)
 			modexClearRegion(page, x, y, t->tileWidth, t->tileHeight, 1); //currently the over scan color!
 		break;
 		default:
-			rx = (((i-1) % ((t->spri->sprite_vrl_cont->vrl_header->width)/t->tileWidth)) * t->tileWidth);
-			ry = (((i-1) / ((t->spri->sprite_vrl_cont->vrl_header->height)/t->tileHeight)) * t->tileHeight);
+			rx = (((i-1) % ((t->spri->sprite_vrl_cont.vrl_header->width)/t->tileWidth)) * t->tileWidth);
+			ry = (((i-1) / ((t->spri->sprite_vrl_cont.vrl_header->height)/t->tileHeight)) * t->tileHeight);
 #ifndef TILERENDER
 			if(!pagenorendermap) modexClearRegion(page, x, y, t->tileWidth, t->tileHeight, i+1);
 #else
diff --git a/src/vrstest.c b/src/vrstest.c
index 9e3a9ae1..69ee777a 100755
--- a/src/vrstest.c
+++ b/src/vrstest.c
@@ -154,8 +154,6 @@ void main()
 	MM_DumpData(&gvar);
 	MM_Report_(&gvar);
 	Shutdown16(&gvar);
-	free(enti.spri.sprite_vrl_cont);
-	free(enti.spri.spritesheet->vrl_line_offsets);
 	MM_FreePtr(&bigbuffer, &gvar);
 	//MM_FreePtr(&((void __based(sega)*)enti.spri.spritesheet->buffer), &mm);
 	//printf("CPU to VGA: %f\n", t1);
-- 
2.39.5