doom

a minimalistic implementation of doom
git clone git://ssnf.xyz/doom
Log | Files | Refs

wadread.c (3363B)


      1 #include <fcntl.h>
      2 #include <sys/stat.h>
      3 #include <stdio.h>
      4 #include <string.h>
      5 #include <stdlib.h>
      6 #include <ctype.h>
      7 #include <unistd.h>
      8 #include "soundsrv.h"
      9 int* sfxlengths;
     10 typedef struct wadinfo_struct {
     11 	char identification[4];		                 
     12 	int  numlumps;
     13 	int  infotableofs;
     14 } wadinfo_t;
     15 typedef struct filelump_struct {
     16     int  filepos;
     17     int  size;
     18     char name[8];
     19 } filelump_t;
     20 typedef struct lumpinfo_struct {
     21     int  handle;
     22     int  filepos;
     23     int  size;
     24     char name[8];
     25 } lumpinfo_t;
     26 lumpinfo_t* lumpinfo;		                                
     27 int         numlumps;
     28 void**      lumpcache;
     29 #define strcmpi strcasecmp
     30 #ifndef __BIG_ENDIAN__
     31 #define LONG(x) (x)
     32 #define SHORT(x) (x)
     33 #else
     34 #define LONG(x) ((long)SwapLONG((unsigned long) (x)))
     35 #define SHORT(x) ((short)SwapSHORT((unsigned short) (x)))
     36 ulong SwapLONG(unsigned long x)
     37 {
     38     return
     39 	(x>>24)
     40 	| ((x>>8) & 0xff00)
     41 	| ((x<<8) & 0xff0000)
     42 	| (x<<24);
     43 }
     44 unsigned short SwapSHORT(unsigned short x)
     45 {
     46     return
     47 	(x>>8) | (x<<8);
     48 }
     49 #endif
     50 static void derror(char* msg)
     51 {
     52     fprintf(stderr, "\nwadread error: %s\n", msg);
     53     exit(-1);
     54 }
     55 void strupr (char *s)
     56 {
     57     while (*s)
     58 	*s++ = toupper(*s);
     59 }
     60 int filelength (int handle)
     61 {
     62     struct stat	fileinfo;
     63     if (fstat (handle,&fileinfo) == -1)
     64 	fprintf (stderr, "Error fstating\n");
     65     return fileinfo.st_size;
     66 }
     67 void openwad(char* wadname)
     68 {
     69     int		wadfile;
     70     int		tableoffset;
     71     int		tablelength;
     72     int		tablefilelength;
     73     int		i;
     74     wadinfo_t	header;
     75     filelump_t*	filetable;
     76     wadfile = open(wadname, O_RDONLY);
     77     if (wadfile < 0)
     78 	derror("Could not open wadfile");
     79     read(wadfile, &header, sizeof header);
     80     if (strncmp(header.identification, "IWAD", 4))
     81 	derror("wadfile has weirdo header");
     82     numlumps = LONG(header.numlumps);
     83     tableoffset = LONG(header.infotableofs);
     84     tablelength = numlumps * sizeof(lumpinfo_t);
     85     tablefilelength = numlumps * sizeof(filelump_t);
     86     lumpinfo = (lumpinfo_t *) malloc(tablelength);
     87     filetable = (filelump_t *) ((char*)lumpinfo + tablelength - tablefilelength);
     88     lseek(wadfile, tableoffset, SEEK_SET);
     89     read(wadfile, filetable, tablefilelength);
     90     for (i=0 ; i<numlumps ; i++)
     91     {
     92 	strncpy(lumpinfo[i].name, filetable[i].name, 8);
     93 	lumpinfo[i].handle = wadfile;
     94 	lumpinfo[i].filepos = LONG(filetable[i].filepos);
     95 	lumpinfo[i].size = LONG(filetable[i].size);
     96 	
     97     }
     98 }
     99 void*
    100 loadlump
    101 ( char*		lumpname,
    102   int*		size )
    103 {
    104     int		i;
    105     void*	lump;
    106     for (i=0 ; i<numlumps ; i++)
    107     {
    108 	if (!strncasecmp(lumpinfo[i].name, lumpname, 8))
    109 	    break;
    110     }
    111     if (i == numlumps)
    112     {
    113 	
    114 	
    115 	lump = 0;
    116     }
    117     else
    118     {
    119 	lump = (void *) malloc(lumpinfo[i].size);
    120 	lseek(lumpinfo[i].handle, lumpinfo[i].filepos, SEEK_SET);
    121 	read(lumpinfo[i].handle, lump, lumpinfo[i].size);
    122 	*size = lumpinfo[i].size;
    123     }
    124     return lump;
    125 }
    126 void*
    127 getsfx
    128 ( char*		sfxname,
    129   int*		len )
    130 {
    131     unsigned char*	sfx;
    132     unsigned char*	paddedsfx;
    133     int			i;
    134     int			size;
    135     int			paddedsize;
    136     char		name[20];
    137     sprintf(name, "ds%s", sfxname);
    138     sfx = (unsigned char *) loadlump(name, &size);
    139     paddedsize = ((size-8 + (SAMPLECOUNT-1)) / SAMPLECOUNT) * SAMPLECOUNT;
    140     paddedsfx = (unsigned char *) realloc(sfx, paddedsize+8);
    141     for (i=size ; i<paddedsize+8 ; i++)
    142 	paddedsfx[i] = 128;
    143     *len = paddedsize;
    144     return (void *) (paddedsfx + 8);
    145 }