Browse Source

add util.h single file header

main
cinnaboot 5 years ago
parent
commit
7a452ff97f
  1. 2
      src/main.cpp
  2. 1
      src/shader.cpp
  3. 36
      src/util.h

2
src/main.cpp

@ -11,6 +11,8 @@
#include "GLDebug.h" #include "GLDebug.h"
#include "shader.h" #include "shader.h"
#include "types.h" #include "types.h"
#define UTIL_IMPLEMENTATION
#include "util.h"
struct SDLHandles struct SDLHandles

1
src/shader.cpp

@ -13,6 +13,7 @@
#define GL_DEBUG_IMPLEMENTATION #define GL_DEBUG_IMPLEMENTATION
#include "GLDebug.h" #include "GLDebug.h"
#include "shader.h" #include "shader.h"
#include "util.h"

36
src/util.h

@ -0,0 +1,36 @@
#include <cassert>
#include <cstring>
#include "types.h"
char* utilAllocateCStr(const char* str, u32 max_len = 256);
void utilSafeFree(void* p);
#ifdef UTIL_IMPLEMENTATION
char*
utilAllocateCStr(const char* str, u32 max_len)
{
u32 len = strlen(str) + 1;
if (len > max_len) {
printf("%s(), %s , longer than %i\n", __FUNCTION__, str, max_len);
return nullptr;
}
char* out = (char*) std::calloc(len, sizeof(u8));
strncpy(out, str, len - 1);
return out;
}
void
utilSafeFree(void* p)
{
assert(p != nullptr);
free(p);
}
#endif
Loading…
Cancel
Save