DEADSOFTWARE

sound: separate and rewrite
[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 "error.h"
30 #include "sound.h"
31 #include "view.h"
32 #include "player.h"
33 #include "switch.h"
34 #include "menu.h"
35 #include "misc.h"
36 #include "render.h"
37 #include "config.h"
38 #include "game.h"
39 #include "player.h"
40 #include "sound.h"
41 #include "music.h"
42 #include "input.h"
44 #include <sys/stat.h>
46 #define QSND_NUM 14
48 enum{HIT100,ARMOR,JUMP,WPNS,IMMORTAL,SPEED,OPEN,EXIT};
50 static byte panim[] = "BBDDAACCDDAABBDDAACCDDAABBDDAACCDDAAEEEEEFEFEFEFEFEFEFEFEFEFEEEEE";
51 byte *panimp = panim;
52 byte _warp;
54 byte pcolortab[PCOLORN] = {
55 0x18, 0x20, 0x40, 0x58, 0x60, 0x70, 0x80, 0xB0, 0xC0, 0xD0
56 };
57 int p1color = 5;
58 int p2color = 4;
60 char ibuf[24];
61 byte input=0;
62 static int icur;
64 enum{MENU,MSG};
65 enum{CANCEL,NEWGAME,LOADGAME,SAVEGAME,OPTIONS,QUITGAME,QUIT,ENDGAME,ENDGM,
66 PLR1,PLR2,COOP,DM,VOLUME,GAMMA,LOAD,SAVE,PLCOLOR,PLCEND,MUSIC,INTERP,
67 SVOLM,SVOLP,MVOLM,MVOLP,GAMMAM,GAMMAP,PL1CM,PL1CP,PL2CM,PL2CP};
69 #ifndef DEMO
70 static int qsnd[QSND_NUM];
71 #endif
73 static char *main_txt[]={
74 "NEW GAME","LOAD GAME","SAVE GAME","OPTIONS","EXIT"
75 },*opt_txt[]={
76 "RESTART","VOLUME","BRIGHTNESS","MUSIC","FULLSCREEN:"
77 },*ngplr_txt[]={
78 "ONE PLAYER","TWO PLAYERS"
79 },*ngdm_txt[]={
80 "COOPERATIVE","DEATHMATCH"
81 },*vol_txt[]={
82 "SOUND","MUSIC"
83 },*plcolor_txt[]={
84 "FIRST","SECOND"
85 },*gamma_txt[]={
86 ""
87 };
89 static byte main_typ[]={
90 NEWGAME,LOADGAME,SAVEGAME,OPTIONS,QUITGAME
91 },ngplr_typ[]={
92 PLR1,PLR2
93 },ngdm_typ[]={
94 COOP,DM
95 },opt_typ[]={
96 ENDGAME,VOLUME,GAMMA,MUSIC,INTERP
97 },quit_typ[]={
98 QUIT,CANCEL
99 },endgm_typ[]={
100 ENDGM,CANCEL
101 },vol_typ[]={
102 SVOLM,MVOLM
103 },plcolor_typ[]={
104 PL1CM,PL2CM
105 },gamma_typ[]={
106 GAMMAM
107 },load_typ[]={
108 LOAD,LOAD,LOAD,LOAD,LOAD,LOAD,LOAD
109 },save_typ[]={
110 SAVE,SAVE,SAVE,SAVE,SAVE,SAVE,SAVE
111 };
113 menu_t main_mnu={
114 MENU,5,0,80,"MENU",main_txt,main_typ
115 },opt_mnu={
116 MENU,5,0,75,"OPTIONS",opt_txt,opt_typ
117 },ngplr_mnu={
118 MENU,2,0,90,"NEW GAME",ngplr_txt,ngplr_typ
119 },ngdm_mnu={
120 MENU,2,0,90,"GAME TYPE",ngdm_txt,ngdm_typ
121 },vol_mnu={
122 MENU,2,0,40,"VOLUME",vol_txt,vol_typ
123 },plcolor_mnu={
124 MENU,2,0,90,"COLOR",plcolor_txt,plcolor_typ
125 },gamma_mnu={
126 MENU,1,0,85,"BRIGHTNESS",gamma_txt,gamma_typ
127 },load_mnu={
128 MENU,7,0,85,"LOAD GAME",NULL,load_typ
129 },save_mnu={
130 MENU,7,0,85,"SAVE GAME",NULL,save_typ
131 },quit1_msg={
132 MSG,0,0,0,"ARE YOU SURE?",NULL,quit_typ
133 },quit2_msg={
134 MSG,0,0,0,"ARE YOU SURE?",NULL,quit_typ
135 },quit3_msg={
136 MSG,0,0,0,"ARE YOU SURE?",NULL,quit_typ
137 },endgm_msg={
138 MSG,0,0,0,"RESTART LEVEL?",NULL,endgm_typ
139 };
141 static menu_t *qmsg[3]={&quit1_msg,&quit2_msg,&quit3_msg};
143 menu_t *mnu=NULL;
144 byte gm_redraw=0;
146 short lastkey=0;
147 static void *csnd1,*csnd2,*msnd1,*msnd2,*msnd3,*msnd4,*msnd5,*msnd6;
148 static int movsndt=0;
149 static byte cbuf[32];
151 static snd_t *voc=NULL;
152 static int voc_ch=0;
154 static void GMV_stop (void) {
155 if(voc) {
156 if(voc_ch) {S_stop(voc_ch);voc_ch=0;}
157 free(voc);voc=NULL;
161 void GMV_say (char *nm) {
162 // TODO fix this
163 /*
164 int r,len;
165 snd_t *p;
166 byte *d;
168 if((r=F_findres(nm))==-1) return;
169 if(!(p=malloc((len=F_getreslen(r))+16))) return;
170 p->len=len;p->rate=11000;
171 p->lstart=p->llen=0;
172 GMV_stop();
173 F_loadres(r,p+1,0,len);
174 for(d=(byte*)(p+1);len;--len,++d) *d^=128;
175 voc=p;
176 voc_ch=S_play(voc,-1,1024,255);
177 */
180 static void GM_set (menu_t *m) {
181 mnu=m;gm_redraw=1;
182 if(g_st==GS_GAME) {
183 //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);
184 //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();}
185 //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();}
186 //pl1.drawst=pl2.drawst=0xFF;V_setrect(0,SCRW,0,SCRH);//V_setrect(0,320,0,200);
190 void G_code (void) {
191 void *s;
192 s=csnd2;
193 if(memcmp(cbuf+32-5,"IDDQD",5)==0) {
194 PL_hit(&pl1,400,0,HIT_SOME);
195 if(_2pl) PL_hit(&pl2,400,0,HIT_SOME);
196 s=csnd1;
197 }else if(memcmp(cbuf+32-4,"TANK",4)==0) {
198 pl1.life=pl1.armor=200;pl1.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;
199 if(_2pl) {pl2.life=pl2.armor=200;pl2.drawst|=PL_DRAWARMOR|PL_DRAWLIFE;}
200 }else if(memcmp(cbuf+32-8,"BULLFROG",8)==0) {
201 PL_JUMP=(PL_JUMP==10)?20:10;
202 }else if(memcmp(cbuf+32-8,"FORMULA1",8)==0) {
203 PL_RUN=(PL_RUN==8)?24:8;
204 }else if(memcmp(cbuf+32-5,"RAMBO",5)==0) {
205 pl1.ammo=pl1.shel=pl1.rock=pl1.cell=pl1.fuel=30000;
206 pl1.wpns=0x7FF;pl1.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
207 pl1.keys=0x70;
208 if(_2pl) {
209 pl2.ammo=pl2.shel=pl2.rock=pl2.cell=pl1.fuel=30000;
210 pl2.wpns=0x7FF;pl2.drawst|=PL_DRAWWPN|PL_DRAWKEYS;
211 pl2.keys=0x70;
213 }else if(memcmp(cbuf+32-5,"UJHTW",5)==0) {
214 p_immortal=!p_immortal;
215 }else if(memcmp(cbuf+32-9,",TKSQJHTK",9)==0) {
216 p_fly=!p_fly;
217 }else if(memcmp(cbuf+32-6,"CBVCBV",6)==0) {
218 SW_cheat_open();
219 }else if(memcmp(cbuf+32-7,"GOODBYE",7)==0) {
220 g_exit=1;
221 }else if(memcmp(cbuf+32-9,"GJITKYF",7)==0) {
222 if(cbuf[30]>='0' && cbuf[30]<='9' && cbuf[31]>='0' && cbuf[31]<='9') {
223 g_map=(cbuf[30]=='0')?0:(cbuf[30]-'0')*10;
224 g_map+=(cbuf[31]=='0')?0:(cbuf[31]-'0');
225 G_start();
227 }else return;
228 memset(cbuf,0,32);
229 Z_sound(s,128);
232 static void GM_command (int c) {
233 switch(c) {
234 case CANCEL:
235 GM_set(NULL);break;
236 case INTERP:
237 R_toggle_fullscreen();
238 GM_set(mnu);
239 break;
240 case MUSIC:
241 F_freemus();
242 F_nextmus(g_music);
243 F_loadmus(g_music);
244 S_startmusic(music_time*2);
245 GM_set(mnu);
246 break;
247 case NEWGAME:
248 GMV_say("_NEWGAME");
249 GM_set(&ngplr_mnu);break;
250 case PLR2:
251 GMV_say("_2PLAYER");
252 GM_set(&ngdm_mnu);break;
253 case PLR1:
254 GMV_say("_1PLAYER");
255 ngdm_mnu.cur=0;
256 case COOP: case DM:
257 if(c==COOP) GMV_say("_COOP");
258 else if(c==DM) GMV_say("_DM");
259 if(c!=PLR1) {GM_set(&plcolor_mnu);break;}
260 case PLCEND:
261 _2pl=ngplr_mnu.cur;
262 g_dm=ngdm_mnu.cur;
263 g_map=(_warp)?_warp:1;
264 PL_reset();
265 if(_2pl) {
266 pl1.color=pcolortab[p1color];
267 pl2.color=pcolortab[p2color];
268 }else pl1.color=0x70;
269 G_start();
270 GM_set(NULL);break;
271 case OPTIONS:
272 GMV_say("_RAZNOE");
273 GM_set(&opt_mnu);break;
274 case LOADGAME:
275 GMV_say("_OLDGAME");
276 F_getsavnames();GM_set(&load_mnu);break;
277 case SAVEGAME:
278 if(g_st!=GS_GAME) break;
279 GMV_say("_SAVEGAM");
280 F_getsavnames();GM_set(&save_mnu);break;
281 case SAVE:
282 input=1;memcpy(ibuf,savname[save_mnu.cur],24);icur=strlen(ibuf);
283 GM_set(mnu);break;
284 case LOAD:
285 if(!savok[load_mnu.cur]) break;
286 load_game(load_mnu.cur);
287 GM_set(NULL);break;
288 case VOLUME:
289 GMV_say("_VOLUME");
290 GM_set(&vol_mnu);break;
291 case GAMMA:
292 GMV_say("_GAMMA");
293 GM_set(&gamma_mnu);break;
294 case QUITGAME:
295 GMV_say((rand()&1)?"_EXIT1":"_EXIT2");
296 GM_set(qmsg[myrand(3)]);break;
297 case ENDGAME:
298 if(g_st!=GS_GAME) break;
299 GMV_say("_RESTART");
300 GM_set(&endgm_msg);break;
301 case QUIT:
302 F_freemus();
303 GMV_stop();
304 #ifndef DEMO
305 c = Z_sound(S_get(qsnd[myrand(QSND_NUM)]), 255);
306 S_wait();
307 #endif
308 ERR_quit();break;
309 case ENDGM:
310 PL_reset();G_start();
311 GM_set(NULL);break;
312 case PL1CM:
313 if(--p1color<0) p1color=PCOLORN-1; break;
314 case PL1CP:
315 if(++p1color>=PCOLORN) p1color=0; break;
316 case PL2CM:
317 if(--p2color<0) p2color=PCOLORN-1; break;
318 case PL2CP:
319 if(++p2color>=PCOLORN) p2color=0; break;
320 case SVOLM:
321 S_volume(snd_vol-8);break;
322 case SVOLP:
323 S_volume(snd_vol+8);break;
324 case MVOLM:
325 S_volumemusic(mus_vol-8);break;
326 case MVOLP:
327 S_volumemusic(mus_vol+8);break;
328 case GAMMAM:
329 R_setgamma(R_getgamma() - 1);
330 break;
331 case GAMMAP:
332 R_setgamma(R_getgamma() + 1);
333 break;
337 struct {
338 int keysym;
339 byte ch;
340 } keychar[] = {
341 {KEY_SPACE, ' '},
342 {KEY_0, '0'},
343 {KEY_1, '1'},
344 {KEY_2, '2'},
345 {KEY_3, '3'},
346 {KEY_4, '4'},
347 {KEY_5, '5'},
348 {KEY_6, '6'},
349 {KEY_7, '7'},
350 {KEY_8, '8'},
351 {KEY_9, '9'},
352 //{KEY_UNDERSCORE, '_'},
353 {KEY_A, 'A'},
354 {KEY_B, 'B'},
355 {KEY_C, 'C'},
356 {KEY_D, 'D'},
357 {KEY_E, 'E'},
358 {KEY_F, 'F'},
359 {KEY_G, 'G'},
360 {KEY_H, 'H'},
361 {KEY_I, 'I'},
362 {KEY_J, 'J'},
363 {KEY_K, 'K'},
364 {KEY_L, 'L'},
365 {KEY_M, 'M'},
366 {KEY_N, 'N'},
367 {KEY_O, 'O'},
368 {KEY_P, 'P'},
369 {KEY_Q, 'Q'},
370 {KEY_R, 'R'},
371 {KEY_S, 'S'},
372 {KEY_T, 'T'},
373 {KEY_U, 'U'},
374 {KEY_V, 'V'},
375 {KEY_W, 'W'},
376 {KEY_X, 'X'},
377 {KEY_Y, 'Y'},
378 {KEY_Z, 'Z'},
379 {KEY_COMMA,','},
380 {0}
381 };
383 static byte get_keychar (int keysym) {
384 int i = 0;
385 while (keychar[i].keysym) {
386 if (keychar[i].keysym == keysym) return keychar[i].ch;
387 i++;
389 return 0;
392 static void shot (void) {
393 /*
394 static int num=1;
395 char fn[100];//...
396 #ifndef WIN32
397 char *e = getenv("HOME");
398 strncpy(fn, e, 60);
399 sprintf(&fn[strlen(fn)],"/.doom2d-rembo",num);
400 mkdir(fn, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
401 sprintf(&fn[strlen(fn)],"/shot%04d.bmp",num);
402 #else
403 sprintf(fn,"shot%04d.bmp",num);
404 #endif
405 SDL_SaveBMP(screen, fn);
406 ++num;
407 */
410 int GM_act (void) {
411 byte c;
413 if(mnu==&plcolor_mnu) {
414 if(*(++panimp)==0) panimp=panim;
415 GM_set(mnu);
417 if(movsndt>0) --movsndt; else movsndt=0;
418 if(g_st==GS_TITLE) if(!mnu) if(lastkey) {
419 GM_set(&main_mnu);Z_sound(msnd3,128);
420 lastkey=0;
421 return 1;
423 if (input) {
424 switch (lastkey) {
425 case KEY_RETURN:
426 case KEY_KP_ENTER:
427 F_savegame(save_mnu.cur, ibuf);
428 input = 0;
429 GM_set(NULL);
430 break;
431 case KEY_ESCAPE:
432 input = 0;
433 GM_set(mnu);
434 break;
435 case KEY_BACKSPACE:
436 if (icur) {
437 icur -= 1;
438 ibuf[icur] = 0;
439 GM_set(mnu);
441 break;
442 default:
443 if (icur < 23) {
444 c = get_keychar(lastkey);
445 if (c != 0) {
446 ibuf[icur] = c;
447 icur += 1;
448 ibuf[icur] = 0;
449 GM_set(mnu);
452 break;
454 } else {
455 switch (lastkey) {
456 case KEY_ESCAPE:
457 if (mnu == NULL) {
458 GM_set(&main_mnu);
459 Z_sound(msnd3, 128);
460 } else {
461 GM_set(NULL);
462 Z_sound(msnd4, 128);
464 break;
465 case KEY_F5:
466 if (mnu == NULL) {
467 Z_sound(msnd3, 128);
468 GMV_say("_GAMMA");
469 GM_set(&gamma_mnu);
471 break;
472 case KEY_F4:
473 if (mnu == NULL) {
474 Z_sound(msnd3, 128);
475 GMV_say("_VOLUME");
476 GM_set(&vol_mnu);
478 break;
479 case KEY_F2:
480 if (mnu == NULL && g_st == GS_GAME) {
481 Z_sound(msnd3, 128);
482 F_getsavnames();
483 GM_set(&save_mnu);
485 break;
486 case KEY_F3:
487 if (mnu == NULL) {
488 Z_sound(msnd3, 128);
489 F_getsavnames();
490 GM_set(&load_mnu);
492 break;
493 case KEY_F10:
494 if (mnu == NULL) {
495 Z_sound(msnd3, 128);
496 GM_command(QUITGAME);
498 break;
499 case KEY_UP:
500 case KEY_KP_8:
501 if (mnu != NULL && mnu->type == MENU) {
502 mnu->cur -= 1;
503 if (mnu->cur < 0) {
504 mnu->cur = mnu->n - 1;
506 GM_set(mnu);
507 Z_sound(msnd1, 128);
509 break;
510 case KEY_DOWN:
511 case KEY_KP_5:
512 case KEY_KP_2:
513 if (mnu != NULL && mnu->type == MENU) {
514 mnu->cur += 1;
515 if (mnu->cur >= mnu->n) {
516 mnu->cur = 0;
518 GM_set(mnu);
519 Z_sound(msnd1, 128);
521 break;
522 case KEY_LEFT:
523 case KEY_RIGHT:
524 case KEY_KP_4:
525 case KEY_KP_6:
526 if (mnu != NULL && mnu->type == MENU && mnu->t[mnu->cur] >= SVOLM) {
527 GM_command(mnu->t[mnu->cur] + (lastkey == KEY_LEFT || lastkey == KEY_KP_4));
528 GM_set(mnu);
529 if (!movsndt) {
530 movsndt = Z_sound(lastkey == KEY_LEFT || lastkey == KEY_KP_4 ? msnd5 : msnd6, 255);
533 break;
534 case KEY_RETURN:
535 case KEY_SPACE:
536 case KEY_KP_ENTER:
537 if (mnu != NULL && mnu->type == MENU) {
538 if (mnu->t[mnu->cur] >= PL1CM) {
539 Z_sound(msnd2, 128);
540 GM_command(PLCEND);
541 break;
542 } else if (mnu->t[mnu->cur] < SVOLM) {
543 Z_sound(msnd2,128);
544 GM_command(mnu->t[mnu->cur]);
547 break;
548 case KEY_Y:
549 if (mnu != NULL && mnu->type == MSG) {
550 Z_sound(msnd3, 128);
551 GM_command(mnu->t[0]);
553 break;
554 case KEY_N:
555 if (mnu != NULL && mnu->type == MSG) {
556 Z_sound(msnd4, 128);
557 GM_command(mnu->t[1]);
559 break;
560 case KEY_F1:
561 if (shot_vga) {
562 shot();
563 Z_sound(msnd4, 128);
565 break;
568 lastkey = KEY_UNKNOWN;
569 return mnu ? 1 : 0;
572 void G_keyf (int key, int down) {
573 int i;
574 if (down) {
575 lastkey = key;
576 if (!_2pl || cheat) {
577 for (i = 0; i < 31; ++i) {
578 cbuf[i] = cbuf[i + 1];
580 cbuf[31] = get_keychar(key);
585 void GM_init (void) {
586 #ifndef DEMO
587 int i;
588 static char nm[QSND_NUM][6]={
589 "CYBSIT","KNTDTH","MNPAIN","PEPAIN","SLOP","MANSIT","BOSPN","VILACT",
590 "PLFALL","BGACT","BGDTH2","POPAIN","SGTATK","VILDTH"
591 };
592 char s[8];
594 s[0]='D';s[1]='S';
595 for(i=0;i<QSND_NUM;++i) {
596 memcpy(s+2,nm[i],6);
597 qsnd[i]=F_getresid(s);
599 #endif
600 csnd1=Z_getsnd("HAHA1");
601 csnd2=Z_getsnd("RADIO");
602 msnd1=Z_getsnd("PSTOP");
603 msnd2=Z_getsnd("PISTOL");
604 msnd3=Z_getsnd("SWTCHN");
605 msnd4=Z_getsnd("SWTCHX");
606 msnd5=Z_getsnd("SUDI");
607 msnd6=Z_getsnd("TUDI");
608 F_loadmus("MENU");
609 S_startmusic(0);