From 80a734de03eaf3d534f95010653c47bf54389f48 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 12:50:39 +1100 Subject: new string function signatures just dropped --- examples/main_loop/ex_main_loop.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'examples') diff --git a/examples/main_loop/ex_main_loop.c b/examples/main_loop/ex_main_loop.c index 0ea9ded..de0caa6 100644 --- a/examples/main_loop/ex_main_loop.c +++ b/examples/main_loop/ex_main_loop.c @@ -1,22 +1,45 @@ #include #include "core.h" +#include "file.h" #include "render.h" +#include "str.h" int main() { core* core = core_bringup(); - // Main loop - while (!glfwWindowShouldClose(core->renderer.window)) { - input_update(&core->input); - threadpool_process_results(&core->threadpool, 1); + size_t arena_size = 1024; + arena scratch = arena_create(malloc(arena_size), arena_size); + arena* a = &scratch; + + str8 hello = str8lit("Hello World"); + + // this works but we should be careful because str8 is not *guaranteed* to point to + // a null-terminated string + printf("String before: '%s' (null-terminated: %s) \n ", hello.buf, + str8_is_null_term(hello) ? "true" : "false"); - render_frame_begin(&core->renderer); + char* c = str8_to_cstr(&scratch, hello); - // insert work here + printf("String after: %s\n", c); - render_frame_end(&core->renderer); + str8_opt test_file = str8_from_file(&scratch, str8lit("assets/shaders/ui_rect.vert")); + if (test_file.has_value) { + printf("Contents: %.*s \n", (int)test_file.contents.len, test_file.contents.buf); + printf("Null-terminated: %s\n", str8_is_null_term(test_file.contents) ? "true" : "false"); } + // Main loop + // while (!glfwWindowShouldClose(core->renderer.window)) { + // input_update(&core->input); + // threadpool_process_results(&core->threadpool, 1); + // + // render_frame_begin(&core->renderer); + // + // // insert work here + // + // render_frame_end(&core->renderer); + // } + return 0; -} \ No newline at end of file +} -- cgit v1.2.3-70-g09d2 From 348161b7ac920ea450becd35685b81e87342d1e5 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:44:43 +1100 Subject: leave main loop in the example --- examples/main_loop/ex_main_loop.c | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'examples') diff --git a/examples/main_loop/ex_main_loop.c b/examples/main_loop/ex_main_loop.c index 9814fbe..5870f59 100644 --- a/examples/main_loop/ex_main_loop.c +++ b/examples/main_loop/ex_main_loop.c @@ -8,6 +8,7 @@ int main() { core* core = core_bringup(); + // Examples of how to work with arenas and strings size_t arena_size = 1024; arena scratch = arena_create(malloc(arena_size), arena_size); arena* a = &scratch; @@ -30,16 +31,16 @@ int main() { } // Main loop - // while (!glfwWindowShouldClose(core->renderer.window)) { - // input_update(&core->input); - // threadpool_process_results(&core->threadpool, 1); - // - // render_frame_begin(&core->renderer); - // - // // insert work here - // - // render_frame_end(&core->renderer); - // } + while (!glfwWindowShouldClose(core->renderer.window)) { + input_update(&core->input); + threadpool_process_results(&core->threadpool, 1); + + render_frame_begin(&core->renderer); + + // insert work here + + render_frame_end(&core->renderer); + } return 0; } -- cgit v1.2.3-70-g09d2 From da13dc8b030b9c0ed4805dca9dab3c280583a91f Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:46:34 +1100 Subject: split into two examples --- examples/main_loop/ex_main_loop.c | 24 ------------------------ xmake.lua | 7 +++++++ 2 files changed, 7 insertions(+), 24 deletions(-) (limited to 'examples') diff --git a/examples/main_loop/ex_main_loop.c b/examples/main_loop/ex_main_loop.c index 5870f59..3b2354a 100644 --- a/examples/main_loop/ex_main_loop.c +++ b/examples/main_loop/ex_main_loop.c @@ -1,35 +1,11 @@ #include #include "core.h" -#include "file.h" #include "render.h" -#include "str.h" int main() { core* core = core_bringup(); - // Examples of how to work with arenas and strings - size_t arena_size = 1024; - arena scratch = arena_create(malloc(arena_size), arena_size); - arena* a = &scratch; - - str8 hello = str8lit("Hello World"); - - // this works but we should be careful because str8 is not *guaranteed* to point to - // a null-terminated string - printf("String before: '%s' (null-terminated: %s) \n ", hello.buf, - str8_is_null_term(hello) ? "true" : "false"); - - char* c = str8_to_cstr(&scratch, hello); - - printf("String after: %s\n", c); - - str8_opt test_file = str8_from_file(&scratch, str8lit("assets/shaders/ui_rect.vert")); - if (test_file.has_value) { - printf("Contents: %.*s \n", (int)test_file.contents.len, test_file.contents.buf); - printf("Null-terminated: %s\n", str8_is_null_term(test_file.contents) ? "true" : "false"); - } - // Main loop while (!glfwWindowShouldClose(core->renderer.window)) { input_update(&core->input); diff --git a/xmake.lua b/xmake.lua index 3d267dd..e6b77c2 100644 --- a/xmake.lua +++ b/xmake.lua @@ -87,4 +87,11 @@ target("main_loop") set_group("examples") add_deps("core_shared") add_files("examples/main_loop/ex_main_loop.c") + set_rundir("$(projectdir)") + +target("std") + set_kind("binary") + set_group("examples") + add_deps("core_static") + add_files("examples/standard_lib/ex_std.c") set_rundir("$(projectdir)") \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 938f745f052c14fccaa987b98ea4ac84e70c1fcc Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:49:12 +1100 Subject: chore: format --- examples/standard_lib/ex_std.c | 30 ++++++++++++++++++++++++++++++ src/platform/file.h | 4 ++-- src/std/str.h | 20 ++++++++++---------- 3 files changed, 42 insertions(+), 12 deletions(-) create mode 100644 examples/standard_lib/ex_std.c (limited to 'examples') diff --git a/examples/standard_lib/ex_std.c b/examples/standard_lib/ex_std.c new file mode 100644 index 0000000..9d474de --- /dev/null +++ b/examples/standard_lib/ex_std.c @@ -0,0 +1,30 @@ +#include +#include +#include "file.h" +#include "str.h" + +int main() { + // Examples of how to work with arenas and strings + size_t arena_size = 1024; + arena scratch = arena_create(malloc(arena_size), arena_size); + arena* a = &scratch; + + str8 hello = str8lit("Hello World"); + + // this works but we should be careful because str8 is not *guaranteed* to point to + // a null-terminated string + printf("String before: '%s' (null-terminated: %s) \n ", hello.buf, + str8_is_null_term(hello) ? "true" : "false"); + + char* c = str8_to_cstr(&scratch, hello); + + printf("String after: %s\n", c); + + str8_opt test_file = str8_from_file(&scratch, str8lit("assets/shaders/ui_rect.vert")); + if (test_file.has_value) { + printf("Contents: %.*s \n", (int)test_file.contents.len, test_file.contents.buf); + printf("Null-terminated: %s\n", str8_is_null_term(test_file.contents) ? "true" : "false"); + } + + return 0; +} diff --git a/src/platform/file.h b/src/platform/file.h index fa09891..8bb22c8 100644 --- a/src/platform/file.h +++ b/src/platform/file.h @@ -10,8 +10,8 @@ #include "str.h" typedef struct str8_opt { - str8 contents; - bool has_value; + str8 contents; + bool has_value; } str8_opt; const char* string_from_file(const char* path); diff --git a/src/std/str.h b/src/std/str.h index d2fa891..f6f8820 100644 --- a/src/std/str.h +++ b/src/std/str.h @@ -1,12 +1,12 @@ /** * @file str.h * @author your name (you@domain.com) - * @brief + * @brief * @version 0.1 * @date 2024-02-25 - * + * * @copyright Copyright (c) 2024 - * + * */ #pragma once @@ -19,22 +19,22 @@ print routines are written. alternatively wrap in `cstr()` and pass to `%s`. */ typedef struct { - u8 *buf; + u8* buf; size_t len; } str8; - // --- Constructors /** @brief Take a string literal and turn it into a `str8` */ -#define str8lit(s) (str8){(u8 *)s, ((sizeof(s) / sizeof(*(s)) - 1))} +#define str8lit(s) \ + (str8) { (u8*)s, ((sizeof(s) / sizeof(*(s)) - 1)) } str8 str8_create(u8* buf, size_t len); /** @brief Return a null-terminated C string cloned onto an arena */ char* str8_to_cstr(arena* a, str8 s); -#define cstr(a, s) (str8_to_cstr(a, s)) // Shorthand +#define cstr(a, s) (str8_to_cstr(a, s)) // Shorthand // --- Comparisons @@ -43,8 +43,8 @@ bool str8_equals(str8 a, str8 b); /** * @brief Compare the first `first_nchars` of each string for equality - * @details If either of the strings are shorter than the number only the characters up until the end - of the shorter string will be compared. + * @details If either of the strings are shorter than the number only the characters up until the + end of the shorter string will be compared. * @returns 0 if they are fully equal up until `first_nchars`, i.e they never differed, else it returns the index at which the first string differed from the second string. */ @@ -69,5 +69,5 @@ str8 str8_concat(arena* a, str8 left, str8 right); /// --- Misc static inline bool str8_is_null_term(str8 a) { - return a.buf[a.len] == 0; // This doesn't seem safe. YOLO + return a.buf[a.len] == 0; // This doesn't seem safe. YOLO } \ No newline at end of file -- cgit v1.2.3-70-g09d2