actually replace everything correctly my dumbass forgor about the fast both strcmp and stricmp matter in sort stuff so we cannot use the fastcmp stuff there, which means we should not redifine stricmp as fasticmp in case something relies on that behaviour ugh
1988 lines
48 KiB
C
1988 lines
48 KiB
C
// SONIC ROBO BLAST 2
|
|
//-----------------------------------------------------------------------------
|
|
// Copyright (C) 1993-1996 by id Software, Inc.
|
|
// Copyright (C) 1998-2000 by DooM Legacy Team.
|
|
// Copyright (C) 1999-2018 by Sonic Team Junior.
|
|
//
|
|
// This program is free software distributed under the
|
|
// terms of the GNU General Public License, version 2.
|
|
// See the 'LICENSE' file for more details.
|
|
//-----------------------------------------------------------------------------
|
|
/// \file m_misc.h
|
|
/// \brief Commonly used routines
|
|
/// Default config file, PCX screenshots, file i/o
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
|
|
#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
|
|
// Ignore "argument might be clobbered by longjmp" warning in GCC
|
|
// (if libpng is compiled with setjmp error handling)
|
|
#pragma GCC diagnostic ignored "-Wclobbered"
|
|
#endif
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#include <errno.h>
|
|
|
|
// Extended map support.
|
|
#include <ctype.h>
|
|
|
|
#include "am_map.h"
|
|
|
|
#include "doomdef.h"
|
|
#include "g_game.h"
|
|
#include "m_misc.h"
|
|
#include "hu_stuff.h"
|
|
#include "st_stuff.h"
|
|
#include "v_video.h"
|
|
#include "z_zone.h"
|
|
#include "g_input.h"
|
|
#include "i_time.h"
|
|
#include "i_video.h"
|
|
#include "d_main.h"
|
|
#include "m_argv.h"
|
|
#include "i_system.h"
|
|
#include "command.h" // cv_execversion
|
|
|
|
#include "m_anigif.h"
|
|
|
|
// So that the screenshot menu auto-updates...
|
|
#include "m_menu.h"
|
|
|
|
#ifdef HWRENDER
|
|
#include "hardware/hw_main.h"
|
|
#endif
|
|
|
|
#ifdef HAVE_SDL
|
|
#include "sdl/hwsym_sdl.h"
|
|
#endif
|
|
|
|
#ifdef __linux__
|
|
#ifndef _LARGEFILE64_SOURCE
|
|
typedef off_t off64_t;
|
|
#endif
|
|
#endif
|
|
|
|
#if defined(__MINGW32__) && ((__GNUC__ > 7) || (__GNUC__ == 6 && __GNUC_MINOR__ >= 3)) && (__GNUC__ < 8)
|
|
#define PRIdS "u"
|
|
#elif defined(_WIN32) && !defined(__MINGW64__)
|
|
#define PRIdS "Iu"
|
|
#elif defined(_WIN32) && defined(__MINGW64__)
|
|
#define PRIdS PRIuPTR
|
|
#else
|
|
#define PRIdS "zu"
|
|
#endif
|
|
|
|
#ifdef HAVE_PNG
|
|
|
|
#ifndef _LARGEFILE64_SOURCE
|
|
#define _LARGEFILE64_SOURCE
|
|
#endif
|
|
|
|
#ifndef _LFS64_LARGEFILE
|
|
#define _LFS64_LARGEFILE
|
|
#endif
|
|
|
|
#ifndef _FILE_OFFSET_BITS
|
|
#define _FILE_OFFSET_BITS 0
|
|
#endif
|
|
|
|
#include "zlib.h"
|
|
#include "png.h"
|
|
#if (PNG_LIBPNG_VER_MAJOR > 1) || (PNG_LIBPNG_VER_MAJOR == 1 && PNG_LIBPNG_VER_MINOR >= 4)
|
|
#define NO_PNG_DEBUG // 1.4.0 move png_debug to pngpriv.h
|
|
#endif
|
|
#ifdef PNG_WRITE_SUPPORTED
|
|
#define USE_PNG // Only actually use PNG if write is supported.
|
|
// See hardware/hw_draw.c for a similar check to this one.
|
|
#endif
|
|
#endif
|
|
|
|
static CV_PossibleValue_t screenshot_cons_t[] = {{0, "Default"}, {1, "HOME"}, {2, "SRB2"}, {3, "CUSTOM"}, {0, NULL}};
|
|
consvar_t cv_screenshot_option = {"screenshot_option", "Default", CV_SAVE|CV_CALL, screenshot_cons_t, Screenshot_option_Onchange, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_screenshot_folder = {"screenshot_folder", "", CV_SAVE, NULL, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
static CV_PossibleValue_t moviemode_cons_t[] = {{MM_GIF, "GIF"}, {MM_SCREENSHOT, "Screenshots"}, {0, NULL}};
|
|
consvar_t cv_moviemode = {"moviemode_mode", "GIF", CV_SAVE|CV_CALL, moviemode_cons_t, Moviemode_mode_Onchange, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
consvar_t cv_movie_option = {"movie_option", "Default", CV_SAVE|CV_CALL, screenshot_cons_t, Moviemode_option_Onchange, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_movie_folder = {"movie_folder", "", CV_SAVE, NULL, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
static CV_PossibleValue_t zlib_mem_level_t[] = {
|
|
{1, "(Min Memory) 1"},
|
|
{2, "2"}, {3, "3"}, {4, "4"}, {5, "5"}, {6, "6"}, {7, "7"},
|
|
{8, "(Optimal) 8"}, //libpng Default
|
|
{9, "(Max Memory) 9"}, {0, NULL}};
|
|
|
|
static CV_PossibleValue_t zlib_level_t[] = {
|
|
{0, "No Compression"}, //Z_NO_COMPRESSION
|
|
{1, "(Fastest) 1"}, //Z_BEST_SPEED
|
|
{2, "2"}, {3, "3"}, {4, "4"}, {5, "5"},
|
|
{6, "(Optimal) 6"}, //Zlib Default
|
|
{7, "7"}, {8, "8"},
|
|
{9, "(Maximum) 9"}, //Z_BEST_COMPRESSION
|
|
{0, NULL}};
|
|
|
|
static CV_PossibleValue_t zlib_strategy_t[] = {
|
|
{0, "Normal"}, //Z_DEFAULT_STRATEGY
|
|
{1, "Filtered"}, //Z_FILTERED
|
|
{2, "Huffman Only"}, //Z_HUFFMAN_ONLY
|
|
{3, "RLE"}, //Z_RLE
|
|
{4, "Fixed"}, //Z_FIXED
|
|
{0, NULL}};
|
|
|
|
static CV_PossibleValue_t zlib_window_bits_t[] = {
|
|
#ifdef WBITS_8_OK
|
|
{8, "256"},
|
|
#endif
|
|
{9, "512"}, {10, "1k"}, {11, "2k"}, {12, "4k"}, {13, "8k"},
|
|
{14, "16k"}, {15, "32k"},
|
|
{0, NULL}};
|
|
|
|
// zlib memory usage is as follows:
|
|
// (1 << (zlib_window_bits+2)) + (1 << (zlib_level+9))
|
|
consvar_t cv_zlib_memory = {"png_memory_level", "7", CV_SAVE, zlib_mem_level_t, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_zlib_level = {"png_compress_level", "(Optimal) 6", CV_SAVE, zlib_level_t, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_zlib_strategy = {"png_strategy", "Normal", CV_SAVE, zlib_strategy_t, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
consvar_t cv_zlib_window_bits = {"png_window_size", "32k", CV_SAVE, zlib_window_bits_t, NULL, 0, NULL, NULL, 0, 0, NULL};
|
|
|
|
boolean takescreenshot = false; // Take a screenshot this tic
|
|
|
|
moviemode_t moviemode = MM_OFF;
|
|
|
|
/** Returns the map number for a map identified by the last two characters in
|
|
* its name.
|
|
*
|
|
* \param first The first character after MAP.
|
|
* \param second The second character after MAP.
|
|
* \return The map number, or 0 if no map corresponds to these characters.
|
|
* \sa G_BuildMapName
|
|
*/
|
|
INT32 M_MapNumber(char first, char second)
|
|
{
|
|
if (isdigit(first))
|
|
{
|
|
if (isdigit(second))
|
|
return ((INT32)first - '0') * 10 + ((INT32)second - '0');
|
|
return 0;
|
|
}
|
|
|
|
if (!isalpha(first))
|
|
return 0;
|
|
if (!isalnum(second))
|
|
return 0;
|
|
|
|
return 100 + ((INT32)tolower(first) - 'a') * 36 + (isdigit(second) ? ((INT32)second - '0') :
|
|
((INT32)tolower(second) - 'a') + 10);
|
|
}
|
|
|
|
// ==========================================================================
|
|
// FILE INPUT / OUTPUT
|
|
// ==========================================================================
|
|
|
|
//
|
|
// FIL_WriteFile
|
|
//
|
|
#ifndef O_BINARY
|
|
#define O_BINARY 0
|
|
#endif
|
|
|
|
/** Writes out a file.
|
|
*
|
|
* \param name Name of the file to write.
|
|
* \param source Memory location to write from.
|
|
* \param length How many bytes to write.
|
|
* \return True on success, false on failure.
|
|
*/
|
|
boolean FIL_WriteFile(char const *name, const void *source, size_t length)
|
|
{
|
|
FILE *handle = NULL;
|
|
size_t count;
|
|
|
|
//if (FIL_WriteFileOK(name))
|
|
handle = fopen(name, "w+b");
|
|
|
|
if (!handle)
|
|
return false;
|
|
|
|
count = fwrite(source, 1, length, handle);
|
|
fclose(handle);
|
|
|
|
if (count < length)
|
|
return false;
|
|
|
|
return true;
|
|
}
|
|
|
|
/** Reads in a file, appending a zero byte at the end.
|
|
*
|
|
* \param name Filename to read.
|
|
* \param buffer Pointer to a pointer, which will be set to the location of a
|
|
* newly allocated buffer holding the file's contents.
|
|
* \return Number of bytes read, not counting the zero byte added to the end,
|
|
* or 0 on error.
|
|
*/
|
|
size_t FIL_ReadFileTag(char const *name, UINT8 **buffer, INT32 tag)
|
|
{
|
|
FILE *handle = NULL;
|
|
size_t count, length;
|
|
UINT8 *buf;
|
|
|
|
if (FIL_ReadFileOK(name))
|
|
handle = fopen(name, "rb");
|
|
|
|
if (!handle)
|
|
return 0;
|
|
|
|
fseek(handle,0,SEEK_END);
|
|
length = ftell(handle);
|
|
fseek(handle,0,SEEK_SET);
|
|
|
|
buf = Z_Malloc(length + 1, tag, NULL);
|
|
count = fread(buf, 1, length, handle);
|
|
fclose(handle);
|
|
|
|
if (count < length)
|
|
{
|
|
Z_Free(buf);
|
|
return 0;
|
|
}
|
|
|
|
// append 0 byte for script text files
|
|
buf[length] = 0;
|
|
|
|
*buffer = buf;
|
|
return length;
|
|
}
|
|
|
|
/** Check if the filename exists
|
|
*
|
|
* \param name Filename to check.
|
|
* \return true if file exists, false if it doesn't.
|
|
*/
|
|
boolean FIL_FileExists(char const *name)
|
|
{
|
|
return access(name,0)+1; //F_OK
|
|
}
|
|
|
|
|
|
/** Check if the filename OK to write
|
|
*
|
|
* \param name Filename to check.
|
|
* \return true if file write-able, false if it doesn't.
|
|
*/
|
|
boolean FIL_WriteFileOK(char const *name)
|
|
{
|
|
return access(name,2)+1; //W_OK
|
|
}
|
|
|
|
|
|
/** Check if the filename OK to read
|
|
*
|
|
* \param name Filename to check.
|
|
* \return true if file read-able, false if it doesn't.
|
|
*/
|
|
boolean FIL_ReadFileOK(char const *name)
|
|
{
|
|
return access(name,4)+1; //R_OK
|
|
}
|
|
|
|
/** Check if the filename OK to read/write
|
|
*
|
|
* \param name Filename to check.
|
|
* \return true if file (read/write)-able, false if it doesn't.
|
|
*/
|
|
boolean FIL_FileOK(char const *name)
|
|
{
|
|
return access(name,6)+1; //R_OK|W_OK
|
|
}
|
|
|
|
/** Copies contents of one file to another
|
|
*
|
|
* \param src File to copy from
|
|
* \param dst File to write into
|
|
* \return true if copy was succesful, false if it doesn't.
|
|
*/
|
|
boolean FIL_CopyFile(const char *src, const char *dst)
|
|
{
|
|
FILE *srcFile = fopen(src, "rb");
|
|
|
|
if (srcFile == NULL)
|
|
return false;
|
|
|
|
FILE *dstFile = fopen(dst, "wb");
|
|
|
|
if (dstFile == NULL)
|
|
{
|
|
fclose(srcFile);
|
|
return false;
|
|
}
|
|
|
|
char buffer[1024];
|
|
size_t n;
|
|
|
|
while ((n = fread(buffer, 1, sizeof(buffer), srcFile)) > 0)
|
|
fwrite(buffer, 1, n, dstFile);
|
|
|
|
fclose(srcFile);
|
|
fclose(dstFile);
|
|
|
|
return true;
|
|
}
|
|
|
|
/** Checks if a pathname has a file extension and adds the extension provided
|
|
* if not.
|
|
*
|
|
* \param path Pathname to check.
|
|
* \param extension Extension to add if no extension is there.
|
|
*/
|
|
void FIL_DefaultExtension(char *path, const char *extension)
|
|
{
|
|
char *src;
|
|
|
|
// search for '.' from end to begin, add .EXT only when not found
|
|
src = path + strlen(path) - 1;
|
|
|
|
while (*src != '/' && src != path)
|
|
{
|
|
if (*src == '.')
|
|
return; // it has an extension
|
|
src--;
|
|
}
|
|
|
|
strcat(path, extension);
|
|
}
|
|
|
|
void FIL_ForceExtension(char *path, const char *extension)
|
|
{
|
|
char *src;
|
|
|
|
// search for '.' from end to begin, add .EXT only when not found
|
|
src = path + strlen(path) - 1;
|
|
|
|
while (*src != '/' && src != path)
|
|
{
|
|
if (*src == '.')
|
|
{
|
|
*src = '\0';
|
|
break; // it has an extension
|
|
}
|
|
src--;
|
|
}
|
|
|
|
strcat(path, extension);
|
|
}
|
|
|
|
/** Checks if a filename extension is found.
|
|
* Lump names do not contain dots.
|
|
*
|
|
* \param in String to check.
|
|
* \return True if an extension is found, otherwise false.
|
|
*/
|
|
boolean FIL_CheckExtension(const char *in)
|
|
{
|
|
while (*in++)
|
|
if (*in == '.')
|
|
return true;
|
|
|
|
return false;
|
|
}
|
|
|
|
// ==========================================================================
|
|
// CONFIGURATION FILE
|
|
// ==========================================================================
|
|
|
|
//
|
|
// DEFAULTS
|
|
//
|
|
|
|
char configfile[MAX_WADPATH];
|
|
|
|
// ==========================================================================
|
|
// CONFIGURATION
|
|
// ==========================================================================
|
|
|
|
/** Saves a player's config, possibly to a particular file.
|
|
*
|
|
* \sa Command_LoadConfig_f
|
|
*/
|
|
void Command_SaveConfig_f(void)
|
|
{
|
|
char tmpstr[MAX_WADPATH];
|
|
|
|
if (COM_Argc() < 2)
|
|
{
|
|
CONS_Printf(M_GetText("saveconfig <filename[.cfg]> [-silent] : save config to a file\n"));
|
|
return;
|
|
}
|
|
strcpy(tmpstr, COM_Argv(1));
|
|
FIL_ForceExtension(tmpstr, ".cfg");
|
|
|
|
M_SaveConfig(tmpstr);
|
|
if (!fasticmp(COM_Argv(2), "-silent"))
|
|
CONS_Printf(M_GetText("config saved as %s\n"), configfile);
|
|
}
|
|
|
|
/** Loads a game config, possibly from a particular file.
|
|
*
|
|
* \sa Command_SaveConfig_f, Command_ChangeConfig_f
|
|
*/
|
|
void Command_LoadConfig_f(void)
|
|
{
|
|
if (COM_Argc() != 2)
|
|
{
|
|
CONS_Printf(M_GetText("loadconfig <filename[.cfg]> : load config from a file\n"));
|
|
return;
|
|
}
|
|
|
|
strcpy(configfile, COM_Argv(1));
|
|
FIL_ForceExtension(configfile, ".cfg");
|
|
|
|
// load default control
|
|
G_ClearAllControlKeys();
|
|
G_Controldefault(0);
|
|
|
|
// temporarily reset execversion to default
|
|
CV_ToggleExecVersion(true);
|
|
COM_BufInsertText(va("%s \"%s\"\n", cv_execversion.name, cv_execversion.defaultvalue));
|
|
CV_InitFilterVar();
|
|
|
|
// exec the config
|
|
COM_BufInsertText(va("exec \"%s\"\n", configfile));
|
|
|
|
// don't filter anymore vars and don't let this convsvar be changed
|
|
COM_BufInsertText(va("%s \"%d\"\n", cv_execversion.name, EXECVERSION));
|
|
CV_ToggleExecVersion(false);
|
|
}
|
|
|
|
/** Saves the current configuration and loads another.
|
|
*
|
|
* \sa Command_LoadConfig_f, Command_SaveConfig_f
|
|
*/
|
|
void Command_ChangeConfig_f(void)
|
|
{
|
|
if (COM_Argc() != 2)
|
|
{
|
|
CONS_Printf(M_GetText("changeconfig <filename[.cfg]> : save current config and load another\n"));
|
|
return;
|
|
}
|
|
|
|
COM_BufAddText(va("saveconfig \"%s\"\n", configfile));
|
|
COM_BufAddText(va("loadconfig \"%s\"\n", COM_Argv(1)));
|
|
}
|
|
|
|
/** Loads the default config file.
|
|
*
|
|
* \sa Command_LoadConfig_f
|
|
*/
|
|
void M_FirstLoadConfig(void)
|
|
{
|
|
// configfile is initialised by d_main when searching for the wad?
|
|
|
|
// check for a custom config file
|
|
if (M_CheckParm("-config") && M_IsNextParm())
|
|
{
|
|
strcpy(configfile, M_GetNextParm());
|
|
CONS_Printf(M_GetText("config file: %s\n"), configfile);
|
|
}
|
|
|
|
// load default control
|
|
G_Controldefault(0);
|
|
|
|
// register execversion here before we load any configs
|
|
CV_RegisterVar(&cv_execversion);
|
|
|
|
// temporarily reset execversion to default
|
|
// we shouldn't need to do this, but JUST in case...
|
|
CV_ToggleExecVersion(true);
|
|
COM_BufInsertText(va("%s \"%s\"\n", cv_execversion.name, cv_execversion.defaultvalue));
|
|
CV_InitFilterVar();
|
|
|
|
// load config, make sure those commands doesnt require the screen...
|
|
COM_BufInsertText(va("exec \"%s\"\n", configfile));
|
|
// no COM_BufExecute() needed; that does it right away
|
|
|
|
// don't filter anymore vars and don't let this convsvar be changed
|
|
COM_BufInsertText(va("%s \"%d\"\n", cv_execversion.name, EXECVERSION));
|
|
CV_ToggleExecVersion(false);
|
|
}
|
|
|
|
static boolean M_BackupConfig(const char *filename)
|
|
{
|
|
char backupfile[MAX_WADPATH+4];
|
|
|
|
snprintf(backupfile, sizeof backupfile, "%s.bak", filename);
|
|
backupfile[sizeof backupfile - 1] = '\0';
|
|
|
|
FILE *config = fopen(filename, "r");
|
|
|
|
if (config != NULL)
|
|
{
|
|
fclose(config);
|
|
if (FIL_CopyFile(filename, backupfile) == false)
|
|
{
|
|
CONS_Alert(CONS_WARNING,"Failed to create a backup of the configuration file. Will not attempt to write to file\n");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/** Saves the game configuration.
|
|
*
|
|
* \sa Command_SaveConfig_f
|
|
*/
|
|
void M_SaveConfig(const char *filename)
|
|
{
|
|
FILE *f;
|
|
char *filepath;
|
|
|
|
// make sure not to write back the config until it's been correctly loaded
|
|
if (!loaded_config)
|
|
return;
|
|
|
|
// can change the file name
|
|
if (filename)
|
|
{
|
|
if (!strstr(filename, ".cfg"))
|
|
{
|
|
CONS_Alert(CONS_NOTICE, M_GetText("Config filename must be .cfg\n"));
|
|
return;
|
|
}
|
|
|
|
// append srb2home to beginning of filename
|
|
// but check if srb2home isn't already there, first
|
|
if (!strstr(filename, srb2home))
|
|
filepath = va(pandf,srb2home, filename);
|
|
else
|
|
filepath = Z_StrDup(filename);
|
|
|
|
// If failed to backup, do not proceed
|
|
if (!M_BackupConfig(filepath))
|
|
return;
|
|
|
|
f = fopen(filepath, "w");
|
|
// change it only if valid
|
|
if (f)
|
|
strcpy(configfile, filepath);
|
|
else
|
|
{
|
|
CONS_Alert(CONS_ERROR, M_GetText("Couldn't save game config file %s\n"), filepath);
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (!strstr(configfile, ".cfg"))
|
|
{
|
|
CONS_Alert(CONS_NOTICE, M_GetText("Config filename must be .cfg\n"));
|
|
return;
|
|
}
|
|
|
|
// If failed to backup, do not proceed
|
|
if (!M_BackupConfig(configfile))
|
|
return;
|
|
|
|
f = fopen(configfile, "w");
|
|
if (!f)
|
|
{
|
|
CONS_Alert(CONS_ERROR, M_GetText("Couldn't save game config file %s\n"), configfile);
|
|
return;
|
|
}
|
|
}
|
|
|
|
// header message
|
|
fprintf(f, "// SRB2Kart configuration file.\n");
|
|
|
|
// print execversion FIRST, because subsequent consvars need to be filtered
|
|
// always print current EXECVERSION
|
|
fprintf(f, "%s \"%d\"\n", cv_execversion.name, EXECVERSION);
|
|
|
|
// FIXME: save key aliases if ever implemented..
|
|
|
|
CV_SaveVariables(f);
|
|
if (!dedicated) G_SaveKeySetting(f);
|
|
|
|
fclose(f);
|
|
}
|
|
|
|
// ==========================================================================
|
|
// SCREENSHOTS
|
|
// ==========================================================================
|
|
static UINT8 screenshot_palette[768];
|
|
static void M_CreateScreenShotPalette(void)
|
|
{
|
|
size_t i, j;
|
|
for (i = 0, j = 0; i < 768; i += 3, j++)
|
|
{
|
|
RGBA_t locpal = pLocalPalette[(max(st_palette,0)*256)+j];
|
|
screenshot_palette[i] = locpal.s.red;
|
|
screenshot_palette[i+1] = locpal.s.green;
|
|
screenshot_palette[i+2] = locpal.s.blue;
|
|
}
|
|
}
|
|
|
|
#if NUMSCREENS > 2
|
|
static const char *Newsnapshotfile(const char *pathname, const char *ext)
|
|
{
|
|
static char freename[13] = "kartXXXX.ext";
|
|
int i = 5000; // start in the middle: num screenshots divided by 2
|
|
int add = i; // how much to add or subtract if wrong; gets divided by 2 each time
|
|
int result; // -1 = guess too high, 0 = correct, 1 = guess too low
|
|
|
|
// find a file name to save it to
|
|
strcpy(freename+9,ext);
|
|
|
|
for (;;)
|
|
{
|
|
freename[4] = (char)('0' + (char)(i/1000));
|
|
freename[5] = (char)('0' + (char)((i/100)%10));
|
|
freename[6] = (char)('0' + (char)((i/10)%10));
|
|
freename[7] = (char)('0' + (char)(i%10));
|
|
|
|
if (FIL_WriteFileOK(va(pandf,pathname,freename))) // access succeeds
|
|
result = 1; // too low
|
|
else // access fails: equal or too high
|
|
{
|
|
if (!i)
|
|
break; // not too high, so it must be equal! YAY!
|
|
|
|
freename[4] = (char)('0' + (char)((i-1)/1000));
|
|
freename[5] = (char)('0' + (char)(((i-1)/100)%10));
|
|
freename[6] = (char)('0' + (char)(((i-1)/10)%10));
|
|
freename[7] = (char)('0' + (char)((i-1)%10));
|
|
if (!FIL_WriteFileOK(va(pandf,pathname,freename))) // access fails
|
|
result = -1; // too high
|
|
else
|
|
break; // not too high, so equal, YAY!
|
|
}
|
|
|
|
add /= 2;
|
|
|
|
if (!add) // don't get stuck at 5 due to truncation!
|
|
add = 1;
|
|
|
|
i += add * result;
|
|
|
|
if (i < 0 || i > 9999)
|
|
return NULL;
|
|
}
|
|
|
|
freename[4] = (char)('0' + (char)(i/1000));
|
|
freename[5] = (char)('0' + (char)((i/100)%10));
|
|
freename[6] = (char)('0' + (char)((i/10)%10));
|
|
freename[7] = (char)('0' + (char)(i%10));
|
|
|
|
return freename;
|
|
}
|
|
#endif
|
|
|
|
#ifdef HAVE_PNG
|
|
FUNCNORETURN static void PNG_error(png_structp PNG, png_const_charp pngtext)
|
|
{
|
|
//CONS_Debug(DBG_RENDER, "libpng error at %p: %s", PNG, pngtext);
|
|
I_Error("libpng error at %p: %s", PNG, pngtext);
|
|
}
|
|
|
|
static void PNG_warn(png_structp PNG, png_const_charp pngtext)
|
|
{
|
|
CONS_Debug(DBG_RENDER, "libpng warning at %p: %s", PNG, pngtext);
|
|
}
|
|
|
|
static void M_PNGhdr(png_structp png_ptr, png_infop png_info_ptr, PNG_CONST png_uint_32 width, PNG_CONST png_uint_32 height, PNG_CONST png_byte *palette)
|
|
{
|
|
const png_byte png_interlace = PNG_INTERLACE_NONE; //PNG_INTERLACE_ADAM7
|
|
if (palette)
|
|
{
|
|
png_colorp png_PLTE = png_malloc(png_ptr, sizeof(png_color)*256); //palette
|
|
const png_byte *pal = palette;
|
|
png_uint_16 i;
|
|
for (i = 0; i < 256; i++)
|
|
{
|
|
png_PLTE[i].red = *pal; pal++;
|
|
png_PLTE[i].green = *pal; pal++;
|
|
png_PLTE[i].blue = *pal; pal++;
|
|
}
|
|
png_set_IHDR(png_ptr, png_info_ptr, width, height, 8, PNG_COLOR_TYPE_PALETTE,
|
|
png_interlace, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
png_write_info_before_PLTE(png_ptr, png_info_ptr);
|
|
png_set_PLTE(png_ptr, png_info_ptr, png_PLTE, 256);
|
|
png_free(png_ptr, (png_voidp)png_PLTE); // safe in libpng-1.2.1+
|
|
png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_FILTER_NONE);
|
|
png_set_compression_strategy(png_ptr, Z_DEFAULT_STRATEGY);
|
|
}
|
|
else
|
|
{
|
|
png_set_IHDR(png_ptr, png_info_ptr, width, height, 8, PNG_COLOR_TYPE_RGB,
|
|
png_interlace, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
|
png_write_info_before_PLTE(png_ptr, png_info_ptr);
|
|
png_set_compression_strategy(png_ptr, Z_FILTERED);
|
|
}
|
|
}
|
|
|
|
static void M_PNGText(png_structp png_ptr, png_infop png_info_ptr, PNG_CONST png_byte movie)
|
|
{
|
|
#ifdef PNG_TEXT_SUPPORTED
|
|
#define SRB2PNGTXT 11 //PNG_KEYWORD_MAX_LENGTH(79) is the max
|
|
png_text png_infotext[SRB2PNGTXT];
|
|
char keytxt[SRB2PNGTXT][12] = {
|
|
"Title", "Description", "Playername", "Mapnum", "Mapname",
|
|
"Location", "Interface", "Render Mode", "Revision", "Build Date", "Build Time"};
|
|
char titletxt[] = "SRB2Kart " VERSIONSTRING;
|
|
png_charp playertxt = cv_playername.zstring;
|
|
char desctxt[] = "SRB2Kart Screenshot";
|
|
char Movietxt[] = "SRB2Kart Movie";
|
|
size_t i;
|
|
char interfacetxt[] =
|
|
#ifdef HAVE_SDL
|
|
"SDL";
|
|
#else
|
|
"Unknown";
|
|
#endif
|
|
char rendermodetxt[9];
|
|
char maptext[8];
|
|
char lvlttltext[48];
|
|
char locationtxt[40];
|
|
char ctrevision[40];
|
|
char ctdate[40];
|
|
char cttime[40];
|
|
|
|
switch (rendermode)
|
|
{
|
|
case render_soft:
|
|
strcpy(rendermodetxt, "Software");
|
|
break;
|
|
case render_opengl:
|
|
strcpy(rendermodetxt, "OpenGL");
|
|
break;
|
|
default: // Just in case
|
|
strcpy(rendermodetxt, "None");
|
|
break;
|
|
}
|
|
|
|
if (gamestate == GS_LEVEL)
|
|
snprintf(maptext, 8, "%s", G_BuildMapName(gamemap));
|
|
else
|
|
snprintf(maptext, 8, "Unknown");
|
|
|
|
if (gamestate == GS_LEVEL && mapheaderinfo[gamemap-1]->lvlttl[0] != '\0')
|
|
snprintf(lvlttltext, 48, "%s%s%s",
|
|
mapheaderinfo[gamemap-1]->lvlttl,
|
|
(strlen(mapheaderinfo[gamemap-1]->zonttl) > 0) ? va(" %s",mapheaderinfo[gamemap-1]->zonttl) : // SRB2kart
|
|
((mapheaderinfo[gamemap-1]->levelflags & LF_NOZONE) ? "" : " ZONE"),
|
|
(strlen(mapheaderinfo[gamemap-1]->actnum) > 0) ? va(" %s",mapheaderinfo[gamemap-1]->actnum) : "");
|
|
else
|
|
snprintf(lvlttltext, 48, "Unknown");
|
|
|
|
if (gamestate == GS_LEVEL && players[displayplayers[0]].mo)
|
|
snprintf(locationtxt, 40, "X:%d Y:%d Z:%d A:%d",
|
|
players[displayplayers[0]].mo->x>>FRACBITS,
|
|
players[displayplayers[0]].mo->y>>FRACBITS,
|
|
players[displayplayers[0]].mo->z>>FRACBITS,
|
|
FixedInt(AngleFixed(players[displayplayers[0]].mo->angle)));
|
|
else
|
|
snprintf(locationtxt, 40, "Unknown");
|
|
|
|
memset(png_infotext,0x00,sizeof (png_infotext));
|
|
|
|
for (i = 0; i < SRB2PNGTXT; i++)
|
|
png_infotext[i].key = keytxt[i];
|
|
|
|
png_infotext[0].text = titletxt;
|
|
if (movie)
|
|
png_infotext[1].text = Movietxt;
|
|
else
|
|
png_infotext[1].text = desctxt;
|
|
png_infotext[2].text = playertxt;
|
|
png_infotext[3].text = maptext;
|
|
png_infotext[4].text = lvlttltext;
|
|
png_infotext[5].text = locationtxt;
|
|
png_infotext[6].text = interfacetxt;
|
|
png_infotext[7].text = rendermodetxt;
|
|
png_infotext[8].text = strncpy(ctrevision, comprevision, sizeof(ctrevision)-1);
|
|
png_infotext[9].text = strncpy(ctdate, compdate, sizeof(ctdate)-1);
|
|
png_infotext[10].text = strncpy(cttime, comptime, sizeof(cttime)-1);
|
|
|
|
png_set_text(png_ptr, png_info_ptr, png_infotext, SRB2PNGTXT);
|
|
#undef SRB2PNGTXT
|
|
#endif
|
|
}
|
|
|
|
static inline void M_PNGImage(png_structp png_ptr, png_infop png_info_ptr, PNG_CONST png_uint_32 height, png_bytep png_buf)
|
|
{
|
|
png_uint_32 pitch = png_get_rowbytes(png_ptr, png_info_ptr);
|
|
png_bytepp row_pointers = png_malloc(png_ptr, height* sizeof (png_bytep));
|
|
png_uint_32 y;
|
|
for (y = 0; y < height; y++)
|
|
{
|
|
row_pointers[y] = png_buf;
|
|
png_buf += pitch;
|
|
}
|
|
png_write_image(png_ptr, row_pointers);
|
|
png_free(png_ptr, (png_voidp)row_pointers);
|
|
}
|
|
#endif
|
|
|
|
// ==========================================================================
|
|
// MOVIE MODE
|
|
// ==========================================================================
|
|
#if NUMSCREENS > 2
|
|
|
|
static inline moviemode_t M_StartMovieGIF(const char *pathname)
|
|
{
|
|
#ifdef HAVE_ANIGIF
|
|
const char *freename;
|
|
|
|
if (!(freename = Newsnapshotfile(pathname,"gif")))
|
|
{
|
|
CONS_Alert(CONS_ERROR, "Couldn't create GIF: no slots open in %s\n", pathname);
|
|
return MM_OFF;
|
|
}
|
|
|
|
if (!GIF_open(va(pandf,pathname,freename)))
|
|
{
|
|
CONS_Alert(CONS_ERROR, "Couldn't create GIF: error creating %s in %s\n", freename, pathname);
|
|
return MM_OFF;
|
|
}
|
|
return MM_GIF;
|
|
#else
|
|
// no GIF support exists
|
|
(void)pathname;
|
|
CONS_Alert(CONS_ERROR, "Couldn't create GIF: this build lacks GIF support\n");
|
|
return MM_OFF;
|
|
#endif
|
|
}
|
|
#endif
|
|
|
|
void M_StartMovie(void)
|
|
{
|
|
#if NUMSCREENS > 2
|
|
char pathname[MAX_WADPATH];
|
|
|
|
if (moviemode)
|
|
return;
|
|
|
|
if (cv_movie_option.value == 0)
|
|
strcpy(pathname, usehome ? srb2home : srb2path);
|
|
else if (cv_movie_option.value == 1)
|
|
strcpy(pathname, srb2home);
|
|
else if (cv_movie_option.value == 2)
|
|
strcpy(pathname, srb2path);
|
|
else if (cv_movie_option.value == 3 && *cv_movie_folder.string != '\0')
|
|
strcpy(pathname, cv_movie_folder.string);
|
|
|
|
if (cv_movie_option.value != 3)
|
|
{
|
|
strcat(pathname, PATHSEP"gifs"PATHSEP);
|
|
I_mkdir(pathname, 0755);
|
|
}
|
|
|
|
if (rendermode == render_none)
|
|
I_Error("Can't make a movie without a render system\n");
|
|
|
|
switch (cv_moviemode.value)
|
|
{
|
|
case MM_GIF:
|
|
moviemode = M_StartMovieGIF(pathname);
|
|
break;
|
|
case MM_SCREENSHOT:
|
|
moviemode = MM_SCREENSHOT;
|
|
break;
|
|
default: //???
|
|
return;
|
|
}
|
|
|
|
if (moviemode == MM_GIF)
|
|
CONS_Printf(M_GetText("Movie mode enabled (%s).\n"), "GIF");
|
|
else if (moviemode == MM_SCREENSHOT)
|
|
CONS_Printf(M_GetText("Movie mode enabled (%s).\n"), "screenshots");
|
|
#endif
|
|
}
|
|
|
|
void M_SaveFrame(void)
|
|
{
|
|
#if NUMSCREENS > 2
|
|
// paranoia: should be unnecessary without singletics
|
|
static tic_t oldtic = 0;
|
|
|
|
if (oldtic == I_GetTime())
|
|
return;
|
|
else
|
|
oldtic = I_GetTime();
|
|
|
|
switch (moviemode)
|
|
{
|
|
case MM_SCREENSHOT:
|
|
takescreenshot = true;
|
|
return;
|
|
case MM_GIF:
|
|
GIF_frame();
|
|
return;
|
|
default:
|
|
return;
|
|
}
|
|
#endif
|
|
}
|
|
|
|
void M_StopMovie(void)
|
|
{
|
|
#if NUMSCREENS > 2
|
|
switch (moviemode)
|
|
{
|
|
case MM_GIF:
|
|
if (!GIF_close())
|
|
return;
|
|
break;
|
|
case MM_SCREENSHOT:
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
moviemode = MM_OFF;
|
|
CONS_Printf(M_GetText("Movie mode disabled.\n"));
|
|
#endif
|
|
}
|
|
|
|
// ==========================================================================
|
|
// SCREEN SHOTS
|
|
// ==========================================================================
|
|
#ifdef USE_PNG
|
|
/** Writes a PNG file to disk.
|
|
*
|
|
* \param filename Filename to write to.
|
|
* \param data The image data.
|
|
* \param width Width of the picture.
|
|
* \param height Height of the picture.
|
|
* \param palette Palette of image data.
|
|
* \note if palette is NULL, BGR888 format
|
|
*/
|
|
boolean M_SavePNG(const char *filename, void *data, int width, int height, const UINT8 *palette)
|
|
{
|
|
png_structp png_ptr;
|
|
png_infop png_info_ptr;
|
|
PNG_CONST png_byte *PLTE = (const png_byte *)palette;
|
|
#ifdef PNG_SETJMP_SUPPORTED
|
|
#ifdef USE_FAR_KEYWORD
|
|
jmp_buf jmpbuf;
|
|
#endif
|
|
#endif
|
|
png_FILE_p png_FILE;
|
|
|
|
png_FILE = fopen(filename,"wb");
|
|
if (!png_FILE)
|
|
{
|
|
CONS_Debug(DBG_RENDER, "M_SavePNG: Error on opening %s for write\n", filename);
|
|
return false;
|
|
}
|
|
|
|
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, PNG_error, PNG_warn);
|
|
if (!png_ptr)
|
|
{
|
|
CONS_Debug(DBG_RENDER, "M_SavePNG: Error on initialize libpng\n");
|
|
fclose(png_FILE);
|
|
remove(filename);
|
|
return false;
|
|
}
|
|
|
|
png_info_ptr = png_create_info_struct(png_ptr);
|
|
if (!png_info_ptr)
|
|
{
|
|
CONS_Debug(DBG_RENDER, "M_SavePNG: Error on allocate for libpng\n");
|
|
png_destroy_write_struct(&png_ptr, NULL);
|
|
fclose(png_FILE);
|
|
remove(filename);
|
|
return false;
|
|
}
|
|
|
|
#ifdef USE_FAR_KEYWORD
|
|
if (setjmp(jmpbuf))
|
|
#else
|
|
if (setjmp(png_jmpbuf(png_ptr)))
|
|
#endif
|
|
{
|
|
//CONS_Debug(DBG_RENDER, "libpng write error on %s\n", filename);
|
|
png_destroy_write_struct(&png_ptr, &png_info_ptr);
|
|
fclose(png_FILE);
|
|
remove(filename);
|
|
return false;
|
|
}
|
|
#ifdef USE_FAR_KEYWORD
|
|
png_memcpy(png_jmpbuf(png_ptr),jmpbuf, sizeof (jmp_buf));
|
|
#endif
|
|
png_init_io(png_ptr, png_FILE);
|
|
|
|
#ifdef PNG_SET_USER_LIMITS_SUPPORTED
|
|
png_set_user_limits(png_ptr, MAXVIDWIDTH, MAXVIDHEIGHT);
|
|
#endif
|
|
|
|
//png_set_filter(png_ptr, 0, PNG_ALL_FILTERS);
|
|
|
|
png_set_compression_level(png_ptr, cv_zlib_level.value);
|
|
png_set_compression_mem_level(png_ptr, cv_zlib_memory.value);
|
|
png_set_compression_strategy(png_ptr, cv_zlib_strategy.value);
|
|
png_set_compression_window_bits(png_ptr, cv_zlib_window_bits.value);
|
|
|
|
M_PNGhdr(png_ptr, png_info_ptr, width, height, PLTE);
|
|
|
|
M_PNGText(png_ptr, png_info_ptr, false);
|
|
|
|
png_write_info(png_ptr, png_info_ptr);
|
|
|
|
M_PNGImage(png_ptr, png_info_ptr, height, data);
|
|
|
|
png_write_end(png_ptr, png_info_ptr);
|
|
png_destroy_write_struct(&png_ptr, &png_info_ptr);
|
|
|
|
fclose(png_FILE);
|
|
return true;
|
|
}
|
|
#else
|
|
/** PCX file structure.
|
|
*/
|
|
typedef struct
|
|
{
|
|
UINT8 manufacturer;
|
|
UINT8 version;
|
|
UINT8 encoding;
|
|
UINT8 bits_per_pixel;
|
|
|
|
UINT16 xmin, ymin;
|
|
UINT16 xmax, ymax;
|
|
UINT16 hres, vres;
|
|
UINT8 palette[48];
|
|
|
|
UINT8 reserved;
|
|
UINT8 color_planes;
|
|
UINT16 bytes_per_line;
|
|
UINT16 palette_type;
|
|
|
|
char filler[58];
|
|
UINT8 data; ///< Unbounded; used for all picture data.
|
|
} pcx_t;
|
|
|
|
/** Writes a PCX file to disk.
|
|
*
|
|
* \param filename Filename to write to.
|
|
* \param data The image data.
|
|
* \param width Width of the picture.
|
|
* \param height Height of the picture.
|
|
* \param palette Palette of image data
|
|
*/
|
|
#if NUMSCREENS > 2
|
|
static boolean WritePCXfile(const char *filename, const UINT8 *data, int width, int height, const UINT8 *palette)
|
|
{
|
|
int i;
|
|
size_t length;
|
|
pcx_t *pcx;
|
|
UINT8 *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 = 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->reserved = 0;
|
|
pcx->color_planes = 1; // chunky image
|
|
pcx->bytes_per_line = SHORT(width);
|
|
pcx->palette_type = SHORT(1); // 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 - (UINT8 *)pcx;
|
|
i = FIL_WriteFile(filename, pcx, length);
|
|
|
|
Z_Free(pcx);
|
|
return i;
|
|
}
|
|
#endif
|
|
#endif
|
|
|
|
void M_ScreenShot(void)
|
|
{
|
|
takescreenshot = true;
|
|
}
|
|
|
|
/** Takes a screenshot.
|
|
* The screenshot is saved as "srb2xxxx.png" where xxxx is the lowest
|
|
* four-digit number for which a file does not already exist.
|
|
*
|
|
* \sa HWR_ScreenShot
|
|
*/
|
|
void M_DoScreenShot(void)
|
|
{
|
|
#if NUMSCREENS > 2
|
|
const char *freename = NULL;
|
|
char pathname[MAX_WADPATH];
|
|
boolean ret = false;
|
|
UINT8 *linear = NULL;
|
|
UINT8 *palette = NULL;
|
|
|
|
// Don't take multiple screenshots, obviously
|
|
takescreenshot = false;
|
|
|
|
// how does one take a screenshot without a render system?
|
|
if (rendermode == render_none)
|
|
return;
|
|
|
|
if (cv_screenshot_option.value == 0)
|
|
strcpy(pathname, usehome ? srb2home : srb2path);
|
|
else if (cv_screenshot_option.value == 1)
|
|
strcpy(pathname, srb2home);
|
|
else if (cv_screenshot_option.value == 2)
|
|
strcpy(pathname, srb2path);
|
|
else if (cv_screenshot_option.value == 3 && *cv_screenshot_folder.string != '\0')
|
|
strcpy(pathname, cv_screenshot_folder.string);
|
|
|
|
if (cv_screenshot_option.value != 3)
|
|
{
|
|
strcat(pathname, PATHSEP"screenshots"PATHSEP);
|
|
I_mkdir(pathname, 0755);
|
|
}
|
|
|
|
#ifdef USE_PNG
|
|
freename = Newsnapshotfile(pathname,"png");
|
|
#else
|
|
if (rendermode == render_soft)
|
|
freename = Newsnapshotfile(pathname,"pcx");
|
|
else if (rendermode == render_opengl)
|
|
freename = Newsnapshotfile(pathname,"tga");
|
|
#endif
|
|
|
|
if (!freename)
|
|
goto failure;
|
|
|
|
if (rendermode == render_soft)
|
|
{
|
|
// munge planar buffer to linear
|
|
linear = vid.screens[2];
|
|
I_ReadScreen(linear, 1);
|
|
M_CreateScreenShotPalette();
|
|
palette = screenshot_palette;
|
|
}
|
|
#ifdef HWRENDER
|
|
else if (rendermode == render_opengl)
|
|
{
|
|
linear = HWR_GetScreenshot(1);
|
|
palette = NULL;
|
|
}
|
|
#endif
|
|
else
|
|
{
|
|
I_Error("rendermode %d is not valid", rendermode);
|
|
}
|
|
|
|
#ifdef USE_PNG
|
|
ret = M_SavePNG(va(pandf,pathname,freename), linear, vid.width, vid.height, palette);
|
|
#else
|
|
ret = WritePCXfile(va(pandf,pathname,freename), linear, vid.width, vid.height, palette);
|
|
#endif
|
|
|
|
failure:
|
|
if (ret)
|
|
{
|
|
if (moviemode != MM_SCREENSHOT)
|
|
CONS_Printf(M_GetText("Screen shot %s saved in %s\n"), freename, pathname);
|
|
}
|
|
else
|
|
{
|
|
if (freename)
|
|
CONS_Alert(CONS_ERROR, M_GetText("Couldn't create screen shot %s in %s\n"), freename, pathname);
|
|
else
|
|
CONS_Alert(CONS_ERROR, M_GetText("Couldn't create screen shot in %s (all 10000 slots used!)\n"), pathname);
|
|
|
|
if (moviemode == MM_SCREENSHOT)
|
|
M_StopMovie();
|
|
}
|
|
#endif
|
|
}
|
|
|
|
boolean M_ScreenshotResponder(event_t *ev)
|
|
{
|
|
INT32 ch = -1;
|
|
if (dedicated || ev->type != ev_keydown)
|
|
return false;
|
|
|
|
ch = ev->data1;
|
|
|
|
if (ch >= KEY_MOUSE1 && menuactive) // If it's not a keyboard key, then don't allow it in the menus!
|
|
return false;
|
|
|
|
if (ch == KEY_F8 || ch == gamecontrol[0][gc_screenshot][0] || ch == gamecontrol[0][gc_screenshot][1]) // remappable F8
|
|
M_ScreenShot();
|
|
else if (ch == KEY_F9 || ch == gamecontrol[0][gc_recordgif][0] || ch == gamecontrol[0][gc_recordgif][1]) // remappable F9
|
|
((moviemode) ? M_StopMovie : M_StartMovie)();
|
|
else
|
|
return false;
|
|
return true;
|
|
}
|
|
|
|
|
|
void M_ScrollString(const char name[], size_t len, char result[], size_t maxlen, tic_t timer)
|
|
{
|
|
// How much should we scroll. Not sure why +1 is needed, but without it this function skips 2
|
|
// characters at once sometimes
|
|
const size_t amount = len - maxlen + 1;
|
|
|
|
// Note: anything above 17 will cause zero division
|
|
const size_t MAXSPEED = 6;
|
|
const tic_t t = timer / (35/min(amount, MAXSPEED));
|
|
|
|
const size_t state = (t / amount) % 4;
|
|
|
|
switch (state)
|
|
{
|
|
// Show beginning of the name
|
|
case 0:
|
|
memcpy(result, name, maxlen-1);
|
|
break;
|
|
|
|
// Scroll towards end of the name
|
|
case 1:
|
|
{
|
|
const size_t advance = t % amount;
|
|
memcpy(result, name+advance, maxlen-1);
|
|
}
|
|
break;
|
|
|
|
// Show end of the name
|
|
case 2:
|
|
memcpy(result, name+len+1-maxlen, maxlen-1);
|
|
break;
|
|
|
|
// Scroll towards start of the name
|
|
case 3:
|
|
{
|
|
const size_t advance = t % amount;
|
|
memcpy(result, name+len+1-maxlen-advance, maxlen-1);
|
|
}
|
|
break;
|
|
}
|
|
|
|
// Technically not necessary, since it gets set again after function call, but just in case
|
|
result[maxlen] = 0;
|
|
}
|
|
|
|
void M_MinimapGenerate(void)
|
|
{
|
|
#ifdef USE_PNG
|
|
char *filepath;
|
|
boolean ret = false;
|
|
minigen_t *minigen = NULL;
|
|
size_t option_scale;
|
|
INT32 mul = 1;
|
|
|
|
if (gamestate != GS_LEVEL)
|
|
{
|
|
CONS_Alert(CONS_ERROR, "You must be in a level to generate a preliminary minimap!\n");
|
|
return;
|
|
}
|
|
|
|
if (automapactive)
|
|
{
|
|
CONS_Alert(CONS_ERROR, "The automap is active! Please deactivate it and try again.\n");
|
|
return;
|
|
}
|
|
|
|
option_scale = COM_CheckPartialParm("-m");
|
|
|
|
if (option_scale)
|
|
{
|
|
if (COM_Argc() < option_scale + 2)/* no argument after? */
|
|
{
|
|
CONS_Alert(CONS_ERROR,
|
|
"No multiplier follows parameter '%s'.\n",
|
|
COM_Argv(option_scale));
|
|
return;
|
|
}
|
|
|
|
mul = atoi(COM_Argv(option_scale + 1));
|
|
|
|
if (mul < 1 || mul > 10)
|
|
{
|
|
CONS_Alert(CONS_ERROR,
|
|
"Multiplier %d must be within range 1-10.\n",
|
|
mul);
|
|
return;
|
|
}
|
|
|
|
filepath = va("%s" PATHSEP "%s-MINIMAP-%d.png", srb2home, G_BuildMapName(gamemap), mul);
|
|
}
|
|
else
|
|
{
|
|
filepath = va("%s" PATHSEP "%s-MINIMAP.png", srb2home, G_BuildMapName(gamemap));
|
|
}
|
|
|
|
minigen = AM_MinimapGenerate(mul);
|
|
|
|
if (minigen == NULL || minigen->buf == NULL)
|
|
goto failure;
|
|
|
|
M_CreateScreenShotPalette();
|
|
ret = M_SavePNG(filepath, minigen->buf, minigen->w, minigen->h, screenshot_palette);
|
|
|
|
failure:
|
|
if (minigen->buf != NULL)
|
|
free(minigen->buf);
|
|
|
|
if (ret)
|
|
{
|
|
CONS_Printf(M_GetText("%s saved.\nRemember that this is not a complete minimap,\nand must be edited before putting in-game.\n"), filepath);
|
|
if (mul != 1)
|
|
{
|
|
CONS_Printf("You should divide its size by %d!\n", mul);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
CONS_Alert(CONS_ERROR, M_GetText("Couldn't create %s\n"), filepath);
|
|
}
|
|
#endif //#ifdef USE_PNG
|
|
}
|
|
|
|
// ==========================================================================
|
|
// MISC STRING FUNCTIONS
|
|
// ==========================================================================
|
|
|
|
/** Returns a temporary string made out of varargs.
|
|
* For use with CONS_Printf().
|
|
*
|
|
* \param format Format string.
|
|
* \return Pointer to a static buffer of 1024 characters, containing the
|
|
* resulting string.
|
|
*/
|
|
char *va(const char *format, ...)
|
|
{
|
|
va_list argptr;
|
|
static char string[1024];
|
|
|
|
va_start(argptr, format);
|
|
vsnprintf(string, 1024, format, argptr);
|
|
va_end(argptr);
|
|
|
|
return string;
|
|
}
|
|
|
|
/** Creates a string in the first argument that is the second argument followed
|
|
* by the third argument followed by the first argument.
|
|
* Useful for making filenames with full path. s1 = s2+s3+s1
|
|
*
|
|
* \param s1 First string, suffix, and destination.
|
|
* \param s2 Second string. Ends up first in the result.
|
|
* \param s3 Third string. Ends up second in the result.
|
|
*/
|
|
void strcatbf(char *s1, const char *s2, const char *s3)
|
|
{
|
|
char tmp[1024];
|
|
|
|
strcpy(tmp, s1);
|
|
strcpy(s1, s2);
|
|
strcat(s1, s3);
|
|
strcat(s1, tmp);
|
|
}
|
|
|
|
/** Converts an ASCII Hex string into an integer. Thanks, Borland!
|
|
* <Inuyasha> I don't know if this belongs here specifically, but it sure
|
|
* doesn't belong in p_spec.c, that's for sure
|
|
*
|
|
* \param hexStg Hexadecimal string.
|
|
* \return an Integer based off the contents of the string.
|
|
*/
|
|
INT32 axtoi(const char *hexStg)
|
|
{
|
|
INT32 n = 0;
|
|
INT32 m = 0;
|
|
INT32 count;
|
|
INT32 intValue = 0;
|
|
INT32 digit[8];
|
|
while (n < 8)
|
|
{
|
|
if (hexStg[n] == '\0')
|
|
break;
|
|
if (hexStg[n] >= '0' && hexStg[n] <= '9') // 0-9
|
|
digit[n] = (hexStg[n] & 0x0f);
|
|
else if (hexStg[n] >= 'a' && hexStg[n] <= 'f') // a-f
|
|
digit[n] = (hexStg[n] & 0x0f) + 9;
|
|
else if (hexStg[n] >= 'A' && hexStg[n] <= 'F') // A-F
|
|
digit[n] = (hexStg[n] & 0x0f) + 9;
|
|
else
|
|
break;
|
|
n++;
|
|
}
|
|
count = n;
|
|
m = n - 1;
|
|
n = 0;
|
|
while (n < count)
|
|
{
|
|
intValue = intValue | (digit[n] << (m << 2));
|
|
m--;
|
|
n++;
|
|
}
|
|
return intValue;
|
|
}
|
|
|
|
void CopyCaretColors(char *p, const char *s, int n)
|
|
{
|
|
char *t;
|
|
int m;
|
|
int c;
|
|
|
|
if (!n)
|
|
return;
|
|
|
|
while (( t = strchr(s, '^') ))
|
|
{
|
|
m = ( t - s );
|
|
|
|
if (m >= n)
|
|
{
|
|
memcpy(p, s, n);
|
|
return;
|
|
}
|
|
else
|
|
memcpy(p, s, m);
|
|
|
|
p += m;
|
|
n -= m;
|
|
s += m;
|
|
|
|
if (!n)
|
|
return;
|
|
|
|
if (s[1])
|
|
{
|
|
c = toupper(s[1]);
|
|
if (isdigit(c))
|
|
c = 0x80 + ( c - '0' );
|
|
else if (c >= 'A' && c <= 'F')
|
|
c = 0x80 + ( c - 'A' );
|
|
else
|
|
c = 0;
|
|
|
|
if (c)
|
|
{
|
|
*p++ = c;
|
|
n--;
|
|
|
|
if (!n)
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
if (n < 2)
|
|
break;
|
|
|
|
memcpy(p, s, 2);
|
|
|
|
p += 2;
|
|
n -= 2;
|
|
}
|
|
|
|
s += 2;
|
|
}
|
|
else
|
|
break;
|
|
}
|
|
|
|
strncpy(p, s, n);
|
|
}
|
|
|
|
void StripColors(char *dst, char *src, size_t n)
|
|
{
|
|
size_t j = 0;
|
|
|
|
for (size_t i = 0; j < n && src[i] != 0; ++i)
|
|
{
|
|
char c = src[i];
|
|
|
|
if ((c & 0x80) == 0)
|
|
dst[j++] = c;
|
|
}
|
|
|
|
dst[j] = 0;
|
|
}
|
|
|
|
/** Token parser for TEXTURES, ANIMDEFS, and potentially other lumps later down the line.
|
|
* Was originally R_GetTexturesToken when I was coding up the TEXTURES parser, until I realized I needed it for ANIMDEFS too.
|
|
* Parses up to the next whitespace character or comma. When finding the start of the next token, whitespace is skipped.
|
|
* Commas are not; if a comma is encountered, then THAT'S returned as the token.
|
|
* -Shadow Hog
|
|
*
|
|
* \param inputString The string to be parsed. If NULL is supplied instead of a string, it will continue parsing the last supplied one.
|
|
* The pointer to the last string supplied is stored as a static variable, so be careful not to free it while this function is still using it!
|
|
* \return A pointer to a string, containing the fetched token. This is in freshly allocated memory, so be sure to Z_Free() it as appropriate.
|
|
*/
|
|
char *M_GetToken(const char *inputString)
|
|
{
|
|
static const char *stringToUse = NULL; // Populated if inputString != NULL; used otherwise
|
|
static UINT32 startPos = 0;
|
|
static UINT32 endPos = 0;
|
|
static UINT32 stringLength = 0;
|
|
static UINT8 inComment = 0; // 0 = not in comment, 1 = // Single-line, 2 = /* Multi-line */
|
|
char *texturesToken = NULL;
|
|
UINT32 texturesTokenLength = 0;
|
|
|
|
if (inputString != NULL)
|
|
{
|
|
stringToUse = inputString;
|
|
startPos = 0;
|
|
endPos = 0;
|
|
stringLength = strlen(inputString);
|
|
}
|
|
else
|
|
{
|
|
startPos = endPos;
|
|
}
|
|
if (stringToUse == NULL)
|
|
return NULL;
|
|
|
|
// Try to detect comments now, in case we're pointing right at one
|
|
if (startPos < stringLength - 1
|
|
&& inComment == 0)
|
|
{
|
|
if (stringToUse[startPos] == '/'
|
|
&& stringToUse[startPos+1] == '/')
|
|
{
|
|
//Single-line comment start
|
|
inComment = 1;
|
|
}
|
|
else if (stringToUse[startPos] == '/'
|
|
&& stringToUse[startPos+1] == '*')
|
|
{
|
|
//Multi-line comment start
|
|
inComment = 2;
|
|
}
|
|
}
|
|
|
|
// Find the first non-whitespace char, or else the end of the string trying
|
|
while ((stringToUse[startPos] == ' '
|
|
|| stringToUse[startPos] == '\t'
|
|
|| stringToUse[startPos] == '\r'
|
|
|| stringToUse[startPos] == '\n'
|
|
|| stringToUse[startPos] == '\0'
|
|
|| stringToUse[startPos] == '"' // we're treating this as whitespace because SLADE likes adding it for no good reason
|
|
|| inComment != 0)
|
|
&& startPos < stringLength)
|
|
{
|
|
// Try to detect comment endings now
|
|
if (inComment == 1
|
|
&& stringToUse[startPos] == '\n')
|
|
{
|
|
// End of line for a single-line comment
|
|
inComment = 0;
|
|
}
|
|
else if (inComment == 2
|
|
&& startPos < stringLength - 1
|
|
&& stringToUse[startPos] == '*'
|
|
&& stringToUse[startPos+1] == '/')
|
|
{
|
|
// End of multi-line comment
|
|
inComment = 0;
|
|
startPos++; // Make damn well sure we're out of the comment ending at the end of it all
|
|
}
|
|
|
|
startPos++;
|
|
|
|
// Try to detect comment starts now
|
|
if (startPos < stringLength - 1
|
|
&& inComment == 0)
|
|
{
|
|
if (stringToUse[startPos] == '/'
|
|
&& stringToUse[startPos+1] == '/')
|
|
{
|
|
//Single-line comment start
|
|
inComment = 1;
|
|
}
|
|
else if (stringToUse[startPos] == '/'
|
|
&& stringToUse[startPos+1] == '*')
|
|
{
|
|
//Multi-line comment start
|
|
inComment = 2;
|
|
}
|
|
}
|
|
}
|
|
|
|
// If the end of the string is reached, no token is to be read
|
|
if (startPos == stringLength) {
|
|
endPos = stringLength;
|
|
return NULL;
|
|
}
|
|
// Else, if it's one of these three symbols, capture only this one character
|
|
else if (stringToUse[startPos] == ','
|
|
|| stringToUse[startPos] == '{'
|
|
|| stringToUse[startPos] == '}')
|
|
{
|
|
endPos = startPos + 1;
|
|
texturesToken = (char *)Z_Malloc(2*sizeof(char),PU_STATIC,NULL);
|
|
texturesToken[0] = stringToUse[startPos];
|
|
texturesToken[1] = '\0';
|
|
return texturesToken;
|
|
}
|
|
|
|
// Now find the end of the token. This includes several additional characters that are okay to capture as one character, but not trailing at the end of another token.
|
|
endPos = startPos + 1;
|
|
while ((stringToUse[endPos] != ' '
|
|
&& stringToUse[endPos] != '\t'
|
|
&& stringToUse[endPos] != '\r'
|
|
&& stringToUse[endPos] != '\n'
|
|
&& stringToUse[endPos] != ','
|
|
&& stringToUse[endPos] != '{'
|
|
&& stringToUse[endPos] != '}'
|
|
&& stringToUse[endPos] != '"' // see above
|
|
&& inComment == 0)
|
|
&& endPos < stringLength)
|
|
{
|
|
endPos++;
|
|
// Try to detect comment starts now; if it's in a comment, we don't want it in this token
|
|
if (endPos < stringLength - 1
|
|
&& inComment == 0)
|
|
{
|
|
if (stringToUse[endPos] == '/'
|
|
&& stringToUse[endPos+1] == '/')
|
|
{
|
|
//Single-line comment start
|
|
inComment = 1;
|
|
}
|
|
else if (stringToUse[endPos] == '/'
|
|
&& stringToUse[endPos+1] == '*')
|
|
{
|
|
//Multi-line comment start
|
|
inComment = 2;
|
|
}
|
|
}
|
|
}
|
|
texturesTokenLength = endPos - startPos;
|
|
|
|
// Assign the memory. Don't forget an extra byte for the end of the string!
|
|
texturesToken = (char *)Z_Malloc((texturesTokenLength+1)*sizeof(char),PU_STATIC,NULL);
|
|
// Copy the string.
|
|
M_Memcpy(texturesToken, stringToUse+startPos, (size_t)texturesTokenLength);
|
|
// Make the final character NUL.
|
|
texturesToken[texturesTokenLength] = '\0';
|
|
return texturesToken;
|
|
}
|
|
|
|
|
|
const char * M_Ftrim (double f)
|
|
{
|
|
static char dig[9];/* "0." + 6 digits (6 is printf's default) */
|
|
int i;
|
|
/* I know I said it's the default, but just in case... */
|
|
sprintf(dig, "%.6f", fabs(modf(f, &f)));
|
|
/* trim trailing zeroes */
|
|
for (i = strlen(dig)-1; dig[i] == '0'; --i)
|
|
;
|
|
if (dig[i] == '.')/* :NOTHING: */
|
|
return "";
|
|
else
|
|
{
|
|
dig[i + 1] = '\0';
|
|
return &dig[1];/* skip the 0 */
|
|
}
|
|
}
|
|
|
|
/** Count bits in a number.
|
|
*/
|
|
UINT8 M_CountBits(UINT32 num, UINT8 size)
|
|
{
|
|
UINT8 i, sum = 0;
|
|
|
|
for (i = 0; i < size; ++i)
|
|
if (num & (1 << i))
|
|
++sum;
|
|
return sum;
|
|
}
|
|
|
|
const char *GetRevisionString(void)
|
|
{
|
|
static char rev[9] = {0};
|
|
if (rev[0])
|
|
return rev;
|
|
|
|
if (comprevision[0] == 'r')
|
|
strncpy(rev, comprevision, 7);
|
|
else
|
|
snprintf(rev, 7, "r%s", comprevision);
|
|
rev[7] = '\0';
|
|
|
|
return rev;
|
|
}
|
|
|
|
// Vector/matrix math
|
|
TVector *VectorMatrixMultiply(TVector v, TMatrix m)
|
|
{
|
|
static TVector ret;
|
|
|
|
ret[0] = FixedMul(v[0],m[0][0]) + FixedMul(v[1],m[1][0]) + FixedMul(v[2],m[2][0]) + FixedMul(v[3],m[3][0]);
|
|
ret[1] = FixedMul(v[0],m[0][1]) + FixedMul(v[1],m[1][1]) + FixedMul(v[2],m[2][1]) + FixedMul(v[3],m[3][1]);
|
|
ret[2] = FixedMul(v[0],m[0][2]) + FixedMul(v[1],m[1][2]) + FixedMul(v[2],m[2][2]) + FixedMul(v[3],m[3][2]);
|
|
ret[3] = FixedMul(v[0],m[0][3]) + FixedMul(v[1],m[1][3]) + FixedMul(v[2],m[2][3]) + FixedMul(v[3],m[3][3]);
|
|
|
|
return &ret;
|
|
}
|
|
|
|
TMatrix *RotateXMatrix(angle_t rad)
|
|
{
|
|
static TMatrix ret;
|
|
const angle_t fa = rad>>ANGLETOFINESHIFT;
|
|
const fixed_t cosrad = FINECOSINE(fa), sinrad = FINESINE(fa);
|
|
|
|
ret[0][0] = FRACUNIT; ret[0][1] = 0; ret[0][2] = 0; ret[0][3] = 0;
|
|
ret[1][0] = 0; ret[1][1] = cosrad; ret[1][2] = sinrad; ret[1][3] = 0;
|
|
ret[2][0] = 0; ret[2][1] = -sinrad; ret[2][2] = cosrad; ret[2][3] = 0;
|
|
ret[3][0] = 0; ret[3][1] = 0; ret[3][2] = 0; ret[3][3] = FRACUNIT;
|
|
|
|
return &ret;
|
|
}
|
|
|
|
#if 0
|
|
TMatrix *RotateYMatrix(angle_t rad)
|
|
{
|
|
static TMatrix ret;
|
|
const angle_t fa = rad>>ANGLETOFINESHIFT;
|
|
const fixed_t cosrad = FINECOSINE(fa), sinrad = FINESINE(fa);
|
|
|
|
ret[0][0] = cosrad; ret[0][1] = 0; ret[0][2] = -sinrad; ret[0][3] = 0;
|
|
ret[1][0] = 0; ret[1][1] = FRACUNIT; ret[1][2] = 0; ret[1][3] = 0;
|
|
ret[2][0] = sinrad; ret[2][1] = 0; ret[2][2] = cosrad; ret[2][3] = 0;
|
|
ret[3][0] = 0; ret[3][1] = 0; ret[3][2] = 0; ret[3][3] = FRACUNIT;
|
|
|
|
return &ret;
|
|
}
|
|
#endif
|
|
|
|
TMatrix *RotateZMatrix(angle_t rad)
|
|
{
|
|
static TMatrix ret;
|
|
const angle_t fa = rad>>ANGLETOFINESHIFT;
|
|
const fixed_t cosrad = FINECOSINE(fa), sinrad = FINESINE(fa);
|
|
|
|
ret[0][0] = cosrad; ret[0][1] = sinrad; ret[0][2] = 0; ret[0][3] = 0;
|
|
ret[1][0] = -sinrad; ret[1][1] = cosrad; ret[1][2] = 0; ret[1][3] = 0;
|
|
ret[2][0] = 0; ret[2][1] = 0; ret[2][2] = FRACUNIT; ret[2][3] = 0;
|
|
ret[3][0] = 0; ret[3][1] = 0; ret[3][2] = 0; ret[3][3] = FRACUNIT;
|
|
|
|
return &ret;
|
|
}
|
|
|
|
/** Set of functions to take in a size_t as an argument,
|
|
* put the argument in a character buffer, and return the
|
|
* pointer to that buffer.
|
|
* This is to eliminate usage of PRIdS, so gettext can work
|
|
* with *all* of SRB2's strings.
|
|
*/
|
|
char *sizeu1(size_t num)
|
|
{
|
|
static char sizeu1_buf[28];
|
|
sprintf(sizeu1_buf, "%"PRIdS, num);
|
|
return sizeu1_buf;
|
|
}
|
|
|
|
char *sizeu2(size_t num)
|
|
{
|
|
static char sizeu2_buf[28];
|
|
sprintf(sizeu2_buf, "%"PRIdS, num);
|
|
return sizeu2_buf;
|
|
}
|
|
|
|
char *sizeu3(size_t num)
|
|
{
|
|
static char sizeu3_buf[28];
|
|
sprintf(sizeu3_buf, "%"PRIdS, num);
|
|
return sizeu3_buf;
|
|
}
|
|
|
|
char *sizeu4(size_t num)
|
|
{
|
|
static char sizeu4_buf[28];
|
|
sprintf(sizeu4_buf, "%"PRIdS, num);
|
|
return sizeu4_buf;
|
|
}
|
|
|
|
char *sizeu5(size_t num)
|
|
{
|
|
static char sizeu5_buf[28];
|
|
sprintf(sizeu5_buf, "%"PRIdS, num);
|
|
return sizeu5_buf;
|
|
}
|
|
|
|
/** Return the appropriate message for a file error or end of file.
|
|
*/
|
|
const char *M_FileError(FILE *fp)
|
|
{
|
|
if (ferror(fp))
|
|
return strerror(errno);
|
|
else
|
|
return "end-of-file";
|
|
}
|
|
|
|
/** Return the number of parts of this path.
|
|
*/
|
|
int M_PathParts(const char *path)
|
|
{
|
|
int n;
|
|
const char *p;
|
|
const char *t;
|
|
if (path == NULL)
|
|
return 0;
|
|
for (n = 0, p = path ;; ++n)
|
|
{
|
|
t = p;
|
|
if (( p = strchr(p, PATHSEP[0]) ))
|
|
p += strspn(p, PATHSEP);
|
|
else
|
|
{
|
|
if (*t)/* there is something after the final delimiter */
|
|
n++;
|
|
break;
|
|
}
|
|
}
|
|
return n;
|
|
}
|
|
|
|
/** Check whether a path is an absolute path.
|
|
*/
|
|
boolean M_IsPathAbsolute(const char *path)
|
|
{
|
|
#ifdef _WIN32
|
|
return ( strncmp(&path[1], ":\\", 2) == 0 );
|
|
#else
|
|
return ( path[0] == '/' );
|
|
#endif
|
|
}
|
|
|
|
/** I_mkdir for each part of the path.
|
|
*/
|
|
void M_MkdirEachUntil(const char *cpath, int start, int end, int mode)
|
|
{
|
|
char path[MAX_WADPATH];
|
|
char *p;
|
|
char *t;
|
|
|
|
if (end > 0 && end <= start)
|
|
return;
|
|
|
|
strlcpy(path, cpath, sizeof path);
|
|
#ifdef _WIN32
|
|
if (strncmp(&path[1], ":\\", 2) == 0)
|
|
p = &path[3];
|
|
else
|
|
#endif
|
|
p = path;
|
|
|
|
if (end > 0)
|
|
end -= start;
|
|
|
|
for (; start > 0; --start)
|
|
{
|
|
p += strspn(p, PATHSEP);
|
|
if (!( p = strchr(p, PATHSEP[0]) ))
|
|
return;
|
|
}
|
|
p += strspn(p, PATHSEP);
|
|
for (;;)
|
|
{
|
|
if (end > 0 && !--end)
|
|
break;
|
|
|
|
t = p;
|
|
if (( p = strchr(p, PATHSEP[0]) ))
|
|
{
|
|
*p = '\0';
|
|
I_mkdir(path, mode);
|
|
*p = PATHSEP[0];
|
|
p += strspn(p, PATHSEP);
|
|
}
|
|
else
|
|
{
|
|
if (*t)
|
|
I_mkdir(path, mode);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void M_MkdirEach(const char *path, int start, int mode)
|
|
{
|
|
M_MkdirEachUntil(path, start, -1, mode);
|
|
}
|
|
|
|
// Hashes some message using FNV-1a
|
|
#define FNV1A_OFFSET_BASIS 0x811C9DC5
|
|
#define FNV1A_PRIME 0x01000193
|
|
|
|
UINT32 FNV1a_Hash(const char *message, size_t size)
|
|
{
|
|
UINT32 hash = FNV1A_OFFSET_BASIS;
|
|
|
|
for (size_t i = 0; i < size; i++)
|
|
{
|
|
hash ^= message[i];
|
|
hash *= FNV1A_PRIME;
|
|
}
|
|
|
|
return hash;
|
|
}
|
|
|
|
UINT32 FNV1a_HashString(const char *message)
|
|
{
|
|
UINT32 hash = FNV1A_OFFSET_BASIS;
|
|
|
|
while (*message)
|
|
{
|
|
hash ^= *message;
|
|
hash *= FNV1A_PRIME;
|
|
message++;
|
|
}
|
|
|
|
return hash;
|
|
}
|
|
|
|
UINT32 FNV1a_HashLowercaseString(const char *message)
|
|
{
|
|
UINT32 hash = FNV1A_OFFSET_BASIS;
|
|
|
|
while (*message)
|
|
{
|
|
hash ^= tolower(*message);
|
|
hash *= FNV1A_PRIME;
|
|
message++;
|
|
}
|
|
|
|
return hash;
|
|
}
|
|
|
|
#ifdef __cplusplus
|
|
} // extern "C"
|
|
#endif
|