Subversion Repositories Kolibri OS

Rev

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