home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 5 / DATAFILE_PDCD5.iso / utilities / s1 / sesanim / !SESanim / Sources / c / MakeOne < prev    next >
Text File  |  1997-05-14  |  4KB  |  160 lines

  1. /* SaveSprite.c */
  2.  
  3. /* Saving an anim as a RiscOS sprite file
  4.  * (K) All Rites Reversed - Copy What You Like (see file Copying)
  5.  *
  6.  * Authors:
  7.  *      Peter Hartley       <peter@ant.co.uk>
  8.  *      Peter Hillman       <pmh@dcs.ed.ac.uk>: Severely buggering up
  9.                  SpriteSave.c to make to work (ish) for my own evil purposes
  10.  * History:
  11.  *      23-Oct-96 pdh Started
  12.  *      27-Oct-96 pdh Frob to cope with 3/5/6/7bpp anim's
  13.  *      27-Oct-96 *** Release 4beta1
  14.  *      29-Oct-96 pdh Fix mask code so it expands to whole pixels properly
  15.  *      29-Oct-96 *** Release 4beta2
  16.  *      07-Nov-96 *** Release 4
  17.  *      15-Dec-96 *** Release 5beta1
  18.  *      01-Jan-97 pdh Fix bug in saving 2bpp sprites
  19.  *      27-Jan-97 *** Release 5beta2
  20.  *      29-Jan-97 *** Release 5beta3
  21.  *      03-Feb-97 *** Release 5
  22.  *      07-Feb-97 *** Release 5.01
  23.  *      11-May-97 pmh ... And then I came and screwed it all up
  24.  */
  25.  
  26. #include <string.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29.  
  30. #include "DeskLib:Sprite.h"
  31. #include "MakeGif:bitmap.h"
  32. #include "MakeGif:Count.h"
  33. #include "MakeGif:utils.h"
  34. #if 0
  35. #define debugf printf
  36. #define DEBUG 1
  37. #else
  38. #define debugf 1?0:printf
  39. #define DEBUG 0
  40. #endif
  41.  
  42.  
  43. extern void CompressSpriteLine( char *dest, char *src, int n, int bpp );
  44. extern void CompressMaskLine( char *dest, char *src, int n, int bpp );
  45.  
  46. /* Sprites are only available in 1, 2, 4, 8 bpp */
  47. extern const char bppmap[8];
  48.  
  49.  
  50. /* Give this: your animation pointer (returned from OpenFile or whatever
  51.               a block of memory big wnough to store the sprite
  52.               how big the block was
  53.               which frame you want
  54.               a pointer to an int, to be loaded with the delay
  55.               (i.e. call with &dly to load (int) dly with the delay)
  56. pmh
  57. */
  58.  
  59. int Anim_MakeOneSprite( anim a, void * spritearea /*pmh*/, int buffsize, int framenum, int * delay)
  60. {
  61.     sprite_areainfo sai;
  62.     sprite_header *psh;
  63.     int nSpriteSize;
  64.     int i,y;
  65.     int newbpp;
  66.     int abw;
  67.     void *data;
  68. /*    unsigned int palette[256];*/
  69.     int newncol;
  70.     frameptr f2 = Anim_OpenFrame( a, framenum );
  71.     int nFrames = a->nFrames;
  72.  
  73.     if ( !f2 )
  74.     {
  75.         /* There was an error */
  76.         return -1;
  77.     }
  78.  
  79.     newbpp = bppmap[ MinBpp( f2->nColours ) - 1 ];
  80.     newncol = 1 << newbpp;
  81.     abw = ( ( (a->nWidth*newbpp)+31 ) >> 3 ) & ~3;  /* aligned byte width */
  82.  
  83.     nSpriteSize = abw;
  84.     nSpriteSize *= a->nHeight;
  85.     nSpriteSize += 8*newncol;
  86.     nSpriteSize += sizeof( sprite_header );
  87.  
  88.     Anim_CloseFrame( a, &f2 );
  89.  
  90.     if(buffsize<nSpriteSize) return nSpriteSize;
  91.  
  92.     /* Write the header */
  93.  /*   sai.numsprites = nFrames;
  94.     sai.firstoffset = 16;
  95.     sai.freeoffset = 16 + nFrames*nSpriteSize;
  96.     fwrite( &sai.numsprites, 1, 12, output );
  97.   */
  98.     data = spritearea /*pmh*/;
  99.    /* if ( !data )
  100.     {
  101.         Anim_NoMemory( "savesprite" );
  102.         return FALSE;
  103.     }*/
  104.  
  105.     psh = (sprite_header*) data;
  106.  
  107.     /* Write the frames */
  108.     psh->offset_next = nSpriteSize;
  109.     psh->width = (abw>>2)-1;
  110.     psh->height = a->nHeight-1;
  111.     psh->leftbit = 0;
  112.     psh->rightbit = (a->nWidth*newbpp - 1 ) & 31;
  113.     psh->imageoffset = sizeof(sprite_header) + 8*newncol;
  114.     psh->maskoffset = psh->imageoffset;
  115.     switch ( newbpp )
  116.     {
  117.     case 1: psh->screenmode = 18; break;
  118.     case 2: psh->screenmode = 19; break;
  119.     case 4: psh->screenmode = 20; break;
  120.     case 8: psh->screenmode = 21; break;
  121.     }
  122.  
  123.     for ( i=framenum; i == framenum; i+=428 )
  124.     {
  125.         frameptr f = Anim_OpenFrame( a, i );
  126.         char *src;
  127.         char *dest;
  128.         unsigned int *pPal = (unsigned int*)(psh+1);
  129.         src = f->pImage;
  130.         memset( psh->name, 0, 12 );
  131.         *delay = Anim_GetDelay( a, i, -1 );
  132.         sprintf( psh->name, "%03d", i );
  133.         for ( y=0; y < f->nColours; y++ )       /* NOT newncol */
  134.         {
  135.             *pPal++ = f->pPalette[y];
  136.             *pPal++ = f->pPalette[y];
  137.         }
  138.  
  139.         src = f->pImage;
  140.         dest = ((char*)(psh+1)) + newncol*8;
  141.         for ( y=0; y < a->nHeight; y++ )
  142.         {
  143.             CompressSpriteLine( dest, src, a->nWidth, newbpp );
  144.             dest += abw;
  145.             src += a->nWidth;
  146.         }
  147.         /*
  148.         src = f->pMask;
  149.         for ( y=0; y < a->nHeight; y++ )
  150.         {
  151.             CompressMaskLine( dest, src, a->nWidth, newbpp );
  152.             dest += abw;
  153.             src += a->nWidth;
  154.         }
  155.         */
  156.         Anim_CloseFrame( a, &f );
  157.     }
  158.     return 0;
  159. }
  160.