Annotation of doom/m_misc.c, revision 1.1.1.3

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: //
        !             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: //-----------------------------------------------------------------------------
1.1       root       26: 
1.1.1.3 ! root       27: static const char
        !            28: rcsid[] = "$Id: m_misc.c,v 1.6 1997/02/03 22:45:10 b1 Exp $";
1.1       root       29: 
1.1.1.3 ! root       30: #include <sys/stat.h>
        !            31: #include <sys/types.h>
        !            32: #include <fcntl.h>
        !            33: #include <stdlib.h>
        !            34: #include <unistd.h>
1.1.1.2   root       35: 
1.1.1.3 ! root       36: #include <ctype.h>
1.1.1.2   root       37: 
                     38: 
1.1.1.3 ! root       39: #include "doomdef.h"
1.1.1.2   root       40: 
1.1.1.3 ! root       41: #include "z_zone.h"
1.1.1.2   root       42: 
1.1.1.3 ! root       43: #include "m_swap.h"
        !            44: #include "m_argv.h"
1.1.1.2   root       45: 
1.1.1.3 ! root       46: #include "w_wad.h"
1.1.1.2   root       47: 
1.1.1.3 ! root       48: #include "i_system.h"
        !            49: #include "i_video.h"
        !            50: #include "v_video.h"
1.1.1.2   root       51: 
1.1.1.3 ! root       52: #include "hu_stuff.h"
1.1.1.2   root       53: 
1.1.1.3 ! root       54: // State.
        !            55: #include "doomstat.h"
1.1       root       56: 
1.1.1.3 ! root       57: // Data.
        !            58: #include "dstrings.h"
1.1.1.2   root       59: 
1.1.1.3 ! root       60: #include "m_misc.h"
1.1.1.2   root       61: 
1.1       root       62: //
1.1.1.3 ! root       63: // M_DrawText
        !            64: // Returns the final X coordinate
        !            65: // HU_Init must have been called to init the font
1.1.1.2   root       66: //
1.1.1.3 ! root       67: extern patch_t*                hu_font[HU_FONTSIZE];
1.1       root       68: 
1.1.1.3 ! root       69: int
        !            70: M_DrawText
        !            71: ( int          x,
        !            72:   int          y,
        !            73:   boolean      direct,
        !            74:   char*                string )
1.1       root       75: {
1.1.1.3 ! root       76:     int        c;
        !            77:     int                w;
1.1.1.2   root       78: 
1.1.1.3 ! root       79:     while (*string)
        !            80:     {
        !            81:        c = toupper(*string) - HU_FONTSTART;
        !            82:        string++;
        !            83:        if (c < 0 || c> HU_FONTSIZE)
1.1       root       84:        {
1.1.1.3 ! root       85:            x += 4;
        !            86:            continue;
1.1       root       87:        }
1.1.1.3 ! root       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:     }
1.1       root       98: 
1.1.1.3 ! root       99:     return x;
1.1       root      100: }
                    101: 
                    102: 
                    103: 
                    104: 
1.1.1.3 ! root      105: //
        !           106: // M_WriteFile
        !           107: //
1.1       root      108: #ifndef O_BINARY
                    109: #define O_BINARY 0
                    110: #endif
                    111: 
1.1.1.3 ! root      112: boolean
        !           113: M_WriteFile
        !           114: ( char const*  name,
        !           115:   void*                source,
        !           116:   int          length )
1.1       root      117: {
1.1.1.3 ! root      118:     int                handle;
        !           119:     int                count;
        !           120:        
        !           121:     handle = open ( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
1.1       root      122: 
1.1.1.3 ! root      123:     if (handle == -1)
        !           124:        return false;
1.1       root      125: 
1.1.1.3 ! root      126:     count = write (handle, source, length);
        !           127:     close (handle);
        !           128:        
        !           129:     if (count < length)
        !           130:        return false;
        !           131:                
        !           132:     return true;
1.1       root      133: }
                    134: 
1.1.1.3 ! root      135: 
1.1.1.2   root      136: //
                    137: // M_ReadFile
                    138: //
1.1.1.3 ! root      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;
1.1.1.2   root      163: }
                    164: 
1.1       root      165: 
1.1.1.2   root      166: //
1.1.1.3 ! root      167: // DEFAULTS
1.1.1.2   root      168: //
1.1.1.3 ! root      169: int            usemouse;
        !           170: int            usejoystick;
1.1.1.2   root      171: 
1.1.1.3 ! root      172: extern int     key_right;
        !           173: extern int     key_left;
        !           174: extern int     key_up;
        !           175: extern int     key_down;
