Fix FS#10486 - "fuze screen not drawn correctly on backlight turn-on if playlist...
[kugel-rb/myfork.git] / apps / plugins / doom / rockdoom.c
blobca08ec1665c8b49f3ef031c4a395259f6458c57d
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
9 * Copyright (C) 2005 Karl Kurbjun
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
17 * KIND, either express or implied.
19 * H300 Port by Karl Kurbjun
20 * IPod port by Dave Chapman and Paul Louden
21 * Additional code contributed by Thom Johansen
22 * Based off work by: Digita Doom, IDoom, Prboom, lSDLDoom, LxDoom,
23 * MBF, Boom, DosDoom,
24 * and of course Original Doom by ID Software
25 * See: http://prboom.sourceforge.net/about.html for the history
28 ****************************************************************************/
30 #include "d_main.h"
31 #include "doomdef.h"
32 #include "settings.h"
33 #include "m_fixed.h"
34 #include "m_argv.h"
35 #include "m_misc.h"
36 #include "g_game.h"
37 #include "rockmacros.h"
38 #include "doomstat.h"
39 #include "i_system.h"
40 #include "hu_stuff.h"
41 #include "st_stuff.h"
42 #include "lib/helper.h"
44 PLUGIN_HEADER
45 PLUGIN_IRAM_DECLARE
47 extern boolean timingdemo, singledemo, demoplayback, fastdemo; // killough
49 int filearray[9];
50 int fpoint=1; // save 0 for closing
52 int fileexists(const char * fname)
54 int fd;
55 fd = open(fname,O_RDONLY);
57 if (fd>=0)
59 close(fd);
60 return 0;
62 return -1;
65 #ifndef SIMULATOR
66 int my_open(const char *file, int flags)
68 if(fpoint==8)
69 return -1;
70 #undef open
71 filearray[fpoint]=rb->open(file, flags);
73 if(filearray[fpoint]<0)
74 return filearray[fpoint];
76 fpoint++;
77 return filearray[fpoint-1];
80 int my_close(int id)
82 int i=0;
83 if(id<0)
84 return id;
85 while(filearray[i]!=id && i<8)
86 i++;
88 if(i==8)
90 printf("A requested FID did not exist!!!!");
91 return -9;
93 #undef close
94 rb->close(id);
96 for(; i<fpoint-1; i++)
97 filearray[i]=filearray[i+1];
99 fpoint--;
100 return 0;
102 #endif
103 #define MAXARGVS 100
105 bool noprintf=0; // Variable disables printf lcd updates to protect grayscale lib/direct lcd updates
107 #ifndef SIMULATOR
108 // Here is a hacked up printf command to get the output from the game.
109 int printf(const char *fmt, ...)
111 static int p_xtpt;
112 char p_buf[50];
113 bool ok;
114 rb->yield();
115 va_list ap;
117 va_start(ap, fmt);
118 ok = vsnprintf(p_buf,sizeof(p_buf), fmt, ap);
119 va_end(ap);
121 rb->lcd_putsxy(1,p_xtpt, (unsigned char *)p_buf);
122 if (!noprintf)
123 rb->lcd_update();
125 p_xtpt+=8;
126 if(p_xtpt>LCD_HEIGHT-8)
128 p_xtpt=0;
129 if (!noprintf)
130 rb->lcd_clear_display();
132 return 1;
134 #endif
136 char *my_strtok( char * s, const char * delim )
138 register char *spanp;
139 register int c, sc;
140 char *tok;
141 static char *lasts;
144 if (s == NULL && (s = lasts) == NULL)
145 return (NULL);
148 * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
150 cont:
151 c = *s++;
152 for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
153 if (c == sc)
154 goto cont;
157 if (c == 0) { /* no non-delimiter characters */
158 lasts = NULL;
159 return (NULL);
161 tok = s - 1;
164 * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
165 * Note that delim must have one NUL; we stop if we see that, too.
167 for (;;) {
168 c = *s++;
169 spanp = (char *)delim;
170 do {
171 if ((sc = *spanp++) == c) {
172 if (c == 0)
173 s = NULL;
174 else
175 s[-1] = 0;
176 lasts = s;
177 return (tok);
179 } while (sc != 0);
181 /* NOTREACHED */
184 inline void* memcpy(void* dst, const void* src, size_t size)
186 return rb->memcpy(dst, src, size);
189 struct argvlist
191 int timedemo; // 1 says there's a timedemo
192 int demonum;
193 int addonnum;
194 } argvlist;
196 const unsigned char versions_builtin[7][20] =
198 "Doom Shareware",
199 "Doom Registered",
200 "Ultimate Doom",
201 "Doom 2",
202 "Freedoom",
203 "Plutonia",
204 "TNT"
207 const unsigned char wads_builtin[7][30] =
209 GAMEBASE"doom1.wad",
210 GAMEBASE"doom.wad",
211 GAMEBASE"doomu.wad",
212 GAMEBASE"doom2.wad",
213 GAMEBASE"doomf.wad",
214 GAMEBASE"plutonia.wad",
215 GAMEBASE"tnt.wad"
218 int namemap[7];
219 static char **addons;
220 static char **demolmp;
221 char addon[200];
222 // This sets up the base game and builds up myargv/c
223 bool Dhandle_ver (int dver)
225 switch (dver) {
226 case 0: /* Doom Shareware */
227 gamemode = shareware;
228 gamemission = doom;
229 D_AddFile(wads_builtin[0],source_iwad);
230 break;
231 case 1: /* Doom registered */
232 gamemode = registered;
233 gamemission = doom;
234 D_AddFile(wads_builtin[1],source_iwad);
235 break;
236 case 2: /* Ultimate Doom */
237 gamemode = retail;
238 gamemission = doom;
239 D_AddFile(wads_builtin[2],source_iwad);
240 break;
241 case 3: /* Doom2 */
242 gamemode = commercial;
243 gamemission = doom2;
244 D_AddFile(wads_builtin[3],source_iwad);
245 break;
246 case 4: /* Doom2f */
247 gamemode = commercial;
248 gamemission = doom2;
249 D_AddFile(wads_builtin[4],source_iwad);
250 break;
251 case 5: /* Plutonia */
252 gamemode = commercial;
253 gamemission = pack_plut;
254 D_AddFile(wads_builtin[5],source_iwad);
255 break;
256 case 6: /* TNT */
257 gamemode = commercial;
258 gamemission = pack_tnt;
259 D_AddFile(wads_builtin[6],source_iwad);
260 break;
261 default:
262 gamemission = none;
263 return 0;
265 // Start adding to myargv
266 if(argvlist.timedemo && (gamemode == shareware))
268 singletics = true;
269 timingdemo = true; // show stats after quit
270 G_DeferedPlayDemo("demo3");
271 singledemo = true; // quit after one demo
274 if(argvlist.addonnum)
276 snprintf(addon,sizeof(addon),"%s%s", GAMEBASE"addons/", addons[argvlist.addonnum]);
277 D_AddFile(addon,source_pwad);
278 modifiedgame = true;
281 if(argvlist.demonum)
283 snprintf(addon, sizeof(addon),"%s%s", GAMEBASE"demos/", demolmp[argvlist.demonum]);
284 D_AddFile(addon, source_lmp);
285 G_DeferedPlayDemo(addon);
286 singledemo = true; // quit after one demo
288 return 1;
291 // This function builds up the basegame list for use in the options selection
292 // it also sets the defaults for the argvlist
293 // Now checking for rcokdoom.wad based on prboom.wad
294 int Dbuild_base (struct opt_items *names)
296 if ( fileexists(GAMEBASE"rockdoom.wad") )
297 return 0;
299 D_AddFile (GAMEBASE"rockdoom.wad", source_pwad);
301 int i=0, j;
302 /* Doom Shareware */
303 /* Doom registered */
304 /* Ultimate Doom */
305 /* Doom2 */
306 /* Doom2f */
307 /* Plutonia */
308 /* TNT */
309 for(j=0;j<7;j++)
310 if ( !fileexists (wads_builtin[j]) )
312 names[i].string=versions_builtin[j];
313 names[i].voice_id=-1;
314 namemap[i]=j;
315 i++;
317 // Set argvlist defaults
318 argvlist.timedemo=0;
320 return i;
323 // This is a general function that takes in a menu_item structure and makes a list
324 // of files within it based on matching the string stringmatch to the files.
325 int Dbuild_filelistm(char ***names, char *firstentry, char *directory, char *stringmatch)
327 int i=0;
328 DIR *filedir;
329 struct dirent *dptr;
330 char *startpt;
331 char **temp;
333 filedir=rb->opendir(directory);
335 if(filedir==NULL)
337 temp=malloc(sizeof(char *));
338 temp[0]=firstentry;
339 *names=temp;
340 return 1;
343 // Get the total number of entries
344 while((dptr=rb->readdir(filedir)))
345 i++;
347 // Reset the directory
348 rb->closedir(filedir);
349 filedir=rb->opendir(directory);
351 i++;
352 temp=malloc(i*sizeof(char *));
353 temp[0]=firstentry;
354 i=1;
356 while((dptr=rb->readdir(filedir)))
358 if(rb->strcasestr(dptr->d_name, stringmatch))
360 startpt=malloc(strlen(dptr->d_name)*sizeof(char));
361 strcpy(startpt,dptr->d_name);
362 temp[i]=startpt;
363 i++;
366 rb->closedir(filedir);
367 *names=temp;
368 return i;
371 static int translatekey(int key) __attribute__ ((noinline));
373 // This key configuration code is not the cleanest or the most efficient, but it works
374 static int translatekey(int key)
376 if (key<31)
378 switch(key)
380 case 0:
381 return 0;
382 case 1:
383 return KEY_RIGHTARROW;
384 case 2:
385 return KEY_LEFTARROW;
386 case 3:
387 return KEY_UPARROW;
388 case 4:
389 return KEY_DOWNARROW;
390 case 5:
391 return KEY_ENTER;
392 case 6:
393 return KEY_RCTRL;
394 case 7:
395 return ' ';
396 case 8:
397 return KEY_ESCAPE;
398 case 9:
399 return 'w';
400 case 10:
401 return KEY_TAB;
402 default:
403 return 0;
406 else
408 switch(key)
410 case 0:
411 return 0;
412 case KEY_RIGHTARROW:
413 return 1;
414 case KEY_LEFTARROW:
415 return 2;
416 case KEY_UPARROW:
417 return 3;
418 case KEY_DOWNARROW:
419 return 4;
420 case KEY_ENTER:
421 return 5;
422 case KEY_RCTRL:
423 return 6;
424 case ' ':
425 return 7;
426 case KEY_ESCAPE:
427 return 8;
428 case 'w':
429 return 9;
430 case KEY_TAB:
431 return 10;
432 default:
433 return 0;
438 // I havn't added configurable keys for enter or escape because this requires some modification to
439 // m_menu.c which hasn't been done yet.
441 int Oset_keys()
443 int selected=0, result;
444 int menuquit=0;
447 static const struct opt_items doomkeys[] = {
448 { "Unmapped", -1 },
449 { "Key Right", -1 },
450 { "Key Left", -1 },
451 { "Key Up", -1 },
452 { "Key Down", -1 },
453 { "Key Select", -1 },
454 #if defined(TOSHIBA_GIGABEAT_F)
455 { "Key A", -1 },
456 { "Key Menu", -1 },
457 { "Key Power", -1 },
458 { "Key Volume Down", -1 },
459 { "Key Volume Up", -1 },
460 #else
461 { "Key Record", -1 },
462 { "Key Mode", -1 },
463 { "Key Off", -1 },
464 { "Key On", -1 },
465 #endif
468 int *keys[]={
469 &key_right,
470 &key_left,
471 &key_up,
472 &key_down,
473 &key_fire,
474 &key_use,
475 &key_strafe,
476 &key_weapon,
477 &key_map
480 int numdoomkeys=sizeof(doomkeys) / sizeof(*doomkeys);
482 MENUITEM_STRINGLIST(menu, "Set Keys", NULL,
483 "Game Right", "Game Left", "Game Up", "Game Down",
484 "Game Shoot", "Game Open", "Game Strafe",
485 "Game Weapon", "Game Automap");
487 while(!menuquit)
489 result = rb->do_menu(&menu, &selected, NULL, false);
490 if(result<0)
491 menuquit=1;
492 else
494 *keys[result]=translatekey(*keys[result]);
495 rb->set_option(menu_[result], keys[result], INT, doomkeys, numdoomkeys, NULL );
496 *keys[result]=translatekey(*keys[result]);
500 return (1);
503 extern int fake_contrast;
505 static bool Doptions()
507 static const struct opt_items onoff[2] = {
508 { "Off", -1 },
509 { "On", -1 },
512 int selected=0, result;
513 int menuquit=0;
515 MENUITEM_STRINGLIST(menu, "Options", NULL,
516 "Set Keys", "Sound", "Timedemo", "Player Bobbing",
517 "Weapon Recoil", "Translucency", "Fake Contrast",
518 "Always Run", "Headsup Display", "Statusbar Always Red",
519 #if(LCD_HEIGHT>LCD_WIDTH)
520 "Rotate Screen 90 deg",
521 #endif
524 void *options[]={
525 &enable_sound,
526 &argvlist.timedemo,
527 &default_player_bobbing,
528 &default_weapon_recoil,
529 &default_translucency,
530 &fake_contrast,
531 &autorun,
532 &hud_displayed,
533 &sts_always_red,
534 #if(LCD_HEIGHT>LCD_WIDTH)
535 &rotate_screen,
536 #endif
539 while(!menuquit)
541 result = rb->do_menu(&menu, &selected, NULL, false);
542 if(result==0)
543 Oset_keys();
544 else if (result > 0)
545 rb->set_option(menu_[result], options[result-1], INT, onoff, 2, NULL );
546 else
547 menuquit=1;
550 return (1);
553 char* choice_get_name(int selected_item, void * data,
554 char * buffer, size_t buffer_len)
556 char **names = (char **) data;
557 (void) buffer;
558 (void) buffer_len;
559 return names[selected_item];
561 int list_action_callback(int action, struct gui_synclist *lists)
563 (void) lists;
564 if (action == ACTION_STD_OK)
565 return ACTION_STD_CANCEL;
566 return action;
568 bool menuchoice(char **names, int count, int *selected)
570 struct simplelist_info info;
571 rb->simplelist_info_init(&info, NULL, count, (void*)names);
572 info.selection = *selected;
573 info.get_name = choice_get_name;
574 info.action_callback = list_action_callback;
575 if(rb->simplelist_show_list(&info))
576 return true;
578 if(info.selection<count && info.selection>=0)
579 *selected = info.selection;
580 return false;
584 // Doom Menu
586 int doom_menu()
588 int selected=0, result;
589 int status;
590 int gamever;
591 bool menuquit=0;
593 static struct opt_items names[7];
595 MENUITEM_STRINGLIST(menu, "Doom Menu", NULL,
596 "Game", "Addons", "Demos",
597 "Options", "Play Game", "Quit");
599 if( (status=Dbuild_base(names)) == 0 ) // Build up the base wad files (select last added file)
601 rb->splash(HZ*2, "Missing Base WAD!");
602 return -2;
605 int numadd=Dbuild_filelistm(&addons, "No Addon", GAMEBASE"addons/", ".WAD" );
607 int numdemos=Dbuild_filelistm(&demolmp, "No Demo", GAMEBASE"demos/", ".LMP" );
609 argvlist.demonum=0;
610 argvlist.addonnum=0;
612 gamever=status-1;
614 /* Clean out the button Queue */
615 while (rb->button_get(false) != BUTTON_NONE)
616 rb->yield();
618 while(!menuquit)
620 result = rb->do_menu(&menu, &selected, NULL, false);
621 switch (result) {
622 case 0: /* Game picker */
623 rb->set_option("Game WAD", &gamever, INT, names, status, NULL );
624 break;
626 case 1: /* Addon picker */
627 menuchoice(addons,numadd,&argvlist.addonnum);
628 break;
630 case 2: /* Demos */
631 menuchoice(demolmp,numdemos,&argvlist.demonum);
632 break;
634 case 3: /* Options */
635 Doptions();
636 break;
638 case 4: /* Play Game */
639 menuquit=1;
640 break;
642 case 5: /* Quit */
643 menuquit=1;
644 gamever=-1;
645 break;
647 default:
648 break;
652 return (gamever);
655 extern int systemvol;
656 /* this is the plugin entry point */
657 enum plugin_status plugin_start(const void* parameter)
659 PLUGIN_IRAM_INIT(rb)
661 (void)parameter;
663 doomexit=0;
665 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
666 rb->cpu_boost(true);
667 #endif
669 rb->lcd_setfont(0);
671 // We're using doom's memory management since it implements a proper free (and re-uses the memory)
672 // and now with prboom's code: realloc and calloc
673 printf ("Z_Init: Init zone memory allocation daemon.\n");
674 Z_Init ();
676 printf ("M_LoadDefaults: Load system defaults.\n");
677 M_LoadDefaults (); // load before initing other systems
679 rb->splash(HZ*2, "Welcome to RockDoom");
681 myargv =0;
682 myargc=0;
684 rb->lcd_clear_display();
686 int result = doom_menu();
687 if (result < 0)
689 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
690 rb->cpu_boost(false);
691 #endif
692 if( result == -1 )
693 return PLUGIN_OK; // Quit was selected
694 else
695 return PLUGIN_ERROR; // Missing base wads
698 #if(LCD_HEIGHT>LCD_WIDTH)
699 if(rotate_screen)
701 SCREENHEIGHT=LCD_WIDTH;
702 SCREENWIDTH=LCD_HEIGHT;
704 else
706 SCREENHEIGHT=LCD_HEIGHT;
707 SCREENWIDTH=LCD_WIDTH;
709 #endif
711 Dhandle_ver( namemap[ result ] );
713 rb->lcd_setfont(0);
715 rb->lcd_clear_display();
717 int mod = (rb->sound_max(SOUND_VOLUME)-rb->sound_min(SOUND_VOLUME))/15;
718 if(mod == 0)
719 mod = rb->global_settings->volume;
720 systemvol= rb->global_settings->volume-rb->global_settings->volume%mod;
721 general_translucency = default_translucency; // phares
723 backlight_force_on();
724 #ifdef RB_PROFILE
725 rb->profile_thread();
726 #endif
728 #if LCD_DEPTH>1
729 rb->lcd_set_backdrop(NULL);
730 #endif
732 D_DoomMain ();
734 #ifdef RB_PROFILE
735 rb->profstop();
736 #endif
737 backlight_use_settings();
739 M_SaveDefaults ();
741 I_Quit(); // Make SURE everything was closed out right
743 printf("There were still: %d files open\n", fpoint);
744 while(fpoint>0)
746 #ifdef SIMULATOR
747 close(filearray[fpoint]);
748 #else
749 rb->close(filearray[fpoint]);
750 #endif
751 fpoint--;
754 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
755 rb->cpu_boost(false);
756 #endif
758 return PLUGIN_OK;