From e5c2eddc151a59db4e611a77901732d7c15fde90 Mon Sep 17 00:00:00 2001 From: DeaDDooMER Date: Tue, 10 Mar 2020 13:02:45 +0300 Subject: [PATCH] ported to osx-pcc --- src/config.c | 103 ++++++++++++++------------ src/files.c | 203 +++++++++++++++++++++++++++++++++------------------ src/game.c | 8 +- src/items.c | 4 + src/memory.c | 10 +++ src/memory.h | 2 + src/menu.c | 18 ++--- src/misc.h | 3 + src/miscc.c | 66 ++++++++++++++--- src/my.c | 10 ++- src/switch.c | 1 + src/vga.c | 19 +++++ src/vga.h | 3 + src/view.c | 43 +++++++++-- 14 files changed, 339 insertions(+), 154 deletions(-) diff --git a/src/config.c b/src/config.c index d0c4a9f..2bf7f73 100644 --- a/src/config.c +++ b/src/config.c @@ -202,43 +202,51 @@ void CFG_load(void) { } while(!feof(h)) { F_readstr(h,s,127); - if(*s==';' || s[1]==';') continue; // comment - if(!(p1=strtok(s,"\r\n\t=;"))) continue;//if(!(p1=strtok(s,"\r\n\t =;"))) continue; - if(!(p2=strtok(NULL,"\r\n\t=;"))) continue;//if(!(p2=strtok(NULL,"\r\n\t =;"))) continue; - for(j=0;cfg[j].t;++j) if(cfg[j].cfg && !cfg[j].o) - if(strcasecmp(p1,cfg[j].cfg)==0) { - switch(cfg[j].t) { - case BYTE: - n=strtol(p2,NULL,0); - *((byte *)cfg[j].p)=(byte)n; - break; - case WORD: - n=strtol(p2,NULL,0); - *((word *)cfg[j].p)=(word)n; - break; - case DWORD: - n=strtol(p2,NULL,0); - *((dword *)cfg[j].p)=n; - break; - case STRING: - strcpy((char *)cfg[j].p,p2); - break; - case SW_ON: - case SW_OFF: - if(strcasecmp(p2,"ON")==0) {*((byte *)cfg[j].p)=ON;break;} - if(strcasecmp(p2,"OFF")==0) {*((byte *)cfg[j].p)=OFF;break;} - *((byte *)cfg[j].p)=strtol(p2,NULL,0); - break; - case FILES: - break; - - case KEY: - { - int k = get_key(p2); - if (k) { - *((int *)cfg[j].p)=k; - } - else { + if(*s==';' || s[1]==';') + continue; // comment + if(!(p1=strtok(s,"\r\n\t=;"))) + continue; //if(!(p1=strtok(s,"\r\n\t =;"))) continue; + if(!(p2=strtok(NULL,"\r\n\t=;"))) + continue;//if(!(p2=strtok(NULL,"\r\n\t =;"))) continue; + for(j=0;cfg[j].t;++j) { + if(cfg[j].cfg && !cfg[j].o) { + if(strcasecmp(p1,cfg[j].cfg)==0) { + switch(cfg[j].t) { + case BYTE: + n=strtol(p2,NULL,0); + *((byte *)cfg[j].p)=(byte)n; + break; + case WORD: + n=strtol(p2,NULL,0); + *((word *)cfg[j].p)=(word)n; + break; + case DWORD: + n=strtol(p2,NULL,0); + *((dword *)cfg[j].p)=n; + break; + case STRING: + strcpy((char *)cfg[j].p,p2); + break; + case SW_ON: + case SW_OFF: + if(strcasecmp(p2,"ON")==0) { + *((byte *)cfg[j].p)=ON; + break; + } + if(strcasecmp(p2,"OFF")==0) { + *((byte *)cfg[j].p)=OFF; + break; + } + *((byte *)cfg[j].p)=strtol(p2,NULL,0); + break; + case FILES: + break; + case KEY: + { + int k = get_key(p2); + if (k) { + *((int *)cfg[j].p)=k; + } else { logo("Unknown key in cfg: %s=%s\n",p1,p2); logo("List available key names:\n"); int i; @@ -249,16 +257,17 @@ void CFG_load(void) { logo("%s\n", s); } } - } - } - break; - - default: - ERR_failinit("!!! Неизвестный тип в cfg !!!"); - } - break; - } - } + } + } + break; + default: + ERR_failinit("!!! Неизвестный тип в cfg !!!"); + } // switch + break; + } // if + } // if + } // for + } // while fclose(h); } diff --git a/src/files.c b/src/files.c index 220d72c..eb6d36e 100644 --- a/src/files.c +++ b/src/files.c @@ -180,67 +180,120 @@ int myfilelength(FILE *h) extern void mysplitpath(const char* path, char* drv, char* dir, char* name, char* ext); +static int myread_int32 (FILE *f) { + int x; + myfread(&x, 4, 1, f); + return int2host(x); +} + // build wad directory -void F_initwads(void) { - int i,j,k,p; +void F_initwads (void) { + int i, j, k, p; FILE *h; char s[4]; - int n,o; + int n, o; wad_t w; logo("F_initwads: подключение WAD-файлов\n"); - for(i=0;i=MAX_WAD) ERR_failinit("Слишком много элементов WAD'а"); - memcpy(wad[p].n,w.n,8); - wad[p].o=w.o;wad[p].l=w.l;wad[p].f=0; - ++p; + for (i = 0; i < MAX_WAD; ++i) { + wad[i].n[0] = 0; + } + + logo(" подключается %s\n", wads[0]); + if ((wadh[0] = h = fopen(wads[0], "rb")) == NULL) { + ERR_failinit("Не могу открыть файл: %s", wads[0]); + } + + s[0] = '\0'; + myfread(s, 1, 4, h); + if (strncmp(s, "IWAD", 4) != 0 && strncmp(s, "PWAD", 4) != 0) { + ERR_failinit("Нет подписи IWAD или PWAD (1)"); } + + p = 0; // wad number + n = myread_int32(h); // num + o = myread_int32(h); // offset + fseek(h, o, SEEK_SET); + for (j = 0; j < n; ++j) { + w.o = myread_int32(h); // offset + w.l = myread_int32(h); // len + myfread(w.n, 1, 8, h); // name + if (p >= MAX_WAD) { + ERR_failinit("Слишком много элементов WAD'а"); + } + wad[p].o = w.o; + wad[p].l = w.l; + memcpy(wad[p].n, w.n, 8); + wad[p].f = 0; + ++p; + } + //fclose(h); - for(i=1;i=MAX_WAD) { - if(p>=MAX_WAD) ERR_failinit("Слишком много элементов WAD'а"); - memset(wad[p].n,0,8); - strncpy(wad[p].n,f_name,8); - wad[p].o=0L;wad[p].l=myfilelength(h);wad[p].f=i; - ++p; - } - continue; - } - *s=0;myfread(s,1,4,h); - if(strncmp(s,"IWAD",4)!=0 && strncmp(s,"PWAD",4)!=0) - ERR_failinit("Нет подписи IWAD или PWAD"); - myfread(&n,1,4,h);myfread(&o,1,4,h);fseek(h,o,SEEK_SET); - for(j=0;j=MAX_WAD) { - if(p>=MAX_WAD) ERR_failinit("Слишком много элементов WAD'а"); - memcpy(wad[p].n,w.n,8); - wad[p].o=w.o;wad[p].l=w.l;wad[p].f=i; - ++p; + + for (i = 1; i < MAX_WADS; ++i) { + if (wads[i][0] != 0) { + logo(" подключается %s\n", wads[i]); + if ((wadh[i] = h = fopen(wads[i], "rb")) == NULL) { + ERR_failinit("Не могу открыть файл2: %s", wads[i]); + } + mysplitpath(wads[i], f_drive, f_dir, f_name, f_ext); + if (strcasecmp(f_ext, ".lmp") == 0) { + for (k = 0; k < MAX_WAD; ++k) { + if (strncasecmp(wad[k].n, f_name, 8) == 0) { + wad[k].o = 0; + wad[k].l = myfilelength(h); + wad[k].f = i; + break; + } + if (k >= MAX_WAD) { + if (p >= MAX_WAD) { + ERR_failinit("Слишком много элементов WAD'а"); + } + memset(wad[p].n, 0, 8); + strncpy(wad[p].n, f_name, 8); + wad[p].o = 0; + wad[p].l = myfilelength(h); + wad[p].f = i; + ++p; + } + } + + s[0] = '\0'; + myfread(s, 1, 4, h); + if (strncmp(s, "IWAD", 4) != 0 && strncmp(s, "PWAD", 4) != 0) { + ERR_failinit("Нет подписи IWAD или PWAD (2)"); + } + n = myread_int32(h); // num + o = myread_int32(h); // offset + fseek(h, o, SEEK_SET); + for (j = 0; j < n; ++j) { + w.o = myread_int32(h); // offset + w.l = myread_int32(h); // len + myfread(w.n, 1, 8, h); // name + for (k = 0; k < MAX_WAD; ++k) { + if (strncasecmp(wad[k].n, w.n, 8) == 0) { + wad[k].o = w.o; + wad[k].l = w.l; + wad[k].f = i; + break; + } + if (k >= MAX_WAD) { + if (p >= MAX_WAD) { + ERR_failinit("Слишком много элементов WAD'а"); + } + memcpy(wad[p].n, w.n, 8); + wad[p].o = w.o; + wad[p].l = w.l; + wad[p].f = i; + ++p; + } + } + } } } - } - wad_num=p; - + } + + wad_num = p; } // allocate resources @@ -356,31 +409,37 @@ void F_randmus(char *s) { } // reads bytes from file until CR -void F_readstr(FILE* h,char *s,int m) { - int i; - static char c; - - for(i=0;;) { - c=13; - myfreadc(&c,1,1,h); - if(c==13) break; - if(i0;++i,blk.sz-=8) { myfread(&t,1,sizeof(t),h); + t.x = short2host(t.x); + t.y = short2host(t.y); + t.t = short2host(t.t); + t.f = short2host(t.f); it[i].o.x=t.x;it[i].o.y=t.y; it[i].t=t.t;it[i].s=t.f; if(!it[i].t) break; diff --git a/src/memory.c b/src/memory.c index 5c7953c..af93fa6 100644 --- a/src/memory.c +++ b/src/memory.c @@ -90,3 +90,13 @@ void M_unlock(void *p) { if(!resl[h]) return; --resl[h]; } + +int M_locked (int h) { + h&=-1-0x8000; + return (h != -1) && (h != 0xFFFF) && (resl[h] != 0); +} + +int M_was_locked (int h) { + h&=-1-0x8000; + return (h != -1) && (h != 0xFFFF) && (resp[h] != NULL); +} diff --git a/src/memory.h b/src/memory.h index b635016..84ca70f 100644 --- a/src/memory.h +++ b/src/memory.h @@ -25,3 +25,5 @@ void M_startup(void); void M_shutdown(void); void *M_lock(int); void M_unlock(void *); +int M_locked(int); +int M_was_locked(int); diff --git a/src/menu.c b/src/menu.c index ff6932d..aca7cd4 100644 --- a/src/menu.c +++ b/src/menu.c @@ -573,16 +573,16 @@ void GM_init(void) { msnd4=Z_getsnd("SWTCHX"); msnd5=Z_getsnd("SUDI"); msnd6=Z_getsnd("TUDI"); - msklh[0]=M_lock(F_getresid("M_SKULL1")); + msklh[0]=V_loadvgaimg("M_SKULL1"); // msklh[0]=load_vga("vga\\spr.vga","M_SKULL1"); - msklh[1]=M_lock(F_getresid("M_SKULL2")); - mbarl=M_lock(F_getresid("M_THERML")); - mbarm=M_lock(F_getresid("M_THERMM")); - mbarr=M_lock(F_getresid("M_THERMR")); - mbaro=M_lock(F_getresid("M_THERMO")); - mslotl=M_lock(F_getresid("M_LSLEFT")); - mslotm=M_lock(F_getresid("M_LSCNTR")); - mslotr=M_lock(F_getresid("M_LSRGHT")); + msklh[1]=V_loadvgaimg("M_SKULL2"); + mbarl=V_loadvgaimg("M_THERML"); + mbarm=V_loadvgaimg("M_THERMM"); + mbarr=V_loadvgaimg("M_THERMR"); + mbaro=V_loadvgaimg("M_THERMO"); + mslotl=V_loadvgaimg("M_LSLEFT"); + mslotm=V_loadvgaimg("M_LSCNTR"); + mslotr=V_loadvgaimg("M_LSRGHT"); K_setkeyproc(G_keyf); } diff --git a/src/misc.h b/src/misc.h index 6d4ac76..60eaee5 100644 --- a/src/misc.h +++ b/src/misc.h @@ -82,3 +82,6 @@ void Z_gotoxy(int,int); int Z_getacid(int x,int y,int r,int h); void Z_clrst(); + +unsigned short int short2host (unsigned short int x); +unsigned int int2host (unsigned int x); diff --git a/src/miscc.c b/src/miscc.c index 4fd6353..7123938 100644 --- a/src/miscc.c +++ b/src/miscc.c @@ -69,7 +69,8 @@ void *Z_getspr(char n[4],int s,int d,char *dir) { h=F_getsprid(n,s,d); if(dir) *dir=(h&0x8000)?1:0; - return M_lock(h); + //return M_lock(h); + return V_getvgaimg(h); } void *Z_getsnd(char n[6]) { @@ -78,7 +79,17 @@ void *Z_getsnd(char n[6]) { //if(snd_type==-1) return NULL; strncpy(s+2,n,6);s[0]='D'; s[1]='S'; - return M_lock(F_getresid(s)); + + int id = F_getresid(s); + int loaded = M_was_locked(id); + snd_t *snd = M_lock(id); + if (snd != NULL && !loaded) { + snd->len = int2host(snd->len); + snd->rate = int2host(snd->rate); + snd->lstart = int2host(snd->lstart); + snd->llen = int2host(snd->llen); + } + return snd; } int Z_sound(void *s,int v) { @@ -102,30 +113,30 @@ void Z_initst(void) { "PLASA0","BFUGA0","GUN2A0" }; - stone=M_lock(F_getresid("STONE")); - stone2=M_lock(F_getresid("STONE2")); - keys[0]=M_lock(F_getresid("KEYRA0")); - keys[1]=M_lock(F_getresid("KEYGA0")); - keys[2]=M_lock(F_getresid("KEYBA0")); + stone=V_loadvgaimg("STONE"); + stone2=V_loadvgaimg("STONE2"); + keys[0]=V_loadvgaimg("KEYRA0"); + keys[1]=V_loadvgaimg("KEYGA0"); + keys[2]=V_loadvgaimg("KEYBA0"); for(i=0;i<22;++i) - sth[i]=M_lock(F_getresid(nm[i])); + sth[i]=V_loadvgaimg(nm[i]); strcpy(s,"STBF_*"); for(i='!';i<160;++i) { s[5]=i; - bfh[i-'!']=M_lock(F_findres(s)); + bfh[i-'!']=V_getvgaimg(F_findres(s)); if(!(i&15)) logo_gas(GAS_START+((i-'!')>>4),GAS_TOTAL); } for(i='!';i<160;++i) { sprintf(s,"STCFN%03d",i); - sfh[i-'!']=M_lock(F_findres(s)); + sfh[i-'!']=V_getvgaimg(F_findres(s)); if(!(i&15)) logo_gas(GAS_START+8+((i-'!')>>4),GAS_TOTAL); } strcpy(s,"WINUM*"); for(i='0';i<='9';++i) { s[5]=i; - bfh[i-'!']=M_lock(F_getresid(s)); + bfh[i-'!']=V_loadvgaimg(s); } - bfh[':'-'!']=M_lock(F_getresid("WICOLON")); + bfh[':'-'!']=V_loadvgaimg("WICOLON"); bulsnd[0]=Z_getsnd("BUL1"); bulsnd[1]=Z_getsnd("BUL2"); } @@ -601,3 +612,34 @@ void Z_calc_time(dword t,word *h,word *m,word *s) t = t / 60; *h = t; } + +unsigned short int short2host (unsigned short int x) { +#if __BIG_ENDIAN__ + union { + unsigned char a[2]; + unsigned short int x; + } y; + y.x = x; + unsigned char t = y.a[0]; y.a[0] = y.a[1]; y.a[1] = t; + return y.x; +#else + return x; +#endif +} + +unsigned int int2host (unsigned int x) { +#if __BIG_ENDIAN__ + union { + unsigned char a[4]; + unsigned int x; + } y; + y.x = x; + #define SWAP_VAR(a, b) do { unsigned char t = a; a = b; b = t; } while(0) + SWAP_VAR(y.a[0], y.a[3]); + SWAP_VAR(y.a[1], y.a[2]); + #undef SWAP_VAR + return y.x; +#else + return x; +#endif +} diff --git a/src/my.c b/src/my.c index c7fe41c..0a7fcbc 100644 --- a/src/my.c +++ b/src/my.c @@ -78,12 +78,14 @@ void mysplitpath(const char* path, char* drv, char* dir, char* name, char* ext) } } -void myfread(void *ptr, size_t n, size_t size, FILE *f) { - if (fread(ptr,n,size,f) != size) ERR_fatal("File reading error\n"); +size_t myfreadc(void *ptr, size_t size, size_t n, FILE *f) { + return fread(ptr, size, n, f); } -size_t myfreadc(void *ptr, size_t n, size_t size, FILE *f) { - return fread(ptr,n,size,f); +void myfread(void *ptr, size_t size, size_t n, FILE *f) { + if (myfreadc(ptr, size, n, f) != n) { + ERR_fatal("File reading error\n"); + } } void myfwrite(void *ptr, size_t n, size_t size, FILE *f) { diff --git a/src/switch.c b/src/switch.c index cc418b8..980955d 100644 --- a/src/switch.c +++ b/src/switch.c @@ -72,6 +72,7 @@ int SW_load(FILE* h) { sw_secrets=0; for(i=0;i0;++i,blk.sz-=sizeof(sw_t)) { myfread(sw+i,1,sizeof(sw_t),h); + sw[i].c = short2host(sw[i].c); sw[i].tm=0;sw[i].d=0; sw[i].f|=0x80; if(sw[i].t==SW_SECRET) ++sw_secrets; diff --git a/src/vga.c b/src/vga.c index e06baf4..5db6b53 100644 --- a/src/vga.c +++ b/src/vga.c @@ -25,6 +25,9 @@ #include #include "error.h" #include "view.h" +#include "memory.h" + +#include // адрес экранного буфера @@ -45,6 +48,22 @@ char fullscreen = OFF; #define HQ 2 +vgaimg *V_getvgaimg (int id) { + int loaded = M_was_locked(id); + vgaimg *v = M_lock(id); + if (v != NULL && !loaded) { + v->w = short2host(v->w); + v->h = short2host(v->h); + v->sx = short2host(v->sx); + v->sy = short2host(v->sy); + } + return v; +} + +vgaimg *V_loadvgaimg (char *name) { + return V_getvgaimg(F_getresid(name)); +} + short V_init(void) { Uint32 flags = SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_HWPALETTE; diff --git a/src/vga.h b/src/vga.h index 132af8c..cc90ca5 100644 --- a/src/vga.h +++ b/src/vga.h @@ -52,6 +52,9 @@ typedef void redraw_f(void); typedef void spr_f(int, int, unsigned char);//typedef void spr_f(void); +vgaimg *V_getvgaimg (int id); +vgaimg *V_loadvgaimg (char *name); + // переключение в режим VGA 320x200,256 цветов // возвращает 0, если все о'кей short V_init(void); diff --git a/src/view.c b/src/view.c index 0d19be0..3bf3677 100644 --- a/src/view.c +++ b/src/view.c @@ -118,7 +118,10 @@ void W_loadgame(FILE* h) { walani[i]=getani(s); if(strncasecmp(s,"_WATER_",7)==0) { //if(memicmp(s,"_WATER_",7)==0) { walh[i]=-2;walp[i]=(void*)(s[7]-'0'+1); - }else walp[i]=M_lock(walh[i]=F_getresid(s)); + } else { + walh[i]=F_getresid(s); + walp[i]=V_getvgaimg(walh[i]); + } } myfread(walf,1,sizeof(walf),h); for(i=1;i<256;++i) if(walf[i]&1) walh[i]|=0x8000; @@ -128,7 +131,7 @@ void W_loadgame(FILE* h) { myfread(fldf,1,FLDW*FLDH,h); strcpy(s,"RSKY1");s[4]=sky_type+'0'; M_unlock(horiz); - horiz=M_lock(F_getresid(s)); + horiz=V_loadvgaimg(s); } void W_adjust(void) { @@ -210,7 +213,7 @@ void W_init(void) { PL_init(); MN_init(); M_unlock(horiz); - horiz=M_lock(F_getresid("RSKY1")); + horiz=V_loadvgaimg("RSKY1"); free_chunks(); } @@ -221,10 +224,11 @@ void W_act(void) { if(g_time%3!=0) return; for(i=1;i<256;++i) if((a=walani[i])!=0) { if(anih[a][++anic[a]]==-1) anic[a]=0; - walp[i]=M_lock(anih[a][anic[a]]); + walp[i]=V_getvgaimg(anih[a][anic[a]]); } } +/* static void unpack(void *buf,int len,void *obuf) { byte *p,*o; int l,n; @@ -233,6 +237,26 @@ static void unpack(void *buf,int len,void *obuf) { n=*((word*)(++p));memset(o,*(p+=2),n);o+=n;l-=3; }else *(o++)=*p; } +*/ + +static void unpack(void *buf, int len, void *obuf) { + int i = 0; + int j = 0; + unsigned char *p = buf; + unsigned char *q = obuf; + while (i < len) { + int id = p[i]; + int step = 1; + i += 1; + if (id == 0xff) { + step = p[i] | p[i + 1] << 8; + id = p[i + 2]; + i += 3; + } + memset(&q[j], id, step); + j += step; + } +} int W_load(FILE* h) { int i,j,k,g; @@ -247,7 +271,9 @@ int W_load(FILE* h) { if(strncasecmp(w.n,"_WATER_",7)==0) //if(memicmp(w.n,"_WATER_",7)==0) {walp[i]=(void*)(w.n[7]-'0'+1);walh[i]=-2;continue;} - walp[i]=M_lock(walh[i]=F_getresid(w.n)); + + walh[i]=F_getresid(w.n); + walp[i]=V_getvgaimg(walh[i]); if(w.n[0]=='S' && w.n[1]=='W' && w.n[4]=='_') walswp[i]=0; walf[i]=(w.t)?1:0;if(w.t) walh[i]|=0x8000; @@ -261,7 +287,7 @@ int W_load(FILE* h) { g=F_getresid(w.n)|(walh[i]&0x8000); for(k=1;k<256;++k) if(walh[k]==g) break; if(k>=256) { - walh[k=j++]=g;walp[k]=M_lock(g); + walh[k=j++]=g;walp[k]=V_getvgaimg(g); walf[k]=(g&0x8000)?1:0; } walswp[i]=k;walswp[k]=i; @@ -280,10 +306,11 @@ int W_load(FILE* h) { default: return 0; }return 1; case MB_SKY: - sky_type=0;myfread(&sky_type,1,2,h); + sky_type=0;myfread(&sky_type,2,1,h); + sky_type = short2host(sky_type); strcpy(w.n,"RSKY1");w.n[4]=sky_type+'0'; M_unlock(horiz); - horiz=M_lock(F_getresid(w.n)); + horiz=V_loadvgaimg(w.n); return 1; }return 0; } -- 2.29.2