Subversion Repositories Kolibri OS

Rev

Rev 300 | Blame | Last modification | View Log | Download | RSS feed

  1. // Emacs style mode select   -*- C++ -*-
  2. //-----------------------------------------------------------------------------
  3. //
  4. // $Id:$
  5. //
  6. // Copyright (C) 1993-1996 by id Software, Inc.
  7. //
  8. // This source is available for distribution and/or modification
  9. // only under the terms of the DOOM Source Code License as
  10. // published by id Software. All rights reserved.
  11. //
  12. // The source is distributed in the hope that it will be useful,
  13. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. // FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
  15. // for more details.
  16. //
  17. //
  18. // $Log:$
  19. //
  20. // DESCRIPTION:
  21. //      Main loop menu stuff.
  22. //      Default Config File.
  23. //      PCX Screenshots.
  24. //
  25. //-----------------------------------------------------------------------------
  26.  
  27. static const char
  28. rcsid[] = "$Id: m_misc.c,v 1.6 1997/02/03 22:45:10 b1 Exp $";
  29.  
  30. #include <stdlib.h>
  31. #include <ctype.h>
  32. //#include "//kolibc.h"
  33.  
  34. //extern int access(char *file, int mode);
  35.  
  36. #include "doomdef.h"
  37.  
  38. #include "z_zone.h"
  39.  
  40. #include "m_swap.h"
  41. #include "m_argv.h"
  42.  
  43. #include "w_wad.h"
  44.  
  45. #include "i_system.h"
  46. #include "i_video.h"
  47. #include "v_video.h"
  48.  
  49. #include "hu_stuff.h"
  50.  
  51. // State.
  52. #include "doomstat.h"
  53.  
  54. // Data.
  55. #include "dstrings.h"
  56.  
  57. #include "m_misc.h"
  58.  
  59. //
  60. // M_DrawText
  61. // Returns the final X coordinate
  62. // HU_Init must have been called to init the font
  63. //
  64. extern patch_t*         hu_font[HU_FONTSIZE];
  65.  
  66. int
  67. M_DrawText
  68. ( int           x,
  69.   int           y,
  70.   boolean       direct,
  71.   char*         string )
  72. {
  73.     int         c;
  74.     int         w;
  75.  
  76.     while (*string)
  77.     {
  78.         c = toupper(*string) - HU_FONTSTART;
  79.         string++;
  80.         if (c < 0 || c> HU_FONTSIZE)
  81.         {
  82.             x += 4;
  83.             continue;
  84.         }
  85.                
  86.         w = SHORT (hu_font[c]->width);
  87.         if (x+w > SCREENWIDTH)
  88.             break;
  89.         if (direct)
  90.             V_DrawPatchDirect(x, y, 0, hu_font[c]);
  91.         else
  92.             V_DrawPatch(x, y, 0, hu_font[c]);
  93.         x+=w;
  94.     }
  95.  
  96.     return x;
  97. }
  98.  
  99.  
  100.  
  101.  
  102. //
  103. // M_WriteFile
  104. //
  105. #ifndef O_BINARY
  106. #define O_BINARY 0
  107. #endif
  108.  
  109. boolean
  110. M_WriteFile
  111. ( char const*   name,
  112.   void*         source,
  113.   int           length )
  114. {
  115.     FILE       *handle;
  116.     int         count;
  117.        
  118.     handle = fopen ( name, "wb");
  119.  
  120.     if (handle == NULL)
  121.         return false;
  122.  
  123.   //  count = fwrite (source, 1, length, handle);
  124.     fclose (handle);
  125.        
  126.     if (count < length)
  127.         return false;
  128.                
  129.     return true;
  130. }
  131.  
  132.  
  133. //
  134. // M_ReadFile
  135. //
  136. int
  137. M_ReadFile
  138. ( char const*   name,
  139.   byte**        buffer )
  140. {
  141.     FILE *handle;
  142.     int count, length;
  143.     byte        *buf;
  144.  
  145.     handle=0;
  146.     buf=0;
  147.        
  148.     handle = fopen (name, "rb");
  149.     if (handle == NULL)
  150.         I_Error ("Couldn't read file %s", name);
  151.     fseek(handle, 0, SEEK_END);
  152.     length = ftell(handle);
  153.     rewind(handle);
  154.     buf = Z_Malloc (length, PU_STATIC, NULL);
  155.     count = fread (buf, 1, length, handle);
  156.     fclose (handle);
  157.        
  158.     if (count < length)
  159.         I_Error ("Couldn't read file %s", name);
  160.                
  161.     *buffer = buf;
  162.     return length;
  163. }
  164.  
  165.  
  166. //
  167. // DEFAULTS
  168. //
  169. int             usemouse;
  170. int             usejoystick;
  171.  
  172. extern int      key_right;
  173. extern int      key_left;
  174. extern int      key_up;
  175. extern int      key_down;
  176.  
  177. extern int      key_strafeleft;
  178. extern int      key_straferight;
  179.  
  180. extern int      key_fire;
  181. extern int      key_use;
  182. extern int      key_strafe;
  183. extern int      key_speed;
  184.  
  185. extern int      mousebfire;
  186. extern int      mousebstrafe;
  187. extern int      mousebforward;
  188.  
  189. extern int      joybfire;
  190. extern int      joybstrafe;
  191. extern int      joybuse;
  192. extern int      joybspeed;
  193.  
  194. extern int      viewwidth;
  195. extern int      viewheight;
  196.  
  197. extern int      mouseSensitivity;
  198. extern int      showMessages;
  199.  
  200. extern int      detailLevel;
  201.  
  202. extern int      screenblocks;
  203.  
  204. extern int      showMessages;
  205.  
  206. // machine-independent sound params
  207. extern  int     numChannels;
  208.  
  209.  
  210. extern char*    chat_macros[];
  211.  
  212.  
  213.  
  214. typedef struct
  215. {
  216.     char*       name;
  217.     int*        location;
  218.     int         defaultvalue;
  219.     int         scantranslate;          // PC scan code hack
  220.     int         untranslated;           // lousy hack
  221. } default_t;
  222.  
  223. default_t       defaults[] =
  224. {
  225.     {"mouse_sensitivity",&mouseSensitivity, 5},
  226.     {"sfx_volume",&snd_SfxVolume, 8},
  227.     {"music_volume",&snd_MusicVolume, 8},
  228.     {"show_messages",&showMessages, 1},
  229.    
  230.  
  231.     {"key_right",&key_right, KEY_RIGHTARROW},
  232.     {"key_left",&key_left, KEY_LEFTARROW},
  233.     {"key_up",&key_up, KEY_UPARROW},
  234.     {"key_down",&key_down, KEY_DOWNARROW},
  235.  
  236. //    {"key_right",&key_right, KEY_D},
  237. //    {"key_left",&key_left, KEY_A},
  238. //    {"key_up",&key_up, KEY_W},
  239. //    {"key_down",&key_down, KEY_S},
  240.  
  241.     {"key_strafeleft",&key_strafeleft, ','},
  242.     {"key_straferight",&key_straferight, '.'},
  243.  
  244.     {"key_fire",&key_fire, KEY_RCTRL},
  245.     {"key_use",&key_use, ' '},
  246.     {"key_strafe",&key_strafe, KEY_RALT},
  247.     {"key_speed",&key_speed, KEY_RSHIFT},
  248.  
  249.     {"use_mouse",&usemouse, 1},
  250.     {"mouseb_fire",&mousebfire,0},
  251.     {"mouseb_strafe",&mousebstrafe,1},
  252.     {"mouseb_forward",&mousebforward,2},
  253.  
  254.     {"use_joystick",&usejoystick, 0},
  255.     {"joyb_fire",&joybfire,0},
  256.     {"joyb_strafe",&joybstrafe,1},
  257.     {"joyb_use",&joybuse,3},
  258.     {"joyb_speed",&joybspeed,2},
  259.  
  260.     {"screenblocks",&screenblocks, 9},
  261.     {"detaillevel",&detailLevel, 0},
  262.  
  263.     {"snd_channels",&numChannels, 3},
  264.  
  265.  
  266.  
  267.     {"usegamma",&usegamma, 0},
  268.  
  269. #ifndef __BEOS__
  270.     {"chatmacro0", (int *) &chat_macros[0], (int) HUSTR_CHATMACRO0 },
  271.     {"chatmacro1", (int *) &chat_macros[1], (int) HUSTR_CHATMACRO1 },
  272.     {"chatmacro2", (int *) &chat_macros[2], (int) HUSTR_CHATMACRO2 },
  273.     {"chatmacro3", (int *) &chat_macros[3], (int) HUSTR_CHATMACRO3 },
  274.     {"chatmacro4", (int *) &chat_macros[4], (int) HUSTR_CHATMACRO4 },
  275.     {"chatmacro5", (int *) &chat_macros[5], (int) HUSTR_CHATMACRO5 },
  276.     {"chatmacro6", (int *) &chat_macros[6], (int) HUSTR_CHATMACRO6 },
  277.     {"chatmacro7", (int *) &chat_macros[7], (int) HUSTR_CHATMACRO7 },
  278.     {"chatmacro8", (int *) &chat_macros[8], (int) HUSTR_CHATMACRO8 },
  279.     {"chatmacro9", (int *) &chat_macros[9], (int) HUSTR_CHATMACRO9 }
  280. #endif
  281.  
  282. };
  283.  
  284. int     numdefaults;
  285. char*   defaultfile;
  286.  
  287.  
  288. //
  289. // M_SaveDefaults
  290. //
  291. void M_SaveDefaults (void)
  292. {
  293.     int         i;
  294.     int         v;
  295.     FILE*       f;
  296.        
  297.     f = fopen (defaultfile, "w");
  298.     if (!f)
  299.         return; // can't write the file, but don't complain
  300.                
  301.     for (i=0 ; i<numdefaults ; i++)
  302.     {
  303.         if (defaults[i].defaultvalue > -0xfff
  304.             && defaults[i].defaultvalue < 0xfff)
  305.         {
  306.             v = *defaults[i].location;
  307.             printf ("%s\t\t%i\n",defaults[i].name,v);
  308.         } else {
  309.             printf ("%s\t\t\"%s\"\n",defaults[i].name,
  310.                      * (char **) (defaults[i].location));
  311.         }
  312.     }
  313.        
  314.     fclose (f);
  315. }
  316.  
  317.  
  318. //
  319. // M_LoadDefaults
  320. //
  321. extern byte     scantokey[128];
  322.  
  323. size_t FileSize(FILE *handle);
  324.  
  325. void M_LoadDefaults (void)
  326. {
  327.     int         i;
  328.     int         len;
  329.     FILE*       f;
  330.     char        def[80];
  331.     char        strparm[100];
  332.     char*       newstring;
  333.     int         parm;
  334.     boolean     isstring;
  335.     char        *buf;
  336.     char        *p;
  337.    
  338.     size_t val;
  339.     // set everything to base values
  340.     numdefaults = sizeof(defaults)/sizeof(defaults[0]);
  341.     for (i=0 ; i<numdefaults ; i++)
  342.         *defaults[i].location = defaults[i].defaultvalue;
  343.    
  344.     // check for a custom default file
  345.     i = M_CheckParm ("-config");
  346.     if (i && i<myargc-1)
  347.     {
  348.         defaultfile = myargv[i+1];
  349. //      __libclog_printf ("     default file: %s\n",defaultfile);
  350.     }
  351.     else
  352.         defaultfile = basedefault;
  353.  
  354.     // read the file in, overriding any set defaults
  355.     f = fopen (defaultfile, "rb");
  356.     if (f)
  357.     {
  358.       len=FileSize(f)+1;  
  359.       buf = malloc(len);
  360.       memset(buf,0,len);
  361.       val = fread(buf,1,len,f);
  362.       fclose (f);
  363.      
  364.       p = buf;
  365.      
  366.       while(*p)
  367.       {
  368.         isstring = false;
  369.  
  370.         if (sscanf (p, "%79s %[^\n]\n", def, strparm) == 2)
  371.         {
  372.           if (strparm[0] == '"')
  373.           {
  374.                     // get a string default
  375.             isstring = true;
  376.             len = strlen(strparm);
  377.             newstring = (char *) malloc(len);
  378.             strparm[len-1] = 0;
  379.             strcpy(newstring, strparm+1);
  380.           }
  381.           else
  382.            if (strparm[0] == '0' && strparm[1] == 'x')
  383.              sscanf(strparm+2, "%x", &parm);
  384.            else
  385.              sscanf(strparm, "%i", &parm);
  386.            for (i=0 ; i<numdefaults ; i++)
  387.              if (!strcmp(def, defaults[i].name))
  388.              {
  389.                if (!isstring)
  390.                  *defaults[i].location = parm;
  391.                else
  392.                  *defaults[i].location = (int) newstring;
  393.                break;
  394.              }
  395.         };
  396.         p=strchr(p, '\n')+1;
  397.       };
  398.       free(buf);
  399.     };
  400. };
  401.  
  402.  
  403.  
  404.  
  405. //
  406. // SCREEN SHOTS
  407. //
  408.  
  409.  
  410. typedef struct
  411. {
  412.     char                manufacturer;
  413.     char                version;
  414.     char                encoding;
  415.     char                bits_per_pixel;
  416.  
  417.     unsigned short      xmin;
  418.     unsigned short      ymin;
  419.     unsigned short      xmax;
  420.     unsigned short      ymax;
  421.    
  422.     unsigned short      hres;
  423.     unsigned short      vres;
  424.  
  425.     unsigned char       palette[48];
  426.    
  427.     char                reserved;
  428.     char                color_planes;
  429.     unsigned short      bytes_per_line;
  430.     unsigned short      palette_type;
  431.    
  432.     char                filler[58];
  433.     unsigned char       data;           // unbounded
  434. } pcx_t;
  435.  
  436.  
  437. //
  438. // WritePCXfile
  439. //
  440. void
  441. WritePCXfile
  442. ( char*         filename,
  443.   byte*         data,
  444.   int           width,
  445.   int           height,
  446.   byte*         palette )
  447. {
  448.     int         i;
  449.     int         length;
  450.     pcx_t*      pcx;
  451.     byte*       pack;
  452.        
  453.     pcx = Z_Malloc (width*height*2+1000, PU_STATIC, NULL);
  454.  
  455.     pcx->manufacturer = 0x0a;           // PCX id
  456.     pcx->version = 5;                   // 256 color
  457.     pcx->encoding = 1;                  // uncompressed
  458.     pcx->bits_per_pixel = 8;            // 256 color
  459.     pcx->xmin = 0;
  460.     pcx->ymin = 0;
  461.     pcx->xmax = SHORT(width-1);
  462.     pcx->ymax = SHORT(height-1);
  463.     pcx->hres = SHORT(width);
  464.     pcx->vres = SHORT(height);
  465.     memset (pcx->palette,0,sizeof(pcx->palette));
  466.     pcx->color_planes = 1;              // chunky image
  467.     pcx->bytes_per_line = SHORT(width);
  468.     pcx->palette_type = SHORT(2);       // not a grey scale
  469.     memset (pcx->filler,0,sizeof(pcx->filler));
  470.  
  471.  
  472.     // pack the image
  473.     pack = &pcx->data;
  474.        
  475.     for (i=0 ; i<width*height ; i++)
  476.     {
  477.         if ( (*data & 0xc0) != 0xc0)
  478.             *pack++ = *data++;
  479.         else
  480.         {
  481.             *pack++ = 0xc1;
  482.             *pack++ = *data++;
  483.         }
  484.     }
  485.    
  486.     // write the palette
  487.     *pack++ = 0x0c;     // palette ID byte
  488.     for (i=0 ; i<768 ; i++)
  489.         *pack++ = *palette++;
  490.    
  491.     // write output file
  492.     length = pack - (byte *)pcx;
  493.     M_WriteFile (filename, pcx, length);
  494.  
  495.     Z_Free (pcx);
  496. }
  497.  
  498.  
  499. //
  500. // M_ScreenShot
  501. //
  502. void M_ScreenShot (void)
  503. {
  504.     int         i;
  505.     byte*       linear;
  506.     char        lbmname[12];
  507.    
  508.     // munge planar buffer to linear
  509.     linear = screens[2];
  510.     I_ReadScreen (linear);
  511.    
  512.     // find a file name to save it to
  513.     strcpy(lbmname,"DOOM00.pcx");
  514.                
  515.     for (i=0 ; i<=99 ; i++)
  516.     {
  517.         lbmname[4] = i/10 + '0';
  518.         lbmname[5] = i%10 + '0';
  519.         if (access(lbmname,0) == -1)
  520.             break;      // file doesn't exist
  521.     }
  522.     if (i==100)
  523.         I_Error ("M_ScreenShot: Couldn't create a PCX");
  524.    
  525.     // save the pcx file
  526.     WritePCXfile (lbmname, linear,
  527.                   SCREENWIDTH, SCREENHEIGHT,
  528.                   W_CacheLumpName ("PLAYPAL",PU_CACHE));
  529.        
  530.     players[consoleplayer].message = "screen shot";
  531. }
  532.  
  533.  
  534.