install: add install sources and resources
[doom2d-restoration.git] / GAME / A8.C
blob096327418a6c417986e43cc957086ac387ac736c
1 #include "glob.h"
2 #include <stdio.h>
3 #include <io.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <vga.h>
8 #include <keyb.h>
9 #include "error.h"
10 #include "files.h"
11 #include "sound.h"
13 #define WAIT_SZ 400000
15 extern byte gamcor[5][64];
17 extern char cd_path[];
19 extern int gamma;
20 void setgamma(int);
22 extern void *cd_scr;
24 static byte skipping=0,credits=0;
26 #define A8_ID 0xA8
28 enum{
29   A8C_ENDFR,A8C_PAL,A8C_CLRSCR,A8C_DRAW,
30   A8C_FILL,A8C_DRAW2C,A8C_DRAW2
33 typedef struct{
34   unsigned char id,ver;
35   short width,height,frames;
36   long maxfsize;
37   long f1size;
38 }a8_hdr_t;
40 typedef unsigned char uchar;
42 #define SQ 4
44 static int sqw,sqh;
46 static int norm_gamma;
48 static unsigned char *frp,sqc[2][50][80];
49 static int frame;
50 static a8_hdr_t ah;
51 static snd_t *strk;
52 static int strk_ch;
54 static signed char *unpack(char *d,signed char *p,int l) {
55   for(;l>0;) if(*p>0) {
56     memcpy(d,p+1,*p);d+=*p;l-=*p;p+=*p+1;
57   }else if(*p<0) {
58     memset(d,p[1],-*p);d+=-*p;l-=-*p;p+=2;
59   }else return p+1;
60   return p;
63 static unsigned char *draw(signed char *p) {
64   int x,y,sy,yc,n;
66   for(y=0;y<sqh;) if(*p>0) {
67     for(yc=*p++;yc;--yc,++y) for(x=0;x<sqw;) if(*p>0) {
68       n=(*p++)*SQ;
69       for(sy=0;sy<SQ;++sy) {
70         p=unpack(scra+(y*SQ+sy)*320+x*SQ,p,n);
71       }
72       x+=n/SQ;
73     }else x+=-*p++;
74   }else y+=-*p++;
75   return p;
78 static unsigned char *fill(signed char *p) {
79   int x,y,yc,n;
81   for(y=0;y<sqh;) if(*p>0) {
82     for(yc=*p++;yc;--yc,++y) for(x=0;x<sqw;) if(*p>0) {
83       for(n=*p++;n;--n,++p,++x)
84         V_clr(x*SQ,SQ,y*SQ,SQ,*p);
85     }else x+=-*p++;
86   }else y+=-*p++;
87   return p;
90 static unsigned char *draw2c(signed char *p) {
91   int x,y,sx,sy,yc,n;
92   unsigned short w;
94   for(y=0;y<sqh;) if(*p>0) {
95     for(yc=*p++;yc;--yc,++y) for(x=0;x<sqw;) if(*p>0) {
96       for(n=*p++;n;--n,++x) {
97         sqc[0][y][x]=*p++;
98         sqc[1][y][x]=*p++;
99         w=*(unsigned short*)p;p+=2;
100         for(sy=0;sy<SQ;++sy)
101           for(sx=0;sx<SQ;++sx,w>>=1)
102             scra[(y*SQ+sy)*320+x*SQ+sx]=sqc[w&1][y][x];
103       }
104     }else x+=-*p++;
105   }else y+=-*p++;
106   return p;
109 static unsigned char *draw2(signed char *p) {
110   int x,y,sx,sy,yc,n;
111   unsigned short w;
113   for(y=0;y<sqh;) if(*p>0) {
114     for(yc=*p++;yc;--yc,++y) for(x=0;x<sqw;) if(*p>0) {
115       for(n=*p++;n;--n,++x) {
116         w=*(unsigned short*)p;p+=2;
117         for(sy=0;sy<SQ;++sy)
118           for(sx=0;sx<SQ;++sx,w>>=1)
119             scra[(y*SQ+sy)*320+x*SQ+sx]=sqc[w&1][y][x];
120       }
121     }else x+=-*p++;
122   }else y+=-*p++;
123   return p;
126 //---------------------------------------------------------------//
128 static int fh,fsz,fdptr;
129 static char *fdata;
131 //static void f_read(void *p,int l) {
132 //  if(fdata) {
133 //    memcpy(p,fdata+fdptr,l);fdptr+=l;
134 //  }else read(fh,p,l);
137 //static void f_skip(int n) {
138 //  lseek(fh,n,SEEK_CUR);
141 static void f_close(void) {
142   if(fdata) {
143     free(fdata);fdata=NULL;
144   }else if(fh!=-1) {close(fh);fh=-1;}
147 static char end_clr=1;
149 void A8_close(void) {
150   if(strk) if(strk_ch) S_stop(strk_ch);
151   f_close();
152   if(frp) {free(frp);frp=NULL;}
153   if(strk) {free(strk);strk=NULL;}
154   if(end_clr) {
155     VP_fill(0,0,0);
156     V_clr(0,320,0,200,0);V_copytoscr(0,320,0,200);
157     setgamma(norm_gamma);
158   }
161 int A8_nextframe(void) {
162   unsigned char *p;
163   int i,j,k;
164   static int len;
166   if(credits) if(keys[0x33] && keys[0x34]) skipping=1;
167   if(frame==-1) if(strk) strk_ch=S_play(strk,-1,1024,255);
168     if(fdata) {
169       len=*(int*)(fdata+fdptr);fdptr+=4;
170     }else {len=0;read(fh,&len,4);}
171     len-=4;
172     if(len<=0) {
173       A8_close();
174       return 0;
175     }
176     if(fdata) {
177       p=fdata+fdptr;fdptr+=len;
178     }else {read(fh,frp,len);p=frp;}
179     for(;*p;) switch(*p++) {
180       case A8C_PAL:
181         i=*p++;j=*p++;if(!j) j=256;
182         for(k=0;k<j*3;++k) p[k]=gamcor[3][p[k]];
183         VP_set(p,i,j);
184         p+=j*3;
185         break;
186       case A8C_CLRSCR:
187         V_clr(0,ah.width,0,ah.height,*p++);
188         break;
189       case A8C_DRAW:
190         p=draw(p);
191         break;
192       case A8C_FILL:
193         p=fill(p);
194         break;
195       case A8C_DRAW2C:
196         p=draw2c(p);
197         break;
198       case A8C_DRAW2:
199         p=draw2(p);
200         break;
201       default:
202         ERR_fatal("Плохой блок в файле A8");
203     }
204     ++frame;
205   return 1;
208 static char wscr;
210 static void wait_scr(int s) {
211   if(!end_clr) return;
212   if(s<WAIT_SZ) return;
213   F_freemus();
214   V_setrect(0,320,0,200);
215   V_clr(0,320,0,200,0);
216   V_copytoscr(0,320,0,200);
217   V_pic(0,0,(void*)((char*)cd_scr+768));
218   VP_setall(cd_scr);
219   V_copytoscr(0,320,0,200);
220   wscr=1;
223 static void blank_scr(void) {
224   VP_fill(0,0,0);
225   V_setrect(0,320,0,200);
226   V_clr(0,320,0,200,0);
227   V_copytoscr(0,320,0,200);
230 int A8_start(char *nm) {
231   static char s[40];
232   int sz,h;
233   unsigned char *p;
235   end_clr=1;
236   if(stricmp(nm,"FINAL")==0 || stricmp(nm,"CREDITS")==0) end_clr=0;
237   else if(stricmp(nm,"KONEC")==0) end_clr=0;
238   credits=(stricmp(nm,"FINAL")==0)?1:0;
239   if(stricmp(nm,"CREDITS")==0) if(skipping) return 0;
240   wscr=0;
241   strk=NULL;strk_ch=0;
242   fdata=NULL;frp=NULL;
243   if(snd_type!=ST_NONE) {
244     sprintf(s,"%sA8\\%s.SND",cd_path,nm);
245     if((h=open(s,O_BINARY|O_RDONLY))!=-1) {
246       lseek(h,0,SEEK_END);sz=tell(h);lseek(h,0,SEEK_SET);
247       if((strk=malloc(sz+sizeof(snd_t)))!=NULL) {
248         wait_scr(sz);
249         read(h,strk+1,sz);
250         strk->rate=11000;
251         strk->len=sz;
252         strk->lstart=strk->llen=0;
253         for(p=(unsigned char *)(strk+1);sz;--sz,++p) *p^=0x80;
254       }
255       close(h);
256     }
257   }
258   sprintf(s,"%sA8\\%s.A8",cd_path,nm);
259   if((fh=open(s,O_BINARY|O_RDONLY))==-1) {
260 //    if(strk) {free(strk);strk=NULL;}
261 //    return 0;
262     ERR_fatal("Не могу открыть файл %s",s);
263   }
264   read(fh,&ah,sizeof(ah)-4);
265   if(ah.id!=A8_ID || ah.ver!=0) ERR_fatal("Испорченный файл A8 %s",s);
266   lseek(fh,0,SEEK_END);
267   fsz=tell(fh)-sizeof(ah)+4;
268   lseek(fh,sizeof(ah)-4,SEEK_SET);
269   if((fdata=malloc(fsz))!=NULL) {
270     wait_scr(fsz);
271     read(fh,fdata,fsz);
272     fdptr=0;
273     close(fh);fh=-1;
274   }else if(!(frp=malloc(ah.maxfsize))) {
275     if(strk) {free(strk);strk=NULL;}
276     if(!(frp=malloc(ah.maxfsize))) {
277       close(fh);fh=-1;return 0;
278     }
279   }
280   sqw=ah.width/SQ;sqh=ah.height/SQ;
281   frame=-1;
282   norm_gamma=gamma;
283   if(wscr) blank_scr();
284 //  if(gamma<3) setgamma(3);
285   return 1;