]> 4ch.mooo.com Git - 16.git/blobdiff - src/lib/16_sprit.c
I THINK I GOT THE ANIMATION FIXED AGAINrm data/spri/chikyuu.vrs
[16.git] / src / lib / 16_sprit.c
index ab6e33785ba4f27bf55df60722dfff5b7c429e5a..0be43073a11528728aeaf7fb1ee606b0e45f9865 100755 (executable)
-/* 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 <http://www.gnu.org/licenses/>, or
- * write to the Free Software Foundation, Inc., 51 Franklin Street,
- * Fifth Floor, Boston, MA 02110-1301 USA.
- *
- */
-#include "src/lib/16_sprit.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]);
-}
-
-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 = anim_index;
-       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]);
-
-       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)
-{
-       // Events go here
-
-       // Draw sprite
-       get_vrl_by_id(spri->spritesheet, spri->curr_spri_id, spri->sprite_vrl_cont);
-       draw_vrl1_vgax_modex(   spri->x, spri->y,
-                               spri->sprite_vrl_cont->vrl_header, spri->sprite_vrl_cont->line_offsets,
-                               spri->sprite_vrl_cont->buffer + sizeof(struct vrl1_vgax_header),
-                               spri->sprite_vrl_cont->data_size);
-
-       // 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;
-       }
-}
+/* Project 16 Source Code~\r
+ * Copyright (C) 2012-2016 sparky4 & pngwen & andrius4669 & joncampbell123 & yakui-lover\r
+ *\r
+ * This file is part of Project 16.\r
+ *\r
+ * Project 16 is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Project 16 is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>, or\r
+ * write to the Free Software Foundation, Inc., 51 Franklin Street,\r
+ * Fifth Floor, Boston, MA 02110-1301 USA.\r
+ *\r
+ */\r
+\r
+#include "src/lib/16_sprit.h"\r
+\r
+char* get_curr_anim_name(struct sprite *spri)\r
+{\r
+       // Retrive animation name list\r
+       struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;\r
+       uint32_t far *anim_names_offsets =      (uint32_t far *)\r
+                                               ((byte far *)vrs +\r
+                                                vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_NAME_LIST]);\r
+\r
+       return (char *)(vrs + anim_names_offsets[spri->curr_anim]);\r
+}\r
+\r
+void init_anim(struct sprite *spri, int anim_index)\r
+{\r
+       struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;\r
+       uint32_t far *anim_lists_offsets =      (uint32_t far *)\r
+                                               ((byte far *)vrs +\r
+                                                vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_LIST]);\r
+       struct vrs_animation_list_entry_t far *anim_list =      (struct vrs_animation_list_entry_t far *)\r
+                                                               ((byte far *)vrs +\r
+                                                                anim_lists_offsets[anim_index]);\r
+\r
+       // Upon new animation, start from the first sprite in it\r
+       spri->curr_anim = anim_index;\r
+       spri->curr_anim_spri = 0;\r
+       spri->curr_spri_id = anim_list[0].sprite_id;\r
+       spri->delay = anim_list[0].delay;\r
+\r
+       spri->curr_anim_list = anim_list;\r
+}\r
+\r
+int set_anim_by_id(struct sprite *spri, int anim_id)\r
+{\r
+       int new_anim_index = 0;\r
+       int iter_id;\r
+       struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;\r
+       // Retruve animation ids list\r
+        uint16_t far *anim_ids =       (uint16_t far *)\r
+                                       ((byte far *)vrs +\r
+                                        vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);\r
+\r
+       // Loop through animation id untill match or end of list\r
+       while(iter_id = anim_ids[new_anim_index])\r
+       {\r
+               // Return on successful match\r
+               if (iter_id == anim_id)\r
+               {\r
+                       init_anim(spri, new_anim_index);\r
+                       return 0;\r
+               }\r
+               new_anim_index++;\r
+       }\r
+       return -1;\r
+}\r
+\r
+void print_anim_ids(struct sprite *spri)\r
+{\r
+       int new_anim_index = 0;\r
+       int iter_id;\r
+       struct vrs_header far *vrs = spri->spritesheet->vrs_hdr;\r
+       // Retruve animation ids list\r
+        uint16_t far *anim_ids =       (uint16_t far *)\r
+                                       ((byte far *)vrs +\r
+                                        vrs->offset_table[VRS_HEADER_OFFSET_ANIMATION_ID_LIST]);\r
+\r
+       if(!anim_ids[new_anim_index])\r
+               return;\r
+       // Loop through animation id untill match or end of list\r
+       while(iter_id = anim_ids[new_anim_index])\r
+       {\r
+               printf("s%d ", iter_id);\r
+               new_anim_index++;\r
+       }\r
+}\r
+\r
+void animate_spri(entity_t *enti, video_t *video)\r
+{\r
+#define GVARVIDEO video\r
+#define VMEMPAGESIZE2  GVARVIDEO->page[0].pagesize+GVARVIDEO->page[1].pagesize\r
+#define VMEMPAGEDATA2  GVARVIDEO->page[2].data\r
+       unsigned int i,o,o2; int j;\r
+       int x,y,rx,ry,w,h;\r
+       int overdraw = 0;//16;  // 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
+\r
+       // Depending on delay, update indices\r
+//#define FRAME1 modexDrawSpriteRegion(pip[(pip->video->p)].page, x, y, 48, player[pn].enti.dire, 24, 32,      PLAYERBMPDATAPTR);\r
+//#define FRAME2 modexDrawSpriteRegion(pip[(pip->video->p)].page, x, y, 24, player[pn].enti.dire, 24, 32,      PLAYERBMPDATAPTR); stand\r
+//#define FRAME3 modexDrawSpriteRegion(pip[(pip->video->p)].page, x, y, 0, player[pn].enti.dire, 24, 32,       PLAYERBMPDATAPTR);\r
+//#define FRAME4 modexDrawSpriteRegion(pip[(pip->video->p)].page, x, y, 24, player[pn].enti.dire, 24, 32,      PLAYERBMPDATAPTR); stand\r
+       switch(enti->spri->delay)\r
+{\r
+               // Delay = 0 means that sprite should loop. Nothing to change here\r
+               case 0:\r
+               break;\r
+\r
+               // Delay = 1 means that on next time unit sprite should be changed\r
+               case 1:\r
+                       if(enti->invq)  enti->spri->curr_anim_spri++;\r
+                       else                    enti->spri->curr_anim_spri--;\r
+//printf("1[%u]        %u", enti->invq, enti->spri->curr_anim_spri);\r
+                       // If we hit the end of an animation sequence, restart it\r
+                       if(!(   enti->spri->curr_spri_id = enti->spri->curr_anim_list[enti->spri->curr_anim_spri].sprite_id)){\r
+                               //enti->spri->curr_anim_spri = 1;\r
+                               enti->invq=!enti->invq;\r
+                               if(enti->invq)                          enti->spri->curr_anim_spri+=2;\r
+                               else if(enti->spri->curr_anim_spri)     enti->spri->curr_anim_spri-=2;\r
+                               enti->spri->curr_spri_id = enti->spri->curr_anim_list[enti->spri->curr_anim_spri].sprite_id;\r
+//printf("     enti->spri->curr_anim_spri=%u   ", enti->spri->curr_anim_spri);\r
+                       }\r
+                       enti->spri->delay = enti->spri->curr_anim_list[enti->spri->curr_anim_spri].delay;\r
+//printf("\n");\r
+//break;\r
+               // Delay > 1 means that we should not change sprite yet. Decrease delay\r
+               default:\r
+//printf("2[%u]        %u\n", enti->invq, enti->spri->curr_anim_spri);\r
+                       enti->spri->delay--;\r
+               break;\r
+       }\r
+\r
+       // Events go here\r
+\r
+\r
+       omemptr = (VGA_RAM_PTR)video->page[0].data;// save original mem ptr\r
+       x=enti->spri->x;\r
+       y=enti->spri->y;\r
+\r
+       // Draw sprite\r
+       j = get_vrl_by_id(enti->spri->spritesheet, enti->spri->curr_spri_id, enti->spri->sprite_vrl_cont);\r
+       if(j < 0)\r
+       {\r
+               //Quit (gv, "Error retriving required sprite");\r
+               return;\r
+       }\r
+\r
+       // render box bounds. y does not need modification, but x and width must be multiple of 4\r
+       if(!GVARVIDEO->rss){\r
+       if (x >= overdraw) rx = (x - overdraw) & (~3);\r
+               else rx = -(GVARVIDEO->page[0].dx);\r
+       if (y >= overdraw) ry = (y - overdraw);\r
+               else ry = -(GVARVIDEO->page[0].dy);\r
+       h = enti->spri->sprite_vrl_cont->vrl_header->height + overdraw + y - ry;\r
+       w = (x + enti->spri->sprite_vrl_cont->vrl_header->width + (overdraw*2) + 3 - rx) & (~3);//round up\r
+       if ((rx+w) > GVARVIDEO->page[0].width) w = GVARVIDEO->page[0].width-rx;\r
+       if ((ry+h) > GVARVIDEO->page[0].height) h = GVARVIDEO->page[0].height-ry;\r
+\r
+       // block copy pattern to where we will draw the sprite\r
+       vga_setup_wm1_block_copy();\r
+       o2 = VMEMPAGESIZE2;\r
+       o = (0x10000UL - (uint16_t)VMEMPAGEDATA2) + (ry * GVARVIDEO->page[0].stridew) + (rx >> 2); // source offscreen\r
+       for (i=0;i < h;i++,o += GVARVIDEO->page[0].stridew,o2 += (w >> 2)) vga_wm1_mem_block_copy(o2,o,w >> 2);\r
+\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 = w >> 2;\r
+       vga_state.vga_graphics_ram = omemptr + VMEMPAGESIZE2;\r
+       }else{ rx=ry=w=h=0; vga_state.vga_graphics_ram = (VGA_RAM_PTR)video->page[0].data; }\r
+       vga_state.vga_draw_stride_limit = (GVARVIDEO->page[0].width + 3 - x) >> 2;//round up\r
+\r
+       // then the sprite. note modding ram ptr means we just draw to (x&3,0)\r
+#ifndef SPRITE\r
+       modexClearRegion(&GVARVIDEO->page[0], x, y, 16, 32, 1);\r
+#else\r
+       draw_vrl1_vgax_modex(\r
+               x-rx,\r
+               y-ry,\r
+               enti->spri->sprite_vrl_cont->vrl_header,\r
+               enti->spri->sprite_vrl_cont->line_offsets,\r
+               enti->spri->sprite_vrl_cont->buffer + sizeof(struct vrl1_vgax_header),\r
+               enti->spri->sprite_vrl_cont->data_size\r
+       );\r
+#endif\r
+       if(!GVARVIDEO->rss){\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 = VMEMPAGESIZE2; // source offscreen\r
+       o2 = (ry * GVARVIDEO->page[0].stridew) + (rx >> 2); // dest visible (original stride)\r
+       for (i=0;i < h;i++,o += vga_state.vga_draw_stride,o2 += GVARVIDEO->page[0].stridew) 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 = GVARVIDEO->page[0].stridew;\r
+\r
+       vga_state.vga_graphics_ram = video->omemptr;\r
+}\r