diff options
author | pryazha <pryadeiniv@mail.ru> | 2025-06-15 15:28:45 +0500 |
---|---|---|
committer | pryazha <pryadeiniv@mail.ru> | 2025-06-15 15:28:45 +0500 |
commit | e7f67b450d8034b532101445035d3b199e702621 (patch) | |
tree | d1193a6044d75800266cec11776358be7270cf8b /example | |
parent | 92850237f42cecfeba519bc15f7f5bb7a76cde5f (diff) |
windows?
Diffstat (limited to 'example')
-rwxr-xr-x | example/build.sh | 17 | ||||
-rwxr-xr-x | example/example | bin | 62744 -> 59280 bytes | |||
-rw-r--r-- | example/example.c | 388 | ||||
-rwxr-xr-x | example/example.exe | bin | 0 -> 315771 bytes |
4 files changed, 154 insertions, 251 deletions
diff --git a/example/build.sh b/example/build.sh index 172aa69..de969e8 100755 --- a/example/build.sh +++ b/example/build.sh @@ -1,7 +1,14 @@ #!/bin/sh -CFLAGS='-g -Wall' -INCLUDE='-I..' -LIBS='-lm' +compiler='gcc' +if [ $# -eq 1 ] ; then + if [ $1 = 'windows' ] ; then + compiler='x86_64-w64-mingw32-gcc' + fi +fi +cflags='-g -Wall' +include='-I..' +libs='-lm' +path=`dirname $0` set -x -#tcc $CFLAGS $INCLUDE $LIBS -o example example.c && ./example -gcc $CFLAGS $INCLUDE $LIBS -o example example.c && ./example +cd $path +$compiler $cflags $include -o example example.c $libs diff --git a/example/example b/example/example Binary files differindex cefef94..cdaf21e 100755 --- a/example/example +++ b/example/example diff --git a/example/example.c b/example/example.c index dc76ffb..6b0085a 100644 --- a/example/example.c +++ b/example/example.c @@ -1,276 +1,172 @@ #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); - - printf("OFFSETOF(SomeStruct, flags): %ld\n\n", OFFSETOF(SomeStruct, flags)); - - F32 range_min = 0.0f, - range_max = 1.0f, - clamped_number = 1.5f, - another_clamped_number = -0.2f; - - printf("CLAMPBOT(%f, %f): %f\n", range_min, - another_clamped_number, CLAMPBOT(another_clamped_number, range_min)); - printf("CLAMPTOP(%f, %f): %f\n", range_max, - clamped_number, CLAMPTOP(clamped_number, range_max)); +#define MAX_NODES 10 - 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; +typedef struct { + i32 i32_element; + i16 i16_element; + u8 u8_element; +} type_t; - 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); +typedef struct stack_node_t { + i32 value; + struct stack_node_t *next; +} stack_node_t; - SomeStruct some_stuct = { 10, 20, 30 }; - printf("SomeStruct: first: %d, second: %d, flags: %d\n", - some_stuct.first, some_stuct.second, some_stuct.flags); - MEM0(&some_stuct, sizeof(SomeStruct)); - printf("After MEM0(&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); +typedef struct { + stack_node_t *first; + stack_node_t *last; +} stack_t; - 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); - MEM0STRUCT(&some_stuct); - printf("After MEM0STRUCT(&some_stuct):\n"); - printf("some_stuct: first: %d, second: %d, flags: %d\n\n", - some_stuct.first, some_stuct.second, some_stuct.flags); +typedef struct node_t { + i32 value; + struct node_t *next; + struct node_t *prev; +} node_t; - 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); +typedef struct { + node_t *first; + node_t *last; +} list_t; - 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' : ' '); - } - MEMCPY(dynamic_array_second, dynamic_array_first, dynamic_array_size); - printf("After MEMCPY(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' : ' '); +i32 main(void) +{ + // os + printf("os: "); + switch (OS) { + case OS_LINUX: + printf("linux\n"); + break; + case OS_WINDOWS: + printf("windows\n"); + break; + default: + printf("unsupported\n"); + return 1; } - S32 memory_match = MEMMATCH(dynamic_array_first, dynamic_array_second, dynamic_array_size); - printf("And MEMMATCH(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); + arena_t path_arena = alloc_arena(MAX_PATH); + char *dir = sys_getbindir(&path_arena); + printf("bin directory \"%s\"\n", dir); + + // macros + u64 offset = offsetof(type_t, u8_element); + assert(offset == 6); + printf("offsetof(type_t, u8_element) = %lu\n", offset); + i32 clamped = clamp(0, -1, 1); + assert(clamped == 0); + printf("clamp(0, -1, 1) = %d\n", clamped); + clamped = clamp(0, 2, 1); + assert(clamped == 1); + printf("clamp(0, 2, 1) = %d\n", clamped); + + i32 nums[] = {0, 1, 2, 3}; + printf("mem0: ["); + for (i32 i = 0; i < array_count(nums); ++i) + printf("%x%s", nums[i], (i == array_count(nums)-1) ? "" : ", "); + memzero(nums, sizeof(nums)); + printf("] -> ["); + for (i32 i = 0; i < array_count(nums); ++i) { + assert(!nums[i]); + printf("%x%s", nums[i], (i == array_count(nums)-1) ? "" : ", "); } - 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("]\n"); - 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("Singly linked list (stack for example):\n"); - printf("Matrices:\n"); - printf("Identity:\n"); - MAT4 m = MAT4_IDENTITY; - printmat4(m); - m = scalemat4(m, v3a(10.0f)); - m = translmat4(m, v3a(1.0f)); - m = translmat4(m, v3(0.0f, 68.0f, 0.0f)); - printmat4(m); - printf("Determinant: %f\n", detmat4(m)); - printf("Transpose:\n"); printmat4(transpmat4(m)); - printf("mat4rotate(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f)):\n"); - printmat4(rotatemat4(MAT4_IDENTITY, v3(0.0f, 45.0f, 0.0f))); + stack_node_t stack_nodes[MAX_NODES]; + stack_t stack = {0}; + for (i32 i = 0; i < MAX_NODES; ++i) { + stack_node_t *node = stack_nodes+i; + node->value = i; + sllpush(stack.first, stack.last, node); + } - sys_printf("Example of using sys_printf without args\n"); - sys_printf("And with some of number : %x : <- like that or string : %s : for example\n", 69, "yeah"); + for (stack_node_t *node = stack.first; node; node = node->next) + printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); - printf("Using Arenas:\n"); + sllpop(stack.first, stack.last); - Arena *a; - U8 *df, - *ds; - Arena *tmpa; + printf("After pop:\n"); + for (stack_node_t *node = stack.first; node; node = node->next) + printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); - a = alloc_arena(10); - df = push_arena(a, 5); - memset(df, 5, 5); - ds = push_arena(a, 5); - memset(ds, 10, 5); - - tmpa = alloc_arena(0); - - flsprint(FLSLIT("|")); - for (S32 i = 0; i < 10; i++) - flsprint(flspushf(tmpa, "%x%s", df[i], ((i+1) % 5 == 0) ? "|" : " ")); - flsprint(FLSLIT("\n")); - - release_arena(tmpa); - release_arena(a); - - a = alloc_arena(0); - SLLNode *first = 0; - SLLNode *last = 0; - S32 node_count = 10; - for (S32 i = 0; i < node_count; ++i) { - SLLNode *new = push_arena(a, sizeof(SLLNode)); - new->val = i; - SLLPUSH(first, last, new); + node_t nodes[MAX_NODES]; + list_t list = {0}; + for (i32 i = 0; i < MAX_NODES; ++i) { + node_t *node = nodes+i; + node->value = i; + dllpushback(list.first, list.last, node); } - for (SLLNode *node = first; node; node = node->next) - printf("%d%s", node->val, ((node->next) ? " -> " : "\n")); - release_arena(a); - - a = alloc_arena(sizeof(verts)); - S32 verts_count = ARRCNT(verts); - F32 *dynamic_verts = push_arena(a, sizeof(verts)); - MEM0(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' : ' '); - } - release_arena(a); - printf("\n"); - - Arena *str_arena = alloc_arena(0); - printf("Strings:\n"); - char *cstr = "This is a C str\n"; - FLS str = flsfromcstr(cstr); - str = flschopstart(str, 10); - cstr = flstocstr(a, str); - printf("%s", cstr); - flsprint(str); - FLS choped_str = flschopend(str, 3); - flsprint(choped_str); + + printf("Doubly linked list:\n"); + for (node_t *node = list.first; node; node = node->next) + printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); + + printf("Remove odd numbers:\n"); + for (node_t *node = list.first; node; node = node->next) + if (node->value % 2 == 1) + dllremove(list.first, list.last, node); + + for (node_t *node = list.first; node; node = node->next) + printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); + printf("\n"); - FLSList *list = flslist(str_arena); - flslistpush(str_arena, list, str, 0); - flslistpush(str_arena, list, flsfromcstr("test"), 0); - flslistpush(str_arena, list, flsfromcstr("and this is also a test\n"), 0); - flslistpush(str_arena, list, flsfromcstr("Kinda works!"), 1); - printf("FLSList: "); - flslistprint(list); - release_arena(str_arena); - flslistprint(list); - str_arena = alloc_arena(KB(10)); - U32 memory_size = 512; - U32 count = memory_size/sizeof(S32); - S32 *some = push_arena(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); + printf("I'm pretty sure the vectors are fine\n"); + printf("At least for the last few months :)\n\n"); - pop_arena(str_arena, memory_size); + printf("Arenas:\n"); - 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); - - FLS new_str = flspushf(str_arena, "Test of the formatted string: %d\n", 69); - flsprint(new_str); - - list = flslist(str_arena); - flslistpushf(str_arena, list, 0, "This is a list %d", 34); - flslistpushf(str_arena, list, 0, " of formatted strings: %d", 35); - flslistpushf(str_arena, list, 1, "And you can push to the start"); - /* TODO(pryazha): Think about simpler way of using string literals. - * Maybe you don't need to, I dunno */ - flslistpushf(str_arena, list, 1, "The most interesting part is that you can include the whole string like that: %*s\n", - FLSEXP(FLSLIT("Absolutely random string with absolutely random content (or not)."))); - flslistprint(list); + arena_t arena = alloc_arena(10); + u8 *first = push_arena(&arena, 5); + prb_memset(first, 5, 5); + u8 *second = push_arena(&arena, 5); + prb_memset(second, 10, 5); + printf("|"); + for (i32 i = 0; i < 10; i++) + printf("%x%s", first[i], ((i+1) % 5 == 0) ? "|" : " "); + printf("\n"); + release_arena(&arena); - release_arena(str_arena); + printf("Strings:\n"); - a = alloc_arena(MB(1)); - flsprint(FLSLIT("build.sh content:\n")); - FLS file_content = fls_read_entire_file(a, FLSLIT("build.sh")); - if (file_content.p) - flsprint(file_content); + arena_t str_arena = alloc_arena(0); + str8_list_t strlist = {0}; + str8_list_push(&str_arena, &strlist, 0, str8lit("first ")); + str8_list_push(&str_arena, &strlist, 0, str8lit("second ")); + str8_list_push(&str_arena, &strlist, 0, str8lit("third\n")); + str8_print(str8lit("strlist: ")); + str8_list_print(&strlist); + + str8_t fstr = str8_pushf(&str_arena, "Formatted string: %d\n", 69); + str8_print(fstr); + + memzero_struct(&strlist); + str8_list_pushf(&str_arena, &strlist, 0, "first %d -> ", 34); + str8_list_pushf(&str_arena, &strlist, 0, "second: %d -> ", 35); + str8_list_pushf(&str_arena, &strlist, 0, "sum: %d\n", 34+35); + str8_list_pushf(&str_arena, &strlist, 1, "%s", "To the front -> "); + + str8_print(str8lit("Formatted string list:\n")); + str8_list_print(&strlist); + + str8_t catstr = str8_list_join(&str_arena, &strlist); + str8_print(str8lit("Concatenated string list:\n")); + str8_print(catstr); + + release_arena(&str_arena); + + const char *filename = "build.sh"; + arena = alloc_arena(megabytes(1)); + u8 *content; + if (sys_read_file(&arena, &content, filename)) + printf("%s content:\n%s", filename, content); else - flsprint(FLSLIT("failed to read build.sh\n")); - release_arena(a); + printf("failed to read %s\n", filename); + release_arena(&arena); return(0); } diff --git a/example/example.exe b/example/example.exe Binary files differnew file mode 100755 index 0000000..ae02648 --- /dev/null +++ b/example/example.exe |