home *** CD-ROM | disk | FTP | other *** search
/ ST-Computer Leser-CD 2000 January / LCD_01_2000.iso / games / doom / pmdoom / src / m_misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-12-17  |  11.4 KB  |  547 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_DrawPatch(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, 8},
  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},    /* Atari : right button */
  270. #ifdef ATARI
  271.     {"mouseb_strafe",&mousebstrafe,2},
  272.     {"mouseb_forward",&mousebforward,1}, /* Atari : left button */
  273. #else
  274.     {"mouseb_strafe",&mousebstrafe,1},
  275.     {"mouseb_forward",&mousebforward,2},
  276. #endif
  277.     {"use_joystick",&usejoystick, 0},
  278.     {"joyb_fire",&joybfire,0},
  279.     {"joyb_strafe",&joybstrafe,1},
  280.     {"joyb_use",&joybuse,3},
  281.     {"joyb_speed",&joybspeed,2},
  282.  
  283.     {"screenblocks",&screenblocks, 9},
  284.     {"detaillevel",&detailLevel, 0},
  285.  
  286.     {"snd_channels",&numChannels, 3},
  287.  
  288.  
  289.  
  290.     {"usegamma",&usegamma, 0},
  291.  
  292.     {"chatmacro0", (int *) &chat_macros[0], (int) HUSTR_CHATMACRO0 },
  293.     {"chatmacro1", (int *) &chat_macros[1], (int) HUSTR_CHATMACRO1 },
  294.     {"chatmacro2", (int *) &chat_macros[2], (int) HUSTR_CHATMACRO2 },
  295.     {"chatmacro3", (int *) &chat_macros[3], (int) HUSTR_CHATMACRO3 },
  296.     {"chatmacro4", (int *) &chat_macros[4], (int) HUSTR_CHATMACRO4 },
  297.     {"chatmacro5", (int *) &chat_macros[5], (int) HUSTR_CHATMACRO5 },
  298.     {"chatmacro6", (int *) &chat_macros[6], (int) HUSTR_CHATMACRO6 },
  299.     {"chatmacro7", (int *) &chat_macros[7], (int) HUSTR_CHATMACRO7 },
  300.     {"chatmacro8", (int *) &chat_macros[8], (int) HUSTR_CHATMACRO8 },
  301.     {"chatmacro9", (int *) &chat_macros[9], (int) HUSTR_CHATMACRO9 }
  302.  
  303. };
  304.  
  305. int    numdefaults;
  306. char*    defaultfile;
  307.  
  308.  
  309. /*  */
  310. /*  M_SaveDefaults */
  311. /*  */
  312. void M_SaveDefaults (void)
  313. {
  314.     int        i;
  315.     int        v;
  316.     FILE*    f;
  317.     
  318.     f = fopen (defaultfile, "w");
  319.     if (!f)
  320.     return; /*  can't write the file, but don't complain */
  321.         
  322.     for (i=0 ; i<numdefaults ; i++)
  323.     {
  324.     if (defaults[i].defaultvalue > -0xfff
  325.         && defaults[i].defaultvalue < 0xfff)
  326.     {
  327.         v = *defaults[i].location;
  328.         fprintf (f,"%s\t\t%i\n",defaults[i].name,v);
  329.     } else {
  330.         fprintf (f,"%s\t\t\"%s\"\n",defaults[i].name,
  331.              * (char **) (defaults[i].location));
  332.     }
  333.     }
  334.     
  335.     fclose (f);
  336. }
  337.  
  338.  
  339. /*  */
  340. /*  M_LoadDefaults */
  341. /*  */
  342. extern byte    scantokey[128];
  343.  
  344. void M_LoadDefaults (void)
  345. {
  346.     int        i;
  347.     int        len;
  348.     FILE*    f;
  349.     char    def[80];
  350.     char    strparm[100];
  351.     char*    newstring="";
  352.     int        parm;
  353.     boolean    isstring;
  354.     
  355.     /*  set everything to base values */
  356.     numdefaults = sizeof(defaults)/sizeof(defaults[0]);
  357.     for (i=0 ; i<numdefaults ; i++)
  358.     *defaults[i].location = defaults[i].defaultvalue;
  359.     
  360.     /*  check for a custom default file */
  361.     i = M_CheckParm ("-config");
  362.     if (i && i<myargc-1)
  363.     {
  364.     defaultfile = myargv[i+1];
  365.     printf ("    default file: %s\n",defaultfile);
  366.     }
  367.     else
  368.     defaultfile = basedefault;
  369.     
  370.     /*  read the file in, overriding any set defaults */
  371.     f = fopen (defaultfile, "r");
  372.     if (f)
  373.     {
  374.     while (!feof(f))
  375.     {
  376.         isstring = false;
  377.         if (fscanf (f, "%79s %[^\n]\n", def, strparm) == 2)
  378.         {
  379.         if (strparm[0] == '"')
  380.         {
  381.             /*  get a string default */
  382.             isstring = true;
  383.             len = strlen(strparm);
  384.             newstring = (char *) malloc(len);
  385.             strparm[len-1] = 0;
  386.             strcpy(newstring, strparm+1);
  387.         }
  388.         else 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 =
  399.                 (int) newstring;
  400.             break;
  401.             }
  402.         }
  403.     }
  404.         
  405.     fclose (f);
  406.     }
  407. }
  408.  
  409.  
  410. /*  */
  411. /*  SCREEN SHOTS */
  412. /*  */
  413.  
  414.  
  415. typedef struct
  416. {
  417.     char        manufacturer;
  418.     char        version;
  419.     char        encoding;
  420.     char        bits_per_pixel;
  421.  
  422.     unsigned short    xmin;
  423.     unsigned short    ymin;
  424.     unsigned short    xmax;
  425.     unsigned short    ymax;
  426.     
  427.     unsigned short    hres;
  428.     unsigned short    vres;
  429.  
  430.     unsigned char    palette[48];
  431.     
  432.     char        reserved;
  433.     char        color_planes;
  434.     unsigned short    bytes_per_line;
  435.     unsigned short    palette_type;
  436.     
  437.     char        filler[58];
  438.     unsigned char    data;        /*  unbounded */
  439. } pcx_t;
  440.  
  441.  
  442. /*  */
  443. /*  WritePCXfile */
  444. /*  */
  445. void
  446. WritePCXfile
  447. ( char*        filename,
  448.   byte*        data,
  449.   int        width,
  450.   int        height,
  451.   byte*        palette )
  452. {
  453.     int        i;
  454.     int        length;
  455.     pcx_t*    pcx;
  456.     byte*    pack;
  457.     
  458.     pcx = Z_Malloc (width*height*2+1000, PU_STATIC, NULL);
  459.  
  460.     pcx->manufacturer = 0x0a;        /*  PCX id */
  461.     pcx->version = 5;            /*  256 color */
  462.     pcx->encoding = 1;            /*  uncompressed */
  463.     pcx->bits_per_pixel = 8;        /*  256 color */
  464.     pcx->xmin = 0;
  465.     pcx->ymin = 0;
  466.     pcx->xmax = SHORT(width-1);
  467.     pcx->ymax = SHORT(height-1);
  468.     pcx->hres = SHORT(width);
  469.     pcx->vres = SHORT(height);
  470.     memset (pcx->palette,0,sizeof(pcx->palette));
  471.     pcx->color_planes = 1;        /*  chunky image */
  472.     pcx->bytes_per_line = SHORT(width);
  473.     pcx->palette_type = SHORT(2);    /*  not a grey scale */
  474.     memset (pcx->filler,0,sizeof(pcx->filler));
  475.  
  476.  
  477.     /*  pack the image */
  478.     pack = &pcx->data;
  479.     
  480.     for (i=0 ; i<width*height ; i++)
  481.     {
  482.     if ( (*data & 0xc0) != 0xc0)
  483.         *pack++ = *data++;
  484.     else
  485.     {
  486.         *pack++ = 0xc1;
  487.         *pack++ = *data++;
  488.     }
  489.     }
  490.     
  491.     /*  write the palette */
  492.     *pack++ = 0x0c;    /*  palette ID byte */
  493.     for (i=0 ; i<768 ; i++)
  494.     *pack++ = *palette++;
  495.     
  496.     /*  write output file */
  497.     length = pack - (byte *)pcx;
  498.     M_WriteFile (filename, pcx, length);
  499.  
  500.     Z_Free (pcx);
  501. }
  502.  
  503.  
  504. /*  */
  505. /*  M_ScreenShot */
  506. /*  */
  507. void M_ScreenShot (void)
  508. {
  509.     int        i;
  510.     byte*    linear;
  511.     char    lbmname[12];
  512.     
  513.  
  514.     /*  Don't save in TrueColor */
  515.     if (pixel_size!=1)
  516.     {
  517.         printf("M_ScreenShot: not in TrueColor\n");
  518.         return;
  519.     }
  520.     
  521.     /*  munge planar buffer to linear */
  522.     linear = screens[2];
  523.     I_ReadScreen (linear);
  524.     
  525.     /*  find a file name to save it to */
  526.     strcpy(lbmname,"DOOM00.pcx");
  527.         
  528.     for (i=0 ; i<=99 ; i++)
  529.     {
  530.     lbmname[4] = i/10 + '0';
  531.     lbmname[5] = i%10 + '0';
  532.     if (access(lbmname,0) == -1)
  533.         break;    /*  file doesn't exist */
  534.     }
  535.     if (i==100)
  536.     I_Error ("M_ScreenShot: Couldn't create a PCX");
  537.     
  538.     /*  save the pcx file */
  539.     WritePCXfile (lbmname, linear,
  540.           SCREENWIDTH, SCREENHEIGHT,
  541.           W_CacheLumpName ("PLAYPAL",PU_CACHE));
  542.     
  543.     players[consoleplayer].message = "screen shot";
  544. }
  545.  
  546.  
  547.