2006-02-24 04:48:15 +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:
|
|
|
|
// Default Config File.
|
|
|
|
// Screenshots.
|
|
|
|
//
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <malloc.h>
|
2008-02-05 05:29:31 +00:00
|
|
|
#include <time.h>
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
#include "doomtype.h"
|
|
|
|
#include "version.h"
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <io.h>
|
|
|
|
#else
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
#include "doomdef.h"
|
|
|
|
|
|
|
|
#include "m_swap.h"
|
|
|
|
#include "m_argv.h"
|
|
|
|
|
|
|
|
#include "w_wad.h"
|
|
|
|
|
|
|
|
#include "c_cvars.h"
|
|
|
|
#include "c_dispatch.h"
|
|
|
|
#include "c_bind.h"
|
|
|
|
|
|
|
|
#include "i_system.h"
|
|
|
|
#include "i_video.h"
|
|
|
|
#include "v_video.h"
|
2008-09-15 14:11:05 +00:00
|
|
|
#include "r_defs.h"
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
#include "hu_stuff.h"
|
|
|
|
|
|
|
|
// State.
|
|
|
|
#include "doomstat.h"
|
|
|
|
|
|
|
|
// Data.
|
|
|
|
#include "m_misc.h"
|
|
|
|
#include "m_png.h"
|
|
|
|
|
|
|
|
#include "cmdlib.h"
|
|
|
|
|
|
|
|
#include "g_game.h"
|
|
|
|
#include "gi.h"
|
|
|
|
|
|
|
|
#include "gameconfigfile.h"
|
|
|
|
|
|
|
|
FGameConfigFile *GameConfig;
|
|
|
|
|
|
|
|
CVAR(Bool, screenshot_quiet, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
|
|
|
|
CVAR(String, screenshot_type, "png", CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
|
|
|
|
CVAR(String, screenshot_dir, "", CVAR_ARCHIVE|CVAR_GLOBALCONFIG);
|
2008-02-16 10:23:12 +00:00
|
|
|
EXTERN_CVAR(Bool, longsavemessages);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2006-07-13 02:08:39 +00:00
|
|
|
extern void FreeKeySections();
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
static long ParseCommandLine (const char *args, int *argc, char **argv);
|
|
|
|
|
|
|
|
//
|
|
|
|
// M_WriteFile
|
|
|
|
//
|
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
2006-09-14 00:02:31 +00:00
|
|
|
bool M_WriteFile (char const *name, void *source, int length)
|
2006-02-24 04:48:15 +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
|
|
|
|
//
|
2006-09-14 00:02:31 +00:00
|
|
|
int M_ReadFile (char const *name, BYTE **buffer)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
int handle, count, length;
|
|
|
|
struct stat fileinfo;
|
2006-09-14 00:02:31 +00:00
|
|
|
BYTE *buf;
|
2006-02-24 04:48:15 +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 = new BYTE[length];
|
|
|
|
count = read (handle, buf, length);
|
|
|
|
close (handle);
|
|
|
|
|
|
|
|
if (count < length)
|
|
|
|
I_Error ("Couldn't read file %s", name);
|
|
|
|
|
|
|
|
*buffer = buf;
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// PROC M_FindResponseFile
|
|
|
|
//
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void M_FindResponseFile (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2008-03-12 02:56:11 +00:00
|
|
|
for (i = 1; i < Args->NumArgs(); i++)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-03-12 02:56:11 +00:00
|
|
|
if (Args->GetArg(i)[0] == '@')
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
char **argv;
|
|
|
|
char *file;
|
|
|
|
int argc;
|
|
|
|
int argcinresp;
|
|
|
|
FILE *handle;
|
|
|
|
int size;
|
|
|
|
long argsize;
|
|
|
|
int k;
|
|
|
|
int index;
|
|
|
|
|
|
|
|
// READ THE RESPONSE FILE INTO MEMORY
|
2008-03-12 02:56:11 +00:00
|
|
|
handle = fopen (Args->GetArg(i) + 1,"rb");
|
2006-02-24 04:48:15 +00:00
|
|
|
if (!handle)
|
|
|
|
{ // [RH] Make this a warning, not an error.
|
2008-03-12 02:56:11 +00:00
|
|
|
Printf ("No such response file (%s)!", Args->GetArg(i) + 1);
|
2006-02-24 04:48:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-03-12 02:56:11 +00:00
|
|
|
Printf ("Found response file %s!\n", Args->GetArg(i) + 1);
|
2006-02-24 04:48:15 +00:00
|
|
|
fseek (handle, 0, SEEK_END);
|
|
|
|
size = ftell (handle);
|
|
|
|
fseek (handle, 0, SEEK_SET);
|
|
|
|
file = new char[size+1];
|
|
|
|
fread (file, size, 1, handle);
|
|
|
|
file[size] = 0;
|
|
|
|
fclose (handle);
|
|
|
|
|
|
|
|
argsize = ParseCommandLine (file, &argcinresp, NULL);
|
2008-03-12 02:56:11 +00:00
|
|
|
argc = argcinresp + Args->NumArgs() - 1;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
if (argc != 0)
|
|
|
|
{
|
2006-05-04 03:49:46 +00:00
|
|
|
argv = (char **)M_Malloc (argc*sizeof(char *) + argsize);
|
2006-02-24 04:48:15 +00:00
|
|
|
argv[i] = (char *)argv + argc*sizeof(char *);
|
|
|
|
ParseCommandLine (file, NULL, argv+i);
|
|
|
|
|
|
|
|
for (index = 0; index < i; ++index)
|
2008-03-12 02:56:11 +00:00
|
|
|
argv[index] = Args->GetArg (index);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-12 02:56:11 +00:00
|
|
|
for (index = i + 1, i += argcinresp; index < Args->NumArgs (); ++index)
|
|
|
|
argv[i++] = Args->GetArg (index);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-03-12 02:56:11 +00:00
|
|
|
Args->Destroy();
|
|
|
|
Args = new DArgs(i, argv);
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete[] file;
|
|
|
|
|
|
|
|
// DISPLAY ARGS
|
2008-03-12 02:56:11 +00:00
|
|
|
Printf ("%d command-line args:\n", Args->NumArgs ());
|
|
|
|
for (k = 1; k < Args->NumArgs (); k++)
|
|
|
|
Printf ("%s\n", Args->GetArg (k));
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ParseCommandLine
|
|
|
|
//
|
|
|
|
// This is just like the version in c_dispatch.cpp, except it does not
|
|
|
|
// do cvar expansion.
|
|
|
|
|
|
|
|
static long ParseCommandLine (const char *args, int *argc, char **argv)
|
|
|
|
{
|
|
|
|
int count;
|
|
|
|
char *buffplace;
|
|
|
|
|
|
|
|
count = 0;
|
|
|
|
buffplace = NULL;
|
|
|
|
if (argv != NULL)
|
|
|
|
{
|
|
|
|
buffplace = argv[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
while (*args <= ' ' && *args)
|
|
|
|
{ // skip white space
|
|
|
|
args++;
|
|
|
|
}
|
|
|
|
if (*args == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if (*args == '\"')
|
|
|
|
{ // read quoted string
|
|
|
|
char stuff;
|
|
|
|
if (argv != NULL)
|
|
|
|
{
|
|
|
|
argv[count] = buffplace;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
args++;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
stuff = *args++;
|
|
|
|
if (stuff == '\\' && *args == '\"')
|
|
|
|
{
|
|
|
|
stuff = '\"', args++;
|
|
|
|
}
|
|
|
|
else if (stuff == '\"')
|
|
|
|
{
|
|
|
|
stuff = 0;
|
|
|
|
}
|
|
|
|
else if (stuff == 0)
|
|
|
|
{
|
|
|
|
args--;
|
|
|
|
}
|
|
|
|
if (argv != NULL)
|
|
|
|
{
|
|
|
|
*buffplace = stuff;
|
|
|
|
}
|
|
|
|
buffplace++;
|
|
|
|
} while (stuff);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // read unquoted string
|
|
|
|
const char *start = args++, *end;
|
|
|
|
|
|
|
|
while (*args && *args > ' ' && *args != '\"')
|
|
|
|
args++;
|
|
|
|
end = args;
|
|
|
|
if (argv != NULL)
|
|
|
|
{
|
|
|
|
argv[count] = buffplace;
|
|
|
|
while (start < end)
|
|
|
|
*buffplace++ = *start++;
|
|
|
|
*buffplace++ = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffplace += end - start + 1;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc != NULL)
|
|
|
|
{
|
|
|
|
*argc = count;
|
|
|
|
}
|
|
|
|
return (long)(buffplace - (char *)0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef unix
|
2006-05-04 06:14:52 +00:00
|
|
|
FString GetUserFile (const char *file, bool nodir)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2009-02-08 02:52:43 +00:00
|
|
|
FString path;
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2009-02-08 02:52:43 +00:00
|
|
|
if (nodir)
|
|
|
|
{
|
|
|
|
path = NicePath("~/");
|
|
|
|
}
|
|
|
|
else
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
struct stat info;
|
2009-02-08 02:52:43 +00:00
|
|
|
|
|
|
|
path = NicePath("~/" GAME_DIR "/");
|
2006-05-03 22:45:01 +00:00
|
|
|
if (stat (path, &info) == -1)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
if (mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR) == -1)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
I_FatalError ("Failed to create %s directory:\n%s",
|
|
|
|
path.GetChars(), strerror (errno));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!S_ISDIR(info.st_mode))
|
|
|
|
{
|
|
|
|
I_FatalError ("%s must be a directory", path.GetChars());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
path += file;
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
//
|
|
|
|
// M_SaveDefaults
|
|
|
|
//
|
|
|
|
|
2006-09-20 02:00:19 +00:00
|
|
|
bool M_SaveDefaults (const char *filename)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2006-09-20 02:00:19 +00:00
|
|
|
FString oldpath;
|
|
|
|
bool success;
|
|
|
|
|
|
|
|
if (filename != NULL)
|
|
|
|
{
|
|
|
|
oldpath = GameConfig->GetPathName();
|
|
|
|
GameConfig->ChangePathName (filename);
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
GameConfig->ArchiveGlobalData ();
|
|
|
|
if (GameNames[gameinfo.gametype] != NULL)
|
|
|
|
{
|
|
|
|
GameConfig->ArchiveGameData (GameNames[gameinfo.gametype]);
|
|
|
|
}
|
2006-09-20 02:00:19 +00:00
|
|
|
success = GameConfig->WriteConfigFile ();
|
|
|
|
if (filename != NULL)
|
|
|
|
{
|
|
|
|
GameConfig->ChangePathName (filename);
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
void M_SaveDefaultsFinal ()
|
|
|
|
{
|
|
|
|
while (!M_SaveDefaults (NULL) && I_WriteIniFailed ())
|
|
|
|
{
|
|
|
|
/* Loop until the config saves or I_WriteIniFailed() returns false */
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
delete GameConfig;
|
2006-04-11 16:27:41 +00:00
|
|
|
GameConfig = NULL;
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
|
2006-09-20 02:00:19 +00:00
|
|
|
CCMD (writeini)
|
|
|
|
{
|
|
|
|
const char *filename = (argv.argc() == 1) ? NULL : argv[1];
|
|
|
|
if (!M_SaveDefaults (filename))
|
|
|
|
{
|
|
|
|
Printf ("Writing config failed: %s\n", strerror(errno));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Printf ("Config saved.\n");
|
|
|
|
}
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// M_LoadDefaults
|
|
|
|
//
|
|
|
|
|
|
|
|
void M_LoadDefaults ()
|
|
|
|
{
|
|
|
|
GameConfig = new FGameConfigFile;
|
|
|
|
GameConfig->DoGlobalSetup ();
|
2006-07-13 02:08:39 +00:00
|
|
|
atterm (FreeKeySections);
|
2006-09-20 02:00:19 +00:00
|
|
|
atterm (M_SaveDefaultsFinal);
|
2006-02-24 04:48:15 +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 hdpi;
|
|
|
|
unsigned short vdpi;
|
|
|
|
|
|
|
|
unsigned char palette[48];
|
|
|
|
|
|
|
|
char reserved;
|
|
|
|
char color_planes;
|
|
|
|
unsigned short bytes_per_line;
|
|
|
|
unsigned short palette_type;
|
|
|
|
|
|
|
|
char filler[58];
|
|
|
|
} pcx_t;
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// WritePCXfile
|
|
|
|
//
|
2008-01-12 06:27:13 +00:00
|
|
|
void WritePCXfile (FILE *file, const BYTE *buffer, const PalEntry *palette,
|
|
|
|
ESSType color_type, int width, int height, int pitch)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-01-12 06:27:13 +00:00
|
|
|
BYTE temprow[MAXWIDTH * 3];
|
|
|
|
const BYTE *data;
|
2006-02-24 04:48:15 +00:00
|
|
|
int x, y;
|
|
|
|
int runlen;
|
2008-01-12 06:27:13 +00:00
|
|
|
int bytes_per_row_minus_one;
|
2006-02-24 04:48:15 +00:00
|
|
|
BYTE color;
|
|
|
|
pcx_t pcx;
|
|
|
|
|
|
|
|
pcx.manufacturer = 10; // PCX id
|
2008-01-12 06:27:13 +00:00
|
|
|
pcx.version = 5; // 256 (or more) colors
|
2006-02-24 04:48:15 +00:00
|
|
|
pcx.encoding = 1;
|
2008-01-12 06:27:13 +00:00
|
|
|
pcx.bits_per_pixel = 8; // 256 (or more) colors
|
2006-02-24 04:48:15 +00:00
|
|
|
pcx.xmin = 0;
|
|
|
|
pcx.ymin = 0;
|
|
|
|
pcx.xmax = LittleShort(width-1);
|
|
|
|
pcx.ymax = LittleShort(height-1);
|
|
|
|
pcx.hdpi = LittleShort(75);
|
|
|
|
pcx.vdpi = LittleShort(75);
|
|
|
|
memset (pcx.palette, 0, sizeof(pcx.palette));
|
|
|
|
pcx.reserved = 0;
|
2008-01-12 06:27:13 +00:00
|
|
|
pcx.color_planes = (color_type == SS_PAL) ? 1 : 3; // chunky image
|
2006-02-24 04:48:15 +00:00
|
|
|
pcx.bytes_per_line = width + (width & 1);
|
|
|
|
pcx.palette_type = 1; // not a grey scale
|
|
|
|
memset (pcx.filler, 0, sizeof(pcx.filler));
|
|
|
|
|
|
|
|
fwrite (&pcx, 128, 1, file);
|
|
|
|
|
2008-01-12 06:27:13 +00:00
|
|
|
bytes_per_row_minus_one = ((color_type == SS_PAL) ? width : width * 3) - 1;
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
// pack the image
|
|
|
|
for (y = height; y > 0; y--)
|
|
|
|
{
|
2008-01-12 06:27:13 +00:00
|
|
|
switch (color_type)
|
|
|
|
{
|
|
|
|
case SS_PAL:
|
|
|
|
data = buffer;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SS_RGB:
|
|
|
|
// Unpack RGB into separate planes.
|
|
|
|
for (int i = 0; i < width; ++i)
|
|
|
|
{
|
|
|
|
temprow[i ] = buffer[i*3];
|
|
|
|
temprow[i + width ] = buffer[i*3 + 1];
|
|
|
|
temprow[i + width * 2] = buffer[i*3 + 2];
|
|
|
|
}
|
|
|
|
data = temprow;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case SS_BGRA:
|
|
|
|
// Unpack RGB into separate planes, discarding A.
|
|
|
|
for (int i = 0; i < width; ++i)
|
|
|
|
{
|
|
|
|
temprow[i ] = buffer[i*4 + 2];
|
|
|
|
temprow[i + width ] = buffer[i*4 + 1];
|
|
|
|
temprow[i + width * 2] = buffer[i*4];
|
|
|
|
}
|
|
|
|
data = temprow;
|
|
|
|
break;
|
2008-01-12 17:35:32 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
// Should never happen.
|
|
|
|
return;
|
2008-01-12 06:27:13 +00:00
|
|
|
}
|
|
|
|
buffer += pitch;
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
color = *data++;
|
|
|
|
runlen = 1;
|
|
|
|
|
2008-01-12 06:27:13 +00:00
|
|
|
for (x = bytes_per_row_minus_one; x > 0; x--)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
if (*data == color)
|
|
|
|
{
|
|
|
|
runlen++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (runlen > 1 || color >= 0xc0)
|
|
|
|
{
|
|
|
|
while (runlen > 63)
|
|
|
|
{
|
|
|
|
putc (0xff, file);
|
|
|
|
putc (color, file);
|
|
|
|
runlen -= 63;
|
|
|
|
}
|
|
|
|
if (runlen > 0)
|
|
|
|
{
|
|
|
|
putc (0xc0 + runlen, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (runlen > 0)
|
|
|
|
{
|
|
|
|
putc (color, file);
|
|
|
|
}
|
|
|
|
runlen = 1;
|
|
|
|
color = *data;
|
|
|
|
}
|
|
|
|
data++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (runlen > 1 || color >= 0xc0)
|
|
|
|
{
|
|
|
|
while (runlen > 63)
|
|
|
|
{
|
|
|
|
putc (0xff, file);
|
|
|
|
putc (color, file);
|
|
|
|
runlen -= 63;
|
|
|
|
}
|
|
|
|
if (runlen > 0)
|
|
|
|
{
|
|
|
|
putc (0xc0 + runlen, file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (runlen > 0)
|
|
|
|
{
|
|
|
|
putc (color, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width & 1)
|
|
|
|
putc (0, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the palette
|
2008-01-12 06:27:13 +00:00
|
|
|
if (color_type == SS_PAL)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-01-12 06:27:13 +00:00
|
|
|
putc (12, file); // palette ID byte
|
|
|
|
for (x = 0; x < 256; x++, palette++)
|
|
|
|
{
|
|
|
|
putc (palette->r, file);
|
|
|
|
putc (palette->g, file);
|
|
|
|
putc (palette->b, file);
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// WritePNGfile
|
|
|
|
//
|
2008-01-12 06:27:13 +00:00
|
|
|
void WritePNGfile (FILE *file, const BYTE *buffer, const PalEntry *palette,
|
|
|
|
ESSType color_type, int width, int height, int pitch)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-01-12 06:27:13 +00:00
|
|
|
if (!M_CreatePNG (file, buffer, palette, color_type, width, height, pitch) ||
|
2007-12-26 09:56:09 +00:00
|
|
|
!M_AppendPNGText (file, "Software", GAMENAME DOTVERSIONSTR) ||
|
2006-02-24 04:48:15 +00:00
|
|
|
!M_FinishPNG (file))
|
|
|
|
{
|
|
|
|
Printf ("Could not create screenshot.\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// M_ScreenShot
|
|
|
|
//
|
2006-09-14 00:02:31 +00:00
|
|
|
static bool FindFreeName (FString &fullname, const char *extension)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2006-05-03 22:45:01 +00:00
|
|
|
FString lbmname;
|
2006-02-24 04:48:15 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i <= 9999; i++)
|
|
|
|
{
|
2008-09-21 18:02:38 +00:00
|
|
|
const char *gamename = GameNames[gameinfo.gametype];
|
2008-02-05 05:29:31 +00:00
|
|
|
|
|
|
|
time_t now;
|
|
|
|
tm *tm;
|
|
|
|
|
|
|
|
time(&now);
|
|
|
|
tm = localtime(&now);
|
|
|
|
|
|
|
|
if (tm == NULL)
|
|
|
|
{
|
2008-09-21 18:02:38 +00:00
|
|
|
lbmname.Format ("%sScreenshot_%s_%04d.%s", fullname.GetChars(), gamename, i, extension);
|
2008-02-05 05:29:31 +00:00
|
|
|
}
|
|
|
|
else if (i == 0)
|
|
|
|
{
|
2008-09-21 18:02:38 +00:00
|
|
|
lbmname.Format ("%sScreenshot_%s_%04d%02d%02d_%02d%02d%02d.%s", fullname.GetChars(), gamename,
|
2008-02-05 05:29:31 +00:00
|
|
|
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
|
|
|
|
tm->tm_hour, tm->tm_min, tm->tm_sec,
|
|
|
|
extension);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-09-21 18:02:38 +00:00
|
|
|
lbmname.Format ("%sScreenshot_%s_%04d%02d%02d_%02d%02d%02d_%02d.%s", fullname.GetChars(), gamename,
|
2008-02-05 05:29:31 +00:00
|
|
|
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
|
|
|
|
tm->tm_hour, tm->tm_min, tm->tm_sec,
|
|
|
|
i, extension);
|
|
|
|
}
|
|
|
|
|
2006-02-24 04:48:15 +00:00
|
|
|
if (!FileExists (lbmname.GetChars()))
|
|
|
|
{
|
|
|
|
fullname = lbmname;
|
|
|
|
return true; // file doesn't exist
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-08-15 04:34:35 +00:00
|
|
|
void M_ScreenShot (const char *filename)
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2008-01-12 06:27:13 +00:00
|
|
|
FILE *file;
|
2006-05-03 22:45:01 +00:00
|
|
|
FString autoname;
|
2008-01-12 06:27:13 +00:00
|
|
|
bool writepcx = (stricmp (screenshot_type, "pcx") == 0); // PNG is the default
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
// find a file name to save it to
|
2006-08-15 04:34:35 +00:00
|
|
|
if (filename == NULL || filename[0] == '\0')
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
|
|
|
#ifndef unix
|
2008-03-12 02:56:11 +00:00
|
|
|
if (Args->CheckParm ("-cdrom"))
|
2006-02-24 04:48:15 +00:00
|
|
|
{
|
2007-12-26 09:56:09 +00:00
|
|
|
autoname = CDROM_DIR "\\";
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
{
|
2009-02-08 02:52:43 +00:00
|
|
|
int dirlen;
|
|
|
|
autoname = Args->CheckValue("-shotdir");
|
|
|
|
if (autoname == NULL)
|
|
|
|
{
|
|
|
|
autoname = screenshot_dir;
|
|
|
|
}
|
|
|
|
dirlen = strlen(autoname);
|
2006-02-24 04:48:15 +00:00
|
|
|
if (dirlen == 0)
|
|
|
|
{
|
2009-02-08 02:52:43 +00:00
|
|
|
#ifdef unix
|
|
|
|
autoname = "~/.zdoom/screenshots/";
|
|
|
|
#else
|
2006-02-24 04:48:15 +00:00
|
|
|
autoname = progdir;
|
2009-02-08 02:52:43 +00:00
|
|
|
#endif
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
else if (dirlen > 0)
|
|
|
|
{
|
|
|
|
autoname = screenshot_dir;
|
|
|
|
if (autoname[dirlen-1] != '/' && autoname[dirlen-1] != '\\')
|
|
|
|
{
|
|
|
|
autoname += '/';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-08 02:52:43 +00:00
|
|
|
autoname = NicePath(autoname);
|
2006-02-24 04:48:15 +00:00
|
|
|
if (!FindFreeName (autoname, writepcx ? "pcx" : "png"))
|
|
|
|
{
|
|
|
|
Printf ("M_ScreenShot: Delete some screenshots\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
autoname = filename;
|
|
|
|
DefaultExtension (autoname, writepcx ? ".pcx" : ".png");
|
|
|
|
}
|
2006-05-14 14:30:13 +00:00
|
|
|
CreatePath(screenshot_dir);
|
2006-02-24 04:48:15 +00:00
|
|
|
|
|
|
|
// save the screenshot
|
2008-01-12 06:27:13 +00:00
|
|
|
const BYTE *buffer;
|
|
|
|
int pitch;
|
|
|
|
ESSType color_type;
|
2007-12-26 09:56:09 +00:00
|
|
|
|
2008-01-12 06:27:13 +00:00
|
|
|
screen->GetScreenshotBuffer(buffer, pitch, color_type);
|
|
|
|
if (buffer != NULL)
|
2007-12-26 09:56:09 +00:00
|
|
|
{
|
2008-01-12 06:27:13 +00:00
|
|
|
PalEntry palette[256];
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-01-12 06:27:13 +00:00
|
|
|
if (color_type == SS_PAL)
|
|
|
|
{
|
|
|
|
screen->GetFlashedPalette(palette);
|
|
|
|
}
|
|
|
|
file = fopen (autoname, "wb");
|
|
|
|
if (file == NULL)
|
|
|
|
{
|
|
|
|
Printf ("Could not open %s\n", autoname.GetChars());
|
|
|
|
screen->ReleaseScreenshotBuffer();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (writepcx)
|
|
|
|
{
|
|
|
|
WritePCXfile(file, buffer, palette, color_type,
|
|
|
|
screen->GetWidth(), screen->GetHeight(), pitch);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WritePNGfile(file, buffer, palette, color_type,
|
|
|
|
screen->GetWidth(), screen->GetHeight(), pitch);
|
|
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
screen->ReleaseScreenshotBuffer();
|
2006-02-24 04:48:15 +00:00
|
|
|
|
2008-01-12 06:27:13 +00:00
|
|
|
if (!screenshot_quiet)
|
|
|
|
{
|
2008-02-16 10:23:12 +00:00
|
|
|
int slash = -1;
|
|
|
|
if (!longsavemessages) slash = autoname.LastIndexOfAny(":/\\");
|
|
|
|
Printf ("Captured %s\n", autoname.GetChars()+slash+1);
|
2008-01-12 06:27:13 +00:00
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-01-12 06:27:13 +00:00
|
|
|
if (!screenshot_quiet)
|
|
|
|
{
|
|
|
|
Printf ("Could not create screenshot.\n");
|
|
|
|
}
|
2006-02-24 04:48:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CCMD (screenshot)
|
|
|
|
{
|
|
|
|
if (argv.argc() == 1)
|
|
|
|
G_ScreenShot (NULL);
|
|
|
|
else
|
|
|
|
G_ScreenShot (argv[1]);
|
|
|
|
}
|