DEADSOFTWARE

rembo 22222
[flatwaifu.git] / a8.c
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"
14 /*
15 #define WAIT_SZ 400000
17 extern byte gamcor[5][64];
19 extern char cd_path[];
21 extern int gammaa;
22 void setgamma(int);
24 extern void *cd_scr;
26 static byte skipping=0,credits=0;
28 #define A8_ID 0xA8
30 enum{
31 A8C_ENDFR,A8C_PAL,A8C_CLRSCR,A8C_DRAW,
32 A8C_FILL,A8C_DRAW2C,A8C_DRAW2
33 };
35 typedef struct{
36 unsigned char id,ver;
37 short width,height,frames;
38 long maxfsize;
39 long f1size;
40 }a8_hdr_t;
42 typedef unsigned char uchar;
44 #define SQ 4
46 static int sqw,sqh;
48 static int norm_gamma;
50 static unsigned char *frp,sqc[2][50][80];
51 static int frame;
52 static a8_hdr_t ah;
53 static snd_t *strk;
54 static int strk_ch;
56 static signed char *unpack(char *d,signed char *p,int l) {
57 for(;l>0;) if(*p>0) {
58 memcpy(d,p+1,*p);d+=*p;l-=*p;p+=*p+1;
59 }else if(*p<0) {
60 memset(d,p[1],-*p);d+=-*p;l-=-*p;p+=2;
61 }else return p+1;
62 return p;
63 }
65 static unsigned char *draw(signed char *p) {
66 int x,y,sy,yc,n;
68 for(y=0;y<sqh;) if(*p>0) {
69 for(yc=*p++;yc;--yc,++y) for(x=0;x<sqw;) if(*p>0) {
70 n=(*p++)*SQ;
71 for(sy=0;sy<SQ;++sy) {
72 p=unpack(scra+(y*SQ+sy)*320+x*SQ,p,n);
73 }
74 x+=n/SQ;
75 }else x+=-*p++;
76 }else y+=-*p++;
77 return p;
78 }
80 static unsigned char *fill(signed char *p) {
81 int x,y,yc,n;
83 for(y=0;y<sqh;) if(*p>0) {
84 for(yc=*p++;yc;--yc,++y) for(x=0;x<sqw;) if(*p>0) {
85 for(n=*p++;n;--n,++p,++x)
86 V_clr(x*SQ,SQ,y*SQ,SQ,*p);
87 }else x+=-*p++;
88 }else y+=-*p++;
89 return p;
90 }
92 static unsigned char *draw2c(signed char *p) {
93 int x,y,sx,sy,yc,n;
94 unsigned short w;
96 for(y=0;y<sqh;) if(*p>0) {
97 for(yc=*p++;yc;--yc,++y) for(x=0;x<sqw;) if(*p>0) {
98 for(n=*p++;n;--n,++x) {
99 sqc[0][y][x]=*p++;
100 sqc[1][y][x]=*p++;
101 w=*(unsigned short*)p;p+=2;
102 for(sy=0;sy<SQ;++sy)
103 for(sx=0;sx<SQ;++sx,w>>=1)
104 scra[(y*SQ+sy)*320+x*SQ+sx]=sqc[w&1][y][x];
106 }else x+=-*p++;
107 }else y+=-*p++;
108 return p;
111 static unsigned char *draw2(signed char *p) {
112 int x,y,sx,sy,yc,n;
113 unsigned short w;
115 for(y=0;y<sqh;) if(*p>0) {
116 for(yc=*p++;yc;--yc,++y) for(x=0;x<sqw;) if(*p>0) {
117 for(n=*p++;n;--n,++x) {
118 w=*(unsigned short*)p;p+=2;
119 for(sy=0;sy<SQ;++sy)
120 for(sx=0;sx<SQ;++sx,w>>=1)
121 scra[(y*SQ+sy)*320+x*SQ+sx]=sqc[w&1][y][x];
123 }else x+=-*p++;
124 }else y+=-*p++;
125 return p;
128 //---------------------------------------------------------------//
130 static int fsz,fdptr;
131 static FILE *fh;
132 static char *fdata;
135 static void f_close(void) {
136 if(fdata) {
137 free(fdata);fdata=NULL;
138 }else if(fh!=NULL) {fclose(fh);fh=NULL;}
141 static char end_clr=1;
142 */
144 void A8_close(void) {
145 /*
146 if(strk) if(strk_ch) S_stop(strk_ch);
147 f_close();
148 if(frp) {free(frp);frp=NULL;}
149 if(strk) {free(strk);strk=NULL;}
150 if(end_clr) {
151 VP_fill(0,0,0);
152 V_clr(0,320,0,200,0);V_copytoscr(0,320,0,200);
153 setgamma(norm_gamma);
155 */
158 int A8_nextframe(void) {
159 /*
160 unsigned char *p;
161 int i,j,k;
162 static int len;
164 if(credits) if(keys[0x33] && keys[0x34]) skipping=1;
165 if(frame==-1) if(strk) strk_ch=S_play(strk,-1,1024,255);
166 if(fdata) {
167 len=*(int*)(fdata+fdptr);fdptr+=4;
168 }else {len=0;myfread(&len,1,4,fh);}
169 len-=4;
170 if(len<=0) {
171 A8_close();
172 return 0;
174 if(fdata) {
175 p=fdata+fdptr;fdptr+=len;
176 }else {myfread(frp,1,len,fh);p=frp;}
177 for(;*p;) switch(*p++) {
178 case A8C_PAL:
179 i=*p++;j=*p++;if(!j) j=256;
180 for(k=0;k<j*3;++k) p[k]=gamcor[3][p[k]];
181 VP_set(p,i,j);
182 p+=j*3;
183 break;
184 case A8C_CLRSCR:
185 V_clr(0,ah.width,0,ah.height,*p++);
186 break;
187 case A8C_DRAW:
188 p=draw(p);
189 break;
190 case A8C_FILL:
191 p=fill(p);
192 break;
193 case A8C_DRAW2C:
194 p=draw2c(p);
195 break;
196 case A8C_DRAW2:
197 p=draw2(p);
198 break;
199 default:
200 ERR_fatal("Плохой блок в файле A8");
202 ++frame;
203 return 1;
204 */
205 return 0;
208 /*
209 static char wscr;
211 static void wait_scr(int s) {
212 if(!end_clr) return;
213 if(s<WAIT_SZ) return;
214 F_freemus();
215 V_setrect(0,320,0,200);
216 V_clr(0,320,0,200,0);
217 V_copytoscr(0,320,0,200);
218 V_pic(0,0,(void*)((char*)cd_scr+768));
219 VP_setall(cd_scr);
220 V_copytoscr(0,320,0,200);
221 wscr=1;
224 static void blank_scr(void) {
225 VP_fill(0,0,0);
226 V_setrect(0,320,0,200);
227 V_clr(0,320,0,200,0);
228 V_copytoscr(0,320,0,200);
230 */
232 int A8_start(char *nm) {
234 /*
235 static char s[40];
236 int sz;
237 FILE* h;
238 unsigned char *p;
240 end_clr=1;
241 if(strcasecmp(nm,"FINAL")==0 || strcasecmp(nm,"CREDITS")==0) end_clr=0;
242 else if(strcasecmp(nm,"KONEC")==0) end_clr=0;
243 credits=(strcasecmp(nm,"FINAL")==0)?1:0;
244 if(strcasecmp(nm,"CREDITS")==0) if(skipping) return 0;
245 wscr=0;
246 strk=NULL;strk_ch=0;
247 fdata=NULL;frp=NULL;
248 if(snd_type!=ST_NONE) {
249 sprintf(s,"%sA8\\%s.SND",cd_path,nm);
250 if((h=fopen(s,"rb"))!=NULL) {
251 fseek(h,0,SEEK_END);sz=ftell(h);fseek(h,0,SEEK_SET);
252 if((strk=malloc(sz+sizeof(snd_t)))!=NULL) {
253 wait_scr(sz);
254 myfread(strk+1,1,sz,h);
255 strk->rate=11000;
256 strk->len=sz;
257 strk->lstart=strk->llen=0;
258 for(p=(unsigned char *)(strk+1);sz;--sz,++p) *p^=0x80;
260 fclose(h);
263 sprintf(s,"%sA8\\%s.A8",cd_path,nm);
264 if((fh=fopen(s,"rb"))==NULL) {
265 ERR_fatal("Не могу открыть файл %s",s);
267 myfread(&ah,1,sizeof(ah)-4,fh);
268 if(ah.id!=A8_ID || ah.ver!=0) ERR_fatal("Испорченный файл A8 %s",s);
269 fseek(fh,0,SEEK_END);
270 fsz=ftell(fh)-sizeof(ah)+4;
271 fseek(fh,sizeof(ah)-4,SEEK_SET);
272 if((fdata=malloc(fsz))!=NULL) {
273 wait_scr(fsz);
274 myfread(fdata,1,fsz,fh);
275 fdptr=0;
276 fclose(fh);fh=NULL;
277 }else if(!(frp=malloc(ah.maxfsize))) {
278 if(strk) {free(strk);strk=NULL;}
279 if(!(frp=malloc(ah.maxfsize))) {
280 fclose(fh);fh=NULL;return 0;
283 sqw=ah.width/SQ;sqh=ah.height/SQ;
284 frame=-1;
285 norm_gamma=gammaa;
286 if(wscr) blank_scr();
289 return 1;
290 */
292 return 0;