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

  1. /**********************************************************************
  2. * spr_fio.c
  3. * The functions for reading sprites from a file.
  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 <alloc.h>
  21.  
  22. #include "sprP.h"
  23. #include "spr.h"
  24.  
  25. /**********************************************************************
  26. * Reads the width and height form the given sprite map file.
  27. * Check the format ID, too.
  28. * Return: 0 if no errors, negative otherwise
  29. **********************************************************************/
  30. int static read_width_height(FILE *fp, WORD *w, WORD *h)
  31. {
  32.     char buf[80], buf2[80];
  33.     int ok;
  34.  
  35.     ok = 0;
  36.     while ((fgets(buf, sizeof(buf), fp)!=NULL) && buf[0]!='#')
  37.         if (strncmp(buf, "/* SMP", 5)==0)
  38.             ok = 1;
  39.         
  40.     if (!ok)            /** no identifier -> not a valid sprite bitmap **/
  41.         return -1;
  42.     
  43.     if (buf[0]!='#')
  44.         return -2;
  45.     if (sscanf(buf, "%s %s %d\n", buf2, buf2, w)!=3)
  46.         return -3;
  47.  
  48.     while ((fgets(buf, sizeof(buf), fp)!=NULL) && buf[0]!='#')
  49.         ;    
  50.     if (buf[0]!='#')
  51.         return -4;
  52.     if (sscanf(buf, "%s %s %d\n", buf2, buf2, h)!=3)
  53.         return -5;
  54.     
  55.     return 0;
  56. }
  57.  
  58. /**********************************************************************
  59. * Read a bitmap of w x h bits from the given file.
  60. * Return: 0 if no errors, negative otherwise
  61. **********************************************************************/
  62. int static read_map(FILE *fp, WORD w, WORD h, BITMAP bmp)
  63. {
  64.     int c,x,y;
  65.     
  66.     while ((c=fgetc(fp))!=EOF && c!='{')
  67.         ;
  68.     
  69.     if (c!='{')
  70.         return -1;
  71.  
  72.     for (y=0; y<h; y++)
  73.         for (x=0; x<w; x+=8) {
  74.             if (fscanf(fp, "%x", bmp)!=1)     /* get one hex byte */
  75.                 return -2;
  76.             bmp++;
  77.             c = fgetc(fp);                    /* remove ',' */
  78.         }
  79.         
  80.     return 0;
  81.     
  82. }
  83.  
  84. /**********************************************************************
  85. * Create a sprite from the given SMP file.
  86. *
  87. * smpfile   The sprite bitmap file name.
  88. * res       The number of steps wanted per 8 bit interval in horizontal
  89. *           direction (1,2,4,8). For example, the value 8 gives one
  90. *           pixel resolution in X-direction.
  91. * ID        The user supplied ID for the sprite (not obligatory)
  92. *
  93. * Return: the newly created sprite or NULL if file not found, read
  94. *         error or out-of-memory
  95. **********************************************************************/
  96. SPRITE spr_fio_read_smp(char *smpfile, BYTE res, WORD ID)
  97. {
  98.     SPRITE spr;
  99.     FILE *fp;
  100.     BITMAP shape, mask;
  101.     WORD w,h,size;
  102.  
  103.     if ((fp=fopen(smpfile, "r"))==NULL)
  104.         return NULL;
  105.  
  106.     if (read_width_height(fp, &w, &h) < 0)
  107.         return NULL;
  108.  
  109.     if (w&7)
  110.         size = h*((w>>3) + 1);    /* not exact byte boundary, round up */
  111.     else
  112.         size = h*(w>>3);
  113.     
  114.     if ((shape=(BITMAP)malloc(size*2))==NULL)
  115.         return NULL;
  116.     mask = shape+size;
  117.     
  118.     if (read_map(fp, w, h, shape) < 0)
  119.         return NULL;
  120.     if (read_map(fp, w, h, mask) < 0)
  121.         return NULL;
  122.  
  123.     fclose(fp);
  124.     
  125.     spr = spr_create(w, h, shape, mask, res, ID);
  126.     free(shape);
  127.     
  128.     return spr;
  129. }
  130.