commit 8646ab8b7fe8b7e17a1b2471647c5517006d5102
parent 4763bf2934bf561d2e83f8bca2bf9890a36ab55d
Author: ssnf <ssnf@ssnf.xyz>
Date: Sun, 20 Jun 2021 23:38:31 +0000
file io functions
Diffstat:
6 files changed, 180 insertions(+), 198 deletions(-)
diff --git a/doom1.wad b/src/doom1.wad
Binary files differ.
diff --git a/src/doomdef.h b/src/doomdef.h
@@ -3,6 +3,9 @@
#include <stdio.h>
#include <string.h>
enum { VERSION = 110 };
+
+typedef unsigned char uchar;
+
typedef enum
{
shareware,
@@ -27,6 +30,7 @@ typedef enum
german,
unknown
} Language_t;
+
#define RANGECHECK
#define SNDSERV 1
#define BASE_WIDTH 320
@@ -36,6 +40,7 @@ typedef enum
#define SCREENHEIGHT 200
#define MAXPLAYERS 4
#define TICRATE 35
+
typedef enum
{
GS_LEVEL,
@@ -43,10 +48,12 @@ typedef enum
GS_FINALE,
GS_DEMOSCREEN
} gamestate_t;
+
#define MTF_EASY 1
#define MTF_NORMAL 2
#define MTF_HARD 4
#define MTF_AMBUSH 8
+
typedef enum
{
sk_baby,
diff --git a/src/r_data.c b/src/r_data.c
@@ -454,7 +454,7 @@ void R_PrecacheLevel (void)
if (flatpresent[i])
{
lump = firstflat + i;
- flatmemory += lumpinfo[lump].size;
+ flatmemory += lumpinfo_g[lump].size;
W_CacheLumpNum(lump, PU_CACHE);
}
}
@@ -476,7 +476,7 @@ void R_PrecacheLevel (void)
for (j=0 ; j<texture->patchcount ; j++)
{
lump = texture->patches[j].patch;
- texturememory += lumpinfo[lump].size;
+ texturememory += lumpinfo_g[lump].size;
W_CacheLumpNum(lump , PU_CACHE);
}
}
@@ -498,7 +498,7 @@ void R_PrecacheLevel (void)
for (k=0 ; k<8 ; k++)
{
lump = firstspritelump + sf->lump[k];
- spritememory += lumpinfo[lump].size;
+ spritememory += lumpinfo_g[lump].size;
W_CacheLumpNum(lump , PU_CACHE);
}
}
diff --git a/src/r_things.c b/src/r_things.c
@@ -96,19 +96,19 @@ void R_InitSpriteDefs (char** namelist)
intname = *(int *)namelist[i];
for (l=start+1 ; l<end ; l++)
{
- if (*(int *)lumpinfo[l].name == intname)
+ if (*(int *)lumpinfo_g[l].name == intname)
{
- frame = lumpinfo[l].name[4] - 'A';
- rotation = lumpinfo[l].name[5] - '0';
+ frame = lumpinfo_g[l].name[4] - 'A';
+ rotation = lumpinfo_g[l].name[5] - '0';
if (modifiedgame)
- patched = W_GetNumForName (lumpinfo[l].name);
+ patched = W_GetNumForName (lumpinfo_g[l].name);
else
patched = l;
R_InstallSpriteLump (patched, frame, rotation, false);
- if (lumpinfo[l].name[6])
+ if (lumpinfo_g[l].name[6])
{
- frame = lumpinfo[l].name[6] - 'A';
- rotation = lumpinfo[l].name[7] - '0';
+ frame = lumpinfo_g[l].name[6] - 'A';
+ rotation = lumpinfo_g[l].name[7] - '0';
R_InstallSpriteLump (l, frame, rotation, true);
}
}
diff --git a/src/w_wad.c b/src/w_wad.c
@@ -6,121 +6,115 @@
#include <fcntl.h>
#include <sys/stat.h>
#include <alloca.h>
+#include <stdarg.h>
#define O_BINARY 0
#include "doomtype.h"
+#include "doomdef.h"
#include "m_swap.h"
#include "i_system.h"
#include "z_zone.h"
-#ifdef __GNUG__
-#pragma implementation "w_wad.h"
-#endif
#include "w_wad.h"
-lumpinfo_t* lumpinfo;
-int numlumps;
+
+lumpinfo_t* lumpinfo_g;
+int numlumps_g;
void** lumpcache;
-#define strcmpi strcasecmp
-void strupr (char* s)
+int reloadlump;
+char* reloadname;
+
+void
+unpack(uchar** buf, char* fmt, ...)
{
- while (*s) { *s = toupper(*s); s++; }
+ va_list ap;
+ char* p, *q, *s;
+ ulong* l;
+ uchar* b;
+ uint i;
+
+ p = q = fmt;
+ b = *buf;
+ i = 0;
+ va_start(ap, fmt);
+ for (;*p;) {
+ switch (*p++) {
+ case 'l':
+ l = va_arg(ap, ulong*);
+ *l = *b++;
+ *l += *b++ << 8;
+ *l += *b++ << 16;
+ *l += *b++ << 24;
+ break;
+ case 's':
+ s = va_arg(ap, char*);
+ if (*p++ == ':') {
+ i = *p - 0x30;
+
+ for (;isdigit(*(++p));) {
+ i *= 10;
+ i += *p - 0x30;
+ }
+ while (i--)
+ *s++ = *b++;
+ }
+ }
+ }
+ *buf = b;
}
-int filelength (int handle)
-{
- struct stat fileinfo;
- if (fstat (handle,&fileinfo) == -1)
- I_Error ("Error fstating");
- return fileinfo.st_size;
+
+void
+unpack_wadinfo(uchar** buf, wadinfo_t* p)
+{
+ unpack(buf, "s:4ll", p->identification, &p->numlumps, &p->infotableofs);
}
+
void
-ExtractFileBase
-( char* path,
- char* dest )
+unpack_filelump(uchar** buf, filelump_t* p)
{
- char* src;
- int length;
- src = path + strlen(path) - 1;
- while (src != path
- && *(src-1) != '\\'
- && *(src-1) != '/')
- {
- src--;
- }
- memset (dest,0,8);
- length = 0;
- while (*src && *src != '.')
- {
- if (++length == 9)
- I_Error ("Filename base of %s >8 chars",path);
- *dest++ = toupper((int)*src++);
- }
+ unpack(buf, "lls:8", &p->filepos, &p->size, p->name);
}
-int reloadlump;
-char* reloadname;
-void W_AddFile (char *filename)
+
+void
+W_AddFile(char *filename)
{
- wadinfo_t header;
- lumpinfo_t* lump_p;
- unsigned i;
- int handle;
- int length;
- int startlump;
- filelump_t* fileinfo;
- filelump_t singleinfo;
- int storehandle;
- if (filename[0] == '~')
- {
- filename++;
- reloadname = filename;
- reloadlump = numlumps;
- }
- if ( (handle = open (filename,O_RDONLY | O_BINARY)) == -1)
- {
- printf (" couldn't open %s\n",filename);
- return;
- }
- printf (" adding %s\n",filename);
- startlump = numlumps;
- if (strcmpi (filename+strlen(filename)-3 , "wad" ) )
- {
- fileinfo = &singleinfo;
- singleinfo.filepos = 0;
- singleinfo.size = LONG(filelength(handle));
- ExtractFileBase (filename, singleinfo.name);
- numlumps++;
- }
- else
- {
- read (handle, &header, sizeof(header));
- if (strncmp(header.identification,"IWAD",4))
- {
- if (strncmp(header.identification,"PWAD",4))
- {
- I_Error ("Wad file %s doesn't have IWAD "
- "or PWAD id\n", filename);
- }
+ lumpinfo_t* lumpinfo_p;
+ filelump_t* filelump_p;
+ wadinfo_t header;
+ FILE* f;
+ uchar* file, *p;
+ ulong l;
+ unsigned i;
+ int startlump;
+
+ f = fopen(filename, "r");
+ if (!f) {
+ printf(" couldn't open %s\n", filename);
+ return;
+ }
+ printf(" adding %s\n",filename);
+ fseek(f, 0, SEEK_END);
+ l = ftell(f);
+ p = file = malloc(l * sizeof(*file) + 1);
+ fseek(f, 0, SEEK_SET);
+ fread(file, l, 1, f);
+ fclose(f);
+ startlump = numlumps_g;
+ unpack_wadinfo(&p, &header);
+ numlumps_g += header.numlumps;
+ lumpinfo_g = realloc(lumpinfo_g, numlumps_g * sizeof(lumpinfo_t));
+ if (!lumpinfo_g)
+ I_Error("Couldn't realloc lumpinfo_g");
+ filelump_p = alloca(header.numlumps * sizeof(filelump_t));
+ p = file + header.infotableofs;
+ for (i = 0; i < header.numlumps; ++i)
+ unpack_filelump(&p, filelump_p+i);
+ lumpinfo_p = lumpinfo_g + startlump;
+ for (i = startlump; i < numlumps_g; ++i) {
+ lumpinfo_p[i].cache = file;
+ lumpinfo_p[i].position = filelump_p[i].filepos;
+ lumpinfo_p[i].size = filelump_p[i].size;
+ strncpy(lumpinfo_p[i].name, filelump_p[i].name, 8);
}
- header.numlumps = LONG(header.numlumps);
- header.infotableofs = LONG(header.infotableofs);
- length = header.numlumps*sizeof(filelump_t);
- fileinfo = alloca (length);
- lseek (handle, header.infotableofs, SEEK_SET);
- read (handle, fileinfo, length);
- numlumps += header.numlumps;
- }
- lumpinfo = realloc (lumpinfo, numlumps*sizeof(lumpinfo_t));
- if (!lumpinfo)
- I_Error ("Couldn't realloc lumpinfo");
- lump_p = &lumpinfo[startlump];
- storehandle = reloadname ? -1 : handle;
- for (i=startlump ; i<numlumps ; i++,lump_p++, fileinfo++)
- {
- lump_p->handle = storehandle;
- lump_p->position = LONG(fileinfo->filepos);
- lump_p->size = LONG(fileinfo->size);
- strncpy (lump_p->name, fileinfo->name, 8);
- }
- if (reloadname)
- close (handle);
}
+
void W_Reload (void)
{
wadinfo_t header;
@@ -141,7 +135,7 @@ void W_Reload (void)
fileinfo = alloca (length);
lseek (handle, header.infotableofs, SEEK_SET);
read (handle, fileinfo, length);
- lump_p = &lumpinfo[reloadlump];
+ lump_p = &lumpinfo_g[reloadlump];
for (i=reloadlump ;
i<reloadlump+lumpcount ;
i++,lump_p++, fileinfo++)
@@ -156,13 +150,13 @@ void W_Reload (void)
void W_InitMultipleFiles (char** filenames)
{
int size;
- numlumps = 0;
- lumpinfo = malloc(1);
+ numlumps_g = 0;
+ lumpinfo_g = malloc(1);
for ( ; *filenames ; filenames++)
W_AddFile (*filenames);
- if (!numlumps)
+ if (!numlumps_g)
I_Error ("W_InitFiles: no files found");
- size = numlumps * sizeof(*lumpcache);
+ size = numlumps_g * sizeof(*lumpcache);
lumpcache = malloc (size);
if (!lumpcache)
I_Error ("Couldn't allocate lumpcache");
@@ -177,33 +171,25 @@ void W_InitFile (char* filename)
}
int W_NumLumps (void)
{
- return numlumps;
+ return numlumps_g;
}
-int W_CheckNumForName (char* name)
+
+int
+W_CheckNumForName(char* name)
{
- union {
- char s[9];
- int x[2];
- } name8;
- int v1;
- int v2;
- lumpinfo_t* lump_p;
- strncpy (name8.s,name,8);
- name8.s[8] = 0;
- strupr (name8.s);
- v1 = name8.x[0];
- v2 = name8.x[1];
- lump_p = lumpinfo + numlumps;
- while (lump_p-- != lumpinfo)
- {
- if ( *(int *)lump_p->name == v1
- && *(int *)&lump_p->name[4] == v2)
- {
- return lump_p - lumpinfo;
- }
- }
- return -1;
+ lumpinfo_t* p;
+ char buf[8], *q;
+
+ strncpy(buf, name, 8);
+ for (q = buf; *q; ++q)
+ if (*q > 0x60)
+ *q -= 0x20;
+ for (p = lumpinfo_g + numlumps_g - 1; p >= lumpinfo_g; --p)
+ if (!memcmp(buf, p->name, 8))
+ return p - lumpinfo_g;
+ return -1;
}
+
int W_GetNumForName (char* name)
{
int i;
@@ -212,45 +198,34 @@ int W_GetNumForName (char* name)
I_Error ("W_GetNumForName: %s not found!", name);
return i;
}
+
int W_LumpLength (int lump)
{
- if (lump >= numlumps)
- I_Error ("W_LumpLength: %i >= numlumps",lump);
- return lumpinfo[lump].size;
+ if (lump >= numlumps_g)
+ I_Error ("W_LumpLength: %i >= numlumps_g",lump);
+ return lumpinfo_g[lump].size;
}
+
void
-W_ReadLump
-( int lump,
- void* dest )
+W_ReadLump(int lump, void* dest)
{
- int c;
- lumpinfo_t* l;
- int handle;
- if (lump >= numlumps)
- I_Error ("W_ReadLump: %i >= numlumps",lump);
- l = lumpinfo+lump;
- if (l->handle == -1)
- {
- if ( (handle = open (reloadname,O_RDONLY | O_BINARY)) == -1)
- I_Error ("W_ReadLump: couldn't open %s",reloadname);
- }
- else
- handle = l->handle;
- lseek (handle, l->position, SEEK_SET);
- c = read (handle, dest, l->size);
- if (c < l->size)
- I_Error ("W_ReadLump: only read %i of %i on lump %i",
- c,l->size,lump);
- if (l->handle == -1)
- close (handle);
+ lumpinfo_t* l;
+ uchar* src;
+
+ if (lump >= numlumps_g)
+ I_Error("W_ReadLump: %i >= numlumps_g",lump);
+ l = lumpinfo_g + lump;
+ src = l->cache + l->position;
+ memcpy(dest, src, l->size);
}
+
void*
W_CacheLumpNum
( int lump,
int tag )
{
- if ((unsigned)lump >= numlumps)
- I_Error ("W_CacheLumpNum: %i >= numlumps",lump);
+ if ((unsigned)lump >= numlumps_g)
+ I_Error ("W_CacheLumpNum: %i >= numlumps_g",lump);
if (!lumpcache[lump]) {
Z_Malloc(W_LumpLength (lump), tag, &lumpcache[lump]);
W_ReadLump(lump, lumpcache[lump]);
@@ -279,7 +254,7 @@ void W_Profile (void)
FILE* f;
int j;
char name[9];
- for (i=0 ; i<numlumps ; i++)
+ for (i=0 ; i<numlumps_g ; i++)
{
ptr = lumpcache[i];
if (!ptr)
@@ -300,9 +275,9 @@ void W_Profile (void)
profilecount++;
f = fopen ("waddump.txt","w");
name[8] = 0;
- for (i=0 ; i<numlumps ; i++)
+ for (i=0 ; i<numlumps_g ; i++)
{
- memcpy (name,lumpinfo[i].name,8);
+ memcpy (name,lumpinfo_g[i].name,8);
for (j=0 ; j<8 ; j++)
if (!name[j])
break;
diff --git a/src/w_wad.h b/src/w_wad.h
@@ -1,36 +1,36 @@
#ifndef __W_WAD__
#define __W_WAD__
-#ifdef __GNUG__
-#pragma interface
-#endif
-typedef struct
-{
- char identification[4];
- int numlumps;
- int infotableofs;
+
+typedef struct {
+ char identification[4];
+ long numlumps;
+ long infotableofs;
} wadinfo_t;
-typedef struct
-{
- int filepos;
- int size;
- char name[8];
+
+typedef struct {
+ long filepos;
+ long size;
+ char name[8];
} filelump_t;
-typedef struct
-{
- char name[8];
- int handle;
- int position;
- int size;
+
+typedef struct {
+ unsigned char* cache;
+ char name[8];
+ long position;
+ long size;
} lumpinfo_t;
+
extern void** lumpcache;
-extern lumpinfo_t* lumpinfo;
-extern int numlumps;
-void W_InitMultipleFiles (char** filenames);
-void W_Reload (void);
-int W_CheckNumForName (char* name);
-int W_GetNumForName (char* name);
-int W_LumpLength (int lump);
-void W_ReadLump (int lump, void *dest);
-void* W_CacheLumpNum (int lump, int tag);
-void* W_CacheLumpName (char* name, int tag);
+extern lumpinfo_t* lumpinfo_g;
+extern int numlumps_g;
+
+void W_InitMultipleFiles(char** filenames);
+void W_Reload();
+int W_CheckNumForName(char* name);
+int W_GetNumForName(char* name);
+int W_LumpLength(int lump);
+void W_ReadLump(int lump, void *dest);
+void* W_CacheLumpNum(int lump, int tag);
+void* W_CacheLumpName(char* name, int tag);
+
#endif