2006-04-13 20:47:06 +00:00
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
/*
|
2010-05-25 10:56:00 +00:00
|
|
|
Copyright (C) 2010 EDuke32 developers and contributors
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-05-25 10:56:00 +00:00
|
|
|
This file is part of EDuke32.
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2007-02-08 04:19:39 +00:00
|
|
|
EDuke32 is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License version 2
|
|
|
|
as published by the Free Software Foundation.
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
2014-07-20 08:55:56 +00:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2006-04-13 20:47:06 +00:00
|
|
|
*/
|
|
|
|
//-------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#include "duke3d.h"
|
|
|
|
|
|
|
|
|
2014-01-30 19:12:08 +00:00
|
|
|
static int32_t rts_numlumps;
|
2010-08-02 08:13:51 +00:00
|
|
|
static void **rts_lumpcache;
|
2014-01-30 19:12:08 +00:00
|
|
|
static lumpinfo_t *rts_lumpinfo;
|
2009-04-26 05:57:42 +00:00
|
|
|
static int32_t RTS_Started = FALSE;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
char rts_lumplockbyte[11];
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
= RTS_AddFile
|
|
|
|
=
|
|
|
|
= All files are optional, but at least one file must be found
|
|
|
|
= Files with a .rts extension are wadlink files with multiple lumps
|
|
|
|
= Other files are single lumps with the base filename for the lump name
|
|
|
|
*/
|
2014-01-30 19:12:08 +00:00
|
|
|
static int32_t RTS_AddFile(const char *filename)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
wadinfo_t header;
|
2014-01-30 19:12:08 +00:00
|
|
|
int32_t i, handle, length, startlump;
|
2006-04-13 20:47:06 +00:00
|
|
|
filelump_t *fileinfo, *fileinfoo;
|
|
|
|
|
|
|
|
// read the entire file in
|
|
|
|
// FIXME: shared opens
|
|
|
|
|
2016-01-11 05:05:38 +00:00
|
|
|
handle = kopen4loadfrommod(filename, 0);
|
2006-11-15 01:16:55 +00:00
|
|
|
if (handle < 0)
|
|
|
|
{
|
2012-06-27 17:45:52 +00:00
|
|
|
initprintf("RTS file \"%s\" was not found\n",filename);
|
2006-04-13 20:47:06 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
startlump = rts_numlumps;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
// WAD file
|
2014-01-30 19:12:08 +00:00
|
|
|
i = kread(handle, &header, sizeof(header));
|
|
|
|
if (i != sizeof(header) || Bmemcmp(header.identification, "IWAD", 4))
|
2006-11-15 01:16:55 +00:00
|
|
|
{
|
2014-01-30 19:12:08 +00:00
|
|
|
initprintf("RTS file \"%s\" too short or doesn't have IWAD id\n", filename);
|
2006-04-13 20:47:06 +00:00
|
|
|
kclose(handle);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-01-30 19:12:08 +00:00
|
|
|
|
|
|
|
header.numlumps = B_LITTLE32(header.numlumps);
|
|
|
|
header.infotableofs = B_LITTLE32(header.infotableofs);
|
|
|
|
|
2006-04-13 20:47:06 +00:00
|
|
|
length = header.numlumps*sizeof(filelump_t);
|
2014-05-30 00:02:19 +00:00
|
|
|
fileinfo = fileinfoo = (filelump_t *)Xmalloc(length);
|
2014-01-30 19:12:08 +00:00
|
|
|
|
2006-11-14 21:35:50 +00:00
|
|
|
klseek(handle, header.infotableofs, SEEK_SET);
|
2006-04-13 20:47:06 +00:00
|
|
|
kread(handle, fileinfo, length);
|
|
|
|
|
2006-11-15 01:16:55 +00:00
|
|
|
{
|
2014-05-30 00:02:19 +00:00
|
|
|
lumpinfo_t *lump_p = (lumpinfo_t *)Xrealloc(
|
2014-01-30 19:12:08 +00:00
|
|
|
rts_lumpinfo, (rts_numlumps + header.numlumps)*sizeof(lumpinfo_t));
|
|
|
|
|
|
|
|
rts_lumpinfo = lump_p;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_numlumps += header.numlumps;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2014-01-30 19:12:08 +00:00
|
|
|
for (i=startlump; i<rts_numlumps; i++, fileinfo++)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2014-01-30 19:12:08 +00:00
|
|
|
lumpinfo_t *lump = &rts_lumpinfo[i];
|
|
|
|
|
|
|
|
lump->handle = handle; // NOTE: cache1d-file is not closed!
|
|
|
|
lump->position = B_LITTLE32(fileinfo->filepos);
|
|
|
|
lump->size = B_LITTLE32(fileinfo->size);
|
|
|
|
|
|
|
|
Bstrncpy(lump->name, fileinfo->name, 8);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2009-10-07 06:47:35 +00:00
|
|
|
Bfree(fileinfoo);
|
2006-04-13 20:47:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-12-23 02:38:47 +00:00
|
|
|
void RTS_Init(const char *filename)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
// open all the files, load headers, and count lumps
|
2014-01-30 19:12:08 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_numlumps = 0;
|
|
|
|
rts_lumpinfo = NULL; // will be realloced as lumps are added
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2014-01-30 19:12:08 +00:00
|
|
|
if (RTS_AddFile(filename))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (rts_numlumps == 0)
|
|
|
|
return;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2014-05-30 00:02:19 +00:00
|
|
|
rts_lumpcache = (void **)Xcalloc(rts_numlumps, sizeof(rts_lumpcache[0]));
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2009-04-26 05:57:42 +00:00
|
|
|
RTS_Started = TRUE;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-01-30 19:12:08 +00:00
|
|
|
int32_t RTS_IsInitialized(void)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2014-01-30 19:12:08 +00:00
|
|
|
return rts_numlumps > 0;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 19:12:08 +00:00
|
|
|
|
|
|
|
#define RTS_BAD_LUMP(lump) ((uint32_t)lump >= (uint32_t)rts_numlumps)
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2009-04-26 05:57:42 +00:00
|
|
|
int32_t RTS_SoundLength(int32_t lump)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
lump++;
|
|
|
|
|
2014-01-30 19:12:08 +00:00
|
|
|
return RTS_BAD_LUMP(lump) ? 0 : rts_lumpinfo[lump].size;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2010-07-19 15:14:00 +00:00
|
|
|
|
2014-01-30 19:12:08 +00:00
|
|
|
/* Loads the lump into the given buffer, which must be >= RTS_SoundLength() */
|
|
|
|
static void RTS_ReadLump(int32_t lump, void *dest)
|
|
|
|
{
|
|
|
|
lumpinfo_t *l = &rts_lumpinfo[lump];
|
2010-07-19 15:14:00 +00:00
|
|
|
|
2006-11-14 21:35:50 +00:00
|
|
|
klseek(l->handle, l->position, SEEK_SET);
|
2014-01-30 19:12:08 +00:00
|
|
|
kread(l->handle, dest, l->size);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
|
2014-01-30 19:12:08 +00:00
|
|
|
|
2009-04-26 05:57:42 +00:00
|
|
|
void *RTS_GetSound(int32_t lump)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
|
|
|
lump++;
|
2010-07-19 15:14:00 +00:00
|
|
|
|
2014-01-30 19:12:08 +00:00
|
|
|
if (RTS_BAD_LUMP(lump))
|
|
|
|
return NULL;
|
2006-04-13 20:47:06 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
if (rts_lumpcache[lump] == NULL)
|
2006-04-13 20:47:06 +00:00
|
|
|
{
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_lumplockbyte[lump] = 200;
|
2014-01-30 19:12:08 +00:00
|
|
|
allocache((intptr_t *)&rts_lumpcache[lump], RTS_SoundLength(lump-1), &rts_lumplockbyte[lump]); // JBF 20030910: char * => int32_t *
|
2010-08-02 08:13:51 +00:00
|
|
|
RTS_ReadLump(lump, rts_lumpcache[lump]);
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-08-02 08:13:51 +00:00
|
|
|
if (rts_lumplockbyte[lump] < 200)
|
|
|
|
rts_lumplockbyte[lump] = 200;
|
2006-04-13 20:47:06 +00:00
|
|
|
else
|
2010-08-02 08:13:51 +00:00
|
|
|
rts_lumplockbyte[lump]++;
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|
2014-01-30 19:12:08 +00:00
|
|
|
|
2010-08-02 08:13:51 +00:00
|
|
|
return rts_lumpcache[lump];
|
2006-04-13 20:47:06 +00:00
|
|
|
}
|