FS#8961 - Anti-Aliased Fonts.
[kugel-rb.git] / apps / plugins / doom / rockdoom.c
blob1d065cc9819ff7eba273962a2d7f3ae8ba229577
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 static const char* choice_get_name(int selected_item, void * data,
554 char * buffer, size_t buffer_len)
556 const char **names = (const char **) data;
557 (void) buffer;
558 (void) buffer_len;
559 return names[selected_item];
562 int list_action_callback(int action, struct gui_synclist *lists)
564 (void) lists;
565 if (action == ACTION_STD_OK)
566 return ACTION_STD_CANCEL;
567 return action;
570 bool menuchoice(char **names, int count, int *selected)
572 struct simplelist_info info;
573 rb->simplelist_info_init(&info, NULL, count, (void*)names);
574 info.selection = *selected;
575 info.get_name = choice_get_name;
576 info.action_callback = list_action_callback;
577 if(rb->simplelist_show_list(&info))
578 return true;
580 if(info.selection<count && info.selection>=0)
581 *selected = info.selection;
582 return false;
586 // Doom Menu
588 int doom_menu()
590 int selected=0, result;
591 int status;
592 int gamever;
593 bool menuquit=0;
595 static struct opt_items names[7];
597 MENUITEM_STRINGLIST(menu, "Doom Menu", NULL,
598 "Game", "Addons", "Demos",
599 "Options", "Play Game", "Quit");
601 if( (status=Dbuild_base(names)) == 0 ) // Build up the base wad files (select last added file)
603 rb->splash(HZ*2, "Missing Base WAD!");
604 return -2;
607 int numadd=Dbuild_filelistm(&addons, "No Addon", GAMEBASE"addons/", ".WAD" );
609 int numdemos=Dbuild_filelistm(&demolmp, "No Demo", GAMEBASE"demos/", ".LMP" );
611 argvlist.demonum=0;
612 argvlist.addonnum=0;
614 gamever=status-1;
616 /* Clean out the button Queue */
617 while (rb->button_get(false) != BUTTON_NONE)
618 rb->yield();
620 while(!menuquit)
622 result = rb->do_menu(&menu, &selected, NULL, false);
623 switch (result) {
624 case 0: /* Game picker */
625 rb->set_option("Game WAD", &gamever, INT, names, status, NULL );
626 break;
628 case 1: /* Addon picker */
629 menuchoice(addons,numadd,&argvlist.addonnum);
630 break;
632 case 2: /* Demos */
633 menuchoice(demolmp,numdemos,&argvlist.demonum);
634 break;
636 case 3: /* Options */
637 Doptions();
638 break;
640 case 4: /* Play Game */
641 menuquit=1;
642 break;
644 case 5: /* Quit */
645 menuquit=1;
646 gamever=-1;
647 break;
649 default:
650 break;
654 return (gamever);
657 extern int systemvol;
658 /* this is the plugin entry point */
659 enum plugin_status plugin_start(const void* parameter)
661 PLUGIN_IRAM_INIT(rb)
663 (void)parameter;
665 doomexit=0;
667 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
668 rb->cpu_boost(true);
669 #endif
671 rb->lcd_setfont(0);
673 // We're using doom's memory management since it implements a proper free (and re-uses the memory)
674 // and now with prboom's code: realloc and calloc
675 printf ("Z_Init: Init zone memory allocation daemon.\n");
676 Z_Init ();
678 printf ("M_LoadDefaults: Load system defaults.\n");
679 M_LoadDefaults (); // load before initing other systems
681 rb->splash(HZ*2, "Welcome to RockDoom");
683 myargv =0;
684 myargc=0;
686 rb->lcd_clear_display();
688 int result = doom_menu();
689 if (result < 0)
691 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
692 rb->cpu_boost(false);
693 #endif
694 if( result == -1 )
695 return PLUGIN_OK; // Quit was selected
696 else
697 return PLUGIN_ERROR; // Missing base wads
700 #if(LCD_HEIGHT>LCD_WIDTH)
701 if(rotate_screen)
703 SCREENHEIGHT=LCD_WIDTH;
704 SCREENWIDTH=LCD_HEIGHT;
706 else
708 SCREENHEIGHT=LCD_HEIGHT;
709 SCREENWIDTH=LCD_WIDTH;
711 #endif
713 Dhandle_ver( namemap[ result ] );
715 rb->lcd_setfont(0);
717 rb->lcd_clear_display();
719 int mod = (rb->sound_max(SOUND_VOLUME)-rb->sound_min(SOUND_VOLUME))/15;
720 if(mod == 0)
721 mod = rb->global_settings->volume;
722 systemvol= rb->global_settings->volume-rb->global_settings->volume%mod;
723 general_translucency = default_translucency; // phares
725 backlight_force_on();
726 #ifdef RB_PROFILE
727 rb->profile_thread();
728 #endif
730 #if LCD_DEPTH>1
731 rb->lcd_set_backdrop(NULL);
732 #endif
734 D_DoomMain ();
736 #ifdef RB_PROFILE
737 rb->profstop();
738 #endif
739 backlight_use_settings();
741 M_SaveDefaults ();
743 I_Quit(); // Make SURE everything was closed out right
745 printf("There were still: %d files open\n", fpoint);
746 while(fpoint>0)
748 #ifdef SIMULATOR
749 close(filearray[fpoint]);
750 #else
751 rb->close(filearray[fpoint]);
752 #endif
753 fpoint--;
756 #ifdef HAVE_ADJUSTABLE_CPU_FREQ
757 rb->cpu_boost(false);
758 #endif
760 return PLUGIN_OK;