home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #2 / amigaacscoverdisc1998-021998.iso / games / doom / source / linuxdoom-1.10 / m_misc.c < prev    next >
C/C++ Source or Header  |  1997-12-22  |  11KB  |  535 lines

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