mirror of
https://github.com/perfect-dark-pc-port/perfect_dark.git
synced 2026-10-24 00:02:43 +02:00
83 lines
1.4 KiB
C
83 lines
1.4 KiB
C
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <SDL.h>
|
|
#include <PR/ultratypes.h>
|
|
#include "system.h"
|
|
|
|
static u64 startTick = 0;
|
|
static u64 tickFreq = 0;
|
|
|
|
static void sysInitTicks(void)
|
|
{
|
|
tickFreq = SDL_GetPerformanceFrequency();
|
|
startTick = SDL_GetPerformanceCounter();
|
|
}
|
|
|
|
u64 sysGetTicks(void)
|
|
{
|
|
if (tickFreq == 0) {
|
|
sysInitTicks();
|
|
}
|
|
return (SDL_GetPerformanceCounter() - startTick);
|
|
}
|
|
|
|
u64 sysGetTicksPerSecond(void)
|
|
{
|
|
if (tickFreq == 0) {
|
|
sysInitTicks();
|
|
}
|
|
return tickFreq;
|
|
}
|
|
|
|
void sysFatalError(const char *fmt, ...)
|
|
{
|
|
char errmsg[2048] = { 0 };
|
|
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
vsnprintf(errmsg, sizeof(errmsg), fmt, ap);
|
|
va_end(ap);
|
|
|
|
fprintf(stderr, "FATAL ERROR: %s\n", errmsg);
|
|
fflush(stderr);
|
|
|
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Fatal error", errmsg, NULL);
|
|
exit(1);
|
|
}
|
|
|
|
void sysGetExecutablePath(char *outPath, const u32 outLen)
|
|
{
|
|
// try asking SDL
|
|
char *sdlPath = SDL_GetBasePath();
|
|
if (sdlPath && *sdlPath) {
|
|
// -1 to trim trailing slash
|
|
const u32 len = strlen(sdlPath) - 1;
|
|
if (len < outLen) {
|
|
memcpy(outPath, sdlPath, len);
|
|
outPath[len] = '\0';
|
|
}
|
|
} else if (outLen > 1) {
|
|
// give up, use working directory instead
|
|
outPath[0] = '.';
|
|
outPath[1] = '\0';
|
|
}
|
|
SDL_free(sdlPath);
|
|
}
|
|
|
|
void *sysMemAlloc(const u32 size)
|
|
{
|
|
return malloc(size);
|
|
}
|
|
|
|
void *sysMemZeroAlloc(const u32 size)
|
|
{
|
|
return calloc(1, size);
|
|
}
|
|
|
|
void sysMemFree(void *ptr)
|
|
{
|
|
free(ptr);
|
|
}
|