DEADSOFTWARE

fully move highter level rendering code in separate file
[flatwaifu.git] / src / menu.c
1 /*
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
21 */
23 #include "glob.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
27 #include "files.h"
28 #include "memory.h"
29 #include "vga.h"
30 #include "error.h"
31 #include "keyb.h"
32 #include "sound.h"
33 #include "view.h"
34 #include "player.h"
35 #include "switch.h"
36 #include "menu.h"
37 #include "misc.h"
39 #include <SDL.h>
40 #include <sys/stat.h>
41 extern SDL_Surface *screen;
43 #define QSND_NUM 14
45 enum{HIT100,ARMOR,JUMP,WPNS,IMMORTAL,SPEED,OPEN,EXIT};
47 extern int PL_JUMP,PL_RUN;
48 extern byte _warp,cheat,p_fly;
50 extern char g_music[8];
52 extern byte savname[7][24],savok[7];
53 void load_game(int);
55 static byte panim[]=
56 "BBDDAACCDDAABBDDAACCDDAABBDDAACCDDAAEEEEEFEFEFEFEFEFEFEFEFEFEEEEE";
57 byte *panimp=panim;
59 byte pcolortab[PCOLORN]={
60 0x18,0x20,0x40,0x58,0x60,0x70,0x80,0xB0,0xC0,0xD0
61 };
62 int p1color=5,p2color=4;
64 char ibuf[24];
65 byte input=0;
66 static int icur;
68 enum{MENU,MSG};
69 enum{CANCEL,NEWGAME,LOADGAME,SAVEGAME,OPTIONS,QUITGAME,QUIT,ENDGAME,ENDGM,
70 PLR1,PLR2,COOP,DM,VOLUME,GAMMA,LOAD,SAVE,PLCOLOR,PLCEND,MUSIC,INTERP,
71 SVOLM,SVOLP,MVOLM,MVOLP,GAMMAM,GAMMAP,PL1CM,PL1CP,PL2CM,PL2CP};
73 #ifndef DEMO
74 static int qsnd[QSND_NUM];
75 #endif
77 static char *main_txt[]={
78 "NEW GAME","LOAD GAME","SAVE GAME","OPTIONS","EXIT"
79 },*opt_txt[]={
80 "RESTART","VOLUME","BRIGHTNESS","MUSIC","FULLSCREEN:"
81 },*ngplr_txt[]={
82 "ONE PLAYER","TWO PLAYERS"
83 },*ngdm_txt[]={
84 "COOPERATIVE","DEATHMATCH"
85 },*vol_txt[]={
86 "SOUND","MUSIC"
87 },*plcolor_txt[]={
88 "FIRST","SECOND"
89 },*gamma_txt[]={
90 ""
91 };
93 static byte main_typ[]={
94 NEWGAME,LOADGAME,SAVEGAME,OPTIONS,QUITGAME
95 },ngplr_typ[]={
96 PLR1,PLR2
97 },ngdm_typ[]={
98 COOP,DM
99 },opt_typ[]={
100 ENDGAME,VOLUME,GAMMA,MUSIC,INTERP
101 },quit_typ[]={
102 QUIT,CANCEL
103 },endgm_typ[]={
104 ENDGM,CANCEL
105 },vol_typ[]={
106 SVOLM,MVOLM
107 },plcolor_typ[]={
108 PL1CM,PL2CM
109 },gamma_typ[]={
110 GAMMAM
111 },load_typ[]={
112 LOAD,LOAD,LOAD,LOAD,LOAD,LOAD,LOAD
113 },save_typ[]={
114 SAVE,SAVE,SAVE,SAVE,SAVE,SAVE,SAVE
115 };
117 menu_t main_mnu={
118 MENU,5,0,80,"MENU",main_txt,main_typ
119 },opt_mnu={
120 MENU,5,0,75,"OPTIONS",opt_txt,opt_typ
121 },ngplr_mnu={
122 MENU,2,0,90,"NEW GAME",ngplr_txt,ngplr_typ
123 },ngdm_mnu={
124 MENU,2,0,90,"GAME TYPE",ngdm_txt,ngdm_typ
125 },vol_mnu={
126 MENU,2,0,40,"VOLUME",vol_txt,vol_typ
127 },plcolor_mnu={
128 MENU,2,0,90,"COLOR",plcolor_txt,plcolor_typ
129 },gamma_mnu={
130 MENU,1,0,85,"BRIGHTNESS",gamma_txt,gamma_typ
131 },load_mnu={
132 MENU,7,0,85,"LOAD GAME",NULL,load_typ
133 },save_mnu={
134 MENU,7,0,85,"SAVE GAME",NULL,save_typ
135 },quit1_msg={
136 MSG,0,0,0,"ARE YOU SURE?",NULL,quit_typ
137 },quit2_msg={
138 MSG,0,0,0,"ARE YOU SURE?",NULL,quit_typ
139 },quit3_msg={
140 MSG,0,0,0,"ARE YOU SURE?",NULL,quit_typ
141 },endgm_msg={
142 MSG,0,0,0,"RESTART LEVEL?",NULL,endgm_typ
143 };
145 static menu_t *qmsg[3]={&quit1_msg,&quit2_msg,&quit3_msg};
147 menu_t *mnu=NULL;
148 byte gm_redraw=0;
150 short lastkey=0;
151 static void *csnd1,*csnd2,*msnd1,*msnd2,*msnd3,*msnd4,*msnd5,*msnd6;
152 static int movsndt=0;
153 static byte cbuf[32];
155 static snd_t *voc=NULL;
156 static int voc_ch=0;
158 void GMV_stop(void) {
159 if(voc) {
160 if(voc_ch) {S_stop(voc_ch);voc_ch=0;}
161 free(voc);voc=NULL;
165 void GMV_say(char *nm) {
166 int r,len;
167 snd_t *p;
168 byte *d;
170 if((r=F_findres(nm))==-1) return;
171 if(!(p=malloc((len=F_getreslen(r))+16))) return;
172 p->len=len;p->rate=11000;
173 p->lstart=p->llen=0;
174 GMV_stop();
175 F_loadres(r,p+1,0,len);
176 for(d=(byte*)(p+1);len;--len,++d) *d^=128;
177 voc=p;
178 voc_ch=S_play(voc,-1,1024,255);
181 void G_code(void) {
182 void *s;
183 s=csnd2;
184 if(memcmp(cbuf+32-5,"IDDQD",5)==0) {
185 PL_hit(&pl1,400,0,HIT_SOME);
186 if(_2pl) PL_hit(&pl2,400,0,HIT_SOME);
187 s=csnd1;
188 }else if(memcmp(cbuf+32-4,"TANK",4)==0) {
189 pl1.life=pl1.armor=200;pl1.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;
190 if(_2pl) {pl2.life=pl2.armor=200;pl2.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;}
191 }else if(memcmp(cbuf+32-8,"BULLFROG",8)==0) {
192 PL_JUMP=(PL_JUMP==10)?20:10;
193 }else if(memcmp(cbuf+32-8,"FORMULA1",8)==0) {
194 PL_RUN=(PL_RUN==8)?24:8;
195 }else if(memcmp(cbuf+32-5,"RAMBO",5)==0) {
196 pl1.ammo=pl1.shel=pl1.rock=pl1.cell=pl1.fuel=30000;
197 pl1.wpns=0x7FF;pl1.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
198 pl1.keys=0x70;
199 if(_2pl) {
200 pl2.ammo=pl2.shel=pl2.rock=pl2.cell=pl1.fuel=30000;
201 pl2.wpns=0x7FF;pl2.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
202 pl2.keys=0x70;
204 }else if(memcmp(cbuf+32-5,"UJHTW",5)==0) {
205 p_immortal=!p_immortal;
206 }else if(memcmp(cbuf+32-9,",TKSQJHTK",9)==0) {
207 p_fly=!p_fly;
208 }else if(memcmp(cbuf+32-6,"CBVCBV",6)==0) {
209 SW_cheat_open();
210 }else if(memcmp(cbuf+32-7,"GOODBYE",7)==0) {
211 g_exit=1;
212 }else if(memcmp(cbuf+32-9,"GJITKYF",7)==0) {
213 if(cbuf[30]>='0' && cbuf[30]<='9' && cbuf[31]>='0' && cbuf[31]<='9') {
214 g_map=(cbuf[30]=='0')?0:(cbuf[30]-'0')*10;
215 g_map+=(cbuf[31]=='0')?0:(cbuf[31]-'0');
216 G_start();
218 }else return;
219 memset(cbuf,0,32);
220 Z_sound(s,128);
223 void GM_set(menu_t *m) {
224 mnu=m;gm_redraw=1;
225 if(g_st==GS_GAME) {
226 //V_setrect(0,SCRW,0,SCRH);V_clr(0,SCRW,0,SCRH,0);//V_setrect(0,320,0,200);V_clr(0,320,0,200,0);
227 //if(_2pl) {V_setrect(SCRW-120,120,0,SCRH);w_o=0;Z_clrst();w_o=SCRH/2;Z_clrst();}//if(_2pl) {V_setrect(200,120,0,200);w_o=0;Z_clrst();w_o=100;Z_clrst();}
228 //else {V_setrect(SCRW-120,120,0,SCRH);w_o=0;Z_clrst();}//else {V_setrect(200,120,50,100);w_o=50;Z_clrst();}
229 //pl1.drawst=pl2.drawst=0xFF;V_setrect(0,SCRW,0,SCRH);//V_setrect(0,320,0,200);
233 void setgamma(int);
235 void GM_command(int c) {
236 switch(c) {
237 case CANCEL:
238 GM_set(NULL);break;
239 case INTERP:
240 fullscreen=!fullscreen;
241 V_toggle();
242 GM_set(mnu);
243 break;
244 case MUSIC:
245 F_freemus();
246 F_nextmus(g_music);
247 F_loadmus(g_music);
248 S_startmusic(music_time*2);
249 GM_set(mnu);
250 break;
251 case NEWGAME:
252 GMV_say("_NEWGAME");
253 GM_set(&ngplr_mnu);break;
254 case PLR2:
255 GMV_say("_2PLAYER");
256 GM_set(&ngdm_mnu);break;
257 case PLR1:
258 GMV_say("_1PLAYER");
259 ngdm_mnu.cur=0;
260 case COOP: case DM:
261 if(c==COOP) GMV_say("_COOP");
262 else if(c==DM) GMV_say("_DM");
263 if(c!=PLR1) {GM_set(&plcolor_mnu);break;}
264 case PLCEND:
265 _2pl=ngplr_mnu.cur;
266 g_dm=ngdm_mnu.cur;
267 g_map=(_warp)?_warp:1;
268 PL_reset();
269 if(_2pl) {
270 pl1.color=pcolortab[p1color];
271 pl2.color=pcolortab[p2color];
272 }else pl1.color=0x70;
273 G_start();
274 GM_set(NULL);break;
275 case OPTIONS:
276 GMV_say("_RAZNOE");
277 GM_set(&opt_mnu);break;
278 case LOADGAME:
279 GMV_say("_OLDGAME");
280 F_getsavnames();GM_set(&load_mnu);break;
281 case SAVEGAME:
282 if(g_st!=GS_GAME) break;
283 GMV_say("_SAVEGAM");
284 F_getsavnames();GM_set(&save_mnu);break;
285 case SAVE:
286 input=1;memcpy(ibuf,savname[save_mnu.cur],24);icur=strlen(ibuf);
287 GM_set(mnu);break;
288 case LOAD:
289 if(!savok[load_mnu.cur]) break;
290 load_game(load_mnu.cur);
291 GM_set(NULL);break;
292 case VOLUME:
293 GMV_say("_VOLUME");
294 GM_set(&vol_mnu);break;
295 case GAMMA:
296 GMV_say("_GAMMA");
297 GM_set(&gamma_mnu);break;
298 case QUITGAME:
299 GMV_say((rand()&1)?"_EXIT1":"_EXIT2");
300 GM_set(qmsg[myrand(3)]);break;
301 case ENDGAME:
302 if(g_st!=GS_GAME) break;
303 GMV_say("_RESTART");
304 GM_set(&endgm_msg);break;
305 case QUIT:
306 F_freemus();
307 GMV_stop();
308 #ifndef DEMO
309 c=Z_sound(M_lock(qsnd[myrand(QSND_NUM)]),256);//for(c=(Z_sound(M_lock(qsnd[random2(QSND_NUM)]),256)+9)<<16,timer=0;timer<c;);
310 S_wait();
311 #endif
312 ERR_quit();break;
313 case ENDGM:
314 PL_reset();G_start();
315 GM_set(NULL);break;
316 case PL1CM:
317 if(--p1color<0) p1color=PCOLORN-1; break;
318 case PL1CP:
319 if(++p1color>=PCOLORN) p1color=0; break;
320 case PL2CM:
321 if(--p2color<0) p2color=PCOLORN-1; break;
322 case PL2CP:
323 if(++p2color>=PCOLORN) p2color=0; break;
324 case SVOLM:
325 S_volume(snd_vol-8);break;
326 case SVOLP:
327 S_volume(snd_vol+8);break;
328 case MVOLM:
329 S_volumemusic(mus_vol-8);break;
330 case MVOLP:
331 S_volumemusic(mus_vol+8);break;
332 case GAMMAM: setgamma(gammaa-1);break;
333 case GAMMAP: setgamma(gammaa+1);break;
337 /*
338 byte keychar[2][128]={{
339 0,0,'1','2','3','4','5','6','7','8','9','0','-','=',0,0,
340 'Q','W','E','R','T','Y','U','I','O','P','[',']','\r',0,'A','S',
341 'D','F','G','H','J','K','L',';','\'',0,0,'\\','Z','X','C','V',
342 'B','N','M',',','.','/',0,'*',0,' ',0,0,0,0,0,0,
343 0,0,0,0,0,0,0,0,0,0,'-',0,0,0,'+',0,
344 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
345 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
346 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
347 },{
348 0,0,'!','\"','#','$','%',':','&','*','(',')','_','+',0,0,
349 'x','x','x','x','x','x','x','x','x','x','x','x','\r',0,'x','x',
350 'x','x','x','x','x','x','x','x','x',0,0,0,'x','x','x','x',
351 'x','x','x','x','x','?',0,'*',0,' ',0,0,0,0,0,0,
352 0,0,0,0,0,0,0,0,0,0,'-',0,0,0,'+',0,
353 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
354 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
355 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
356 }};
357 */
359 struct {
360 int keysym;
361 byte ch;
362 } keychar[] = {
363 {SDLK_SPACE, ' '},
364 {SDLK_0, '0'},
365 {SDLK_1, '1'},
366 {SDLK_2, '2'},
367 {SDLK_3, '3'},
368 {SDLK_4, '4'},
369 {SDLK_5, '5'},
370 {SDLK_6, '6'},
371 {SDLK_7, '7'},
372 {SDLK_8, '8'},
373 {SDLK_9, '9'},
374 {SDLK_UNDERSCORE, '_'},
375 {SDLK_a, 'A'},
376 {SDLK_b, 'B'},
377 {SDLK_c, 'C'},
378 {SDLK_d, 'D'},
379 {SDLK_e, 'E'},
380 {SDLK_f, 'F'},
381 {SDLK_g, 'G'},
382 {SDLK_h, 'H'},
383 {SDLK_i, 'I'},
384 {SDLK_j, 'J'},
385 {SDLK_k, 'K'},
386 {SDLK_l, 'L'},
387 {SDLK_m, 'M'},
388 {SDLK_n, 'N'},
389 {SDLK_o, 'O'},
390 {SDLK_p, 'P'},
391 {SDLK_q, 'Q'},
392 {SDLK_r, 'R'},
393 {SDLK_s, 'S'},
394 {SDLK_t, 'T'},
395 {SDLK_u, 'U'},
396 {SDLK_v, 'V'},
397 {SDLK_w, 'W'},
398 {SDLK_x, 'X'},
399 {SDLK_y, 'Y'},
400 {SDLK_z, 'Z'},
401 {SDLK_COMMA,','},
402 {0}
403 };
405 byte get_keychar(int keysym)
407 int i = 0;
408 while (keychar[i].keysym) {
409 if (keychar[i].keysym == keysym) return keychar[i].ch;
410 i++;
412 return 0;
415 extern vgapal main_pal,std_pal;
416 extern byte shot_vga;
418 static void shot(void) {
419 static int num=1;
420 char fn[100];//...
421 #ifndef WIN32
422 char *e = getenv("HOME");
423 strncpy(fn, e, 60);
424 sprintf(&fn[strlen(fn)],"/.doom2d-rembo",num);
425 mkdir(fn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
426 sprintf(&fn[strlen(fn)],"/shot%04d.bmp",num);
427 #else
428 sprintf(fn,"shot%04d.bmp",num);
429 #endif
430 SDL_SaveBMP(screen, fn);
431 ++num;
434 int GM_act(void) {
435 byte c;
437 if(mnu==&plcolor_mnu) {
438 if(*(++panimp)==0) panimp=panim;
439 GM_set(mnu);
441 if(movsndt>0) --movsndt; else movsndt=0;
442 if(g_st==GS_TITLE) if(!mnu) if(lastkey) {
443 GM_set(&main_mnu);Z_sound(msnd3,128);
444 lastkey=0;
445 return 1;
447 if(input) switch(lastkey) {
448 case SDLK_RETURN: case SDLK_KP_ENTER://case 0x1C: case 0x9C:
449 F_savegame(save_mnu.cur,ibuf);
450 input=0;GM_set(NULL);break;
451 case 1: input=0;GM_set(mnu);break;
452 case SDLK_BACKSPACE://case 0x0E:
453 if(icur) {ibuf[--icur]=0;GM_set(mnu);} break;
454 default:
455 if(icur>=23) break;
456 c=get_keychar(lastkey);//c=keychar[(keys[0x2A] || keys[0x36])?1:0][lastkey];
457 if(!c) break;
458 ibuf[icur]=c;ibuf[++icur]=0;GM_set(mnu);
459 }else {
460 switch(lastkey) {
461 case SDLK_ESCAPE://case 1:
462 if(!mnu) {GM_set(&main_mnu);Z_sound(msnd3,128);}
463 else {GM_set(NULL);Z_sound(msnd4,128);}
464 break;
465 case SDLK_F5:
466 if(mnu) break;
467 Z_sound(msnd3,128);
468 GMV_say("_GAMMA");
469 GM_set(&gamma_mnu);break;
470 case SDLK_F4://case 0x3E:
471 if(mnu) break;
472 Z_sound(msnd3,128);
473 GMV_say("_VOLUME");
474 GM_set(&vol_mnu);break;
475 case SDLK_F2://case 0x3C:
476 if(mnu) break;
477 if(g_st!=GS_GAME) break;
478 Z_sound(msnd3,128);
479 F_getsavnames();GM_set(&save_mnu);break;
480 case SDLK_F3://case 0x3D:
481 if(mnu) break;
482 Z_sound(msnd3,128);
483 F_getsavnames();GM_set(&load_mnu);break;
484 case SDLK_F10://case 0x44:
485 if(mnu) break;
486 Z_sound(msnd3,128);
487 GM_command(QUITGAME);break;
488 case SDLK_UP: case SDLK_KP8://case 0x48: case 0xC8:
489 if(!mnu) break;
490 if(mnu->type!=MENU) break;
491 if(--mnu->cur<0) mnu->cur=mnu->n-1;
492 GM_set(mnu);
493 Z_sound(msnd1,128);break;
494 case SDLK_DOWN: case SDLK_KP5: case SDLK_KP2://case 0x50: case 0xD0: case 0x4C:
495 if(!mnu) break;
496 if(mnu->type!=MENU) break;
497 if(++mnu->cur>=mnu->n) mnu->cur=0;
498 GM_set(mnu);
499 Z_sound(msnd1,128);break;
500 case SDLK_LEFT: case SDLK_RIGHT: case SDLK_KP4: case SDLK_KP6://case 0x4B: case 0x4D: case 0xCB: case 0xCD:
501 if(!mnu) break;
502 if(mnu->type!=MENU) break;
503 if(mnu->t[mnu->cur]<SVOLM) break;
504 GM_command(mnu->t[mnu->cur]+((lastkey==SDLK_LEFT || lastkey==SDLK_KP4)?0:1));//GM_command(mnu->t[mnu->cur]+((lastkey==0x4B || lastkey==0xCB)?0:1));
505 GM_set(mnu);
506 if(!movsndt) movsndt=Z_sound((lastkey==SDLK_LEFT || lastkey==SDLK_KP4)?msnd5:msnd6,255);//if(!movsndt) movsndt=Z_sound((lastkey==0x4B || lastkey==0xCB)?msnd5:msnd6,255);
507 break;
508 case SDLK_RETURN: case SDLK_SPACE: case SDLK_KP_ENTER://case 0x1C: case 0x39: case 0x9C:
509 if(!mnu) break;
510 if(mnu->type!=MENU) break;
511 if(mnu->t[mnu->cur]>=PL1CM) {
512 Z_sound(msnd2,128);
513 GM_command(PLCEND);
514 break;
516 if(mnu->t[mnu->cur]>=SVOLM) break;
517 Z_sound(msnd2,128);
518 GM_command(mnu->t[mnu->cur]);
519 break;
520 case SDLK_y://case 0x15:
521 if(!mnu) break;
522 if(mnu->type!=MSG) break;
523 Z_sound(msnd3,128);
524 GM_command(mnu->t[0]);
525 break;
526 case SDLK_n://case 0x31:
527 if(!mnu) break;
528 if(mnu->type!=MSG) break;
529 Z_sound(msnd4,128);
530 GM_command(mnu->t[1]);
531 break;
532 case SDLK_F1://case 0x3B:
533 if(shot_vga) {shot();Z_sound(msnd4,128);}
534 break;
537 lastkey=0;
538 return((mnu)?1:0);
541 void G_keyf(int k, int press) {
542 int i;
544 lastkey=k;
545 if(!_2pl || cheat) {
546 for(i=0;i<31;++i) cbuf[i]=cbuf[i+1];
547 cbuf[31]=get_keychar(k);
551 void GM_init(void) {
552 #ifndef DEMO
553 int i;
554 static char nm[QSND_NUM][6]={
555 "CYBSIT","KNTDTH","MNPAIN","PEPAIN","SLOP","MANSIT","BOSPN","VILACT",
556 "PLFALL","BGACT","BGDTH2","POPAIN","SGTATK","VILDTH"
557 };
558 char s[8];
560 s[0]='D';s[1]='S';
561 for(i=0;i<QSND_NUM;++i) {
562 memcpy(s+2,nm[i],6);
563 qsnd[i]=F_getresid(s);
565 #endif
566 csnd1=Z_getsnd("HAHA1");
567 csnd2=Z_getsnd("RADIO");
568 msnd1=Z_getsnd("PSTOP");
569 msnd2=Z_getsnd("PISTOL");
570 msnd3=Z_getsnd("SWTCHN");
571 msnd4=Z_getsnd("SWTCHX");
572 msnd5=Z_getsnd("SUDI");
573 msnd6=Z_getsnd("TUDI");
574 K_setkeyproc(G_keyf);