Added image loading

Added some string functions and macros
Added path listing on windows
Added assets
This commit is contained in:
2025-10-04 17:24:30 +01:00
parent 7d55b16c8e
commit b1a805cea8
17 changed files with 617 additions and 5 deletions

View File

@@ -1,7 +1,7 @@
#if !defined(LD_CORE_ARENA_H_)
#define LD_CORE_ARENA_H_
#define AlignUp(x, a) (((x) + ~((a) - 1)) & ~((a) - 1))
#define AlignUp(x, a) (((x) + ((a) - 1)) & ~((a) - 1))
#define AlignDown(x, a) (((x)) & ~((a) - 1))
#define KB(x) ((U64) (x) << 10)

View File

@@ -1 +1,2 @@
#include "impl/arena.c"
#include "impl/string.c"

View File

@@ -5,5 +5,6 @@
#include "platform.h"
#include "macros.h"
#include "arena.h"
#include "string.h"
#endif // LD_CORE_CORE_H_

150
code/core/impl/string.c Normal file
View File

@@ -0,0 +1,150 @@
Str8 Str8_Wrap(S64 count, U8 *data) {
Str8 result;
result.data = data;
result.count = count;
return result;
}
Str8 Str8_WrapRange(U8 *start, U8 *end) {
Str8 result;
result.data = start;
result.count = cast(S64) (end - start);
return result;
}
internal S64 Str8_CountZ(U8 *data) {
S64 result = 0;
while (data[result] != 0) { result += 1; }
return result;
}
Str8 Str8_WrapZ(U8 *data) {
Str8 result;
result.data = data;
result.count = Str8_CountZ(data);
return result;
}
Str8 Str8_Copy(M_Arena *arena, Str8 s) {
Str8 result;
result.count = s.count;
result.data = M_ArenaPush(arena, U8, .count = s.count + 1);
M_CopySize(result.data, s.data, s.count);
return result;
}
Str8 Str8_Format(M_Arena *arena, const char *format, ...) {
va_list args;
va_start(args, format);
Str8 result = Str8_FormatArgs(arena, format, args);
va_end(args);
return result;
}
internal S64 Str8_ProcessFormat(Str8 out, const char *format, va_list args) {
S64 result = vsnprintf((char *) out.data, (int) out.count, format, args);
return result;
}
Str8 Str8_FormatArgs(M_Arena *arena, const char *format, va_list args) {
Str8 result;
va_list copy;
va_copy(copy, args);
U64 offset = M_ArenaOffset(arena);
result.count = KB(1);
result.data = M_ArenaPush(arena, U8, .count = result.count);
S64 needed = Str8_ProcessFormat(result, format, args);
if (needed >= result.count) {
M_ArenaPop(arena, offset);
result.count = needed;
result.data = M_ArenaPush(arena, U8, .count = result.count + 1);
Str8_ProcessFormat(result, format, copy);
}
else {
U64 size = result.count - needed - 1;
M_ArenaPopSize(arena, size);
result.count = needed;
}
return result;
}
B32 Str8_Equal(Str8 a, Str8 b, Str8_EqualFlags flags) {
B32 result = (a.count == b.count);
if (result) {
B32 ignore_case = (flags & STR8_EQUAL_IGNORE_CASE) != 0;
for (S64 it = 0; it < a.count; ++it) {
U8 ac = ignore_case ? Chr_ToLowercase(a.data[it]) : a.data[it];
U8 bc = ignore_case ? Chr_ToLowercase(b.data[it]) : b.data[it];
if (ac != bc) {
result = false;
break;
}
}
}
return result;
}
Str8 Str8_Prefix(Str8 s, S64 count) {
Str8 result;
result.data = s.data;
result.count = Min(s.count, count);
return result;
}
Str8 Str8_Suffix(Str8 s, S64 count) {
Str8 result;
result.count = Min(s.count, count);
result.data = s.data + (s.count - result.count);
return result;
}
Str8 Str8_RemoveAfterLast(Str8 s, U8 c) {
Str8 result;
result.data = s.data;
result.count = s.count;
for (S64 it = s.count - 1; it >= 0; --it) {
if (result.data[it] == c) {
result.count = it;
break;
}
}
return result;
}
B32 Str8_EndsWith(Str8 s, Str8 suffix) {
B32 result = Str8_Equal(Str8_Suffix(s, suffix.count), suffix, 0);
return result;
}
U8 Chr_ToUppercase(U8 c) {
U8 result = (c >= 'a' && c <= 'z') ? (c - ('a' - 'A')) : c;
return result;
}
U8 Chr_ToLowercase(U8 c) {
U8 result = (c >= 'A' && c <= 'Z') ? (c + ('a' - 'A')) : c;
return result;
}

View File

@@ -34,6 +34,8 @@
#define SLL_PushN(h, n, next) ((n)->next = (h), (h) = (n))
#define SLL_PopN(h, next) (((h) != 0) ? (h) = (h)->next : 0)
#define SLL_Enqueue(h, t, n) SLL_EnqueueN(h, t, n, next)
#define function static
#define internal static
#define global_var static

36
code/core/string.h Normal file
View File

@@ -0,0 +1,36 @@
#if !defined(LD_CORE_STRING_H_)
#define LD_CORE_STRING_H_
#define S(x) Str8_Wrap(sizeof(x) - sizeof(*(x)), (U8 *) (x))
#define Sz(x) Str8_WrapZ((U8 *) (x))
#define Sv(x) (int) (x).count, (x).data
#define Sf(arena, fmt, ...) Str8_Format(arena, fmt, ##__VA_ARGS__)
function Str8 Str8_Wrap(S64 count, U8 *data);
function Str8 Str8_WrapRange(U8 *start, U8 *end);
function Str8 Str8_WrapZ(U8 *data);
function Str8 Str8_Copy(M_Arena *arena, Str8 s);
function Str8 Str8_Format(M_Arena *arena, const char *format, ...);
function Str8 Str8_FormatArgs(M_Arena *arena, const char *format, va_list args);
typedef U32 Str8_EqualFlags;
enum {
STR8_EQUAL_IGNORE_CASE = (1 << 0)
};
function B32 Str8_Equal(Str8 a, Str8 b, Str8_EqualFlags flags);
function Str8 Str8_Prefix(Str8 s, S64 count);
function Str8 Str8_Suffix(Str8 s, S64 count);
function Str8 Str8_RemoveAfterLast(Str8 s, U8 c);
function B32 Str8_EndsWith(Str8 s, Str8 suffix);
function U8 Chr_ToUppercase(U8 c);
function U8 Chr_ToLowercase(U8 c);
#endif // LD_CORE_STRING_H_