home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / GRAPHICS / MISC / STK100.ZIP / SPREDSRC.COM / SPREDFIO.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-20  |  8.3 KB  |  295 lines

  1. /**********************************************************************
  2. * spredfio.c
  3. * The sprite editor sprite data file IO
  4. **********************************************************************
  5.                     This file is part of
  6.  
  7.          STK -- The sprite toolkit -- version 1.0
  8.  
  9.               Copyright (C) Jari Karjala 1990
  10.  
  11. The sprite toolkit (STK) is a FreeWare toolkit for creating high
  12. resolution sprite graphics with PCompatible hardware. This toolkit 
  13. is provided as is without any warranty or such thing. See the file
  14. COPYING for further information.
  15.  
  16. **********************************************************************/
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22.  
  23. #include "grtypes.h"
  24. #include "spred.h"
  25.  
  26.  
  27. /**********************************************************************
  28. * Read the sprite map data from the given file.
  29. * First skip until a '{' character is reached, then
  30. * read w*h/8 bytes from the file.
  31. *
  32. * file    the file to use
  33. * smp     the sprite map to read the data into
  34. * w       the width of the map in pixels
  35. * h       the height of the map in pixels
  36. * xmap    if nonzero, then reading X11 bitmap (bits reversed)
  37. *
  38. * Return: 0 if all ok, negative otherwise
  39. **********************************************************************/
  40. int read_spred_map(FILE *file, SPRED_MAP smp, BYTE w, BYTE h, int xmap)
  41. {
  42.     int c, b, x,y,i;
  43.     
  44.     while ((c=fgetc(file))!=EOF && c!='{')
  45.         ;
  46.     
  47.     if (c!='{')
  48.         return -1;
  49.  
  50.     for (y=0; y<h; y++)
  51.         for (x=0; x<w; x+=8) {
  52.             if (fscanf(file, "%x", &b)!=1)      /* get one hex byte */
  53.                 return -2;
  54.             if (xmap)
  55.                 for (i=0; i<8; i++, b>>=1)          /* unpack X bitmap   */
  56.                     smp[x+i][y] = (((BYTE)b & 1) != 0);
  57.             else
  58.                 for (i=0; i<8; i++, b<<=1)          /* unpack sprite map */
  59.                     smp[x+i][y] = (((BYTE)b & (1<<7)) != 0);
  60.             c = fgetc(file);                    /* remove ',' */
  61.         }
  62.         
  63.     return 0;
  64. }
  65.  
  66. /**********************************************************************
  67. * Reads an sprite map file into the given internal data structure 
  68. * If no line starting with the string "/* SMP" is found, the file
  69. * is assumed to be an X11 bitmap.
  70. *
  71. * smpfile file to read the data from
  72. * sdp     pointer into the destination struct
  73. *
  74. * Return: sdp updated and 0 if no errors, negative otherwise
  75. **********************************************************************/
  76. int smp_to_spred_data(FILE *smpfile, SPRED_DATA *sdp)
  77. {
  78.     char buf[80], buf2[80];
  79.     int w,h,xmap;
  80.  
  81.     xmap = 1;
  82.     while ((fgets(buf, sizeof(buf), smpfile)!=NULL) && buf[0]!='#')
  83.         if (strncmp(buf, "/* SMP", 5)==0)
  84.             xmap = 0;
  85.  
  86.     if (buf[0]!='#')
  87.         return -1;
  88.     if (sscanf(buf, "%s %s %d\n", buf2, buf2, &w)!=3)
  89.         return -2;
  90.  
  91.     while ((fgets(buf, sizeof(buf), smpfile)!=NULL) && buf[0]!='#')
  92.         ;    
  93.     if (buf[0]!='#')
  94.         return -4;
  95.     if (sscanf(buf, "%s %s %d\n", buf2, buf2, &h)!=3)
  96.         return -5;
  97.     
  98.     if (w > MAX_SPRITE_WIDTH || h > MAX_SPRITE_HEIGHT)
  99.         return -6;
  100.     
  101.     if ((sdp->w | sdp->h) == 0) {   /* previous values zero, use new values */
  102.         sdp->w = w;
  103.         sdp->h = h;
  104.     }
  105.     
  106.     if (read_spred_map(smpfile, sdp->maps[SPR_SHAPE], w, h, xmap)<0)
  107.         return -7;
  108.     
  109.     if (!xmap)  /** X bitmaps has no mask map **/
  110.         if (read_spred_map(smpfile, sdp->maps[SPR_MASK], w, h, 0)<0)
  111.             return -8;
  112.     
  113.     return 0;
  114. }
  115.  
  116. /**********************************************************************
  117. * Load the sprite sdp->name. If sdp->h and sdp->w were non-zero
  118. * they are preserved, otherwise the read values are used.
  119. * Return: 0 if no errors, negative otherwise 
  120. *           (-1 fopen, -2 fread, -3 fclose error)
  121. **********************************************************************/
  122. int load_sprite(SPRED_DATA *sdp)
  123. {
  124.     FILE *fp;
  125.  
  126.     if ((fp=fopen(sdp->name, "r"))==NULL)
  127.         return -1;
  128.  
  129.     if (smp_to_spred_data(fp, sdp) < 0)
  130.         return -2;
  131.  
  132.     if (fclose(fp))
  133.         return -3;
  134.     
  135.     return 0;
  136. }
  137.  
  138. /**********************************************************************
  139. * Write the sprite map data from the given file
  140. *
  141. * file    the file to use
  142. * smp     the sprite map to write the data from
  143. * w       the width of the map in pixels
  144. * h       the height of the map in pixels
  145. *
  146. * Return: 0 if all ok, negative otherwise
  147. **********************************************************************/
  148. int write_spred_map(FILE *file, SPRED_MAP smp, BYTE w, BYTE h)
  149. {
  150.     int c, x,y,i;
  151.     BYTE b;
  152.     
  153.     for (c=0, y=0; y<h; y++)
  154.         for (x=0; x<w; x+=8) {
  155.             for (b=0, i=0; i<8; i++)            /* pack 8 bits into a byte */
  156.                 b = (b<<1) | smp[x+i][y];
  157.  
  158.             if ((c%12)==0)                      /* line start with a tab */
  159.                 fprintf(file, "\t");
  160.             
  161.             if (fprintf(file, "0x%02X,", b)<5)  /* output it */
  162.                 return -1;
  163.             
  164.             if (((++c) % 12)==0)                /* 12 bytes per line */
  165.                 fprintf(file, "\n");
  166.         }
  167.         
  168.     if ((c % 12)!=0)
  169.         fprintf(file, "\n");
  170.         
  171.     return 0;
  172. }
  173.  
  174. /**********************************************************************
  175. * Writes the given internal data structure into an sprite map file
  176. *
  177. * smpfile file to write the data to
  178. * sdp     pointer into the source struct
  179. *
  180. * Return: 0 if no errors, negative otherwise
  181. **********************************************************************/
  182. int spred_data_to_smp(FILE *smpfile, SPRED_DATA *sdp)
  183. {
  184.     char buf[SPR_NAME_LEN], *cp;
  185.  
  186.     /** take just the basename (no path, no extension) **/
  187.     if ((cp=strchr(sdp->name, '\\'))==NULL)
  188.         cp = sdp->name-1;
  189.     strcpy(buf, cp+1);
  190.     if ((cp=strchr(buf, '.'))!=NULL)
  191.         *cp = '\0';
  192.     for (cp=buf; *cp; cp++)
  193.         if (isupper(*cp))
  194.             *cp = tolower(*cp);
  195.     
  196.     fprintf(smpfile,
  197.         "/* SMP1  Sprite shape and mask bitmaps created by the SPRED */\n\n");
  198.     
  199.     fprintf(smpfile, "#define %s_width  %d\n", buf, sdp->w);
  200.     fprintf(smpfile, "#define %s_height %d\n", buf, sdp->h);
  201.     
  202.     fprintf(smpfile, "\nunsigned char %s_shape[] = {\n", buf);
  203.     if (write_spred_map(smpfile, sdp->maps[SPR_SHAPE], sdp->w, sdp->h)<0)
  204.         return -1;
  205.     fprintf(smpfile, "};\n");
  206.     
  207.     fprintf(smpfile, "unsigned char %s_mask[] = {\n", buf);
  208.     if (write_spred_map(smpfile, sdp->maps[SPR_MASK], sdp->w, sdp->h)<0)
  209.         return -1;
  210.     fprintf(smpfile, "};\n");
  211.  
  212.     return 0;
  213. }
  214.  
  215. /**********************************************************************
  216. * Save the given sprite.
  217. * Return: 0 if no errors, negative otherwise
  218. *           (-1 fopen, -2 fwrite, -3 fclose error)
  219. **********************************************************************/
  220. int save_sprite(SPRED_DATA *sdp)
  221. {
  222.     FILE *fp;
  223.  
  224.     if ((fp=fopen(sdp->name, "w"))==NULL)
  225.         return -1;
  226.  
  227.     if (spred_data_to_smp(fp, sdp) < 0)
  228.         return -2;
  229.  
  230.     if (fclose(fp))
  231.         return -3;
  232.     
  233.     return 0;
  234. }
  235.  
  236.  
  237. #ifdef TEST
  238.  
  239. SPRED_DATA sd;
  240.  
  241. void main(int argc, char **argv)
  242. {
  243.     char buf[50], *cp;
  244.     int i,j;
  245.     FILE *fp;
  246.     
  247.     if (argc<2)
  248.         exit(1);
  249.     
  250.     strcpy(buf, argv[1]);
  251.     strcpy(sd.name, argv[1]);
  252.     
  253.     if ((cp=strrchr(sd.name, '.'))!=NULL)
  254.         *cp='\0';
  255.     
  256.     if ((fp=fopen(buf, "r"))==NULL) {
  257.         perror("main");
  258.         exit(2);
  259.     }
  260.  
  261.     if ((i=smp_to_spred_data(fp, &sd))<0) {
  262.         fprintf(stderr, "main:smp_to_spred_data failed, code %d", i);
  263.         exit(3);
  264.     }
  265.     
  266.     fclose(fp);
  267.     
  268.     for (i=0; i<sd.h; i++) {
  269.         for (j=0; j<sd.w; j++)
  270.             putchar(sd.shape[j][i]+'0');
  271.         putchar('\n');
  272.     }
  273.     for (i=0; i<sd.h; i++) {
  274.         for (j=0; j<sd.w; j++)
  275.             putchar(sd.mask[j][i]+'0');
  276.         putchar('\n');
  277.     }
  278.  
  279.     
  280.     if ((fp=fopen(sd.name, "w"))==NULL) {
  281.         perror("main");
  282.         exit(4);
  283.     }
  284.  
  285.     if ((i=spred_data_to_smp(fp, &sd))<0) {
  286.         fprintf(stderr, "main:spred_data_to_smp failed, code %d", i);
  287.         exit(5);
  288.     }
  289.     fclose(fp);
  290.     
  291. }
  292.  
  293. #endif
  294.