gzdoom/code/M_misc.c

364 lines
6.5 KiB
C
Raw Normal View History

1998-04-07 00:00:00 +00:00
// Emacs style mode select -*- C++ -*-
//-----------------------------------------------------------------------------
//
// $Id:$
//
// Copyright (C) 1993-1996 by id Software, Inc.
//
// This source is available for distribution and/or modification
// only under the terms of the DOOM Source Code License as
// published by id Software. All rights reserved.
//
// The source is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// FITNESS FOR A PARTICULAR PURPOSE. See the DOOM Source Code License
// for more details.
//
//
// $Log:$
//
// DESCRIPTION:
// Main loop menu stuff.
// Default Config File.
// PCX Screenshots.
//
//-----------------------------------------------------------------------------
static const char
rcsid[] = "$Id: m_misc.c,v 1.6 1997/02/03 22:45:10 b1 Exp $";
#if defined(NORMALUNIX)
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#elif defined(_WIN32)
#include <sys/stat.h>
#include <sys/types.h>
#include <io.h>
#include <fcntl.h>
#endif
#include <ctype.h>
1998-04-07 00:00:00 +00:00
#include "m_alloc.h"
1998-04-07 00:00:00 +00:00
#include "doomdef.h"
#include "z_zone.h"
#include "m_swap.h"
#include "m_argv.h"
#include "w_wad.h"
#include "c_cvars.h"
#include "c_dispatch.h"
#include "c_bindings.h"
#include "i_system.h"
#include "i_video.h"
#include "v_video.h"
#include "hu_stuff.h"
// State.
#include "doomstat.h"
// Data.
#include "dstrings.h"
#include "m_misc.h"
#include "cmdlib.h"
//
// M_DrawText
// Returns the final X coordinate
// HU_Init must have been called to init the font
//
// [RH] It wasn't used anywhere, so I got rid of it.
//
// M_WriteFile
//
#ifndef O_BINARY
#define O_BINARY 0
#endif
boolean
M_WriteFile
( char const* name,
void* source,
int length )
{
int handle;
int count;
handle = open ( name, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0666);
if (handle == -1)
return false;
count = write (handle, source, length);
close (handle);
if (count < length)
return false;
return true;
}
//
// M_ReadFile
//
int
M_ReadFile
( char const* name,
byte** buffer )
{
int handle, count, length;
struct stat fileinfo;
byte *buf;
handle = open (name, O_RDONLY | O_BINARY, 0666);
if (handle == -1)
I_Error ("Couldn't read file %s", name);
if (fstat (handle,&fileinfo) == -1)
I_Error ("Couldn't read file %s", name);
length = fileinfo.st_size;
buf = Z_Malloc (length, PU_STATIC, NULL);
count = read (handle, buf, length);
close (handle);
if (count < length)
I_Error ("Couldn't read file %s", name);
*buffer = buf;
return length;
}
//
// DEFAULTS
//
// [RH] Handled by console code now.
// [RH] Get configfile path.
// This file contains commands to set all
// archived cvars, bind commands to keys,
// and set other general game information.
1998-04-07 00:00:00 +00:00
char *GetConfigPath (void)
1998-04-07 00:00:00 +00:00
{
char *path;
1998-04-07 00:00:00 +00:00
path = Malloc (strlen (progdir) + 11);
1998-04-07 00:00:00 +00:00
strcpy (path, progdir);
strcat (path, "config.cfg");
return path;
}
1998-04-07 00:00:00 +00:00
char *GetAutoexecPath (void)
1998-04-07 00:00:00 +00:00
{
char *path;
1998-04-07 00:00:00 +00:00
path = Malloc (strlen (progdir) + 13);
1998-04-07 00:00:00 +00:00
strcpy (path, progdir);
strcat (path, "autoexec.cfg");
return path;
}
//
// M_SaveDefaults
//
void M_SaveDefaults (void)
{
FILE* f;
char* configfile;
configfile = GetConfigPath ();
if (f = fopen (configfile, "w")) {
1998-04-07 00:00:00 +00:00
fprintf (f, "; Generated by ZDOOM - do not modify\n");
1998-04-07 00:00:00 +00:00
// Archive all cvars marked as CVAR_ARCHIVE
C_ArchiveCVars (f);
// Archive all active key bindings
C_ArchiveBindings (f);
// Archive video-related stuff
V_Archive (f);
// Hack. Will change later.
fprintf (f, "exec %s\n", GetAutoexecPath ());
fclose (f);
}
free (configfile);
}
//
// M_LoadDefaults
//
extern byte scantokey[128];
void M_LoadDefaults (void)
{
extern char DefBindings[];
char* configfile = GetConfigPath ();
char* execcommand;
// Set default key bindings. These will be overridden
// by the bindings in the config file if it exists.
AddCommandString (DefBindings);
// Set default cvar values. These will be overridden
// by the configfile.
C_SetCVars ();
// Prepare the standard 1.0 gamma table
AddCommandString ("gamma 1");
1998-04-07 00:00:00 +00:00
execcommand = Malloc (strlen (configfile) + 6);
1998-04-07 00:00:00 +00:00
sprintf (execcommand, "exec %s", configfile);
free (configfile);
AddCommandString (execcommand);
free (execcommand);
}
//
// SCREEN SHOTS
//
typedef struct
{
char manufacturer;
char version;
char encoding;
char bits_per_pixel;
unsigned short xmin;
unsigned short ymin;
unsigned short xmax;
unsigned short ymax;
unsigned short hres;
unsigned short vres;
unsigned char palette[48];
char reserved;
char color_planes;
unsigned short bytes_per_line;
unsigned short palette_type;
char filler[58];
unsigned char data; // unbounded
} pcx_t;
//
// WritePCXfile
//
void
WritePCXfile
( char* filename,
byte* data,
int width,
int height,
byte* palette )
{
int i;
int length;
pcx_t* pcx;
byte* pack;
pcx = Z_Malloc (width*height*2+1000, PU_STATIC, NULL);
pcx->manufacturer = 0x0a; // PCX id
pcx->version = 5; // 256 color
pcx->encoding = 1; // uncompressed
pcx->bits_per_pixel = 8; // 256 color
pcx->xmin = 0;
pcx->ymin = 0;
pcx->xmax = SHORT(width-1);
pcx->ymax = SHORT(height-1);
pcx->hres = SHORT(width);
pcx->vres = SHORT(height);
memset (pcx->palette,0,sizeof(pcx->palette));
pcx->color_planes = 1; // chunky image
pcx->bytes_per_line = SHORT(width);
pcx->palette_type = SHORT(2); // not a grey scale
memset (pcx->filler,0,sizeof(pcx->filler));
// pack the image
pack = &pcx->data;
for (i=0 ; i<width*height ; i++)
{
if ( (*data & 0xc0) != 0xc0)
*pack++ = *data++;
else
{
*pack++ = 0xc1;
*pack++ = *data++;
}
}
// write the palette
*pack++ = 0x0c; // palette ID byte
for (i=0 ; i<768 ; i++)
*pack++ = *palette++;
// write output file
length = pack - (byte *)pcx;
M_WriteFile (filename, pcx, length);
Z_Free (pcx);
}
//
// M_ScreenShot
//
void M_ScreenShot (void)
{
int i;
byte* linear;
char lbmname[16];
// munge planar buffer to linear
linear = screens[2];
I_ReadScreen (linear);
// find a file name to save it to
strcpy(lbmname,"DOOM0000.pcx");
for (i=0 ; i<=9999 ; i++)
{
sprintf (lbmname, "DOOM%04d.pcx", i);
if (!FileExists (lbmname))
break; // file doesn't exist
}
if (i==100)
I_Error ("M_ScreenShot: Couldn't create a PCX");
// save the pcx file
WritePCXfile (lbmname, linear,
SCREENWIDTH, SCREENHEIGHT,
W_CacheLumpName ("PLAYPAL",PU_CACHE));
players[consoleplayer].message = "screen shot";
}