home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************
- * spr_fio.c
- *
- * The functions for reading sprites from a file.
- **********************************************************************
- This file is part of
-
- STK -- The sprite toolkit -- version 1.0
-
- Copyright (C) Jari Karjala 1990
-
- The sprite toolkit (STK) is a FreeWare toolkit for creating high
- resolution sprite graphics with PCompatible hardware. This toolkit
- is provided as is without any warranty or such thing. See the file
- COPYING for further information.
-
- **********************************************************************/
-
- #include <stdio.h>
- #include <string.h>
- #include <alloc.h>
-
- #include "sprP.h"
- #include "spr.h"
-
- /**********************************************************************
- * Reads the width and height form the given sprite map file.
- * Check the format ID, too.
- *
- * Return: 0 if no errors, negative otherwise
- **********************************************************************/
- int static read_width_height(FILE *fp, WORD *w, WORD *h)
- {
- char buf[80], buf2[80];
- int ok;
-
- ok = 0;
- while ((fgets(buf, sizeof(buf), fp)!=NULL) && buf[0]!='#')
- if (strncmp(buf, "/* SMP", 5)==0)
- ok = 1;
-
- if (!ok) /** no identifier -> not a valid sprite bitmap **/
- return -1;
-
- if (buf[0]!='#')
- return -2;
- if (sscanf(buf, "%s %s %d\n", buf2, buf2, w)!=3)
- return -3;
-
- while ((fgets(buf, sizeof(buf), fp)!=NULL) && buf[0]!='#')
- ;
- if (buf[0]!='#')
- return -4;
- if (sscanf(buf, "%s %s %d\n", buf2, buf2, h)!=3)
- return -5;
-
- return 0;
- }
-
- /**********************************************************************
- * Read a bitmap of w x h bits from the given file.
- * Return: 0 if no errors, negative otherwise
- **********************************************************************/
- int static read_map(FILE *fp, WORD w, WORD h, BITMAP bmp)
- {
- int c,x,y;
-
- while ((c=fgetc(fp))!=EOF && c!='{')
- ;
-
- if (c!='{')
- return -1;
-
- for (y=0; y<h; y++)
- for (x=0; x<w; x+=8) {
- if (fscanf(fp, "%x", bmp)!=1) /* get one hex byte */
- return -2;
- bmp++;
- c = fgetc(fp); /* remove ',' */
- }
-
- return 0;
-
- }
-
- /**********************************************************************
- * Create a sprite from the given SMP file.
- *
- * smpfile The sprite bitmap file name.
- * res The number of steps wanted per 8 bit interval in horizontal
- * direction (1,2,4,8). For example, the value 8 gives one
- * pixel resolution in X-direction.
- * ID The user supplied ID for the sprite (not obligatory)
- *
- * Return: the newly created sprite or NULL if file not found, read
- * error or out-of-memory
- **********************************************************************/
- SPRITE spr_fio_read_smp(char *smpfile, BYTE res, WORD ID)
- {
- SPRITE spr;
- FILE *fp;
- BITMAP shape, mask;
- WORD w,h,size;
-
- if ((fp=fopen(smpfile, "r"))==NULL)
- return NULL;
-
- if (read_width_height(fp, &w, &h) < 0)
- return NULL;
-
- if (w&7)
- size = h*((w>>3) + 1); /* not exact byte boundary, round up */
- else
- size = h*(w>>3);
-
- if ((shape=(BITMAP)malloc(size*2))==NULL)
- return NULL;
- mask = shape+size;
-
- if (read_map(fp, w, h, shape) < 0)
- return NULL;
- if (read_map(fp, w, h, mask) < 0)
- return NULL;
-
- fclose(fp);
-
- spr = spr_create(w, h, shape, mask, res, ID);
- free(shape);
-
- return spr;
- }
-