-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRTS.C
240 lines (202 loc) · 5.36 KB
/
RTS.C
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//-------------------------------------------------------------------------
/*
Copyright (C) 1996, 2003 - 3D Realms Entertainment
Copyright (C) 2017-2019 Nuke.YKT
This file is part of Duke Nukem 3D version 1.5 - Atomic Edition
Duke Nukem 3D is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
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
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Original Source: 1996 - Todd Replogle
Prepared for public release: 03/21/2003 - Charlie Wiederhold, 3D Realms
*/
//-------------------------------------------------------------------------
#include "duke3d.h"
//=============
// STATICS
//=============
int32 numlumps;
static void **lumpcache;
static lumpinfo_t *lumpinfo; // location of each lump on disk
static boolean RTS_Started = false;
char lumplockbyte[11];
/*
============================================================================
LUMP BASED ROUTINES
============================================================================
*/
/*
====================
=
= 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
=
====================
*/
void RTS_AddFile (char *filename)
{
wadinfo_t header;
lumpinfo_t *lump_p;
uint32 i;
int32 handle, length;
int32 startlump;
filelump_t *fileinfo;
//
// read the entire file in
// FIXME: shared opens
handle = SafeOpenRead( filename, filetype_binary );
startlump = numlumps;
// WAD file
printf(" Adding %s.\n",filename);
SafeRead( handle, &header, sizeof( header ) );
if (strncmp(header.identification,"IWAD",4))
Error ("RTS file %s doesn't have IWAD id\n",filename);
header.numlumps = IntelLong(header.numlumps);
header.infotableofs = IntelLong(header.infotableofs);
length = header.numlumps*sizeof(filelump_t);
fileinfo = alloca (length);
if (!fileinfo)
Error ("RTS file could not allocate header info on stack");
lseek (handle, header.infotableofs, SEEK_SET);
SafeRead (handle, fileinfo, length);
numlumps += header.numlumps;
//
// Fill in lumpinfo
//
SafeRealloc(&lumpinfo,numlumps*sizeof(lumpinfo_t));
lump_p = &lumpinfo[startlump];
for (i=startlump ; i<numlumps ; i++,lump_p++, fileinfo++)
{
lump_p->handle = handle;
lump_p->position = IntelLong(fileinfo->filepos);
lump_p->size = IntelLong(fileinfo->size);
strncpy (lump_p->name, fileinfo->name, 8);
}
}
/*
====================
=
= RTS_Init
=
= Files with a .rts extension are idlink files with multiple lumps
=
====================
*/
void RTS_Init (char *filename)
{
int32 length;
//
// open all the files, load headers, and count lumps
//
numlumps = 0;
lumpinfo = SafeMalloc(5); // will be realloced as lumps are added
printf("RTS Manager Started\n");
if (SafeFileExists(filename))
RTS_AddFile (filename);
if (!numlumps) return;
//
// set up caching
//
length = (numlumps) * sizeof( *lumpcache );
lumpcache = SafeMalloc(length);
memset(lumpcache,0,length);
RTS_Started = true;
}
/*
====================
=
= RTS_NumSounds
=
====================
*/
int32 RTS_NumSounds (void)
{
return numlumps-1;
}
/*
====================
=
= RTS_SoundLength
=
= Returns the buffer size needed to load the given lump
=
====================
*/
int32 RTS_SoundLength (int32 lump)
{
lump++;
if (lump >= numlumps)
Error ("RTS_SoundLength: %i >= numlumps",lump);
return lumpinfo[lump].size;
}
/*
====================
=
= RTS_GetSoundName
=
====================
*/
char * RTS_GetSoundName (int32 i)
{
i++;
if (i>=numlumps)
Error ("RTS_GetSoundName: %i >= numlumps",i);
return &(lumpinfo[i].name[0]);
}
/*
====================
=
= RTS_ReadLump
=
= Loads the lump into the given buffer, which must be >= RTS_SoundLength()
=
====================
*/
void RTS_ReadLump (int32 lump, void *dest)
{
lumpinfo_t *l;
if (lump >= numlumps)
Error ("RTS_ReadLump: %i >= numlumps",lump);
if (lump < 0)
Error ("RTS_ReadLump: %i < 0",lump);
l = lumpinfo+lump;
lseek (l->handle, l->position, SEEK_SET);
SafeRead(l->handle,dest,l->size);
}
/*
====================
=
= RTS_GetSound
=
====================
*/
void *RTS_GetSound (int32 lump)
{
lump++;
if ((uint32)lump >= numlumps)
Error ("RTS_GetSound: %i >= %i\n",lump,numlumps);
if (lumpcache[lump] == NULL)
{
lumplockbyte[lump] = 200;
allocache((char *)&lumpcache[lump],(long)RTS_SoundLength(lump-1),&lumplockbyte[lump]);
RTS_ReadLump(lump, lumpcache[lump]);
}
else
{
if (lumplockbyte[lump] < 200)
lumplockbyte[lump] = 200;
else
lumplockbyte[lump]++;
}
return lumpcache[lump];
}