Source code for /engineering/2XTILE/MAKESPRI.COriginal file MAKESPRI.C
   1 /*
   2 
   3  MAKESPRI utility for the XTile graphics manager
   4 
   5  COPYRIGHT (C) 1992  Erich P Gatejen   All Rights Reserved
   6 
   7 
   8  File: MAKESPRI.C    	Written for the Turbo C compiler
   9 
  10 
  11  This will convert .VGA bitmaps to XTile sprite format ( .SPI and .SPM )
  12 
  13 
  14  MUST BE COMPILED IN LARGE MODEL !!!
  15 
  16 */
  17 
  18 #include<stdio.h>
  19 #include<conio.h>
  20 #include<alloc.h>
  21 
  22 
  23 
  24 FILE 	*in,		*out,
  25 		*Mask,	*Final;
  26 char		*inb,     *outb,	/* Base pointers to input and output pic buffers */
  27 		*BufI,	*BufO;	/* Pointers into buffers	*/
  28 
  29 char		*base,    *current; /* Pointers for creating the mask	*/
  30 
  31 unsigned char	Hold;		/* Mask hold					*/
  32 
  33 char		File_NameI[13];
  34 char		File_NameO[13];
  35 char		File_NameM[13];	/* File name buffers	*/
  36 
  37 unsigned int	Xsize, Ysize, Totalsize,
  38 			Newsize;				/* X,Y size and Picture size	*/
  39 
  40 unsigned int	AlignSize, PlaneSize;
  41 long			Pos;
  42 
  43 int		Align, Astep;				/* Current alignment		*/
  44 
  45 int       TossNumber;
  46 
  47 int		step, step2, XBlocks,
  48 		NewXBlocks;				/* Loop vars and number of XBlocks	*/
  49 
  50 main () {
  51 
  52 	step = 0;
  53 
  54 	printf("File name : ");
  55 	scanf("%8s", File_NameI );	/* Get file name from user */
  56 
  57 	do {
  58 		File_NameO[ step ] = File_NameI[ step ];
  59 		File_NameM[ step ] = File_NameI[ step ];
  60 
  61 		step++;
  62 
  63 	   } while (File_NameI[ step ] != 0);	/* Pack file name */
  64 
  65 	File_NameI[ step ] = '.';
  66 	File_NameO[ step ] = '.';
  67 	File_NameM[ step ] = '.';
  68 	File_NameI[step+1] = 'V';
  69 	File_NameO[step+1] = 'S';
  70 	File_NameM[step+1] = 'S';
  71 	File_NameI[step+2] = 'G';
  72 	File_NameO[step+2] = 'P';
  73 	File_NameM[step+2] = 'P';
  74 	File_NameI[step+3] = 'A';
  75 	File_NameO[step+3] = 'I';
  76 	File_NameM[step+3] = 'M';
  77 	File_NameI[step+4] = 0;
  78 	File_NameO[step+4] = 0;
  79 	File_NameM[step+4] = 0;		/* Add extentions to file names  */
  80 
  81 
  82 	/* open files */
  83 	in    = fopen( File_NameI, "rb" );
  84 	out   = fopen( "temp.spi", "wb" );
  85 	Final = fopen( File_NameO, "wb" );
  86 	Mask  = fopen( File_NameM, "wb" );
  87 
  88 
  89 	fread( &Xsize, sizeof( int ), 1, in );
  90 	fread( &Ysize, sizeof( int ), 1, in );  /* Read X,Y size from .VGA file */
  91 
  92 	/* Adjust for count from 0 */
  93 	Ysize++;
  94 	Xsize++;
  95 
  96 	/* Calc for input picture								*/
  97 	Totalsize = Xsize * Ysize;	/* Calc total pic size in bytes	*/
  98      XBlocks   = Totalsize / 4;    /* Calc number of XBlocks		*/
  99 
 100      /* Calc for sprite										*/
 101 	Newsize 	 = (Xsize+4) * Ysize;  /* Calc total pic size in bytes	*/
 102 	NewXBlocks = Newsize / 4;        /* Calc number of XBlocks		*/
 103 
 104 	inb  = farmalloc( Newsize );
 105 	outb = farmalloc( Newsize );
 106 
 107 	if (( in == NULL ) || ( out == NULL ) || ( outb == NULL) ||
 108 	    ( Mask == NULL )) exit(1);
 109 
 110 
 111 	/* Iterate for each alignment						*/
 112 	for ( Align = 0; Align < 4; Align++ ) {
 113 
 114 	   /* Read in picture and add bytes for alignment		*/
 115 	   BufI = inb;
 116 	   BufO = outb;
 117 	   for ( step = 0; step < Ysize; step++ ) { /* loop through each line */
 118 		 
 119 		 /* Add line's leading bytes	*/
 120 		 Astep = Align;
 121 		 while ( Astep > 0 ) {
 122 		    *BufI =  0;
 123 		    BufI++;
 124 		    Astep--;
 125 		 }; /* end while */
 126 
 127 		 /* Read data from file */
 128 		 fread( BufI, sizeof( char ), Xsize, in );
 129 		 
 130 		 /* Point to end of data just read */
 131 		 BufI = BufI + Xsize;
 132 
 133 		 /* Add line's trailing bytes */
 134 		 Astep = 4 - Align;
 135 		 while ( Astep > 0 ) {
 136 		    *BufI =  0;
 137 		    BufI++;
 138 		    Astep--;
 139 		 }; /* end while */
 140 
 141 	   }; /* end for */
 142 
 143 	   /* Reset input file for next iteration			    */
 144 	   rewind( in );
 145 
 146 	   /* If this is not the last alignment, read the size in again and toss */
 147 	   if ( Align < 3 ) {
 148 		 fread( &TossNumber, sizeof( int ), 1, in );
 149 		 fread( &TossNumber, sizeof( int ), 1, in ); 
 150 	   };
 151 
 152 	   /* Translate and place pic in output buffer			*/
 153 	   for ( step = 0; step < 4; step++ ) {
 154 
 155 	        BufI = inb + step;
 156 
 157 		   for ( step2 = 0; step2 < NewXBlocks; step2++ ) {
 158 
 159 			   *BufO = *BufI;
 160                   /* Plane at a time, so move every 4th pixel*/
 161 			   BufI  = BufI + 4;	
 162 			   BufO++;
 163 
 164 		   };
 165 
 166         }; /* end transfer */
 167 
 168 	   /* Write picture buffer  */
 169 	   fwrite( outb, sizeof( char ), Newsize, out ); 
 170 
 171 
 172 	 /* -- Make Mask -- */
 173 	   
 174 	   /* Point into picture */
 175 	   base    = outb;
 176 	   current = base;
 177 
 178 	   /* Loop through the picture and make the mask		*/
 179         /* If byte is 0, then mask it's bit				*/
 180 	   for ( step = 1; step <= Newsize; step = step + 4 ) {
 181 
 182 		   Hold = 0;    	/* Clear mask holding var		*/
 183 
 184 		   if (*current != 0) Hold = Hold + 1; /* Plane 0 */
 185 
 186 		   current = current + NewXBlocks;
 187 		   if (*current != 0) Hold = Hold + 2; /* Plane 1 */
 188 
 189 		   current = current + NewXBlocks;
 190 		   if (*current != 0) Hold = Hold + 4; /* Plane 2 */
 191 
 192 		   current = current + NewXBlocks;
 193 		   if (*current != 0) Hold = Hold + 8; /* Plane 3 */
 194 
 195 		   base++; 	/* Goto next XBlock  */
 196 
 197 		   current = base;
 198 
 199 		   fputc( Hold, Mask );  	/* Write mask to the file	*/
 200 
 201 	   }; /* end for */
 202 
 203 
 204 	}; /* end for loop */
 205 
 206 	fclose( in   );
 207 	fclose( out  );
 208 	fclose( Mask );  /* Close files	*/
 209 
 210 	/* Put like alignments back to back */
 211 	in   = fopen( "temp.spi", "rb" );		/* Reopen temp file for read */
 212 
 213 	PlaneSize = Newsize/4;	/* Size of an alignment div by 4. */
 214 
 215 	/* Loop for each plane */
 216 	for (step = 0; step < 4; step++ ) {
 217 
 218 	   /* First alignments plane */         /* SEEK_SET */
 219 	   Pos 	= PlaneSize * step;
 220 	   fseek ( in, Pos, SEEK_SET );
 221 	   fread( inb, 1, PlaneSize, in );
 222 	   fwrite(inb, 1, PlaneSize, Final);
 223 
 224 	   /* Second alignments plane */         /* SEEK_SET */
 225 	   Pos 	= Pos + Newsize;
 226 	   fseek ( in, Pos, SEEK_SET );
 227 	   fread( inb, 1, PlaneSize, in );
 228 	   fwrite(inb, 1, PlaneSize, Final);
 229 
 230 	   /* Third alignments plane */         /* SEEK_SET */
 231 	   Pos 	= Pos + Newsize;
 232 	   fseek ( in, Pos, SEEK_SET );
 233 	   fread( inb, 1, PlaneSize, in );
 234 	   fwrite(inb, 1, PlaneSize, Final);
 235 
 236 	   /* Forth alignments plane */         /* SEEK_SET */
 237 	   Pos 	= Pos + Newsize;
 238 	   fseek ( in, Pos, SEEK_SET );
 239 	   fread( inb, 1, PlaneSize, in );
 240 	   fwrite(inb, 1, PlaneSize, Final);
 241 
 242 	}; /* end plane loop */
 243 
 244 	/* Close last two files and kill temp */
 245 	fclose( in );
 246 	fclose( Final );
 247 	unlink("temp.spi");
 248 
 249 	/* Report new size */
 250 	printf("New size for sprite : %d X  by  %d Y.", Xsize + 4, Ysize );
 251 
 252 	/* Free pointers */
 253 	farfree(inb);
 254 	farfree(outb);
 255 	farfree(BufI);
 256 	farfree(BufO);
 257 	farfree(base);
 258 	farfree(current);
 259 
 260 	return(0);
 261 
 262 };