This commit is contained in:
2025-11-09 13:30:05 +11:00
parent e9290ba9f2
commit 768424e199
2 changed files with 66 additions and 96 deletions

113
core.h
View File

@@ -2,6 +2,7 @@
#define CORE_H
// cstdlib includes
#include <cwchar>
#include <math.h>
#include <stdint.h> // necessary for int type sizes
#include <stdio.h>
@@ -30,6 +31,7 @@ typedef uint64_t uint64;
typedef uint8_t byte;
typedef float real32;
typedef double real64;
typedef struct string string;
// ### Sizes and Numbers ###
#define Bytes(n) (n)
@@ -74,59 +76,53 @@ void scratchEnd(Scratch scratch);
#define PushStructZero(arena, type) (type *)pushSizeFill(arena, sizeof(type), 0)
// ### Vectors ###
template <typename T>
union Vector2 {
union Vec2 {
struct {
T x;
T y;
real32 x;
real32 y;
};
T vec[2];
real32 vec[2];
};
template <typename T>
inline function Vector2<T> vec2(T x, T y) {
Vector2<T> result = {0};
inline function Vec2 vec2(real32 x, real32 y) {
Vec2 result = {0};
result.x = x;
result.y = y;
return result;
}
template <typename T>
union Vector3 {
union Vec3 {
struct {
T x;
T y;
T z;
real32 x;
real32 y;
real32 z;
};
T vec[3];
real32 vec[3];
};
template <typename T>
inline function Vector3<T> vec3(T x, T y, T z) {
Vector3<T> result = {0};
inline function Vec3 vec3(real32 x, real32 y, real32 z) {
Vec3 result = {0};
result.x = x;
result.y = y;
result.z = z;
return result;
}
template <typename T>
union Vector4 {
union Vec4 {
struct {
T r;
T g;
T b;
T a;
real32 r;
real32 g;
real32 b;
real32 a;
};
struct {
T x;
T y;
T z;
T w;
real32 x;
real32 y;
real32 z;
real32 w;
};
T vec[4];
real32 vec[4];
};
template <typename T>
inline function Vector4<T> vec4(T x, T y, T z, T w) {
Vector4<T> result = {0};
inline function Vec4 vec4(real32 x, real32 y, real32 z, real32 w) {
Vec4 result = {0};
result.x = x;
result.y = y;
result.z = z;
@@ -135,24 +131,29 @@ inline function Vector4<T> vec4(T x, T y, T z, T w) {
}
// ### Lists ###
template <typename T>
struct list {
T* data;
size_t length;
size_t head;
};
#define DefineList(type, prefix) \
typedef struct {\
type* data;\
size_t length;\
size_t head;\
} prefix ## List
#define PushList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, 0 })
#define EmptyList(type) (list<type>{ NULL, 0, 0 })
#define PushListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, 0 })
#define PushFullList(arena, type, size) (list<type>{ PushArray(arena, type, size), size, size })
#define PushFullListZero(arena, type, size) (list<type>{ PushArrayZero(arena, type, size), size, size })
#define ArrayAsList(type, array) (list<type>{ array, ArrayCount(array), ArrayCount(array) })
DefineList(string, String);
template <typename T> T *appendList(list<T> *list, T element);
template <typename T> void zeroList(list<T> *list);
template <typename T> void zeroListFull(list<T> *list);
template <typename T> list<T> listSlice(list<T> l, size_t start, size_t stop = 0);
#define PushList(arena, type, size) ((type){ PushArray(arena, type, size), size, 0 })
#define EmptyList(type) ((type){ NULL, 0, 0 })
#define PushListZero(arena, type, size) ((type){ PushArrayZero(arena, type, size), size, 0 })
#define PushFullList(arena, type, size) ((type){ PushArray(arena, type, size), size, size })
#define PushFullListZero(arena, type, size) ((type){ PushArrayZero(arena, type, size), size, size })
#define ArrayAsList(type, array) ((type){ array, ArrayCount(array), ArrayCount(array) })
#define AppendList(type, list, element) \
if ((list)->head < (list)->length) { \
(list)->data[(list)->head++] = (element); \
}
#define ZeroListFull(list) memset((list)->data, 0, (list)->head * sizeof(T))
#define ZeroList(list) (list)->head = 0; \
memset((list)->data, 0, (list)->head * sizeof(T));
// ### Strings ###
struct string {
@@ -162,13 +163,13 @@ struct string {
#define STB_SPRINTF_DECORATE(name) stb_##name // define this before including if you want to change the names
#include "vendor/stb_sprintf.h"
#define strlit(lit) (string{(char *)(lit), sizeof(lit) - 1})
#define s(lit) (string{(char *)(lit), sizeof(lit) - 1})
#define PushString(arena, length) (string{ (char *)pushSize(arena, length), (length) })
#define PushStringFill(arena, length, characterByte) (string{ (char *)pushSizeFill(arena, length, characterByte), (length) })
string operator""_s(const char *cstrLiteral, size_t length);
// C Strings
const char *cstring(Arena *arena, list<char> buf);
DefineList(char, Char);
const char *cstringFromCharList(Arena *arena, CharList buf);
const char *cstring(Arena *arena, string str);
size_t calcStringLen(const char *str);
string strFromCString(Arena *arena, const char *str);
@@ -180,7 +181,7 @@ bool stringContains(string str, char c);
string strReverse(Arena *arena, string str);
string strSlice(string str, size_t start, size_t stop = 0);
string strSlice(char *data, size_t start, size_t stop = 0);
list<string> strSplit(Arena *arena, string splitStr, string inputStr);
StringList strSplit(Arena *arena, string splitStr, string inputStr);
string strPrintfv(Arena *arena, const char *fmt, va_list args);
string strPrintf(Arena *arena, const char *fmt, ...);
@@ -192,7 +193,7 @@ ParsePositiveReal32Result parsePositiveReal32(Arena *arena, string str, size_t *
inline function bool isNumeric(char c);
// ### Cmdline ###
list<string> getArgs(Arena *arena, int argc, char **argv);
StringList getArgs(Arena *arena, int argc, char **argv);
// ### Time ###
typedef uint64 UnixTimestamp;
@@ -261,11 +262,9 @@ enum StdStream {
#define COLOR_TEXT_FG_BG(text, foregroundcolor, backgroundcolor) ANSI_INSTRUCTION_FROM_ENUM(foregroundcolor) ANSI_INSTRUCTION_FROM_ENUM(backgroundcolor) text ANSI_RESET
#define COLOR_TEXT_RGB(text, red, green, blue) ANSI_INSTRUCTION_STR("38;2;" #red ";" #green ";" #blue) text ANSI_RESET
void print(list<int> l, StdStream target = StdStream_stdout);
void print(list<string> l, StdStream target = StdStream_stdout);
void print(list<Vector2<real32>> l, StdStream target = StdStream_stdout);
void print(list<Vector3<real32>> l, StdStream target = StdStream_stdout);
void print(list<Vector4<real32>> l, StdStream target = StdStream_stdout);
DefineList(int, Int);
void printIntList(IntList l, StdStream target = StdStream_stdout);
void printStrList(StringList l, StdStream target = StdStream_stdout);
void print(const char *fmt, ...);
void printErr(const char *fmt, ...);