home *** CD-ROM | disk | FTP | other *** search
/ Dream 44 / Amiga_Dream_44.iso / Linux / Apps / xanim.tgz / xanim / xanim27064 / xa_txt.c < prev    next >
C/C++ Source or Header  |  1997-01-26  |  6KB  |  213 lines

  1.  
  2. /*
  3.  * xa_txt.c
  4.  *
  5.  * Copyright (C) 1990,1991,1992,1993,1994,1995,1996,1997 by Mark Podlipec. 
  6.  * All rights reserved.
  7.  *
  8.  * This software may be freely copied, modified and redistributed without
  9.  * fee for non-commerical purposes provided that this copyright notice is
  10.  * preserved intact on all copies and modified copies.
  11.  * 
  12.  * There is no warranty or other guarantee of fitness of this software.
  13.  * It is provided solely "as is". The author(s) disclaim(s) all
  14.  * responsibility and liability with respect to this software's usage
  15.  * or its effect upon hardware or computer systems.
  16.  *
  17.  */
  18.  
  19. #include "xa_gif.h"
  20.  
  21.  
  22. xaULONG TXT_Read_File();
  23. void GIF_Free_Frame_List();
  24. GIF_FRAME *GIF_Read_File();
  25.  
  26. extern XA_Open_And_ID_File();
  27. extern xaULONG XA_Setup_Input_Methods();
  28.  
  29. typedef struct
  30. {
  31.   xaULONG frame_cnt;
  32.   GIF_FRAME *gframes;
  33. } TXT_FRAME_LST;
  34.  
  35. static char gif_file_name[256];
  36. static xaULONG txt_max_imagex,txt_max_imagey,txt_max_imagec,txt_max_imaged;
  37.  
  38. /*
  39.  * This file parse the txt animation file and converts it into actions. 
  40.  *
  41.  */
  42. xaULONG TXT_Read_File(fname,anim_hdr)
  43. XA_ANIM_HDR *anim_hdr;
  44. char *fname;
  45. { XA_INPUT *xin = anim_hdr->xin;
  46.   FILE *fp;
  47.   xaLONG ret,i,f_cnt;
  48.   xaLONG num_of_files;
  49.   xaLONG total_frame_cnt,txt_frame_cnt,t_time;
  50.   xaLONG *txt_frames,txtframe_num;
  51.   TXT_FRAME_LST *txt_frame_lst;
  52.   char *tmp_fname = anim_hdr->name;
  53.  
  54.   txt_max_imagex = 0;
  55.   txt_max_imagey = 0;
  56.   txt_max_imagec = 0;
  57.   txt_max_imaged = 0;
  58.  
  59.   xin->Close_File(xin); /* POD Temporary */
  60.   if ( (fp=fopen(fname,XA_OPEN_MODE))==0)
  61.   { 
  62.     fprintf(stderr,"Can't open %s for reading.\n",fname); 
  63.     return(xaFALSE);
  64.   }
  65.  
  66.   /* read and throw away txt91 header */
  67.   fscanf(fp,"%*s");
  68.  
  69.   /* Read the number of files */
  70.   fscanf(fp,"%d",&num_of_files);
  71.   if (num_of_files <= 0)
  72.   {
  73.     fprintf(stderr,"num_of_file is invalid %d\n",num_of_files);
  74.     fclose(fp);
  75.     return(xaFALSE);
  76.   }
  77.  
  78.   txt_frame_lst = (TXT_FRAME_LST *) 
  79.         malloc( sizeof(TXT_FRAME_LST) * num_of_files);
  80.   if (txt_frame_lst == 0) TheEnd1("TXT_Read_File: malloc err");
  81.  
  82.   /* Read in the GIF files, use only the 1st one's colormap
  83.    */
  84.   for(i=0; i<num_of_files; i++)
  85.   { xaULONG anim_type;
  86.  
  87.     fscanf(fp,"%s",gif_file_name);
  88.     fprintf(stderr,"Reading %s\n",gif_file_name);
  89.  
  90.     anim_hdr->name = gif_file_name;
  91.  
  92.     if (XA_Setup_Input_Methods(anim_hdr->xin, anim_hdr->name) == xaFALSE)
  93.                                     continue;
  94.  
  95.     anim_type = XA_Open_And_ID_File(anim_hdr);
  96.     if (anim_hdr->xin->type_flag & XA_IN_TYPE_RANDOM)
  97.     { anim_hdr->xin->Seek_FPos(anim_hdr->xin,0,0);
  98.       anim_hdr->xin->buf_cnt = 0;
  99.       anim_hdr->xin->fpos = 0;
  100.     }
  101.     if (anim_type == GIF_ANIM)
  102.     {
  103.       txt_frame_lst[i].gframes 
  104.     = GIF_Read_File(gif_file_name,anim_hdr,&ret);
  105.       txt_frame_lst[i].frame_cnt = ret;
  106.       if (anim_hdr->imagex > txt_max_imagex) txt_max_imagex = anim_hdr->imagex;
  107.       if (anim_hdr->imagey > txt_max_imagey) txt_max_imagey = anim_hdr->imagey;
  108.       if (anim_hdr->imagec > txt_max_imagec) txt_max_imagec = anim_hdr->imagec;
  109.       if (anim_hdr->imaged > txt_max_imaged) txt_max_imaged = anim_hdr->imaged;
  110.     }
  111.     else
  112.     {
  113.       fprintf(stderr,"TXT files only allow GIF images to be listed %x\n",
  114.                                 anim_type);
  115.     }
  116.   }
  117.  
  118.   anim_hdr->name = tmp_fname;
  119.   /* Check for Frame list at end of images.
  120.    */
  121.   ret=fscanf(fp,"%d",&txtframe_num);
  122.   if ( (ret == 1) && (txtframe_num >= 0))
  123.   {
  124.     xaLONG tmp_txtframe; 
  125.  
  126.     /* read in txt frame list, keep track of actual frames since each
  127.      * txt_frame can have several frames(cmaps and images);
  128.      */
  129.     txt_frames = (xaLONG *) malloc(txtframe_num * sizeof(xaLONG) );
  130.     if (txt_frames == 0) TheEnd1("TXT_Read_File: frames malloc err");
  131.     total_frame_cnt = 0;
  132.     txt_frame_cnt   = 0;
  133.  
  134.     for(i=0; i < txtframe_num; i++)
  135.     {
  136.       ret = fscanf(fp,"%d",&tmp_txtframe);
  137.       if ( (ret==1) && (tmp_txtframe >= 0) && (tmp_txtframe < num_of_files) )
  138.       {
  139.         txt_frames[txt_frame_cnt] = tmp_txtframe;
  140.         total_frame_cnt += txt_frame_lst[ tmp_txtframe ].frame_cnt;
  141.         txt_frame_cnt++;
  142.       }
  143.       else
  144.         fprintf(stderr,"TXT_READ: bad frame number (%d) in frame list\n",
  145.                               tmp_txtframe);
  146.     }
  147.   } /* end of frame included at end */
  148.   else
  149.   {
  150.     txt_frames = (xaLONG *) malloc(num_of_files * sizeof(xaLONG) );
  151.     if (txt_frames == 0) TheEnd1("TXT_Read_File: no frames malloc err");
  152.  
  153.     txt_frame_cnt = num_of_files;
  154.     total_frame_cnt = 0;
  155.     for(i=0; i<txt_frame_cnt; i++)
  156.     {
  157.       txt_frames[i] = i;
  158.       total_frame_cnt += txt_frame_lst[ i ].frame_cnt;
  159.     }
  160.   }
  161.  
  162.   /* Allocate a frame_lst of that size.
  163.    */
  164.   anim_hdr->frame_lst 
  165.     = (XA_FRAME *)malloc(sizeof(XA_FRAME) * (total_frame_cnt + 1));
  166.   if (anim_hdr->frame_lst == NULL) TheEnd1("TXT_ANIM: frame malloc err");
  167.  
  168.   f_cnt = 0;
  169.   t_time = 0;
  170.   /* loop through valid frame list, adding gif_frames */
  171.   for(i = 0; i < txt_frame_cnt; i++)
  172.   {
  173.     xaULONG k,frame_cnt;
  174.     GIF_FRAME *gtmp;
  175.  
  176.     gtmp      = txt_frame_lst[ txt_frames[i] ].gframes;
  177.     frame_cnt = txt_frame_lst[ txt_frames[i] ].frame_cnt;
  178.     k = 0;
  179.     while(gtmp != 0)
  180.     {
  181.       if (k >= frame_cnt)
  182.       {
  183.         fprintf(stderr,"TXT_Read_Anim: frame inconsistency %d %d\n",
  184.         k,frame_cnt);
  185.         break;
  186.       }
  187.       anim_hdr->frame_lst[f_cnt].time_dur = gtmp->time;
  188.       anim_hdr->frame_lst[f_cnt].zztime = t_time;
  189.       t_time += gtmp->time;
  190.       anim_hdr->frame_lst[f_cnt].act  = gtmp->act;
  191.       gtmp = gtmp->next;
  192.       k++; f_cnt++;
  193.     } /* end of while */
  194.   } /* end of for */
  195.   anim_hdr->imagex = txt_max_imagex;
  196.   anim_hdr->imagey = txt_max_imagey;
  197.   anim_hdr->imagec = txt_max_imagec;
  198.   anim_hdr->imaged = txt_max_imaged;
  199.   anim_hdr->frame_lst[f_cnt].time_dur = 0;
  200.   anim_hdr->frame_lst[f_cnt].zztime = -1;
  201.   anim_hdr->frame_lst[f_cnt].act  = 0;
  202.   anim_hdr->loop_frame = 0;
  203.   anim_hdr->last_frame = f_cnt - 1;
  204.   FREE(txt_frames,0x4000); txt_frames=0;
  205.   for(i=0; i < num_of_files; i++)
  206.     GIF_Free_Frame_List(txt_frame_lst[i].gframes);
  207.   FREE(txt_frame_lst,0x4001); txt_frame_lst=0;
  208.   fclose(fp);
  209.   return(xaTRUE);
  210. }
  211.  
  212.  
  213.