From 56d64244b2a13bc4aabb73d6392ad968528feea8 Mon Sep 17 00:00:00 2001 From: sparky4 Date: Sun, 21 Aug 2016 11:54:17 -0500 Subject: [PATCH] added files from branch wwww --- {src/lib => 16/src}/scroll.c | 0 src/lib/16_sprite.c | 114 +++++++++++++++++++++++++++++++++++ src/lib/16_sprite.h | 72 ++++++++++++++++++++++ src/lib/16_vrs.c | 92 ++++++++++++++++++++++++---- src/lib/16_vrs.h | 52 ++++++++-------- 5 files changed, 292 insertions(+), 38 deletions(-) rename {src/lib => 16/src}/scroll.c (100%) create mode 100755 src/lib/16_sprite.c create mode 100755 src/lib/16_sprite.h diff --git a/src/lib/scroll.c b/16/src/scroll.c similarity index 100% rename from src/lib/scroll.c rename to 16/src/scroll.c diff --git a/src/lib/16_sprite.c b/src/lib/16_sprite.c new file mode 100755 index 00000000..7667c25d --- /dev/null +++ b/src/lib/16_sprite.c @@ -0,0 +1,114 @@ +#include "src/lib/16_sprite.h" + +char* get_curr_anim_name(struct sprite *spri) +{ + // Retrive animation name list + struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr; + uint32_t huge *anim_names_offsets = (uint32_t huge *) + ((byte huge *)vrs + + vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_NAME_LIST]); + + return (char *)(vrs + anim_names_offsets[spri->curr_anim_spri]); +} + +void init_anim(struct sprite *spri, int anim_index) +{ + struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr; + uint32_t huge *anim_lists_offsets = (uint32_t huge *) + ((byte huge *)vrs + + vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_LIST]); + struct vrs_animation_list_entry_t *anim_list = (struct vrs_animation_list_entry_t huge *) + ((byte huge *)vrs + + anim_lists_offsets[anim_index]); + + // Upon new animation, start from the first sprite in it + spri->curr_anim_spri = 0; + spri->curr_spri_id = anim_list[0].sprite_id; + spri->delay = anim_list[0].delay; + + spri->curr_anim_list = anim_list; +} + + +int set_anim_by_id(struct sprite *spri, int anim_id) +{ + int new_anim_index = 0; + int iter_id; + struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr; + // Retruve animation ids list + uint16_t huge *anim_ids = (uint16_t huge *) + ((byte huge *)vrs + + vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]); + + // Loop through animation id untill match or end of list + while(iter_id = anim_ids[new_anim_index]) + { + // Return on successful match + if (iter_id = anim_id) + { + init_anim(spri, new_anim_index); + return 0; + } + new_anim_index++; + } + return -1; +} + +void print_anim_ids(struct sprite *spri) +{ + int new_anim_index = 0; + int iter_id; + struct vrs_header huge *vrs = spri->spritesheet->vrs_hdr; + // Retruve animation ids list + uint16_t huge *anim_ids = (uint16_t huge *) + ((byte huge *)vrs + + vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]); + + printf("\nPos %lld off %lld\n", (uint32_t)vrs, vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]); + if(!anim_ids[new_anim_index]) + exit(3); + // Loop through animation id untill match or end of list + while(iter_id = anim_ids[new_anim_index]) + { + // Return on successful match + new_anim_index++; + printf("%d, ", iter_id); + } +} + + +void animate_spri(struct sprite *spri) +{ + struct vrl_container *vrl_cont; + // Events go here + + // Draw sprite + vrl_cont = get_vrl_by_id(spri->spritesheet, spri->curr_spri_id); + draw_vrl1_vgax_modex( spri->x, spri->y, + vrl_cont->vrl_header, vrl_cont->line_offsets, + vrl_cont->buffer + sizeof(struct vrl1_vgax_header), + vrl_cont->size - sizeof(struct vrl1_vgax_header)); + + // Depending on delay, update indices + switch(spri->delay){ + // Delay = 0 means that sprite should loop. Nothing to change here + case 0: + break; + + // Delay = 1 means that on next time unit sprite should be changed + case 1: + spri->curr_anim_spri++; + + // If we hit the end of an animation sequence, restart it + if(!(spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id)){ + spri->curr_anim_spri = 0; + spri->curr_spri_id = spri->curr_anim_list[spri->curr_anim_spri].sprite_id; + } + spri->delay = spri->curr_anim_list[spri->curr_anim_spri].delay; + + // Delay > 1 means that we should not change sprite yet. Decrease delay + default: + spri->delay--; + break; + } +} diff --git a/src/lib/16_sprite.h b/src/lib/16_sprite.h new file mode 100755 index 00000000..c2a1d301 --- /dev/null +++ b/src/lib/16_sprite.h @@ -0,0 +1,72 @@ +/* Project 16 Source Code~ + * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover + * + * This file is part of Project 16. + * + * Project 16 is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Project 16 is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see , or + * write to the Free Software Foundation, Inc., 51 Franklin Street, + * Fifth Floor, Boston, MA 02110-1301 USA. + * + */ +#ifndef __16_SPRI__ +#define __16_SPRI__ + +#include "src/lib/16_vrs.h" +#include "src/lib/typdefst.h" + +struct sprite +{ + // VRS container from which we will extract animation and image data + struct vrs_container *spritesheet; + // Current sprite id + int curr_spri_id; + // Index of a current sprite in an animation sequence + int curr_anim_spri; + // Current animation sequence + struct vrs_animation_list_entry_t *curr_anim_list; + // Index of current animation in relevant VRS offsets table + int curr_anim; + // Delay in time units untill we should change sprite + int delay; + // Position of sprite on screen + int x, y; +}; + +/* Retrive current animation name of sprite +* In: +* + struct sprite *spri - sprite to retrive current animation sequence name from +* Out: +* + char* - animation sequence name +*/ +char* get_curr_anim_name(struct sprite *spri); + +/* Change sprite's current animation to the one given by id + * In: + * struct sprite *spri - sprite to manipulate on + * int id - id of a new animation sequence of th sprite + * Out: + * int - 0 on success, -1 on error + */ +int set_anim_by_id(struct sprite *spri, int id); + +/* Animate sprite, triggering any events and changing indices if necessary + * NB: if you want to change animation sequence after a specific sprite is shown, you should call animate_spri first + * In: + * + struct sprite *spri - sprite to animate + */ +void animate_spri(struct sprite *spri); + +void print_anim_ids(struct sprite *spri); + +#endif diff --git a/src/lib/16_vrs.c b/src/lib/16_vrs.c index 8cb41fa4..b3cd9eb7 100755 --- a/src/lib/16_vrs.c +++ b/src/lib/16_vrs.c @@ -20,38 +20,91 @@ * */ #include "src/lib/16_vrs.h" +#include "src/lib/typdefst.h" // Read .vrs file into far memory int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container *vrs_cont){ int fd; dword size; +#ifdef __WATCOMC__ + __segment seg; + void __based(seg)* bigbuffer; +#endif +#ifdef __BORLANDC__ + memptr bigbuffer; +#endif byte huge *buffer; + vrl1_vgax_offset_t **vrl_line_offsets; + uint32_t huge *vrl_headers_offsets; + uint32_t huge *vrl_id_iter; + uint32_t vrl_size; + int num_of_vrl, i; + struct vrl1_vgax_header huge *curr_vrl; + int success; + // Open filename, get size of file, // populate the vrs_container if all tests pass - fd = open((filename), O_RDONLY|O_BINARY); - size = filelength(fd); - close(fd); + fd = open(filename, O_RDONLY|O_BINARY); // Insert sanity cheks later - CA_LoadFile(filename, buffer, &gvar->mm, &gvar->mmi); + size = lseek(fd, 0, SEEK_END); + buffer = malloc(size); + lseek(fd, 0, SEEK_SET); + read(fd, buffer, size); + close(fd); + if(!success) + { + fprintf(stderr, "Unablee to load file"); + exit(3); + } vrs_cont->size = size; vrs_cont->buffer = buffer; + + // Calculate vrl offsets + + // Count sprites + vrl_id_iter = (uint32_t huge *)(buffer + vrs_cont->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_headers_offsets = (uint32_t huge *)(buffer + vrs_cont->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 huge *)(buffer + vrl_headers_offsets[i]); + + // Calc. vrl size as (next_offset - curr_offset) + if (i != num_of_vrl - 1){ + vrl_size = vrl_headers_offsets[i+1] - vrl_headers_offsets[i]; + } + // If it's the last vrl, size is (next_vrs_struct_offset - curr_offset) + else{ + vrl_size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_headers_offsets[i]; + } + vrl_line_offsets[i] = vrl1_vgax_genlineoffsets(curr_vrl, (byte *)curr_vrl + sizeof(*curr_vrl), vrl_size - sizeof(*curr_vrl)); + } + vrs_cont->vrl_line_offsets = vrl_line_offsets; return 0; } // Seek and return a specified .vrl blob from .vrs blob in far memory -struct vrl_container get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id){ +struct vrl_container * get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint16_t id){ uint16_t huge *ids; - uint32_t huge *vrl_list; + uint32_t huge *vrl_offs_list; struct vrl_container huge *vrl_cont; int counter = 0; + // If id is invalid, return null if(id == 0){ // Probably add an error message? return 0; } + // Get id list from .vrs blob (base + offset) ids = (uint16_t huge*)vrs_cont->buffer + (dword)vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST]; + // Loop through the id list until we found the right one or hit the end of the list // Counter is keeping track of the offset(in ids/vrl blobs) while(ids[counter] != id && ids[counter]){ @@ -62,16 +115,29 @@ struct vrl_container get_vrl_by_id(struct vrs_container /*huge*/ *vrs_cont, uint // Error message? return 0; } - // Get vrl list from .vrs blob (base + offset) - vrl_list = (uint32_t huge *)(vrs_cont->buffer + + + // Get vrl offsets list from .vrs blob (base + offset) + vrl_offs_list = (uint32_t huge *)(vrs_cont->buffer + vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_VRS_LIST]); + // Allocate memory for vrl_cont - vrl_cont = (struct vrl_container)malloc(sizeof(struct vrl_container)); + vrl_cont = (struct vrl_container *)malloc(sizeof(struct vrl_container)); + // Get vrl_header from .vrs (base + offset from vrl_list) // Counter is number of vrls to skip (ids and vrls are aligned according to the .vrs specification) - vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_list[counter]); - // Get .vrl size by integer arithmetics (next vrl - current vrl) - // Untested. May be an incorrect way to do so - vrl_cont->size = vrl_list[counter+1] - vrl_list[counter]; + vrl_cont->vrl_header = (struct vrl1_vgax_header huge *)(vrs_cont->buffer + vrl_offs_list[counter]); + + // Get .vrl size by integer arithmetics (next vrl offset - current vrl offset) + if(ids[counter+1]){ + vrl_cont->size = vrl_offs_list[counter+1] - vrl_offs_list[counter]; + } + // If we are retriving the last vrl, size is ids_list offset - current vrl offset, as next vrl offs is 0 + else{ + vrl_cont->size = vrs_cont->vrs_hdr->offset_table[VRS_HEADER_OFFSET_SPRITE_ID_LIST] - vrl_offs_list[counter]; + } + + // Retrive line offsets form .vrs + vrl_cont->line_offsets = vrs_cont->vrl_line_offsets[counter]; + return vrl_cont; } diff --git a/src/lib/16_vrs.h b/src/lib/16_vrs.h index 8fb4ad12..a7ba9d74 100755 --- a/src/lib/16_vrs.h +++ b/src/lib/16_vrs.h @@ -22,52 +22,54 @@ #ifndef __16_VRS__ #define __16_VRS__ -#include "src/lib/16_head.h" #include "src/lib/modex16.h" -#include "src/lib/16_ca.h" -#include "src/lib/16_mm.h" +#include "src/lib/typdefst.h" //#include //#include -//#include - -// Container for .vrs files loaded in memory with useful info -// Includes: -// + size of the .vrs blob in memory -// + pointer to the blob/vrs header +#include +#include "src/lib/16_ca.h" struct vrs_container{ + // Size of a .vrs lob in memory dword size; union{ byte huge *buffer; struct vrs_header huge *vrs_hdr; }; + // Array of corresponding vrl line offsets + vrl1_vgax_offset_t **vrl_line_offsets; }; struct vrl_container{ + // Size of a .vrl blob in memory dword size; union{ byte huge *buffer; struct vrl1_vgax_header huge *vrl_header; }; + // 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 -// + struct vrs_container *vrs_cont - pointer to the vrs_container -// to load the file into -// Out: -// + int - 0 on succes, 1 on failure +/* Read .vrs file into memory +* In: +* + char *filename - name of the file to load +* + struct vrs_container *vrs_cont - pointer to the vrs_container +* to load the file into +* Out: +* + int - 0 on succes, 1 on failure +*/ int read_vrs(global_game_variables_t *gvar, char *filename, struct vrs_container *vrs_cont); -// Seek and return a specified .vrl blob from .vrs blob in memory -// In: -// + struct vrs_container *vrs_cont - pointer to the vrs_container -// with a loaded .vrs file -// + uint16_t id - id of the vrl to retrive -// Out: -// struct vrl_container* - a pointer to a vrl_container with a pointer -// to the requested .vrl blob -struct vrl_container* get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id); +/* Seek and return a specified .vrl blob from .vrs blob in memory +* In: +* + struct vrs_container *vrs_cont - pointer to the vrs_container +* with a loaded .vrs file +* + uint16_t id - id of the vrl to retrive +* Out: +* struct vrl_container* - a pointer to a vrl_container with a pointer +* to the requested .vrl blob +*/ +struct vrl_container * get_vrl_by_id(struct vrs_container *vrs_cont, uint16_t id); #endif -- 2.39.5