home *** CD-ROM | disk | FTP | other *** search
- /**********************************************************************
- * spredfio.c
- *
- * The sprite editor sprite data file IO
- **********************************************************************
- 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 <stdlib.h>
- #include <ctype.h>
-
- #include "grtypes.h"
- #include "spred.h"
-
-
- /**********************************************************************
- * Read the sprite map data from the given file.
- * First skip until a '{' character is reached, then
- * read w*h/8 bytes from the file.
- *
- * file the file to use
- * smp the sprite map to read the data into
- * w the width of the map in pixels
- * h the height of the map in pixels
- * xmap if nonzero, then reading X11 bitmap (bits reversed)
- *
- * Return: 0 if all ok, negative otherwise
- **********************************************************************/
- int read_spred_map(FILE *file, SPRED_MAP smp, BYTE w, BYTE h, int xmap)
- {
- int c, b, x,y,i;
-
- while ((c=fgetc(file))!=EOF && c!='{')
- ;
-
- if (c!='{')
- return -1;
-
- for (y=0; y<h; y++)
- for (x=0; x<w; x+=8) {
- if (fscanf(file, "%x", &b)!=1) /* get one hex byte */
- return -2;
- if (xmap)
- for (i=0; i<8; i++, b>>=1) /* unpack X bitmap */
- smp[x+i][y] = (((BYTE)b & 1) != 0);
- else
- for (i=0; i<8; i++, b<<=1) /* unpack sprite map */
- smp[x+i][y] = (((BYTE)b & (1<<7)) != 0);
- c = fgetc(file); /* remove ',' */
- }
-
- return 0;
- }
-
- /**********************************************************************
- * Reads an sprite map file into the given internal data structure
- * If no line starting with the string "/* SMP" is found, the file
- * is assumed to be an X11 bitmap.
- *
- * smpfile file to read the data from
- * sdp pointer into the destination struct
- *
- * Return: sdp updated and 0 if no errors, negative otherwise
- **********************************************************************/
- int smp_to_spred_data(FILE *smpfile, SPRED_DATA *sdp)
- {
- char buf[80], buf2[80];
- int w,h,xmap;
-
- xmap = 1;
- while ((fgets(buf, sizeof(buf), smpfile)!=NULL) && buf[0]!='#')
- if (strncmp(buf, "/* SMP", 5)==0)
- xmap = 0;
-
- if (buf[0]!='#')
- return -1;
- if (sscanf(buf, "%s %s %d\n", buf2, buf2, &w)!=3)
- return -2;
-
- while ((fgets(buf, sizeof(buf), smpfile)!=NULL) && buf[0]!='#')
- ;
- if (buf[0]!='#')
- return -4;
- if (sscanf(buf, "%s %s %d\n", buf2, buf2, &h)!=3)
- return -5;
-
- if (w > MAX_SPRITE_WIDTH || h > MAX_SPRITE_HEIGHT)
- return -6;
-
- if ((sdp->w | sdp->h) == 0) { /* previous values zero, use new values */
- sdp->w = w;
- sdp->h = h;
- }
-
- if (read_spred_map(smpfile, sdp->maps[SPR_SHAPE], w, h, xmap)<0)
- return -7;
-
- if (!xmap) /** X bitmaps has no mask map **/
- if (read_spred_map(smpfile, sdp->maps[SPR_MASK], w, h, 0)<0)
- return -8;
-
- return 0;
- }
-
- /**********************************************************************
- * Load the sprite sdp->name. If sdp->h and sdp->w were non-zero
- * they are preserved, otherwise the read values are used.
- * Return: 0 if no errors, negative otherwise
- * (-1 fopen, -2 fread, -3 fclose error)
- **********************************************************************/
- int load_sprite(SPRED_DATA *sdp)
- {
- FILE *fp;
-
- if ((fp=fopen(sdp->name, "r"))==NULL)
- return -1;
-
- if (smp_to_spred_data(fp, sdp) < 0)
- return -2;
-
- if (fclose(fp))
- return -3;
-
- return 0;
- }
-
- /**********************************************************************
- * Write the sprite map data from the given file
- *
- * file the file to use
- * smp the sprite map to write the data from
- * w the width of the map in pixels
- * h the height of the map in pixels
- *
- * Return: 0 if all ok, negative otherwise
- **********************************************************************/
- int write_spred_map(FILE *file, SPRED_MAP smp, BYTE w, BYTE h)
- {
- int c, x,y,i;
- BYTE b;
-
- for (c=0, y=0; y<h; y++)
- for (x=0; x<w; x+=8) {
- for (b=0, i=0; i<8; i++) /* pack 8 bits into a byte */
- b = (b<<1) | smp[x+i][y];
-
- if ((c%12)==0) /* line start with a tab */
- fprintf(file, "\t");
-
- if (fprintf(file, "0x%02X,", b)<5) /* output it */
- return -1;
-
- if (((++c) % 12)==0) /* 12 bytes per line */
- fprintf(file, "\n");
- }
-
- if ((c % 12)!=0)
- fprintf(file, "\n");
-
- return 0;
- }
-
- /**********************************************************************
- * Writes the given internal data structure into an sprite map file
- *
- * smpfile file to write the data to
- * sdp pointer into the source struct
- *
- * Return: 0 if no errors, negative otherwise
- **********************************************************************/
- int spred_data_to_smp(FILE *smpfile, SPRED_DATA *sdp)
- {
- char buf[SPR_NAME_LEN], *cp;
-
- /** take just the basename (no path, no extension) **/
- if ((cp=strchr(sdp->name, '\\'))==NULL)
- cp = sdp->name-1;
- strcpy(buf, cp+1);
- if ((cp=strchr(buf, '.'))!=NULL)
- *cp = '\0';
- for (cp=buf; *cp; cp++)
- if (isupper(*cp))
- *cp = tolower(*cp);
-
- fprintf(smpfile,
- "/* SMP1 Sprite shape and mask bitmaps created by the SPRED */\n\n");
-
- fprintf(smpfile, "#define %s_width %d\n", buf, sdp->w);
- fprintf(smpfile, "#define %s_height %d\n", buf, sdp->h);
-
- fprintf(smpfile, "\nunsigned char %s_shape[] = {\n", buf);
- if (write_spred_map(smpfile, sdp->maps[SPR_SHAPE], sdp->w, sdp->h)<0)
- return -1;
- fprintf(smpfile, "};\n");
-
- fprintf(smpfile, "unsigned char %s_mask[] = {\n", buf);
- if (write_spred_map(smpfile, sdp->maps[SPR_MASK], sdp->w, sdp->h)<0)
- return -1;
- fprintf(smpfile, "};\n");
-
- return 0;
- }
-
- /**********************************************************************
- * Save the given sprite.
- * Return: 0 if no errors, negative otherwise
- * (-1 fopen, -2 fwrite, -3 fclose error)
- **********************************************************************/
- int save_sprite(SPRED_DATA *sdp)
- {
- FILE *fp;
-
- if ((fp=fopen(sdp->name, "w"))==NULL)
- return -1;
-
- if (spred_data_to_smp(fp, sdp) < 0)
- return -2;
-
- if (fclose(fp))
- return -3;
-
- return 0;
- }
-
-
- #ifdef TEST
-
- SPRED_DATA sd;
-
- void main(int argc, char **argv)
- {
- char buf[50], *cp;
- int i,j;
- FILE *fp;
-
- if (argc<2)
- exit(1);
-
- strcpy(buf, argv[1]);
- strcpy(sd.name, argv[1]);
-
- if ((cp=strrchr(sd.name, '.'))!=NULL)
- *cp='\0';
-
- if ((fp=fopen(buf, "r"))==NULL) {
- perror("main");
- exit(2);
- }
-
- if ((i=smp_to_spred_data(fp, &sd))<0) {
- fprintf(stderr, "main:smp_to_spred_data failed, code %d", i);
- exit(3);
- }
-
- fclose(fp);
-
- for (i=0; i<sd.h; i++) {
- for (j=0; j<sd.w; j++)
- putchar(sd.shape[j][i]+'0');
- putchar('\n');
- }
- for (i=0; i<sd.h; i++) {
- for (j=0; j<sd.w; j++)
- putchar(sd.mask[j][i]+'0');
- putchar('\n');
- }
-
-
- if ((fp=fopen(sd.name, "w"))==NULL) {
- perror("main");
- exit(4);
- }
-
- if ((i=spred_data_to_smp(fp, &sd))<0) {
- fprintf(stderr, "main:spred_data_to_smp failed, code %d", i);
- exit(5);
- }
- fclose(fp);
-
- }
-
- #endif
-