diff options
Diffstat (limited to 'example/example.c')
-rw-r--r-- | example/example.c | 260 |
1 files changed, 260 insertions, 0 deletions
diff --git a/example/example.c b/example/example.c new file mode 100644 index 0000000..1fa85b3 --- /dev/null +++ b/example/example.c @@ -0,0 +1,260 @@ +#include "prb.h" + +typedef struct { + S32 first; + S32 second; + B32 flags; +} SomeStruct; + +typedef struct SLLNode { + S32 val; + struct SLLNode *next; +} SLLNode; + +typedef struct DLLNode { + S32 val; + struct DLLNode *next; + struct DLLNode *prev; +} DLLNode; + +int main(void) +{ + F32 verts[] = { + 1.0f, 0.0f, 0.0f, + 0.0f, 1.0f, 0.0f, + 0.0f, 0.0f, 1.0f + }; + + Assert(v3a(1.0f).y); + U64 memory_size = Kilobytes(4); + void *some_memory = malloc(memory_size); + *(S32 *)some_memory = 69; + U64 ptr_as_int = IntFromPtr(some_memory); + void *ptr_to_memory = PtrFromInt(ptr_as_int); + printf("IntFromPtr: %lu\n", ptr_as_int); + printf("Using PtrFromInt(%lu) to get the first element of some_memory: %d\n\n", + ptr_as_int, *(S32 *)ptr_to_memory); + free(some_memory); + + printf("OffsetOfMember(SomeStruct, flags): %lld\n\n", + OffsetOfMember(SomeStruct, flags)); + + F32 range_min = 0.0f, range_max = 1.0f; + F32 clamped_number = 1.5f; + F32 another_clamped_number = -0.2f; + printf("ClampBottom(%f, %f): %f\n", range_min, + another_clamped_number, ClampBottom(another_clamped_number, range_min)); + printf("ClampTop(%f, %f): %f\n", range_max, + clamped_number, ClampTop(clamped_number, range_max)); + + printf("Clamp(%f, %f, %f): %f\n", range_min, clamped_number, range_max, + Clamp(range_min, clamped_number, range_max)); + printf("Clamp(%f, %f, %f): %f\n\n", range_min, another_clamped_number, range_max, + Clamp(range_min, another_clamped_number, range_max)); + + F32 swap_test_first = 10.0f, swap_test_second = 20.0f; + printf("Before Swap: swap_test_first = %f, swap_test_second = %f\n", + swap_test_first, swap_test_second); + Swap(F32, swap_test_first, swap_test_second); + printf("After Swap: swap_test_first = %f, swap_test_second = %f\n\n", + swap_test_first, swap_test_second); + + SomeStruct some_stuct = { 10, 20, 30 }; + printf("SomeStruct: first: %d, second: %d, flags: %d\n", + some_stuct.first, some_stuct.second, some_stuct.flags); + MemoryZero(&some_stuct, sizeof(SomeStruct)); + printf("After MemoryZero(&some_stuct, sizeof(SomeStruct)):\n"); + printf("some_stuct: first: %d, second: %d, flags: %d\n\n", + some_stuct.first, some_stuct.second, some_stuct.flags); + + some_stuct = (SomeStruct){ 10, 20, 30 }; + printf("some_stuct: first: %d, second: %d, flags: %d\n", + some_stuct.first, some_stuct.second, some_stuct.flags); + MemoryZeroStruct(&some_stuct); + printf("After MemoryZeroStruct(&some_stuct):\n"); + printf("some_stuct: first: %d, second: %d, flags: %d\n\n", + some_stuct.first, some_stuct.second, some_stuct.flags); + + printf("Before MemoryZeroArray:\n"); + for (S32 i = 0; i < ArrayCount(verts); ++i) + printf("%f%c", verts[i], (((i+1)%4 == 0) || + (i == ArrayCount(verts)-1)) ? '\n' : ' '); + MemoryZeroArray(verts); + printf("After:\n"); + for (S32 i = 0; i < ArrayCount(verts); ++i) + printf("%f%c", verts[i], (((i+1)%4 == 0) || + (i == ArrayCount(verts)-1)) ? '\n' : ' '); + printf("\n"); + + S32 dynamic_array_length = 10; + U64 dynamic_array_size = dynamic_array_length*sizeof(S32); + F32 *dynamic_array_first = malloc(dynamic_array_size); + F32 *dynamic_array_second = malloc(dynamic_array_size); + printf("first dynamic array:\n"); + for (S32 i = 0; i < dynamic_array_length; ++i) { + F32 *element = dynamic_array_first+i; + *element = i+420; + printf("%f%c", *element, (i == dynamic_array_length-1) ? '\n' : ' '); + } + printf("second dynamic array:\n"); + for (S32 i = 0; i < dynamic_array_length; ++i) { + F32 *element = dynamic_array_second+i; + printf("%f%c", *element, (i == dynamic_array_length-1) ? '\n' : ' '); + } + MemoryCopy(dynamic_array_second, dynamic_array_first, dynamic_array_size); + printf("After MemoryCopy(dynamic_array_second, dynamic_array_first, dynamic_array_size):\n"); + printf("second dynamic array:\n"); + for (S32 i = 0; i < dynamic_array_length; ++i) { + F32 *element = dynamic_array_second+i; + printf("%f%c", *element, (i == dynamic_array_length-1) ? '\n' : ' '); + } + S32 memory_match = MemoryMatch(dynamic_array_first, dynamic_array_second, dynamic_array_size); + printf("And MemoryMatch(MemoryMatch(dynamic_array_first, dynamic_array_second, dynamic_array_size): %d\n\n", memory_match); + free(dynamic_array_first); + free(dynamic_array_second); + + printf("Linked Lists:\n"); + printf("Singly linked list: "); + S32 list_length = 5; + U64 list_size = list_length*sizeof(SLLNode); + SLLNode *sllist = malloc(list_size); + SLLNode *sllfirst = 0; + SLLNode *slllast = 0; + for (S32 i = 0; i < list_length; ++i) { + SLLNode *new = sllist+i; + new->val = i; + SLLPush(sllfirst, slllast, new); + } + for (SLLNode *node = sllfirst; node; node = node->next) + printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); + SLLPop(sllfirst, slllast); + printf("After SLLPop(sllfirst, slllast): "); + for (SLLNode *node = sllfirst; node; node = node->next) + printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); + free(sllist); + + printf("Doubly linked list: "); + list_length = 8; + list_size = list_length*sizeof(DLLNode); + DLLNode *dllist = malloc(list_size); + DLLNode *dllfirst = 0; + DLLNode *dlllast = 0; + for (S32 i = 0; i < list_length; ++i) { + DLLNode *new = dllist+i; + new->val = i; + DLLPushBack(dllfirst, dlllast, new); + } + for (DLLNode *node = dllfirst; node; node = node->next) + printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); + printf("Remove odd numbers: "); + for (DLLNode *node = dllfirst; node; node = node->next) + if (node->val % 2 == 1) + DLLRemove(dllfirst, dlllast, node); + for (DLLNode *node = dllfirst; node; node = node->next) + printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); + free(dllist); + printf("\n"); + + printf("I'm pretty sure the vectors are fine.\nAt least for the last few months :)\n\n"); + + printf("Matrices:\n"); + printf("Identity:\n"); + Mat4 m = MAT4_IDENTITY; + mat4print(m); + m = mat4scale(m, v3a(10.0f)); + m = mat4translate(m, v3a(1.0f)); + m = mat4translate(m, v3(0.0f, 68.0f, 0.0f)); + mat4print(m); + printf("Determinant: %f\n", mat4det(m)); + printf("Transpose:\n"); mat4print(mat4transpose(m)); + printf("mat4rotate(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f)):\n"); + mat4print(mat4rotate(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f))); + + printf("Using Arenas:\n"); + Arena *a = arena_alloc(0); + SLLNode *first = 0; + SLLNode *last = 0; + S32 node_count = 10; + for (S32 i = 0; i < node_count; ++i) { + SLLNode *new = arena_push(a, sizeof(SLLNode)); + new->val = i; + SLLPush(first, last, new); + } + for (SLLNode *node = first; node; node = node->next) + printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); + arena_release(a); + + a = arena_alloc(sizeof(verts)); + S32 verts_count = ArrayCount(verts); + F32 *dynamic_verts = arena_push(a, sizeof(verts)); + MemoryZero(dynamic_verts, verts_count*sizeof(F32)); + for (S32 i = 0; i < verts_count; ++i) { + F32 *vert = dynamic_verts+i; + printf("%f%c", *vert, (i == verts_count-1) ? '\n' : ' '); + } + arena_release(a); + printf("\n"); + + Arena *str_arena = arena_alloc(0); + printf("Strings:\n"); + char *cstr = "This is a C str\n"; + Str8 str = str8_from_cstr(cstr); + str = str8_chop_start(str, 10); + cstr = str8_to_cstr(a, str); + printf("%s", cstr); + str8print(str); + Str8 choped_str = str8_chop_end(str, 3); + str8print(choped_str); + printf("\n"); + Str8List *list = str8_list(str_arena); + str8_list_push(str_arena, list, str, 0); + str8_list_push(str_arena, list, str8_from_cstr("test"), 0); + str8_list_push(str_arena, list, str8_from_cstr("and this is also a test\n"), 0); + str8_list_push(str_arena, list, str8_from_cstr("Kinda works!"), 1); + printf("Str8List: "); + str8_list_print(list); + arena_release(str_arena); + str8_list_print(list); + + str_arena = arena_alloc(Kilobytes(10)); + memory_size = 512; + U32 count = memory_size/sizeof(S32); + S32 *some = arena_push(str_arena, memory_size); + printf("str_arena(size): %ld\nMemory:\n", str_arena->cap); + for (S32 i = 0; i < count; i++) { + S32 *e = some+i; + *e = 69; + printf("%d: %d%c", i, *e, ((i == count-1) ? '\n' : ' ')); + } + printf("Memory used: %lu\n", str_arena->used); + printf("Remaining memory: %lu\n", + str_arena->cap-str_arena->used); + + arena_pop(str_arena, memory_size); + + printf("After arena pop\n"); + printf("Memory used: %lu\n", str_arena->used); + printf("Remaining memory: %lu\n", + str_arena->cap-str_arena->used); + printf("some ptr is %s\n", some ? "not null" : "null"); + printf("str_arena(size): %lu\nMemory:\n", str_arena->cap); + + Str8 new_str = str8_pushf(str_arena, "Test of the formatted string: %d\n", 69); + str8print(new_str); + + list = str8_list(str_arena); + str8_list_pushf(str_arena, list, 0, "This is a list %d", 34); + str8_list_pushf(str_arena, list, 0, " of formatted strings: %d", 35); + str8_list_pushf(str_arena, list, 1, "And you can push to the start"); + str8_list_print(list); + + arena_release(str_arena); + + a = arena_alloc(Megabytes(1)); + str8print(str8lit("build.sh content:\n")); + Str8 file_content = str8_read_entire_file(a, str8lit("build.sh")); + str8print(file_content); + arena_release(a); + + return(0); +} |