vn/code/vn_core.h

176 lines
4.4 KiB
C
Raw Normal View History

2023-06-17 17:00:55 +00:00
/* date = April 26th 2023 8:58 pm */
#ifndef VN_CORE_H
#define VN_CORE_H
#define global static
#define persist static
#pragma section(".roglob", read)
#define read_only __declspec(allocate(".roglob"))
#define fallthrough
#if VN_SLOW
#define Assert(Statement) if(!(Statement)) { *(int *)0 = 0; }
#else
#define Assert(Statement)
#endif
#define CTAssert(Statement) static_assert(Statement)
#define InvokeDebugger __debugbreak()
#define UnimplementedCodepath Assert(!"Unimplemented codepath")
#define InvalidCodepath Assert(!"Invalid codepath")
#define InvalidDefaultCase default: { Assert(!"Invalid codepath"); } break
#define ArrayCount(Array) (sizeof(Array)/sizeof((Array)[0]))
#define OffsetOf(type, Member) (umm) &(((type *)0)->Member)
#define Minimum(A, B) (((A)<(B))?(A):(B))
#define Maximum(A, B) (((A)>(B))?(A):(B))
#define Clamp(Value, Min, Max) Minimum(Maximum(Value, Min), Max)
#define Clamp01(Value) Clamp(Value, 0, 1)
#define DeferLoop(Start, End) for(s32 ___ = ((Start), 0); ___ == 0; ++___, (End))
#define _Stringify(x) #x
#define Stringify(x) _Stringify(x)
#define IsNull(x) ((x) == 0)
#define SetNull(x) ((x) = 0)
#define DLLInsert_NP(f,l,p,n,next,prev) \
(IsNull(f) ? (((f) = (l) = (n)), SetNull((n)->next), SetNull((n)->prev)) :\
IsNull(p) ? (SetNull((n)->prev), (n)->next = (f), (IsNull(f) ? (0) : ((f)->prev = (n))), (f) = (n)) :\
((IsNull((p)->next) ? (0) : (((p)->next->prev) = (n))), (n)->next = (p)->next, (n)->prev = (p), (p)->next = (n),\
((p) == (l) ? (l) = (n) : (0))))
#define DLLInsertLast_NP(f,l,n,next,prev) DLLInsert_NP(f,l,l,n,next,prev)
#define DLLInsertFirst_NP(f,l,n,next,prev) DLLInsert_NP(l,f,f,n,prev,next)
#define DLLRemove_NP(f,l,n,next,prev)\
(((f)==(n))?\
((f)=(f)->next, (IsNull(f) ? (SetNull(l)) : SetNull((f)->prev))):\
((l)==(n))?\
((l)=(l)->prev, (IsNull(l) ? (SetNull(f)) : SetNull((l)->next))):\
((IsNull((n)->next) ? (0) : ((n)->next->prev=(n)->prev)),\
(IsNull((n)->prev) ? (0) : ((n)->prev->next=(n)->next))))
#define DLLInsertFirst(First, Last, Element) DLLInsertFirst_NP(First, Last, Element, Next, Prev)
#define DLLInsertLast(First, Last, Element) DLLInsertLast_NP(First, Last, Element, Next, Prev)
#define DLLRemove(First, Last, Element) DLLRemove_NP(First, Last, Element, Next, Prev)
#define DLLIsEmpty(First) ((First) == 0)
#include "vn_types.h"
#include "vn_math.h"
2023-06-19 17:12:26 +00:00
// sixten(TODO): Scuffed AF. Need to make a proper "core" of the codebase.
static s64 UTF8FromCodepoint(u8 *Out, u32 Codepoint);
2023-06-17 17:00:55 +00:00
#define STB_SPRINTF_IMPLEMENTATION
#include "third_party/stb_sprintf.h"
inline void Copy(void *Dest_, void *Source_, umm Count)
{
u8 *Dest = (u8 *)Dest_;
u8 *Source = (u8 *)Source_;
while(Count--)
{
*Dest++ = *Source++;
}
}
inline void ZeroSize(void *Dest_, umm Count)
{
u8 *Dest = (u8 *)Dest_;
while(Count--)
{
*Dest++ = 0;
}
}
inline void *U64ToPointer(u64 Value)
{
void *Result = (void *)Value;
return(Result);
}
inline u64 PointerToU64(void *Value)
{
u64 Result = (u64)Value;
return(Result);
}
inline u64 AtomicExchangeU64(u64 volatile *Value, u64 New)
{
u64 Result = _InterlockedExchange64((__int64 volatile *)Value, New);
return(Result);
}
inline u64 AtomicAddU64(u64 volatile *Value, u64 Addend)
{
u64 Result = _InterlockedExchangeAdd64((__int64 volatile *)Value, Addend);
return(Result);
}
inline void BeginTicketMutex(ticket_mutex *Mutex)
{
u64 Ticket = AtomicAddU64(&Mutex->Ticket, 1);
while(Ticket != Mutex->Serving) { _mm_pause(); }
}
inline void EndTicketMutex(ticket_mutex *Mutex)
{
AtomicAddU64(&Mutex->Serving, 1);
}
2023-06-19 17:12:26 +00:00
inline b32 InRange(range_s64 Range, s64 P)
{
b32 Result = ((P >= Range.Min) && (P < Range.Max));
return(Result);
}
2023-06-17 17:00:55 +00:00
inline b32 InRange(range2_r32 Range, v2 P)
{
b32 Result = ((P.x >= Range.Min.x) &&
(P.y >= Range.Min.y) &&
(P.x < Range.Max.x) &&
(P.y < Range.Max.y));
return(Result);
}
inline range_s64 RangeS64(s64 A, s64 B)
{
range_s64 Result = {Minimum(A, B), Maximum(A, B)};
return(Result);
}
inline range_r32 RangeR32(r32 A, r32 B)
{
range_r32 Result = {Minimum(A, B), Maximum(A, B)};
return(Result);
}
inline range2_r32 Range2R32(v2 A, v2 B)
{
range2_r32 Result = { Min(A, B), Max(A, B) };
return(Result);
}
2023-06-19 17:12:26 +00:00
inline s64 DimOfRange(range_s64 Range)
{
s64 Dim = Range.Max - Range.Min;
return(Dim);
}
2023-06-17 17:00:55 +00:00
inline v2 DimOfRange(range2_r32 Range)
{
v2 Dim = Range.Max - Range.Min;
return(Dim);
}
#endif //VN_CORE_H