2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
4 Copyright (C) <ARembo@gmail.com> 2011
6 This file is part of the Doom2D:Rembo project.
8 Doom2D:Rembo is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation.
12 Doom2D:Rembo is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/> or
19 write to the Free Software Foundation, Inc.,
20 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
50 char wads
[MAX_WADS
][__MAX_PATH
];
57 static unsigned *patp
;
60 static int m_start
, m_end
;
61 static int s_start
, s_end
;
64 static char f_drive
[__MAX_DRIVE
];
65 static char f_dir
[__MAX_DIR
];
66 static char f_name
[__MAX_FNAME
];
67 static char f_ext
[__MAX_EXT
];
69 void F_startup (void) {
70 logo("F_startup: настройка файловой системы\n");
71 memset(wads
,0,sizeof(wads
));
74 void F_addwad (const char *fn
) {
77 for(i
=0;i
<MAX_WADS
;++i
) if(wads
[i
][0]==0) {
78 strcpy(wads
[i
],fn
);return;
80 ERR_failinit("Не могу добавить WAD %s",fn
);
83 static int myfilelength (FILE *h
) {
85 fseek (h
, 0, SEEK_END
);
87 fseek (h
, pos
, SEEK_SET
);
91 // build wad directory
92 void F_initwads (void) {
99 logo("F_initwads: подключение WAD-файлов\n");
100 for (i
= 0; i
< MAX_WAD
; ++i
) {
104 logo(" подключается %s\n", wads
[0]);
105 if ((wadh
[0] = h
= fopen(wads
[0], "rb")) == NULL
) {
106 ERR_failinit("Не могу открыть файл: %s", wads
[0]);
111 if (strncmp(s
, "IWAD", 4) != 0 && strncmp(s
, "PWAD", 4) != 0) {
112 ERR_failinit("Нет подписи IWAD или PWAD (1)");
116 n
= myfread32(h
); // num
117 o
= myfread32(h
); // offset
118 fseek(h
, o
, SEEK_SET
);
119 for (j
= 0; j
< n
; ++j
) {
120 w
.o
= myfread32(h
); // offset
121 w
.l
= myfread32(h
); // len
122 myfread(w
.n
, 1, 8, h
); // name
124 ERR_failinit("Слишком много элементов WAD'а");
128 memcpy(wad
[p
].n
, w
.n
, 8);
135 for (i
= 1; i
< MAX_WADS
; ++i
) {
136 if (wads
[i
][0] != 0) {
137 logo(" подключается %s\n", wads
[i
]);
138 if ((wadh
[i
] = h
= fopen(wads
[i
], "rb")) == NULL
) {
139 ERR_failinit("Не могу открыть файл2: %s", wads
[i
]);
141 mysplitpath(wads
[i
], f_drive
, f_dir
, f_name
, f_ext
);
142 if (cp866_strcasecmp(f_ext
, ".lmp") == 0) {
143 for (k
= 0; k
< MAX_WAD
; ++k
) {
144 if (cp866_strncasecmp(wad
[k
].n
, f_name
, 8) == 0) {
146 wad
[k
].l
= myfilelength(h
);
152 ERR_failinit("Слишком много элементов WAD'а");
154 memset(wad
[p
].n
, 0, 8);
155 strncpy(wad
[p
].n
, f_name
, 8);
157 wad
[p
].l
= myfilelength(h
);
165 if (strncmp(s
, "IWAD", 4) != 0 && strncmp(s
, "PWAD", 4) != 0) {
166 ERR_failinit("Нет подписи IWAD или PWAD (2)");
168 n
= myfread32(h
); // num
169 o
= myfread32(h
); // offset
170 fseek(h
, o
, SEEK_SET
);
171 for (j
= 0; j
< n
; ++j
) {
172 w
.o
= myfread32(h
); // offset
173 w
.o
= myfread32(h
); // len
174 myfread(w
.n
, 1, 8, h
); // name
175 for (k
= 0; k
< MAX_WAD
; ++k
) {
176 if (cp866_strncasecmp(wad
[k
].n
, w
.n
, 8) == 0) {
184 ERR_failinit("Слишком много элементов WAD'а");
186 memcpy(wad
[p
].n
, w
.n
, 8);
201 // allocate resources
202 // (called from M_startup)
203 void F_allocres (void) {
204 d_start
=F_getresid("D_START");
205 d_end
=F_getresid("D_END");
206 m_start
=F_getresid("M_START");
207 m_end
=F_getresid("M_END");
208 s_start
=F_getresid("S_START");
209 s_end
=F_getresid("S_END");
213 void F_loadres (int r
, void *p
, dword o
, dword l
) {
219 oo
=ftell(fh
=wadh
[wad
[r
].f
]);
221 if(fseek(fh
,wad
[r
].o
+o
,SEEK_SET
)!=0)
222 ERR_fatal("Ошибка при чтении файла");
224 if((dword
)myfreadc(p
,1,l
,fh
)!=l
)
225 ERR_fatal("Ошибка при загрузке ресурса %.8s",wad
[r
].n
);
227 fseek(fh
,oo
,SEEK_SET
);
233 void F_saveres(int r, void *p, dword o, dword l) {
234 FILE* fh = wadh[wad[r].f];
236 if (fseek(fh, wad[r].o + o, SEEK_SET) != 0) {
237 ERR_fatal("Ошибка при чтении файла");
239 myfwrite(p, l, 1, fh);
240 fseek(fh, oo, SEEK_SET);
245 int F_findres (const char n
[8]) {
247 for (i
= 0; i
< wad_num
; i
++) {
248 if (cp866_strncasecmp(wad
[i
].n
, n
, 8) == 0) {
256 int F_getresid (const char n
[8]) {
257 int i
= F_findres(n
);
259 ERR_fatal("F_getresid: ресурс %.8s не найден", n
);
264 void F_getresname (char n
[8], int r
) {
265 memcpy(n
, wad
[r
].n
, 8);
269 int F_getsprid (const char n
[4], int s
, int d
, char *dir
) {
274 for (i
= s_start
+ 1; i
< s_end
; i
++) {
275 if (cp866_strncasecmp(wad
[i
].n
, n
, 4) == 0 && (wad
[i
].n
[4] == s
|| wad
[i
].n
[6] == s
)) {
276 a
= wad
[i
].n
[4] == s
? wad
[i
].n
[5] : 0;
277 b
= wad
[i
].n
[6] == s
? wad
[i
].n
[7] : 0;
278 if (a
== '0' || b
== '0' || a
== d
|| b
== d
) {
280 *dir
= a
!= '0' && b
== '0' || a
!= d
&& b
== d
;
286 ERR_fatal("F_getsprid: image %.4s%c%c not found", n
, s
, d
);
290 int F_getreslen (int r
) {
294 void F_nextmus (char *s
) {
297 if(i
<=m_start
|| i
>=m_end
) i
=m_start
;
299 if(i
>=m_end
) i
=m_start
+1;
301 if (cp866_strcasecmp(wad
[i
].n
,"MENU") == 0 ||
302 cp866_strcasecmp(wad
[i
].n
,"INTERMUS") == 0 ||
303 cp866_strcasecmp(wad
[i
].n
,"\x8a\x8e\x8d\x85\x96\x0") == 0)
306 if(cp866_strncasecmp(wad
[i
].n
,"DMI",3)!=0) break;
308 memcpy(s
,wad
[i
].n
,8);
311 void F_randmus (char *s
) {
314 for (i
= 0; i
< n
; i
++) {
319 // reads bytes from file until CR
320 void F_readstr (FILE* h
, char *s
, int m
) {
326 len
= myfreadc(&c
, 1, 1, h
);
327 if (len
== 0 || c
== 13 || c
== 10) {
338 // reads bytes from file until NUL
339 void F_readstrz (FILE* h,char *s,int m) {
345 len = myfreadc(&c, 1, 1, h);
346 if (len == 0 || c == 0) {
356 /*void F_freemus(void) {
362 free(pat);free(patp);
363 for(i=0;i<inum;++i) if(dmi[i]!=NULL) free(dmi[i]);
370 /*void F_loadmus(char n[8]) {
379 struct{byte t;char n[13];word r;}di;
381 if((r=F_findres(n))==-1) return;
382 fseek(h=wadh[wad[r].f],wad[r].o,SEEK_SET);
383 myfread(&d,1,sizeof(d),h);
384 if(memcmp(d.id,"DMM",4)!=0) return;
385 if(!(pat=malloc(d.psz<<2))) return;
386 myfread(pat,1,d.psz<<2,h);
387 myfread(&seqn,1,1,h);if(seqn) myfread(seq,1,seqn,h);
388 inum=0;myfread(&inum,1,1,h);
389 if(!(dmi=malloc(inum*4))) {free(pat);pat=NULL;return;}
390 if(!(patp=malloc((word)d.pat*32))) {free(pat);free(dmi);pat=NULL;return;}
391 for(i=0;i<inum;++i) {
393 myfread(&di,1,16,h);o=ftell(h);
394 for(r=0;r<12;++r) if(di.n[r]=='.') di.n[r]=0;
395 if((r=F_findres(di.n))==-1) continue;
396 if(!(dmi[i]=malloc(wad[r].l+8))) continue;
398 F_loadres(r,dmi[i],0,2);
399 F_loadres(r,(int*)dmi[i]+1,2,2);
400 F_loadres(r,(int*)dmi[i]+2,4,2);
401 F_loadres(r,(int*)dmi[i]+3,6,2);
402 F_loadres(r,(int*)dmi[i]+4,8,wad[r].l-8);
405 for(i=r=0,j=(word)d.pat<<3;i<j;++i) {
407 while(pat[r++].v!=0x80);