A small OpenGL 3+ renderer and game engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

128 lines
3.0 KiB

#pragma once
#include <cstdint>
#include <cassert>
typedef float real32;
typedef float GLfloat;
typedef double real64;
typedef int32_t bool32;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uint8;
typedef uint32_t uint32;
typedef uint32_t uint;
struct v2f
{
v2f(): x(0), y(0) {}
v2f(real64 a, real64 b): x(a), y(b) {}
real64 x;
real64 y;
};
struct v2i
{
v2i(int a, int b): x(a), y(b) {}
v2i() : x(0), y(0) {}
int32 x;
int32 y;
};
struct v3f
{
v3f(): x(0), y(0), z(0) {}
v3f(real64 a, real64 b, real64 c): x(a), y(b), z(c) {}
real64 x;
real64 y;
real64 z;
};
inline real32
SafeRatio(real32 dividend, real32 divisor)
{
if (divisor == 0)
// TODO: log warning (don't require aixlog)
return 0;
else
return dividend / divisor;
}
inline int32
SafeTruncateToInt32(int64 val)
{
assert(val <= INT32_MAX && val >= INT32_MIN);
return (int32) val;
}
inline void
utilConvertColor(GLfloat buf[3], uint32 color)
{
// NOTE: not using the alpha values
buf[0] = (GLfloat) ((color >> 24) & 0xFF) / (GLfloat) 255;
buf[1] = (GLfloat) ((color >> 16) & 0xFF) / (GLfloat) 255;
buf[2] = (GLfloat) ((color >> 8) & 0xFF) / (GLfloat) 255;
}
//-----------------
// C Strings
// NOTE: max_len should be the allocated size of dest
bool utilCopyCStr(char* dest, const char* src, uint max_len);
// NOTE: returns new string with '/' between
// NOTE: max_len should be the size of return buffer.
char* utilConcatPath(char* out, const char* base_dir, const char* file_name, uint max_len);
const char* utilBaseName(const char* path_str);
// NOTE: returns true if the first 'sz' characters from each string match
bool utilMatchPrefix(const char* lhs, const char* rhs, int sz);
//-----------------
// Hashing
// NOTE: FNV1a hashing algorithm http://www.isthe.com/chongo/tech/comp/fnv/
#define FNV1_64_INIT ((uint64_t) 0xcbf29ce484222325ULL)
#define FNV_64_PRIME ((uint64_t) 0x100000001b3ULL)
uint64_t
utilFNV64a_str(const char *str, uint64_t hval = FNV1_64_INIT);
//-----------------
// Memory allocation
// NOTE: Wrapper for calloc that will send error message on out of memory
#define UTIL_ALLOC(len, type) (type *) utilLogAlloc((len), sizeof(type), __FILE__, __LINE__)
void* utilLogAlloc(uint item_count, uint type_size, const char* file_name, const int line);
// TODO: replace instances of 'utilSafeFree()' with macro that casts to void
// pointer reference. Can then set the pointer to nullptr in free function
#define UTIL_FREE(mem_ptr) utilSafeFree((void*&) mem_ptr)
void utilSafeFree(const void* mem);
struct memory_arena
{
size_t max_size;
void* head;
void* next_free;
};
#define DEFAULT_ARENA_SIZE 10 * 1024 * 1024 // 10MB
memory_arena* arenaInit(size_t initial_size = DEFAULT_ARENA_SIZE);
void arenaFree(memory_arena*& arena);
uint arenaGetFreeSize(memory_arena* arena);
void* arenaAllocateBlock(memory_arena* arena, size_t block_size);
//-----------------
// File I/O
char* utilDumpTextFile(const char* filename);
bool utilWriteTextFile(const char* filename, const char* text);