Annotation of doom/m_misc.c, revision 1.1.1.4

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.