1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2020 Warzone 2100 Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/*
* Frame.c
*
* Initialisation and shutdown for the framework library.
*
*/
#include "frame.h"
#include "file.h"
#include "wzapp.h"
#include <physfs.h>
#include "physfs_ext.h"
#include "frameresource.h"
#include "input.h"
#include "file_ext.h"
#include <limits>
/************************************************************************************
*
* Player globals
*/
static bool mousewarp = false;
uint32_t selectedPlayer = 0; /**< Current player */
uint32_t realSelectedPlayer = 0;
/* Global variables for the frame rate stuff */
static int frameCount = 0;
static uint64_t curFrames = 0; // Number of frames elapsed since start
static uint64_t lastFrames = 0;
static uint32_t curTicks = 0; // Number of ticks since execution started
static uint32_t lastTicks = 0;
/* InitFrameStuff - needs to be called once before frame loop commences */
static void InitFrameStuff()
{
frameCount = 0;
curFrames = 0;
lastFrames = 0;
curTicks = 0;
lastTicks = 0;
}
int frameRate()
{
return frameCount;
}
UDWORD frameGetFrameNumber()
{
return (UDWORD)curFrames;
}
/*
* frameInitialise
*
* Initialise the framework library. - PC version
*/
bool frameInitialise()
{
/* Initialise the trig stuff */
if (!trigInitialise())
{
return false;
}
/* Initialise the input system */
inputInitialise();
/* Initialise the frame rate stuff */
InitFrameStuff();
// Initialise the resource stuff
if (!resInitialise())
{
return false;
}
return true;
}
/*!
* Call this each cycle to do general house keeping.
*/
void frameUpdate()
{
curTicks = wzGetTicks();
curFrames++;
// Update the framerate only once per second
if (curTicks >= lastTicks + 1000)
{
frameCount = static_cast<int>(curFrames - lastFrames);
lastTicks = curTicks;
lastFrames = curFrames;
}
}
/*!
* Cleanup framework
*/
void frameShutDown()
{
// Shutdown the resource stuff
debug(LOG_NEVER, "No more resources!");
resShutDown();
}
void setMouseWarp(bool value)
{
mousewarp = value;
}
bool getMouseWarp()
{
return mousewarp;
}
PHYSFS_file *openLoadFile(const char *fileName, bool hard_fail)
{
PHYSFS_file *fileHandle = PHYSFS_openRead(fileName);
debug(LOG_NEVER, "Reading...[directory: %s] %s", WZ_PHYSFS_getRealDir_String(fileName).c_str(), fileName);
if (!fileHandle)
{
if (hard_fail)
{
ASSERT(!"unable to open file", "file %s could not be opened: %s", fileName, WZ_PHYSFS_getLastError());
}
else
{
debug(LOG_WZ, "optional file %s could not be opened: %s", fileName, WZ_PHYSFS_getLastError());
}
}
return fileHandle;
}
/***************************************************************************
Load the file with name pointed to by pFileName into a memory buffer.
If AllocateMem is true then the memory is allocated ... else it is
already in allocated in ppFileData, and the max size is in pFileSize
... this is adjusted to the actual loaded file size.
If hard_fail is true, we will assert and report on failures.
***************************************************************************/
static bool loadFile2(const char *pFileName, char **ppFileData, UDWORD *pFileSize, bool AllocateMem, bool hard_fail)
{
if (WZ_PHYSFS_isDirectory(pFileName))
{
return false;
}
PHYSFS_file *pfile = openLoadFile(pFileName, hard_fail);
if (!pfile)
{
return false;
}
PHYSFS_sint64 filesize = PHYSFS_fileLength(pfile);
if (filesize < 0)
{
return false; // File size could not be determined. Is a directory?
}
ASSERT_OR_RETURN(false, filesize < static_cast<PHYSFS_sint64>(std::numeric_limits<PHYSFS_sint32>::max()), "\"%s\" filesize >= std::numeric_limits<PHYSFS_sint32>::max()", pFileName);
ASSERT_OR_RETURN(false, static_cast<PHYSFS_uint64>(filesize) < static_cast<PHYSFS_uint64>(std::numeric_limits<size_t>::max()), "\"%s\" filesize >= std::numeric_limits<size_t>::max()", pFileName);
if (AllocateMem)
{
// Allocate a buffer to store the data and a terminating zero
*ppFileData = (char *)malloc(static_cast<size_t>(filesize + 1));
}
else
{
if (filesize > *pFileSize)
{
debug(LOG_ERROR, "No room for file %s, buffer is too small! Got: %d Need: %ld", pFileName, *pFileSize, (long)filesize);
assert(false);
return false;
}
assert(*ppFileData != nullptr);
}
/* Load the file data */
PHYSFS_sint64 length_read = WZ_PHYSFS_readBytes(pfile, *ppFileData, static_cast<PHYSFS_uint32>(filesize));
if (length_read != filesize)
{
if (AllocateMem)
{
free(*ppFileData);
*ppFileData = nullptr;
}
debug(LOG_ERROR, "Reading %s short: %s", pFileName, WZ_PHYSFS_getLastError());
assert(false);
return false;
}
if (!PHYSFS_close(pfile))
{
if (AllocateMem)
{
free(*ppFileData);
*ppFileData = nullptr;
}
debug(LOG_ERROR, "Error closing %s: %s", pFileName, WZ_PHYSFS_getLastError());
assert(false);
return false;
}
// Add the terminating zero
*((*ppFileData) + filesize) = 0;
// always set to correct size
ASSERT(static_cast<PHYSFS_uint64>(filesize) <= static_cast<PHYSFS_uint64>(std::numeric_limits<UDWORD>::max()), "filesize exceeds std::numeric_limits<UDWORD>::max()");
*pFileSize = static_cast<UDWORD>(filesize);
return true;
}
bool loadFileToBufferVector(const char *pFileName, std::vector<char>& outputBuffer, bool hard_fail, bool appendNullCharacter)
{
return loadFileToBufferVectorT<char>(pFileName, outputBuffer, hard_fail, appendNullCharacter);
}
PHYSFS_file *openSaveFile(const char *fileName)
{
PHYSFS_file *fileHandle = PHYSFS_openWrite(fileName);
if (!fileHandle)
{
const char *found = PHYSFS_getRealDir(fileName);
debug(LOG_ERROR, "%s could not be opened: %s", fileName, WZ_PHYSFS_getLastError());
if (found)
{
debug(LOG_ERROR, "%s found as %s", fileName, found);
}
assert(!"openSaveFile: couldn't open file for writing");
return nullptr;
}
return fileHandle;
}
/***************************************************************************
Save the data in the buffer into the given file.
***************************************************************************/
bool saveFile(const char *pFileName, const char *pFileData, UDWORD fileSize)
{
PHYSFS_file *pfile;
PHYSFS_uint32 size = fileSize;
debug(LOG_WZ, "We are to write (%s) of size %d", pFileName, fileSize);
pfile = openSaveFile(pFileName);
if (!pfile)
{
ASSERT(false, "Couldn't save file %s (%s)?", pFileName, WZ_PHYSFS_getLastError());
return false;
}
if (WZ_PHYSFS_writeBytes(pfile, pFileData, size) != size)
{
debug(LOG_ERROR, "%s could not write: %s", pFileName, WZ_PHYSFS_getLastError());
assert(false);
return false;
}
if (!PHYSFS_close(pfile))
{
debug(LOG_ERROR, "Error closing %s: %s", pFileName, WZ_PHYSFS_getLastError());
assert(false);
return false;
}
if (PHYSFS_getRealDir(pFileName) == nullptr)
{
// weird
debug(LOG_ERROR, "PHYSFS_getRealDir(%s) returns NULL (%s)?!", pFileName, WZ_PHYSFS_getLastError());
}
else
{
debug(LOG_WZ, "Successfully wrote to %s%s with %d bytes", WZ_PHYSFS_getRealDir_String(pFileName).c_str(), pFileName, size);
}
return true;
}
bool loadFile(const char *pFileName, char **ppFileData, UDWORD *pFileSize, bool hard_fail /*= true*/)
{
return loadFile2(pFileName, ppFileData, pFileSize, true, hard_fail);
}
// load a file from disk into a fixed memory buffer
bool loadFileToBuffer(const char *pFileName, char *pFileBuffer, UDWORD bufferSize, UDWORD *pSize)
{
*pSize = bufferSize;
return loadFile2(pFileName, &pFileBuffer, pSize, false, true);
}
// as above but returns quietly if no file found
bool loadFileToBufferNoError(const char *pFileName, char *pFileBuffer, UDWORD bufferSize, UDWORD *pSize)
{
*pSize = bufferSize;
return loadFile2(pFileName, &pFileBuffer, pSize, false, false);
}
Sha256 findHashOfFile(char const *realFileName)
{
char *realFileData = nullptr;
uint32_t realFileSize = 0;
if (loadFile(realFileName, &realFileData, &realFileSize))
{
Sha256 realFileHash = sha256Sum(realFileData, realFileSize);
free(realFileData);
return realFileHash;
}
Sha256 zero;
zero.setZero();
return zero;
}
bool PHYSFS_printf(PHYSFS_file *file, const char *format, ...)
{
char vaBuffer[PATH_MAX];
va_list ap;
va_start(ap, format);
vssprintf(vaBuffer, format, ap);
va_end(ap);
return WZ_PHYSFS_writeBytes(file, vaBuffer, static_cast<PHYSFS_uint32>(strlen(vaBuffer)));
}
std::string video_backend_names[] =
{
"opengl",
"opengles",
"vulkan",
#if defined(WZ_BACKEND_DIRECTX)
"directx",
#endif
"invalid" // Must be last!
};
static_assert((size_t)video_backend::num_backends == (sizeof(video_backend_names) / sizeof(std::string)) - 1, "video_backend_names must match video_backend enum");
bool video_backend_from_str(const char *str, video_backend &output_backend)
{
for (size_t i = 0; i < (size_t)video_backend::num_backends; i++)
{
if (strcasecmp(video_backend_names[i].c_str(), str) == 0)
{
output_backend = (video_backend)i;
return true;
}
}
return false;
}
std::string to_string(video_backend backend)
{
return video_backend_names[(size_t)backend];
}
std::string to_display_string(const video_backend& backend)
{
switch (backend)
{
case video_backend::opengl:
return "OpenGL";
case video_backend::opengles:
return "OpenGL ES";
case video_backend::vulkan:
return "Vulkan";
#if defined(WZ_BACKEND_DIRECTX)
case video_backend::directx:
return "DirectX (ANGLE)";
#endif
case video_backend::num_backends:
debug(LOG_FATAL, "Should never happen");
break;
}
return "n/a";
}
std::string to_display_string(const WINDOW_MODE& mode)
{
switch (mode)
{
case WINDOW_MODE::desktop_fullscreen:
return "Desktop Full";
case WINDOW_MODE::windowed:
return "Windowed";
case WINDOW_MODE::fullscreen:
return "Fullscreen";
}
return "n/a"; // silence warning
}
|