gzdoom/code/M_misc.c

422 lines
8 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.
//
//-----------------------------------------------------------------------------
1998-07-14 00:00:00 +00:00
#include "doomtype.h"
1998-12-22 00:00:00 +00:00
#include "version.h"
1998-07-14 00:00:00 +00:00
1998-04-07 00:00:00 +00:00
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
1998-07-14 00:00:00 +00:00
#if defined(_WIN32)
1998-04-07 00:00:00 +00:00
#include <io.h>
1998-07-14 00:00:00 +00:00
#else
#include <unistd.h>
1998-04-07 00:00:00 +00:00
#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"
1998-07-14 00:00:00 +00:00
#include "c_dispch.h"
#include "c_bind.h"
1998-04-07 00:00:00 +00:00
#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"
1998-07-14 00:00:00 +00:00
#include "g_game.h"
1998-04-07 00:00:00 +00:00
//
// M_WriteFile
//
#ifndef O_BINARY
#define O_BINARY 0
#endif
1998-07-14 00:00:00 +00:00
BOOL M_WriteFile (char const *name, void *source, int length)
1998-04-07 00:00:00 +00:00
{
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
//
1998-07-14 00:00:00 +00:00
int M_ReadFile (char const *name, byte **buffer)
1998-04-07 00:00:00 +00:00
{
int handle, count, length;
struct stat fileinfo;
1998-07-14 00:00:00 +00:00
byte *buf;
1998-04-07 00:00:00 +00:00
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
{
1998-07-14 00:00:00 +00:00
int p;
1998-04-07 00:00:00 +00:00
char *path;
1998-07-14 00:00:00 +00:00
p = M_CheckParm ("-config");
if (p && p < myargc - 1) {
return copystring (myargv[p+1]);
}
1998-12-22 00:00:00 +00:00
if (M_CheckParm ("-cdrom"))
return copystring ("c:\\zdoomdat\\zdoom.cfg");
1998-04-07 00:00:00 +00:00
path = Malloc (strlen (progdir) + 11);
1998-04-07 00:00:00 +00:00
strcpy (path, progdir);
1998-07-14 00:00:00 +00:00
strcat (path, "zdoom.cfg");
1998-04-07 00:00:00 +00:00
return path;
}
1998-04-07 00:00:00 +00:00
char *GetAutoexecPath (void)
1998-04-07 00:00:00 +00:00
{
1998-07-14 00:00:00 +00:00
cvar_t *autovar;
1998-04-07 00:00:00 +00:00
1998-12-22 00:00:00 +00:00
if (M_CheckParm ("-cdrom")) {
return copystring ("c:\\zdoomdat\\autoexec.cfg");
} else {
char *path = Malloc (strlen (progdir) + 13);
1998-07-14 00:00:00 +00:00
1998-12-22 00:00:00 +00:00
strcpy (path, progdir);
strcat (path, "autoexec.cfg");
1998-07-14 00:00:00 +00:00
1998-12-22 00:00:00 +00:00
autovar = cvar ("autoexec", path, CVAR_ARCHIVE);
free (path);
return autovar->string;
}
1998-04-07 00:00:00 +00:00
}
//
// M_SaveDefaults
//
1998-12-22 00:00:00 +00:00
// [RH] Don't write a config file if M_LoadDefaults hasn't been called.
static BOOL DefaultsLoaded;
1998-04-07 00:00:00 +00:00
void M_SaveDefaults (void)
{
1998-12-22 00:00:00 +00:00
FILE *f;
char *configfile;
if (!DefaultsLoaded)
return;
1998-07-14 00:00:00 +00:00
1998-04-07 00:00:00 +00:00
configfile = GetConfigPath ();
1998-07-14 00:00:00 +00:00
// Make sure the user hasn't changed configver
cvar_set ("configver", VERSIONSTR);
if ( (f = fopen (configfile, "w")) ) {
fprintf (f, "// Generated by ZDOOM - don't hurt anything\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);
1998-07-14 00:00:00 +00:00
// Archive all aliases
C_ArchiveAliases (f);
1998-04-07 00:00:00 +00:00
fclose (f);
}
free (configfile);
}
//
// M_LoadDefaults
//
1998-12-22 00:00:00 +00:00
extern byte scantokey[128];
extern int cvar_defflags;
1998-04-07 00:00:00 +00:00
void M_LoadDefaults (void)
{
extern char DefBindings[];
1998-07-14 00:00:00 +00:00
char* configfile;
1998-04-07 00:00:00 +00:00
char* execcommand;
1998-07-14 00:00:00 +00:00
cvar_t* configver;
1998-04-07 00:00:00 +00:00
// Set default key bindings. These will be overridden
// by the bindings in the config file if it exists.
AddCommandString (DefBindings);
1998-07-14 00:00:00 +00:00
// Used to identify the version of the game that saved
// a config file to compensate for new features that get
// put into newer configfiles.
configver = cvar ("configver", VERSIONSTR, CVAR_ARCHIVE);
1998-04-07 00:00:00 +00:00
1998-07-14 00:00:00 +00:00
configfile = GetConfigPath ();
1998-12-22 00:00:00 +00:00
execcommand = Malloc (strlen (configfile) + 8);
sprintf (execcommand, "exec \"%s\"", configfile);
1998-04-07 00:00:00 +00:00
free (configfile);
1998-12-22 00:00:00 +00:00
cvar_defflags = CVAR_ARCHIVE;
1998-04-07 00:00:00 +00:00
AddCommandString (execcommand);
1998-12-22 00:00:00 +00:00
cvar_defflags = 0;
1998-04-07 00:00:00 +00:00
free (execcommand);
1998-07-14 00:00:00 +00:00
configfile = GetAutoexecPath ();
1998-12-22 00:00:00 +00:00
execcommand = Malloc (strlen (configfile) + 8);
sprintf (execcommand, "exec \"%s\"", configfile);
1998-07-14 00:00:00 +00:00
AddCommandString (execcommand);
free (execcommand);
if (configver->value <= 113.0f) {
AddCommandString ("bind t messagemode; bind \\ +showscores; bind f12 spynext; bind sysrq screenshot");
if (!stricmp (C_GetBinding (KEY_F5), "menu_video"))
AddCommandString ("bind f5 menu_display");
}
1998-12-22 00:00:00 +00:00
DefaultsLoaded = true;
1998-04-07 00:00:00 +00:00
}
//
// 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
//
1998-07-14 00:00:00 +00:00
void WritePCXfile (char *filename, byte *data, int width, int height, unsigned int *palette)
1998-04-07 00:00:00 +00:00
{
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);
1998-07-14 00:00:00 +00:00
pcx->palette_type = SHORT(1); // not a grey scale [RH] Really!
1998-04-07 00:00:00 +00:00
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
1998-07-14 00:00:00 +00:00
for (i=0 ; i<256 ; i++, palette++) {
*pack++ = RPART(*palette);
*pack++ = GPART(*palette);
*pack++ = BPART(*palette);
}
1998-04-07 00:00:00 +00:00
// write output file
length = pack - (byte *)pcx;
M_WriteFile (filename, pcx, length);
Z_Free (pcx);
}
//
// M_ScreenShot
//
1998-07-14 00:00:00 +00:00
static BOOL FindFreeName (char *lbmname, char *extension)
1998-04-07 00:00:00 +00:00
{
1998-07-14 00:00:00 +00:00
int i;
1998-04-07 00:00:00 +00:00
for (i=0 ; i<=9999 ; i++)
{
1998-07-14 00:00:00 +00:00
sprintf (lbmname, "DOOM%04d.%s", i, extension);
1998-04-07 00:00:00 +00:00
if (!FileExists (lbmname))
break; // file doesn't exist
}
1998-07-14 00:00:00 +00:00
if (i==10000)
return false;
else
return true;
}
extern unsigned IndexedPalette[256];
void M_ScreenShot (char *filename)
{
1998-12-22 00:00:00 +00:00
byte *linear;
char autoname[32];
char *lbmname;
1998-04-07 00:00:00 +00:00
1998-07-14 00:00:00 +00:00
// find a file name to save it to
if (!filename) {
1998-12-22 00:00:00 +00:00
if (M_CheckParm ("-cdrom")) {
strcpy (autoname, "C:\\ZDOOMDAT\\");
lbmname = autoname + 12;
} else {
lbmname = autoname;
}
1998-07-14 00:00:00 +00:00
if (!FindFreeName (lbmname, "tga\0pcx" + (screens[0].is8bit << 2))) {
Printf ("M_ScreenShot: Delete some screenshots\n");
return;
}
1998-12-22 00:00:00 +00:00
filename = autoname;
1998-07-14 00:00:00 +00:00
}
if (screens[0].is8bit) {
// munge planar buffer to linear
linear = Malloc (screens[0].width * screens[0].height);
I_ReadScreen (linear);
1998-04-07 00:00:00 +00:00
1998-07-14 00:00:00 +00:00
// save the pcx file
WritePCXfile (filename, linear,
screens[0].width, screens[0].height,
IndexedPalette);
free (linear);
} else {
// save the tga file
//I_WriteTGAfile (filename, &screens[0]);
}
Printf ("screen shot\n");
1998-04-07 00:00:00 +00:00
}
1998-07-14 00:00:00 +00:00
void Cmd_Screenshot (void *plyr, int argc, char **argv)
{
if (argc == 1)
G_ScreenShot (NULL);
else
G_ScreenShot (argv[1]);
}