1.1       root      176: 
1.1.1.3 ! root      177: extern int     key_strafeleft;
        !           178: extern int     key_straferight;
1.1       root      179: 
1.1.1.3 ! root      180: extern int     key_fire;
        !           181: extern int     key_use;
        !           182: extern int     key_strafe;
        !           183: extern int     key_speed;
1.1       root      184: 
1.1.1.3 ! root      185: extern int     mousebfire;
        !           186: extern int     mousebstrafe;
        !           187: extern int     mousebforward;
1.1       root      188: 
1.1.1.3 ! root      189: extern int     joybfire;
        !           190: extern int     joybstrafe;
        !           191: extern int     joybuse;
        !           192: extern int     joybspeed;
1.1       root      193: 
1.1.1.3 ! root      194: extern int     viewwidth;
        !           195: extern int     viewheight;
1.1       root      196: 
1.1.1.3 ! root      197: extern int     mouseSensitivity;
        !           198: extern int     showMessages;
1.1       root      199: 
1.1.1.3 ! root      200: extern int     detailLevel;
1.1       root      201: 
1.1.1.3 ! root      202: extern int     screenblocks;
1.1       root      203: 
1.1.1.3 ! root      204: extern int     showMessages;
1.1       root      205: 
1.1.1.3 ! root      206: // machine-independent sound params
        !           207: extern int     numChannels;
1.1.1.2   root      208: 
                    209: 
1.1.1.3 ! root      210: // UNIX hack, to be removed.
        !           211: #ifdef SNDSERV
        !           212: extern char*   sndserver_filename;
        !           213: extern int     mb_used;
        !           214: #endif
1.1       root      215: 
1.1.1.3 ! root      216: #ifdef LINUX
        !           217: char*          mousetype;
        !           218: char*          mousedev;
        !           219: #endif
1.1       root      220: 
1.1.1.3 ! root      221: extern char*   chat_macros[];
1.1       root      222: 
                    223: 
                    224: 
                    225: typedef struct
                    226: {
1.1.1.3 ! root      227:     char*      name;
        !           228:     int*       location;
        !           229:     int                defaultvalue;
        !           230:     int                scantranslate;          // PC scan code hack
        !           231:     int                untranslated;           // lousy hack
1.1       root      232: } default_t;
                    233: 
1.1.1.3 ! root      234: default_t      defaults[] =
1.1       root      235: {
1.1.1.3 ! root      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 }
1.1       root      298: 
                    299: };
                    300: 
1.1.1.3 ! root      301: int    numdefaults;
        !           302: char*  defaultfile;
1.1       root      303: 
                    304: 
1.1.1.3 ! root      305: //
        !           306: // M_SaveDefaults
        !           307: //
1.1       root      308: void M_SaveDefaults (void)
                    309: {
1.1.1.3 ! root      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));
1.1       root      328:        }
1.1.1.3 ! root      329:     }
        !           330:        
        !           331:     fclose (f);
1.1       root      332: }
                    333: 
1.1.1.3 ! root      334: 
1.1.1.2   root      335: //
                    336: // M_LoadDefaults
                    337: //
1.1.1.3 ! root      338: extern byte    scantokey[128];
1.1       root      339: 
1.1.1.3 ! root      340: void M_LoadDefaults (void)
1.1       root      341: {
1.1.1.3 ! root      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] == '"')
1.1       root      376:                {
1.1.1.3 ! root      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);
1.1       root      383:                }
1.1.1.3 ! root      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:            }
1.1       root      399:        }
1.1.1.3 ! root      400:                
        !           401:        fclose (f);
        !           402:     }
1.1       root      403: }
                    404: 
                    405: 
1.1.1.3 ! root      406: //
        !           407: // SCREEN SHOTS
        !           408: //
1.1       root      409: 
                    410: 
                    411: typedef struct
                    412: {
1.1.1.3 ! root      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
1.1       root      435: } pcx_t;
                    436: 
                    437: 
                    438: //
1.1.1.3 ! root      439: // WritePCXfile
1.1       root      440: //
1.1.1.3 ! root      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);
1.1       root      455: 
1.1.1.3 ! root      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));
1.1       root      471: 
                    472: 
1.1.1.3 ! root      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
1.1       root      481:        {
1.1.1.3 ! root      482:            *pack++ = 0xc1;
        !           483:            *pack++ = *data++;
1.1       root      484:        }
1.1.1.3 ! root      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: 
1.1       root      499: 
                    500: //
1.1.1.3 ! root      501: // M_ScreenShot
1.1       root      502: //
1.1.1.3 ! root      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: }
1.1       root      533: 
                    534: 

unix.superglobalmegacorp.com

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