Subversion Repositories Kolibri OS

Rev

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