From 223b1056273f5d48d1c0bbfee75be7b82374d4eb Mon Sep 17 00:00:00 2001 From: pryazha Date: Sun, 12 Oct 2025 15:24:30 +0500 Subject: change identation, maybe will change back, add own gl functions loading, and glx example --- arena.c | 38 ++--- arena.h | 6 +- b | 52 +++++++ build.sh | 31 ---- example/arena.c | 18 --- example/build.sh | 31 ---- example/macros.c | 44 ------ example/string.c | 19 --- example/sys.c | 30 ---- example/utf8.txt | 204 -------------------------- examples/arena.c | 18 +++ examples/b | 53 +++++++ examples/macros.c | 44 ++++++ examples/string.c | 20 +++ examples/sys.c | 30 ++++ examples/utf8.txt | 204 ++++++++++++++++++++++++++ linux.c | 115 +++++++++------ macros.h | 51 +++---- prb.h | 8 +- prbm.c | 416 ++++++++++++++++++++++++++++++------------------------ prbm.h | 205 ++++++++++++++------------- prbs.c | 219 +++++++++++++++++----------- prbs.h | 32 +++-- prbwin.c | 121 ++++++++-------- sys.h | 1 + 25 files changed, 1093 insertions(+), 917 deletions(-) create mode 100755 b delete mode 100755 build.sh delete mode 100644 example/arena.c delete mode 100755 example/build.sh delete mode 100644 example/macros.c delete mode 100644 example/string.c delete mode 100644 example/sys.c delete mode 100644 example/utf8.txt create mode 100644 examples/arena.c create mode 100755 examples/b create mode 100644 examples/macros.c create mode 100644 examples/string.c create mode 100644 examples/sys.c create mode 100644 examples/utf8.txt diff --git a/arena.c b/arena.c index 03a1c1c..4459a5d 100644 --- a/arena.c +++ b/arena.c @@ -4,35 +4,35 @@ struct arena alloc_arena(u64 capacity) { - /* TODO(pryazha): Find reasonable maximum capacity through testing */ - assert(capacity <= (u64)gigabytes(16)); - if (!capacity) - capacity = kilobytes(4); - void *memory = sys_alloc(capacity); - assert(memory); - return (struct arena){memory, capacity, 0}; + /* TODO(pryazha): Find reasonable maximum capacity through testing */ + assert(capacity <= (u64)gigabytes(16)); + if (!capacity) + capacity = kilobytes(4); + void *memory = sys_alloc(capacity); + assert(memory); + return (struct arena){memory, capacity, 0}; } void release_arena(struct arena *arena) { - assert(arena); - sys_free(arena->memory, arena->capacity); - arena->memory = 0; - arena->capacity = 0; - arena->used = 0; + assert(arena); + sys_free(arena->memory, arena->capacity); + arena->memory = 0; + arena->capacity = 0; + arena->used = 0; } void *push_arena(struct arena *arena, u64 size) { - assert(arena); - assert(arena->used + size <= arena->capacity); - void *memory = arena->memory + arena->used; - arena->used += size; - return memory; + assert(arena); + assert(arena->used + size <= arena->capacity); + void *memory = arena->memory + arena->used; + arena->used += size; + return memory; } void pop_arena(struct arena *arena, u64 size) { - assert(arena); - arena->used -= min(size, arena->used); + assert(arena); + arena->used -= min(size, arena->used); } diff --git a/arena.h b/arena.h index 38e3483..b05d990 100644 --- a/arena.h +++ b/arena.h @@ -4,9 +4,9 @@ #include "types.h" struct arena { - void *memory; - u64 capacity; - u64 used; + void *memory; + u64 capacity; + u64 used; }; struct arena alloc_arena(u64 capacity); diff --git a/b b/b new file mode 100755 index 0000000..235cc28 --- /dev/null +++ b/b @@ -0,0 +1,52 @@ +#!/bin/sh + +echorun() +{ + echo $1 + $1 +} + +compile() +{ + echorun "$cc $cflags $include $lflags -o $dir/$1.o $dir/$1.c $libs" +} + +cur=`pwd` +dir=`realpath $0` +dir=`dirname $dir` + +echorun "cd $dir" + +if [ "$1" = 'clean' ] ; then + echorun "rm -f *.o" + echorun "cd $cur" + exit +fi +if [ "$1" = 'cleanlib' ] ; then + echorun "rm -f *.o libprb.a" + echorun "cd $cur" + exit +fi + +cc='gcc' +debug='-g' +release='-O2' +cflags="-c" +if [ "`echo \"$*\" | grep debug -`" ] ; then + cflags="$debug $cflags" +else + cflags="$release $cflags" +fi + +compile prbm +compile arena +compile prbs +compile linux + +echorun "ar rcs $dir/libprb.a \ + $dir/prbm.o \ + $dir/arena.o \ + $dir/prbs.o \ + $dir/linux.o" + +echorun "cd $cur" diff --git a/build.sh b/build.sh deleted file mode 100755 index 53ad6cb..0000000 --- a/build.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh -start=`pwd` -dir=`dirname "$0"` -if [ "$1" = 'clean' ] ; then - set -x - cd $dir - rm -f *.o - cd "$start" - exit -fi -cc='tcc' -debug='-g' -release='-O2' -cflags="-c -std=c99" -if [ "$1" = "debug" ] ; then - cflags="$debug $cflags" -else - cflags="$release $cflags" -fi -set -x -cd $dir -$cc $cflags prbm.c -$cc $cflags arena.c -$cc $cflags prbs.c -$cc $cflags linux.c -ar rcs libprb.a\ - prbm.o \ - arena.o \ - prbs.o \ - linux.o -cd "$start" diff --git a/example/arena.c b/example/arena.c deleted file mode 100644 index 6579849..0000000 --- a/example/arena.c +++ /dev/null @@ -1,18 +0,0 @@ -#include "prb.h" -#include -#include - -i32 main(void) -{ - info("arenas"); - struct arena arena = alloc_arena(10); - u8 *first = push_arena(&arena, 5); - memset(first, 5, 5); - u8 *second = push_arena(&arena, 5); - 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); -} diff --git a/example/build.sh b/example/build.sh deleted file mode 100755 index 285b072..0000000 --- a/example/build.sh +++ /dev/null @@ -1,31 +0,0 @@ -#!/bin/sh -start=`pwd` -dir=`dirname "$0"` -if [ "$1" = 'clean' ] ; then - set -x - cd "$dir" - rm -f macros macros.exe arena arena.exe sys sys.exe string string.exe - cd "$start" - exit -fi -cc='tcc' -if [ "$1" = 'windows' ] ; then - cc='x86_64-w64-mingw32-gcc' -fi -libsdir='../..' -if [ ! -d "$libsdir/prb" ] ; then - echo "error: failed to get prb library path" - exit -fi -"$libsdir/prb/build.sh" -cflags='-g -Wall' -include="-I$libsdir/prb" -lflags="-L$libsdir/prb" -libs='-lprb' -set -x -cd "$dir" -$cc $cflags $include $lflags -o macros macros.c $libs -$cc $cflags $include $lflags -o arena arena.c $libs -$cc $cflags $include $lflags -o sys sys.c $libs -$cc $cflags $include $lflags -o string string.c $libs -cd "$start" diff --git a/example/macros.c b/example/macros.c deleted file mode 100644 index 0e836b0..0000000 --- a/example/macros.c +++ /dev/null @@ -1,44 +0,0 @@ -#include "prb.h" -#include - -struct type { - i32 i32_element; - i16 i16_element; - u8 u8_element; -}; - -#define nodes_count 10 - -struct stack_node { - i32 value; - struct stack_node *next; -}; - -struct stack { - struct stack_node *first; - struct stack_node *last; -}; - -static void print_stack(struct stack s) -{ - for (struct stack_node *node = s.first; node; node = node->next) - printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); -} - -i32 main(void) -{ - u64 offset = offsetof(struct type, u8_element); - assert(offset == 6); - info("offsetof(type_t, u8_element) = %lu", offset); - info("stack:"); - struct stack_node nodes[nodes_count]; - struct stack stack = {0}; - for (i32 i = 0; i < array_count(nodes); ++i) { - struct stack_node *node = nodes + i; - node->value = i; - sllpush(stack.first, stack.last, node); - } - print_stack(stack); - sllpop(stack.first, stack.last); - print_stack(stack); -} diff --git a/example/string.c b/example/string.c deleted file mode 100644 index 57c460d..0000000 --- a/example/string.c +++ /dev/null @@ -1,19 +0,0 @@ -#include "prb.h" - -i32 main(void) -{ - struct arena arena = alloc_arena(0); - struct string fstr = push_fstring(&arena, "formatted string: %d\n", 69); - print_string(fstr); - info("string list:"); - struct string_list list = {0}; - push_fstring_to_list(&arena, &list, 0, "first %d -> ", 34); - push_fstring_to_list(&arena, &list, 0, "second: %d -> ", 35); - push_fstring_to_list(&arena, &list, 0, "sum: %d\n", 34+35); - push_fstring_to_list(&arena, &list, 1, "%s", "To the front -> "); - print_string_list(&list); - struct string catstr = join_string_list(&arena, &list); - info("Concatenated string list:"); - print_string(catstr); - return(0); -} diff --git a/example/sys.c b/example/sys.c deleted file mode 100644 index 881d2be..0000000 --- a/example/sys.c +++ /dev/null @@ -1,30 +0,0 @@ -#include "prb.h" -#include -#include - -i32 main(int c, char **v) -{ - info("os:"); - switch (os) { - case os_linux: - info("linux"); - break; - case os_windows: - info("windows"); - break; - default: - info("unsupported"); - return 1; - } - char *dir = dirname(*v); - info("bin directory \"%s\"", dir); - const char *filename = "build.sh"; - char path[1024] = {0}; - snprintf(path, 1024, "%s/%s", dir, filename); - struct arena arena = alloc_arena(megabytes(32)); - char *content = 0; - if (!(content = read_entire_file(&arena, 0, path))) - die("failed to read %s", path); - info("%s content:\n%s", path, content); - return 0; -} diff --git a/example/utf8.txt b/example/utf8.txt deleted file mode 100644 index 3af7875..0000000 --- a/example/utf8.txt +++ /dev/null @@ -1,204 +0,0 @@ -// Original by Markus Kuhn, adapted for HTML by Martin D - -UTF-8 encoded sample plain-text file -‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ - -Markus Kuhn [ˈmaʳkʊs kuːn] mkuhn@acm.org — 1999-08-20 - - -The ASCII compatible UTF-8 encoding of ISO 10646 and Unicode -plain-text files is defined in RFC 2279 and in ISO 10646-1 Annex R. - - -Using Unicode/UTF-8, you can write in emails and source code things such as - -Mathematics and Sciences: - - ∮ E⋅da = Q, n → ∞, ∑ f(i) = ∏ g(i), ∀x∈ℝ: ⌈x⌉ = −⌊−x⌋, α ∧ ¬β = ¬(¬α ∨ β), - - ℕ ⊆ ℕ₀ ⊂ ℤ ⊂ ℚ ⊂ ℝ ⊂ ℂ, ⊥ < a ≠ b ≡ c ≤ d ≪ ⊤ ⇒ (A ⇔ B), - - 2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm - -Linguistics and dictionaries: - - ði ıntəˈnæʃənəl fəˈnɛtık əsoʊsiˈeıʃn - Y [ˈʏpsilɔn], Yen [jɛn], Yoga [ˈjoːgɑ] - -APL: - - ((V⍳V)=⍳⍴V)/V←,V ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈ - -Nicer typography in plain text files: - - ╔══════════════════════════════════════════╗ - ║ ║ - ║ • ‘single’ and “double” quotes ║ - ║ ║ - ║ • Curly apostrophes: “We’ve been here” ║ - ║ ║ - ║ • Latin-1 apostrophe and accents: '´` ║ - ║ ║ - ║ • ‚deutsche‘ „Anführungszeichen“ ║ - ║ ║ - ║ • †, ‡, ‰, •, 3–4, —, −5/+5, ™, … ║ - ║ ║ - ║ • ASCII safety test: 1lI|, 0OD, 8B ║ - ║ ╭─────────╮ ║ - ║ • the euro symbol: │ 14.95 € │ ║ - ║ ╰─────────╯ ║ - ╚══════════════════════════════════════════╝ - -Greek (in Polytonic): - - The Greek anthem: - - Σὲ γνωρίζω ἀπὸ τὴν κόψη - τοῦ σπαθιοῦ τὴν τρομερή, - σὲ γνωρίζω ἀπὸ τὴν ὄψη - ποὺ μὲ βία μετράει τὴ γῆ. - - ᾿Απ᾿ τὰ κόκκαλα βγαλμένη - τῶν ῾Ελλήνων τὰ ἱερά - καὶ σὰν πρῶτα ἀνδρειωμένη - χαῖρε, ὦ χαῖρε, ᾿Ελευθεριά! - - From a speech of Demosthenes in the 4th century BC: - - Οὐχὶ ταὐτὰ παρίσταταί μοι γιγνώσκειν, ὦ ἄνδρες ᾿Αθηναῖοι, - ὅταν τ᾿ εἰς τὰ πράγματα ἀποβλέψω καὶ ὅταν πρὸς τοὺς - λόγους οὓς ἀκούω· τοὺς μὲν γὰρ λόγους περὶ τοῦ - τιμωρήσασθαι Φίλιππον ὁρῶ γιγνομένους, τὰ δὲ πράγματ᾿ - εἰς τοῦτο προήκοντα, ὥσθ᾿ ὅπως μὴ πεισόμεθ᾿ αὐτοὶ - πρότερον κακῶς σκέψασθαι δέον. οὐδέν οὖν ἄλλο μοι δοκοῦσιν - οἱ τὰ τοιαῦτα λέγοντες ἢ τὴν ὑπόθεσιν, περὶ ἧς βουλεύεσθαι, - οὐχὶ τὴν οὖσαν παριστάντες ὑμῖν ἁμαρτάνειν. ἐγὼ δέ, ὅτι μέν - ποτ᾿ ἐξῆν τῇ πόλει καὶ τὰ αὑτῆς ἔχειν ἀσφαλῶς καὶ Φίλιππον - τιμωρήσασθαι, καὶ μάλ᾿ ἀκριβῶς οἶδα· ἐπ᾿ ἐμοῦ γάρ, οὐ πάλαι - γέγονεν ταῦτ᾿ ἀμφότερα· νῦν μέντοι πέπεισμαι τοῦθ᾿ ἱκανὸν - προλαβεῖν ἡμῖν εἶναι τὴν πρώτην, ὅπως τοὺς συμμάχους - σώσομεν. ἐὰν γὰρ τοῦτο βεβαίως ὑπάρξῃ, τότε καὶ περὶ τοῦ - τίνα τιμωρήσεταί τις καὶ ὃν τρόπον ἐξέσται σκοπεῖν· πρὶν δὲ - τὴν ἀρχὴν ὀρθῶς ὑποθέσθαι, μάταιον ἡγοῦμαι περὶ τῆς - τελευτῆς ὁντινοῦν ποιεῖσθαι λόγον. - - Δημοσθένους, Γ´ ᾿Ολυνθιακὸς - -Georgian: - - From a Unicode conference invitation: - - გთხოვთ ახლავე გაიაროთ რეგისტრაცია Unicode-ის მეათე საერთაშორისო - კონფერენციაზე დასასწრებად, რომელიც გაიმართება 10-12 მარტს, - ქ. მაინცში, გერმანიაში. კონფერენცია შეჰკრებს ერთად მსოფლიოს - ექსპერტებს ისეთ დარგებში როგორიცაა ინტერნეტი და Unicode-ი, - ინტერნაციონალიზაცია და ლოკალიზაცია, Unicode-ის გამოყენება - ოპერაციულ სისტემებსა, და გამოყენებით პროგრამებში, შრიფტებში, - ტექსტების დამუშავებასა და მრავალენოვან კომპიუტერულ სისტემებში. - -Russian: - - From a Unicode conference invitation: - - Зарегистрируйтесь сейчас на Десятую Международную Конференцию по - Unicode, которая состоится 10-12 марта 1997 года в Майнце в Германии. - Конференция соберет широкий круг экспертов по вопросам глобального - Интернета и Unicode, локализации и интернационализации, воплощению и - применению Unicode в различных операционных системах и программных - приложениях, шрифтах, верстке и многоязычных компьютерных системах. - -Thai (UCS Level 2): - - Excerpt from a poetry on The Romance of The Three Kingdoms (a Chinese - classic 'San Gua'): - - [----------------------------|------------------------] - ๏ แผ่นดินฮั่นเสื่อมโทรมแสนสังเวช พระปกเกศกองบู๊กู้ขึ้นใหม่ - สิบสองกษัตริย์ก่อนหน้าแลถัดไป สององค์ไซร้โง่เขลาเบาปัญญา - ทรงนับถือขันทีเป็นที่พึ่ง บ้านเมืองจึงวิปริตเป็นนักหนา - โฮจิ๋นเรียกทัพทั่วหัวเมืองมา หมายจะฆ่ามดชั่วตัวสำคัญ - เหมือนขับไสไล่เสือจากเคหา รับหมาป่าเข้ามาเลยอาสัญ - ฝ่ายอ้องอุ้นยุแยกให้แตกกัน ใช้สาวนั้นเป็นชนวนชื่นชวนใจ - พลันลิฉุยกุยกีกลับก่อเหตุ ช่างอาเพศจริงหนาฟ้าร้องไห้ - ต้องรบราฆ่าฟันจนบรรลัย ฤๅหาใครค้ำชูกู้บรรลังก์ ฯ - - (The above is a two-column text. If combining characters are handled - correctly, the lines of the second column should be aligned with the - | character above.) - -Ethiopian: - - Proverbs in the Amharic language: - - ሰማይ አይታረስ ንጉሥ አይከሰስ። - ብላ ካለኝ እንደአባቴ በቆመጠኝ። - ጌጥ ያለቤቱ ቁምጥና ነው። - ደሀ በሕልሙ ቅቤ ባይጠጣ ንጣት በገደለው። - የአፍ ወለምታ በቅቤ አይታሽም። - አይጥ በበላ ዳዋ ተመታ። - ሲተረጉሙ ይደረግሙ። - ቀስ በቀስ፥ ዕንቁላል በእግሩ ይሄዳል። - ድር ቢያብር አንበሳ ያስር። - ሰው እንደቤቱ እንጅ እንደ ጉረቤቱ አይተዳደርም። - እግዜር የከፈተውን ጉሮሮ ሳይዘጋው አይድርም። - የጎረቤት ሌባ፥ ቢያዩት ይስቅ ባያዩት ያጠልቅ። - ሥራ ከመፍታት ልጄን ላፋታት። - ዓባይ ማደሪያ የለው፥ ግንድ ይዞ ይዞራል። - የእስላም አገሩ መካ የአሞራ አገሩ ዋርካ። - ተንጋሎ ቢተፉ ተመልሶ ባፉ። - ወዳጅህ ማር ቢሆን ጨርስህ አትላሰው። - እግርህን በፍራሽህ ልክ ዘርጋ። - -Runes: - - ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ᚩᚾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ ᚹᛁᚦ ᚦᚪ ᚹᛖᛥᚫ - - (Old English, which transcribed into Latin reads 'He cwaeth that he - bude thaem lande northweardum with tha Westsae.' and means 'He said - that he lived in the northern land near the Western Sea.') - -Braille: - - ⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌ - - ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠙⠑⠁⠙⠒ ⠞⠕ ⠃⠑⠛⠔ ⠺⠊⠹⠲ ⡹⠻⠑ ⠊⠎ ⠝⠕ ⠙⠳⠃⠞ - ⠱⠁⠞⠑⠧⠻ ⠁⠃⠳⠞ ⠹⠁⠞⠲ ⡹⠑ ⠗⠑⠛⠊⠌⠻ ⠕⠋ ⠙⠊⠎ ⠃⠥⠗⠊⠁⠇ ⠺⠁⠎ - ⠎⠊⠛⠝⠫ ⠃⠹ ⠹⠑ ⠊⠇⠻⠛⠹⠍⠁⠝⠂ ⠹⠑ ⠊⠇⠻⠅⠂ ⠹⠑ ⠥⠝⠙⠻⠞⠁⠅⠻⠂ - ⠁⠝⠙ ⠹⠑ ⠡⠊⠑⠋ ⠍⠳⠗⠝⠻⠲ ⡎⠊⠗⠕⠕⠛⠑ ⠎⠊⠛⠝⠫ ⠊⠞⠲ ⡁⠝⠙ - ⡎⠊⠗⠕⠕⠛⠑⠰⠎ ⠝⠁⠍⠑ ⠺⠁⠎ ⠛⠕⠕⠙ ⠥⠏⠕⠝ ⠰⡡⠁⠝⠛⠑⠂ ⠋⠕⠗ ⠁⠝⠹⠹⠔⠛ ⠙⠑ - ⠡⠕⠎⠑ ⠞⠕ ⠏⠥⠞ ⠙⠊⠎ ⠙⠁⠝⠙ ⠞⠕⠲ - - ⡕⠇⠙ ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠁⠎ ⠙⠑⠁⠙ ⠁⠎ ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ - - ⡍⠔⠙⠖ ⡊ ⠙⠕⠝⠰⠞ ⠍⠑⠁⠝ ⠞⠕ ⠎⠁⠹ ⠹⠁⠞ ⡊ ⠅⠝⠪⠂ ⠕⠋ ⠍⠹ - ⠪⠝ ⠅⠝⠪⠇⠫⠛⠑⠂ ⠱⠁⠞ ⠹⠻⠑ ⠊⠎ ⠏⠜⠞⠊⠊⠥⠇⠜⠇⠹ ⠙⠑⠁⠙ ⠁⠃⠳⠞ - ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ ⡊ ⠍⠊⠣⠞ ⠙⠁⠧⠑ ⠃⠑⠲ ⠔⠊⠇⠔⠫⠂ ⠍⠹⠎⠑⠇⠋⠂ ⠞⠕ - ⠗⠑⠛⠜⠙ ⠁ ⠊⠕⠋⠋⠔⠤⠝⠁⠊⠇ ⠁⠎ ⠹⠑ ⠙⠑⠁⠙⠑⠌ ⠏⠊⠑⠊⠑ ⠕⠋ ⠊⠗⠕⠝⠍⠕⠝⠛⠻⠹ - ⠔ ⠹⠑ ⠞⠗⠁⠙⠑⠲ ⡃⠥⠞ ⠹⠑ ⠺⠊⠎⠙⠕⠍ ⠕⠋ ⠳⠗ ⠁⠝⠊⠑⠌⠕⠗⠎ - ⠊⠎ ⠔ ⠹⠑ ⠎⠊⠍⠊⠇⠑⠆ ⠁⠝⠙ ⠍⠹ ⠥⠝⠙⠁⠇⠇⠪⠫ ⠙⠁⠝⠙⠎ - ⠩⠁⠇⠇ ⠝⠕⠞ ⠙⠊⠌⠥⠗⠃ ⠊⠞⠂ ⠕⠗ ⠹⠑ ⡊⠳⠝⠞⠗⠹⠰⠎ ⠙⠕⠝⠑ ⠋⠕⠗⠲ ⡹⠳ - ⠺⠊⠇⠇ ⠹⠻⠑⠋⠕⠗⠑ ⠏⠻⠍⠊⠞ ⠍⠑ ⠞⠕ ⠗⠑⠏⠑⠁⠞⠂ ⠑⠍⠏⠙⠁⠞⠊⠊⠁⠇⠇⠹⠂ ⠹⠁⠞ - ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠁⠎ ⠙⠑⠁⠙ ⠁⠎ ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ - - (The first couple of paragraphs of "A Christmas Carol" by Dickens) - -Compact font selection example text: - - ABCDEFGHIJKLMNOPQRSTUVWXYZ /0123456789 - abcdefghijklmnopqrstuvwxyz £©µÀÆÖÞßéöÿ - –—‘“”„†•…‰™œŠŸž€ ΑΒΓΔΩαβγδω АБВГДабвгд - ∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა - -Greetings in various languages: - - Hello world, Καλημέρα κόσμε, コンニチハ - -Box drawing alignment tests: █ - ▉ - ╔══╦══╗ ┌──┬──┐ ╭──┬──╮ ╭──┬──╮ ┏━━┳━━┓ ┎┒┏┑ ╷ ╻ ┏┯┓ ┌┰┐ ▊ ╱╲╱╲╳╳╳ - ║┌─╨─┐║ │╔═╧═╗│ │╒═╪═╕│ │╓─╁─╖│ ┃┌─╂─┐┃ ┗╃╄┙ ╶┼╴╺╋╸┠┼┨ ┝╋┥ ▋ ╲╱╲╱╳╳╳ - ║│╲ ╱│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╿ │┃ ┍╅╆┓ ╵ ╹ ┗┷┛ └┸┘ ▌ ╱╲╱╲╳╳╳ - ╠╡ ╳ ╞╣ ├╢ ╟┤ ├┼─┼─┼┤ ├╫─╂─╫┤ ┣┿╾┼╼┿┫ ┕┛┖┚ ┌┄┄┐ ╎ ┏┅┅┓ ┋ ▍ ╲╱╲╱╳╳╳ - ║│╱ ╲│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╽ │┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▎ - ║└─╥─┘║ │╚═╤═╝│ │╘═╪═╛│ │╙─╀─╜│ ┃└─╂─┘┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▏ - ╚══╩══╝ └──┴──┘ ╰──┴──╯ ╰──┴──╯ ┗━━┻━━┛ └╌╌┘ ╎ ┗╍╍┛ ┋ ▁▂▃▄▅▆▇█ diff --git a/examples/arena.c b/examples/arena.c new file mode 100644 index 0000000..6579849 --- /dev/null +++ b/examples/arena.c @@ -0,0 +1,18 @@ +#include "prb.h" +#include +#include + +i32 main(void) +{ + info("arenas"); + struct arena arena = alloc_arena(10); + u8 *first = push_arena(&arena, 5); + memset(first, 5, 5); + u8 *second = push_arena(&arena, 5); + 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); +} diff --git a/examples/b b/examples/b new file mode 100755 index 0000000..9c29028 --- /dev/null +++ b/examples/b @@ -0,0 +1,53 @@ +#!/bin/sh + +echorun() +{ + echo $1 + $1 +} + +compile() +{ + echorun "$cc $cflags $include $lflags -o $dir/$1 $dir/$1.c $libs" +} + +cur=`pwd` +dir=`realpath $0` +dir=`dirname $dir` + +echorun "cd $dir" + +if [ "$1" = 'clean' ] ; then + echorun "rm -f $dir/macros \ + $dir/macros.exe \ + $dir/arena \ + $dir/arena.exe \ + $dir/sys \ + $dir/sys.exe \ + $dir/string \ + $dir/string.exe" + echorun "cd $cur" + exit +fi + +cc='tcc' +if [ "$1" = 'windows' ] ; then + cc='x86_64-w64-mingw32-gcc' +fi + +prbdir="$dir/.." +if [ ! -e "$prbdir/libprb.a" ] ; then + $prbdir/build.sh +fi + +cflags='-g -Wall -std=c99' +include="-I$prbdir" +lflags="-L$prbdir" +libs='-lprb -lm' + +compile macros +compile arena +compile sys +compile string + +echorun "cd $cur" diff --git a/examples/macros.c b/examples/macros.c new file mode 100644 index 0000000..0e836b0 --- /dev/null +++ b/examples/macros.c @@ -0,0 +1,44 @@ +#include "prb.h" +#include + +struct type { + i32 i32_element; + i16 i16_element; + u8 u8_element; +}; + +#define nodes_count 10 + +struct stack_node { + i32 value; + struct stack_node *next; +}; + +struct stack { + struct stack_node *first; + struct stack_node *last; +}; + +static void print_stack(struct stack s) +{ + for (struct stack_node *node = s.first; node; node = node->next) + printf("%d%s", node->value, ((node->next) ? " -> " : "\n")); +} + +i32 main(void) +{ + u64 offset = offsetof(struct type, u8_element); + assert(offset == 6); + info("offsetof(type_t, u8_element) = %lu", offset); + info("stack:"); + struct stack_node nodes[nodes_count]; + struct stack stack = {0}; + for (i32 i = 0; i < array_count(nodes); ++i) { + struct stack_node *node = nodes + i; + node->value = i; + sllpush(stack.first, stack.last, node); + } + print_stack(stack); + sllpop(stack.first, stack.last); + print_stack(stack); +} diff --git a/examples/string.c b/examples/string.c new file mode 100644 index 0000000..bbda94d --- /dev/null +++ b/examples/string.c @@ -0,0 +1,20 @@ +#include "prb.h" +#include + +i32 main(void) +{ + struct arena arena = alloc_arena(0); + struct string fstr = push_fstring(&arena, "formatted string: %d\n", 69); + print_string(fstr); + info("string list:"); + struct string_list list = {0}; + push_fstring_to_list(&arena, &list, 0, "first %d -> ", 34); + push_fstring_to_list(&arena, &list, 0, "second: %d -> ", 35); + push_fstring_to_list(&arena, &list, 0, "sum: %d\n", 34+35); + push_fstring_to_list(&arena, &list, 1, "%s", "To the front -> "); + print_string_list(&list); + struct string catstr = join_string_list(&arena, &list); + info("Concatenated string list:"); + print_string(catstr); + return 0; +} diff --git a/examples/sys.c b/examples/sys.c new file mode 100644 index 0000000..881d2be --- /dev/null +++ b/examples/sys.c @@ -0,0 +1,30 @@ +#include "prb.h" +#include +#include + +i32 main(int c, char **v) +{ + info("os:"); + switch (os) { + case os_linux: + info("linux"); + break; + case os_windows: + info("windows"); + break; + default: + info("unsupported"); + return 1; + } + char *dir = dirname(*v); + info("bin directory \"%s\"", dir); + const char *filename = "build.sh"; + char path[1024] = {0}; + snprintf(path, 1024, "%s/%s", dir, filename); + struct arena arena = alloc_arena(megabytes(32)); + char *content = 0; + if (!(content = read_entire_file(&arena, 0, path))) + die("failed to read %s", path); + info("%s content:\n%s", path, content); + return 0; +} diff --git a/examples/utf8.txt b/examples/utf8.txt new file mode 100644 index 0000000..3af7875 --- /dev/null +++ b/examples/utf8.txt @@ -0,0 +1,204 @@ +// Original by Markus Kuhn, adapted for HTML by Martin D + +UTF-8 encoded sample plain-text file +‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾‾ + +Markus Kuhn [ˈmaʳkʊs kuːn] mkuhn@acm.org — 1999-08-20 + + +The ASCII compatible UTF-8 encoding of ISO 10646 and Unicode +plain-text files is defined in RFC 2279 and in ISO 10646-1 Annex R. + + +Using Unicode/UTF-8, you can write in emails and source code things such as + +Mathematics and Sciences: + + ∮ E⋅da = Q, n → ∞, ∑ f(i) = ∏ g(i), ∀x∈ℝ: ⌈x⌉ = −⌊−x⌋, α ∧ ¬β = ¬(¬α ∨ β), + + ℕ ⊆ ℕ₀ ⊂ ℤ ⊂ ℚ ⊂ ℝ ⊂ ℂ, ⊥ < a ≠ b ≡ c ≤ d ≪ ⊤ ⇒ (A ⇔ B), + + 2H₂ + O₂ ⇌ 2H₂O, R = 4.7 kΩ, ⌀ 200 mm + +Linguistics and dictionaries: + + ði ıntəˈnæʃənəl fəˈnɛtık əsoʊsiˈeıʃn + Y [ˈʏpsilɔn], Yen [jɛn], Yoga [ˈjoːgɑ] + +APL: + + ((V⍳V)=⍳⍴V)/V←,V ⌷←⍳→⍴∆∇⊃‾⍎⍕⌈ + +Nicer typography in plain text files: + + ╔══════════════════════════════════════════╗ + ║ ║ + ║ • ‘single’ and “double” quotes ║ + ║ ║ + ║ • Curly apostrophes: “We’ve been here” ║ + ║ ║ + ║ • Latin-1 apostrophe and accents: '´` ║ + ║ ║ + ║ • ‚deutsche‘ „Anführungszeichen“ ║ + ║ ║ + ║ • †, ‡, ‰, •, 3–4, —, −5/+5, ™, … ║ + ║ ║ + ║ • ASCII safety test: 1lI|, 0OD, 8B ║ + ║ ╭─────────╮ ║ + ║ • the euro symbol: │ 14.95 € │ ║ + ║ ╰─────────╯ ║ + ╚══════════════════════════════════════════╝ + +Greek (in Polytonic): + + The Greek anthem: + + Σὲ γνωρίζω ἀπὸ τὴν κόψη + τοῦ σπαθιοῦ τὴν τρομερή, + σὲ γνωρίζω ἀπὸ τὴν ὄψη + ποὺ μὲ βία μετράει τὴ γῆ. + + ᾿Απ᾿ τὰ κόκκαλα βγαλμένη + τῶν ῾Ελλήνων τὰ ἱερά + καὶ σὰν πρῶτα ἀνδρειωμένη + χαῖρε, ὦ χαῖρε, ᾿Ελευθεριά! + + From a speech of Demosthenes in the 4th century BC: + + Οὐχὶ ταὐτὰ παρίσταταί μοι γιγνώσκειν, ὦ ἄνδρες ᾿Αθηναῖοι, + ὅταν τ᾿ εἰς τὰ πράγματα ἀποβλέψω καὶ ὅταν πρὸς τοὺς + λόγους οὓς ἀκούω· τοὺς μὲν γὰρ λόγους περὶ τοῦ + τιμωρήσασθαι Φίλιππον ὁρῶ γιγνομένους, τὰ δὲ πράγματ᾿ + εἰς τοῦτο προήκοντα, ὥσθ᾿ ὅπως μὴ πεισόμεθ᾿ αὐτοὶ + πρότερον κακῶς σκέψασθαι δέον. οὐδέν οὖν ἄλλο μοι δοκοῦσιν + οἱ τὰ τοιαῦτα λέγοντες ἢ τὴν ὑπόθεσιν, περὶ ἧς βουλεύεσθαι, + οὐχὶ τὴν οὖσαν παριστάντες ὑμῖν ἁμαρτάνειν. ἐγὼ δέ, ὅτι μέν + ποτ᾿ ἐξῆν τῇ πόλει καὶ τὰ αὑτῆς ἔχειν ἀσφαλῶς καὶ Φίλιππον + τιμωρήσασθαι, καὶ μάλ᾿ ἀκριβῶς οἶδα· ἐπ᾿ ἐμοῦ γάρ, οὐ πάλαι + γέγονεν ταῦτ᾿ ἀμφότερα· νῦν μέντοι πέπεισμαι τοῦθ᾿ ἱκανὸν + προλαβεῖν ἡμῖν εἶναι τὴν πρώτην, ὅπως τοὺς συμμάχους + σώσομεν. ἐὰν γὰρ τοῦτο βεβαίως ὑπάρξῃ, τότε καὶ περὶ τοῦ + τίνα τιμωρήσεταί τις καὶ ὃν τρόπον ἐξέσται σκοπεῖν· πρὶν δὲ + τὴν ἀρχὴν ὀρθῶς ὑποθέσθαι, μάταιον ἡγοῦμαι περὶ τῆς + τελευτῆς ὁντινοῦν ποιεῖσθαι λόγον. + + Δημοσθένους, Γ´ ᾿Ολυνθιακὸς + +Georgian: + + From a Unicode conference invitation: + + გთხოვთ ახლავე გაიაროთ რეგისტრაცია Unicode-ის მეათე საერთაშორისო + კონფერენციაზე დასასწრებად, რომელიც გაიმართება 10-12 მარტს, + ქ. მაინცში, გერმანიაში. კონფერენცია შეჰკრებს ერთად მსოფლიოს + ექსპერტებს ისეთ დარგებში როგორიცაა ინტერნეტი და Unicode-ი, + ინტერნაციონალიზაცია და ლოკალიზაცია, Unicode-ის გამოყენება + ოპერაციულ სისტემებსა, და გამოყენებით პროგრამებში, შრიფტებში, + ტექსტების დამუშავებასა და მრავალენოვან კომპიუტერულ სისტემებში. + +Russian: + + From a Unicode conference invitation: + + Зарегистрируйтесь сейчас на Десятую Международную Конференцию по + Unicode, которая состоится 10-12 марта 1997 года в Майнце в Германии. + Конференция соберет широкий круг экспертов по вопросам глобального + Интернета и Unicode, локализации и интернационализации, воплощению и + применению Unicode в различных операционных системах и программных + приложениях, шрифтах, верстке и многоязычных компьютерных системах. + +Thai (UCS Level 2): + + Excerpt from a poetry on The Romance of The Three Kingdoms (a Chinese + classic 'San Gua'): + + [----------------------------|------------------------] + ๏ แผ่นดินฮั่นเสื่อมโทรมแสนสังเวช พระปกเกศกองบู๊กู้ขึ้นใหม่ + สิบสองกษัตริย์ก่อนหน้าแลถัดไป สององค์ไซร้โง่เขลาเบาปัญญา + ทรงนับถือขันทีเป็นที่พึ่ง บ้านเมืองจึงวิปริตเป็นนักหนา + โฮจิ๋นเรียกทัพทั่วหัวเมืองมา หมายจะฆ่ามดชั่วตัวสำคัญ + เหมือนขับไสไล่เสือจากเคหา รับหมาป่าเข้ามาเลยอาสัญ + ฝ่ายอ้องอุ้นยุแยกให้แตกกัน ใช้สาวนั้นเป็นชนวนชื่นชวนใจ + พลันลิฉุยกุยกีกลับก่อเหตุ ช่างอาเพศจริงหนาฟ้าร้องไห้ + ต้องรบราฆ่าฟันจนบรรลัย ฤๅหาใครค้ำชูกู้บรรลังก์ ฯ + + (The above is a two-column text. If combining characters are handled + correctly, the lines of the second column should be aligned with the + | character above.) + +Ethiopian: + + Proverbs in the Amharic language: + + ሰማይ አይታረስ ንጉሥ አይከሰስ። + ብላ ካለኝ እንደአባቴ በቆመጠኝ። + ጌጥ ያለቤቱ ቁምጥና ነው። + ደሀ በሕልሙ ቅቤ ባይጠጣ ንጣት በገደለው። + የአፍ ወለምታ በቅቤ አይታሽም። + አይጥ በበላ ዳዋ ተመታ። + ሲተረጉሙ ይደረግሙ። + ቀስ በቀስ፥ ዕንቁላል በእግሩ ይሄዳል። + ድር ቢያብር አንበሳ ያስር። + ሰው እንደቤቱ እንጅ እንደ ጉረቤቱ አይተዳደርም። + እግዜር የከፈተውን ጉሮሮ ሳይዘጋው አይድርም። + የጎረቤት ሌባ፥ ቢያዩት ይስቅ ባያዩት ያጠልቅ። + ሥራ ከመፍታት ልጄን ላፋታት። + ዓባይ ማደሪያ የለው፥ ግንድ ይዞ ይዞራል። + የእስላም አገሩ መካ የአሞራ አገሩ ዋርካ። + ተንጋሎ ቢተፉ ተመልሶ ባፉ። + ወዳጅህ ማር ቢሆን ጨርስህ አትላሰው። + እግርህን በፍራሽህ ልክ ዘርጋ። + +Runes: + + ᚻᛖ ᚳᚹᚫᚦ ᚦᚫᛏ ᚻᛖ ᛒᚢᛞᛖ ᚩᚾ ᚦᚫᛗ ᛚᚪᚾᛞᛖ ᚾᚩᚱᚦᚹᛖᚪᚱᛞᚢᛗ ᚹᛁᚦ ᚦᚪ ᚹᛖᛥᚫ + + (Old English, which transcribed into Latin reads 'He cwaeth that he + bude thaem lande northweardum with tha Westsae.' and means 'He said + that he lived in the northern land near the Western Sea.') + +Braille: + + ⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌ + + ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠙⠑⠁⠙⠒ ⠞⠕ ⠃⠑⠛⠔ ⠺⠊⠹⠲ ⡹⠻⠑ ⠊⠎ ⠝⠕ ⠙⠳⠃⠞ + ⠱⠁⠞⠑⠧⠻ ⠁⠃⠳⠞ ⠹⠁⠞⠲ ⡹⠑ ⠗⠑⠛⠊⠌⠻ ⠕⠋ ⠙⠊⠎ ⠃⠥⠗⠊⠁⠇ ⠺⠁⠎ + ⠎⠊⠛⠝⠫ ⠃⠹ ⠹⠑ ⠊⠇⠻⠛⠹⠍⠁⠝⠂ ⠹⠑ ⠊⠇⠻⠅⠂ ⠹⠑ ⠥⠝⠙⠻⠞⠁⠅⠻⠂ + ⠁⠝⠙ ⠹⠑ ⠡⠊⠑⠋ ⠍⠳⠗⠝⠻⠲ ⡎⠊⠗⠕⠕⠛⠑ ⠎⠊⠛⠝⠫ ⠊⠞⠲ ⡁⠝⠙ + ⡎⠊⠗⠕⠕⠛⠑⠰⠎ ⠝⠁⠍⠑ ⠺⠁⠎ ⠛⠕⠕⠙ ⠥⠏⠕⠝ ⠰⡡⠁⠝⠛⠑⠂ ⠋⠕⠗ ⠁⠝⠹⠹⠔⠛ ⠙⠑ + ⠡⠕⠎⠑ ⠞⠕ ⠏⠥⠞ ⠙⠊⠎ ⠙⠁⠝⠙ ⠞⠕⠲ + + ⡕⠇⠙ ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠁⠎ ⠙⠑⠁⠙ ⠁⠎ ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ + + ⡍⠔⠙⠖ ⡊ ⠙⠕⠝⠰⠞ ⠍⠑⠁⠝ ⠞⠕ ⠎⠁⠹ ⠹⠁⠞ ⡊ ⠅⠝⠪⠂ ⠕⠋ ⠍⠹ + ⠪⠝ ⠅⠝⠪⠇⠫⠛⠑⠂ ⠱⠁⠞ ⠹⠻⠑ ⠊⠎ ⠏⠜⠞⠊⠊⠥⠇⠜⠇⠹ ⠙⠑⠁⠙ ⠁⠃⠳⠞ + ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ ⡊ ⠍⠊⠣⠞ ⠙⠁⠧⠑ ⠃⠑⠲ ⠔⠊⠇⠔⠫⠂ ⠍⠹⠎⠑⠇⠋⠂ ⠞⠕ + ⠗⠑⠛⠜⠙ ⠁ ⠊⠕⠋⠋⠔⠤⠝⠁⠊⠇ ⠁⠎ ⠹⠑ ⠙⠑⠁⠙⠑⠌ ⠏⠊⠑⠊⠑ ⠕⠋ ⠊⠗⠕⠝⠍⠕⠝⠛⠻⠹ + ⠔ ⠹⠑ ⠞⠗⠁⠙⠑⠲ ⡃⠥⠞ ⠹⠑ ⠺⠊⠎⠙⠕⠍ ⠕⠋ ⠳⠗ ⠁⠝⠊⠑⠌⠕⠗⠎ + ⠊⠎ ⠔ ⠹⠑ ⠎⠊⠍⠊⠇⠑⠆ ⠁⠝⠙ ⠍⠹ ⠥⠝⠙⠁⠇⠇⠪⠫ ⠙⠁⠝⠙⠎ + ⠩⠁⠇⠇ ⠝⠕⠞ ⠙⠊⠌⠥⠗⠃ ⠊⠞⠂ ⠕⠗ ⠹⠑ ⡊⠳⠝⠞⠗⠹⠰⠎ ⠙⠕⠝⠑ ⠋⠕⠗⠲ ⡹⠳ + ⠺⠊⠇⠇ ⠹⠻⠑⠋⠕⠗⠑ ⠏⠻⠍⠊⠞ ⠍⠑ ⠞⠕ ⠗⠑⠏⠑⠁⠞⠂ ⠑⠍⠏⠙⠁⠞⠊⠊⠁⠇⠇⠹⠂ ⠹⠁⠞ + ⡍⠜⠇⠑⠹ ⠺⠁⠎ ⠁⠎ ⠙⠑⠁⠙ ⠁⠎ ⠁ ⠙⠕⠕⠗⠤⠝⠁⠊⠇⠲ + + (The first couple of paragraphs of "A Christmas Carol" by Dickens) + +Compact font selection example text: + + ABCDEFGHIJKLMNOPQRSTUVWXYZ /0123456789 + abcdefghijklmnopqrstuvwxyz £©µÀÆÖÞßéöÿ + –—‘“”„†•…‰™œŠŸž€ ΑΒΓΔΩαβγδω АБВГДабвгд + ∀∂∈ℝ∧∪≡∞ ↑↗↨↻⇣ ┐┼╔╘░►☺♀ fi�⑀₂ἠḂӥẄɐː⍎אԱა + +Greetings in various languages: + + Hello world, Καλημέρα κόσμε, コンニチハ + +Box drawing alignment tests: █ + ▉ + ╔══╦══╗ ┌──┬──┐ ╭──┬──╮ ╭──┬──╮ ┏━━┳━━┓ ┎┒┏┑ ╷ ╻ ┏┯┓ ┌┰┐ ▊ ╱╲╱╲╳╳╳ + ║┌─╨─┐║ │╔═╧═╗│ │╒═╪═╕│ │╓─╁─╖│ ┃┌─╂─┐┃ ┗╃╄┙ ╶┼╴╺╋╸┠┼┨ ┝╋┥ ▋ ╲╱╲╱╳╳╳ + ║│╲ ╱│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╿ │┃ ┍╅╆┓ ╵ ╹ ┗┷┛ └┸┘ ▌ ╱╲╱╲╳╳╳ + ╠╡ ╳ ╞╣ ├╢ ╟┤ ├┼─┼─┼┤ ├╫─╂─╫┤ ┣┿╾┼╼┿┫ ┕┛┖┚ ┌┄┄┐ ╎ ┏┅┅┓ ┋ ▍ ╲╱╲╱╳╳╳ + ║│╱ ╲│║ │║ ║│ ││ │ ││ │║ ┃ ║│ ┃│ ╽ │┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▎ + ║└─╥─┘║ │╚═╤═╝│ │╘═╪═╛│ │╙─╀─╜│ ┃└─╂─┘┃ ░░▒▒▓▓██ ┊ ┆ ╎ ╏ ┇ ┋ ▏ + ╚══╩══╝ └──┴──┘ ╰──┴──╯ ╰──┴──╯ ┗━━┻━━┛ └╌╌┘ ╎ ┗╍╍┛ ┋ ▁▂▃▄▅▆▇█ diff --git a/linux.c b/linux.c index ff20e92..cfdcf6a 100644 --- a/linux.c +++ b/linux.c @@ -1,70 +1,91 @@ #include "sys.h" #include "macros.h" +#include "prbs.h" + #include #include #include #include #include -void *sys_alloc(u64 length) +void +*sys_alloc(u64 length) { - assert(length); - i32 prot = PROT_READ | PROT_WRITE; - i32 flags = MAP_PRIVATE|MAP_ANONYMOUS; - void *result = mmap(0, length, prot, flags, -1, 0); - return result; + assert(length); + i32 prot = PROT_READ | PROT_WRITE; + i32 flags = MAP_PRIVATE|MAP_ANONYMOUS; + void *result = mmap(0, length, prot, flags, -1, 0); + return result; } -void sys_free(void *memory, u64 length) +void +sys_free(void *memory, u64 length) { - assert(memory); - assert(length); - munmap(memory, length); + assert(memory); + assert(length); + munmap(memory, length); } -void die(const char *format, ...) +void +die(const char *format, ...) { - va_list args; - va_start(args, format); - fprintf(stderr, "error: "); - vfprintf(stderr, format, args); - fprintf(stderr, "\n"); - va_end(args); - _exit(1); + va_list args; + va_start(args, format); + fprintf(stderr, "error: "); + vfprintf(stderr, format, args); + fprintf(stderr, "\n"); + va_end(args); + _exit(1); } -void info(const char *format, ...) +void +info(const char *format, ...) { - va_list args; - va_start(args, format); - vprintf(format, args); - putchar('\n'); - va_end(args); + va_list args; + va_start(args, format); + vprintf(format, args); + putchar('\n'); + va_end(args); } -char *read_entire_file(struct arena *arena, u64 *len, const char *filename) +char * +read_entire_file(struct arena *arena, u64 *len, const char *filename) { - FILE *file = fopen(filename, "rb"); - if (!file) - return 0; - if (fseek(file, 0, SEEK_END) == -1) - goto error; - i64 n = ftell(file); - if (n <= 0) - goto error; - if (len) - *len = n; - if (fseek(file, 0, SEEK_SET) == -1) - goto error; - char *buffer = push_arena(arena, n + 1); - if (!fread(buffer, 1, n, file)) { - pop_arena(arena, n + 1); - goto error; - } - fclose(file); - buffer[n] = 0; - return buffer; + FILE *file = fopen(filename, "rb"); + if (!file) + return 0; + if (fseek(file, 0, SEEK_END) == -1) + goto error; + i64 n = ftell(file); + if (n <= 0) + goto error; + if (len) + *len = n; + if (fseek(file, 0, SEEK_SET) == -1) + goto error; + char *buffer = push_arena(arena, n + 1); + if (!fread(buffer, 1, n, file)) { + pop_arena(arena, n + 1); + goto error; + } + fclose(file); + buffer[n] = 0; + return buffer; + error: - fclose(file); - return 0; + fclose(file); + return 0; +} + +struct string +get_basedir(struct arena *arena) +{ + char buf[1024] = {0}; + if (readlink("/proc/self/exe", buf, 1024 - 1) == -1) + die("failed to read link /proc/self/exe"); + struct arena temp = alloc_arena(0); + struct string full = string_from_cstring(&temp, buf); + struct string base = push_string(arena, get_dirname(full)); + release_arena(&temp); + return base; } diff --git a/macros.h b/macros.h index eb514b5..2fa04a3 100644 --- a/macros.h +++ b/macros.h @@ -1,22 +1,25 @@ #ifndef macros_h #define macros_h +/* #undef assert -#define assert(expr) if (!(expr)) { *(int *)0 = 0; } +#define assert(expr) if (!(expr)) { *(int *)0 = 0; } +*/ +#include -#define array_count(array) (sizeof(array)/sizeof(*(array))) +#define array_count(array) (sizeof(array)/sizeof(*(array))) #undef offsetof -#define offsetof(type, element) ((u64)(&(((type *)0)->element))) +#define offsetof(type, element) ((u64)(&(((type *)0)->element))) -#define min(a, b) (((a) < (b)) ? (a) : (b)) -#define max(a, b) (((a) > (b)) ? (a) : (b)) +#define min(a, b) (((a) < (b)) ? (a) : (b)) +#define max(a, b) (((a) > (b)) ? (a) : (b)) #define clamp(a, x, b) \ - (((x) < (a)) ? (a) : \ - (((x) > (b)) ? (b) : (x))) + (((x) < (a)) ? (a) : \ + (((x) > (b)) ? (b) : (x))) -#define swap(type, a, b) { type tmp = (a); (a) = (b); (b) = tmp; } +#define swap(type, a, b) { type tmp = (a); (a) = (b); (b) = tmp; } #define kilobytes(n) n*1024 #define megabytes(n) kilobytes(n)*1024 @@ -24,27 +27,27 @@ /* NOTE(pryazha): Singly linked list */ #define sllpush(first, last, node) \ - ((first) == 0 ? \ - ((first) = (last) = (node), (node)->next = 0) : \ - ((last)->next = (node), (last) = (node), (node)->next = 0)) + ((first) == 0 ? \ + ((first) = (last) = (node), (node)->next = 0) : \ + ((last)->next = (node), (last) = (node), (node)->next = 0)) #define sllpop(first, last) \ - ((first) == (last) ? \ - ((first) = (last) = 0) : \ - ((first) = (first)->next)) + ((first) == (last) ? \ + ((first) = (last) = 0) : \ + ((first) = (first)->next)) /* NOTE(pryazha): Doubly linked list */ #define dllpush(first, last, node, next, prev) \ - ((first) == 0 ? \ - ((first) = (last) = (node), (node)->next = (node)->prev = 0) : \ - ((node)->prev = (last), (last)->next = (node), (last) = (node), (node)->next = 0)) -#define dllpushback(first, last, node) dllpush(first, last, node, next, prev) -#define dllpushfront(first, last, node) dllpush(last, first, node, prev, next) + ((first) == 0 ? \ + ((first) = (last) = (node), (node)->next = (node)->prev = 0) : \ + ((node)->prev = (last), (last)->next = (node), (last) = (node), (node)->next = 0)) +#define dllpushback(first, last, node) dllpush(first, last, node, next, prev) +#define dllpushfront(first, last, node) dllpush(last, first, node, prev, next) #define dllremove(first, last, node) \ - ((first) == (node) ? \ - ((first) == (last) ? (first) = (last) = 0 : \ - ((first) = (first)->next, (first)->prev = 0)) : \ - ((last) == (node) ? ((last) = (last)->prev, (last)->next = 0) : \ - ((node)->next->prev = (node)->prev, (node)->prev->next = (node)->next))) + ((first) == (node) ? \ + ((first) == (last) ? (first) = (last) = 0 : \ + ((first) = (first)->next, (first)->prev = 0)) : \ + ((last) == (node) ? ((last) = (last)->prev, (last)->next = 0) : \ + ((node)->next->prev = (node)->prev, (node)->prev->next = (node)->next))) #endif diff --git a/prb.h b/prb.h index 829f294..cbf4c52 100644 --- a/prb.h +++ b/prb.h @@ -8,15 +8,15 @@ #include "prbm.h" #include "prbs.h" -#define os_none 0 +#define os_none 0 #define os_windows 1 #define os_linux 2 #if defined(__linux) -# define os os_linux +# define os os_linux #elif defined(__WIN64) -# define os os_windows -# include +# define os os_windows +# include #endif #endif diff --git a/prbm.c b/prbm.c index ae1ac97..3606569 100644 --- a/prbm.c +++ b/prbm.c @@ -1,297 +1,345 @@ #include "prbm.h" +#include "macros.h" #include -struct v2 fillv2(f32 a) +v2 +fillv2(f32 a) { - return (struct v2){a, a}; + return (v2){a, a}; } -struct v2 invv2(struct v2 a) +v2 +invv2(v2 a) { - return (struct v2){-a.x, -a.y}; + return (v2){-a.x, -a.y}; } -struct v2 addv2(struct v2 a, struct v2 b) +v2 +addv2(v2 a, v2 b) { - return (struct v2){a.x + b.x, a.y + b.y}; + return (v2){a.x + b.x, a.y + b.y}; } -struct v2 subv2(struct v2 a, struct v2 b) +v2 +subv2(v2 a, v2 b) { - return (struct v2){a.x - b.x, a.y - b.y}; + return (v2){a.x - b.x, a.y - b.y}; } -struct v2 scalev2(struct v2 a, f32 s) +v2 +scalev2(v2 a, f32 s) { - return (struct v2){a.x * s, a.y * s}; + return (v2){a.x * s, a.y * s}; } -struct v2 scalevv2(struct v2 a, struct v2 s) +v2 +scalevv2(v2 a, v2 s) { - return (struct v2){a.x * s.x, a.y * s.y}; + return (v2){a.x * s.x, a.y * s.y}; } -f32 dotv2(struct v2 a, struct v2 b) +f32 +dotv2(v2 a, v2 b) { - return (a.x * b.x + a.y * b.y); + return (a.x * b.x + a.y * b.y); } -f32 len2v2(struct v2 a) +f32 +len2v2(v2 a) { - return dotv2(a, a); + return dotv2(a, a); } -f32 lenv2(struct v2 a) +f32 +lenv2(v2 a) { - return sqrtf(len2v2(a)); + return sqrtf(len2v2(a)); } -struct v2 normv2(struct v2 a) +v2 +normv2(v2 a) { - f32 len = lenv2(a); - if (len) { - return v2_zero; - } - f32 ilen = 1.0f / len; - return (struct v2){a.x * len, a.y * len}; + f32 len = lenv2(a); + if (len) { + return v2_zero; + } + f32 ilen = 1.0f / len; + return (v2){a.x * len, a.y * len}; } -struct v3 fillv3(f32 a) +v3 +fillv3(f32 a) { - return (struct v3){a, a, a}; + return (v3){a, a, a}; } -struct v3 invv3(struct v3 a) +v3 +invv3(v3 a) { - return (struct v3){-a.x, -a.y, -a.z}; + return (v3){-a.x, -a.y, -a.z}; } -struct v3 addv3(struct v3 a, struct v3 b) +v3 +addv3(v3 a, v3 b) { - return (struct v3){a.x + b.x, a.y + b.y, a.z + b.z}; + return (v3){a.x + b.x, a.y + b.y, a.z + b.z}; } -struct v3 subv3(struct v3 a, struct v3 b) +v3 +subv3(v3 a, v3 b) { - return (struct v3){a.x - b.x, a.y - b.y, a.z - b.z}; + return (v3){a.x - b.x, a.y - b.y, a.z - b.z}; } -struct v3 scalev3(struct v3 a, f32 s) +v3 +scalev3(v3 a, f32 s) { - return (struct v3){a.x * s, a.y * s, a.z * s}; + return (v3){a.x * s, a.y * s, a.z * s}; } -struct v3 scalevv3(struct v3 a, struct v3 s) +v3 +scalevv3(v3 a, v3 s) { - return (struct v3){a.x * s.x, a.y * s.y, a.z * s.z}; + return (v3){a.x * s.x, a.y * s.y, a.z * s.z}; } -f32 dotv3(struct v3 a, struct v3 b) +f32 +dotv3(v3 a, v3 b) { - return (a.x * b.x + a.y * b.y + a.z * b.z); + return (a.x * b.x + a.y * b.y + a.z * b.z); } -struct v3 crossv3(struct v3 l, struct v3 r) +v3 +crossv3(v3 l, v3 r) { - return (struct v3){ - (l.y * r.z - r.y * l.z), - (r.x * l.z - l.x * r.z), - (l.x * r.y - r.x * l.y) - }; + return (v3){ + (l.y * r.z - r.y * l.z), + (r.x * l.z - l.x * r.z), + (l.x * r.y - r.x * l.y) + }; } -f32 len2v3(struct v3 a) +f32 +len2v3(v3 a) { - return dotv3(a, a); + return dotv3(a, a); } -f32 lenv3(struct v3 a) +f32 +lenv3(v3 a) { - return sqrtf(len2v3(a)); + return sqrtf(len2v3(a)); } -struct v3 normv3(struct v3 a) +v3 +normv3(v3 a) { - f32 len = lenv3(a); - if (!len) - return v3_zero; - f32 ilen = 1.0f / len; - return (struct v3){a.x * len, a.y * len, a.z * len}; + f32 len = lenv3(a); + if (!len) + return v3_zero; + f32 ilen = 1.0f / len; + return (v3){a.x * len, a.y * len, a.z * len}; } -struct v4 fillv4(f32 a) +v3 +maxv3(v3 a, v3 b) { - return (struct v4){a, a, a, a}; + return (v3){max(a.x, b.x), max(a.y, b.y), max(a.z, b.z)}; } -struct v4 invv4(struct v4 a) +v4 +fillv4(f32 a) { - return (struct v4){-a.x, -a.y, -a.z, -a.w}; + return (v4){a, a, a, a}; } -struct v4 addv4(struct v4 a, struct v4 b) +v4 +invv4(v4 a) { - return (struct v4){a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; + return (v4){-a.x, -a.y, -a.z, -a.w}; } -struct v4 subv4(struct v4 a, struct v4 b) +v4 +addv4(v4 a, v4 b) { - return (struct v4){a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; + return (v4){a.x + b.x, a.y + b.y, a.z + b.z, a.w + b.w}; } -struct v4 scalev4(struct v4 a, f32 s) +v4 +subv4(v4 a, v4 b) { - return (struct v4){a.x * s, a.y * s, a.z * s, a.w * s}; + return (v4){a.x - b.x, a.y - b.y, a.z - b.z, a.w - b.w}; } -struct v4 scalevv4(struct v4 a, struct v4 s) +v4 +scalev4(v4 a, f32 s) { - return (struct v4){a.x * s.x, a.y * s.y, a.z * s.z, a.w * s.w}; + return (v4){a.x * s, a.y * s, a.z * s, a.w * s}; } -f32 dotv4(struct v4 a, struct v4 b) +v4 +scalevv4(v4 a, v4 s) { - return (a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w); + return (v4){a.x * s.x, a.y * s.y, a.z * s.z, a.w * s.w}; } -f32 len2v4(struct v4 a) +f32 +dotv4(v4 a, v4 b) { - return dotv4(a, a); + return (a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w); } -f32 lenv4(struct v4 a) +f32 +len2v4(v4 a) { - return sqrtf(len2v4(a)); + return dotv4(a, a); } -struct v4 normv4(struct v4 a) +f32 +lenv4(v4 a) { - f32 len = lenv4(a); - if (!len) - return v4_zero; - f32 ilen = 1.0f / len; - return (struct v4){a.x * ilen, a.y * ilen, a.z * ilen, a.w * ilen}; + return sqrtf(len2v4(a)); } -struct mat transpmat(struct mat m) +v4 +normv4(v4 a) { - struct mat r = m; - r.c0.y = m.c1.x; - r.c0.z = m.c2.x; - r.c0.w = m.c3.x; - r.c1.z = m.c2.y; - r.c1.w = m.c3.y; - r.c2.w = m.c3.z; - return r; + f32 len = lenv4(a); + if (!len) + return v4_zero; + f32 ilen = 1.0f / len; + return (v4){a.x * ilen, a.y * ilen, a.z * ilen, a.w * ilen}; } -struct mat mulmat(struct mat left, struct mat right) +mat +transpose_mat(mat m) { - f32 l00 = left.c0.x, l01 = left.c0.y, l02 = left.c0.z, l03 = left.c0.w; - f32 l10 = left.c1.x, l11 = left.c1.y, l12 = left.c1.z, l13 = left.c1.w; - f32 l20 = left.c2.x, l21 = left.c2.y, l22 = left.c2.z, l23 = left.c2.w; - f32 l30 = left.c3.x, l31 = left.c3.y, l32 = left.c3.z, l33 = left.c3.w; - f32 r00 = right.c0.x, r01 = right.c0.y, r02 = right.c0.z, r03 = right.c0.w; - f32 r10 = right.c1.x, r11 = right.c1.y, r12 = right.c1.z, r13 = right.c1.w; - f32 r20 = right.c2.x, r21 = right.c2.y, r22 = right.c2.z, r23 = right.c2.w; - f32 r30 = right.c3.x, r31 = right.c3.y, r32 = right.c3.z, r33 = right.c3.w; - return (struct mat){ - { - l00*r00 + l10*r01 + l20*r02 + l30*r03, - l01*r00 + l11*r01 + l21*r02 + l31*r03, - l02*r00 + l12*r01 + l22*r02 + l32*r03, - l03*r00 + l13*r01 + l23*r02 + l33*r03 - }, - { - l00*r10 + l10*r11 + l20*r12 + l30*r13, - l01*r10 + l11*r11 + l21*r12 + l31*r13, - l02*r10 + l12*r11 + l22*r12 + l32*r13, - l03*r10 + l13*r11 + l23*r12 + l33*r13 - }, - { - l00*r20 + l10*r21 + l20*r22 + l30*r23, - l01*r20 + l11*r21 + l21*r22 + l31*r23, - l02*r20 + l12*r21 + l22*r22 + l32*r23, - l03*r20 + l13*r21 + l23*r22 + l33*r23 - }, - { - l00*r30 + l10*r31 + l20*r32 + l30*r33, - l01*r30 + l11*r31 + l21*r32 + l31*r33, - l02*r30 + l12*r31 + l22*r32 + l32*r33, - l03*r30 + l13*r31 + l23*r32 + l33*r33 - } - }; + mat r = m; + r.c0.y = m.c1.x; + r.c0.z = m.c2.x; + r.c0.w = m.c3.x; + r.c1.z = m.c2.y; + r.c1.w = m.c3.y; + r.c2.w = m.c3.z; + return r; } -struct mat make_translate(struct v3 v) +mat +mulmat(mat left, mat right) { - return (struct mat){ - {1.0f, 0.0f, 0.0f, 0.0f}, - {0.0f, 1.0f, 0.0f, 0.0f}, - {0.0f, 0.0f, 1.0f, 0.0f}, - {v.x, v.y, v.z, 1.0f} - }; + f32 l00 = left.c0.x, l01 = left.c0.y, l02 = left.c0.z, l03 = left.c0.w; + f32 l10 = left.c1.x, l11 = left.c1.y, l12 = left.c1.z, l13 = left.c1.w; + f32 l20 = left.c2.x, l21 = left.c2.y, l22 = left.c2.z, l23 = left.c2.w; + f32 l30 = left.c3.x, l31 = left.c3.y, l32 = left.c3.z, l33 = left.c3.w; + f32 r00 = right.c0.x, r01 = right.c0.y, r02 = right.c0.z, r03 = right.c0.w; + f32 r10 = right.c1.x, r11 = right.c1.y, r12 = right.c1.z, r13 = right.c1.w; + f32 r20 = right.c2.x, r21 = right.c2.y, r22 = right.c2.z, r23 = right.c2.w; + f32 r30 = right.c3.x, r31 = right.c3.y, r32 = right.c3.z, r33 = right.c3.w; + return (mat){ + { + l00*r00 + l10*r01 + l20*r02 + l30*r03, + l01*r00 + l11*r01 + l21*r02 + l31*r03, + l02*r00 + l12*r01 + l22*r02 + l32*r03, + l03*r00 + l13*r01 + l23*r02 + l33*r03 + }, + { + l00*r10 + l10*r11 + l20*r12 + l30*r13, + l01*r10 + l11*r11 + l21*r12 + l31*r13, + l02*r10 + l12*r11 + l22*r12 + l32*r13, + l03*r10 + l13*r11 + l23*r12 + l33*r13 + }, + { + l00*r20 + l10*r21 + l20*r22 + l30*r23, + l01*r20 + l11*r21 + l21*r22 + l31*r23, + l02*r20 + l12*r21 + l22*r22 + l32*r23, + l03*r20 + l13*r21 + l23*r22 + l33*r23 + }, + { + l00*r30 + l10*r31 + l20*r32 + l30*r33, + l01*r30 + l11*r31 + l21*r32 + l31*r33, + l02*r30 + l12*r31 + l22*r32 + l32*r33, + l03*r30 + l13*r31 + l23*r32 + l33*r33 + } + }; } -struct mat make_scale(struct v3 v) +mat +make_translate(v3 v) { - return (struct mat){ - {v.x, 0.0f, 0.0f, 0.0f}, - {0.0f, v.y, 0.0f, 0.0f}, - {0.0f, 0.0f, v.z, 0.0f}, - {0.0f, 0.0f, 0.0f, 1.0f} - }; + return (mat){ + {1.0f, 0.0f, 0.0f, 0.0f}, + {0.0f, 1.0f, 0.0f, 0.0f}, + {0.0f, 0.0f, 1.0f, 0.0f}, + {v.x, v.y, v.z, 1.0f} + }; } -struct mat make_rotate(struct v3 x, struct v3 y, struct v3 z) +mat +make_scale(v3 v) { - return (struct mat){ - {x.x, x.y, x.z, 0.0f}, - {y.x, y.y, y.z, 0.0f}, - {z.x, z.y, z.z, 0.0f}, - {0.0f, 0.0f, 0.0f, 1.0f} - }; + return (mat){ + {v.x, 0.0f, 0.0f, 0.0f}, + {0.0f, v.y, 0.0f, 0.0f}, + {0.0f, 0.0f, v.z, 0.0f}, + {0.0f, 0.0f, 0.0f, 1.0f} + }; } -struct mat translate_mat(struct mat m, struct v3 v) +mat +make_rotate(v3 x, v3 y, v3 z) { - return mulmat(make_translate(v), m); -} - -struct mat scale_mat(struct mat m, struct v3 v) -{ - return mulmat(make_scale(v), m); -} - -struct mat rotate_mat(struct mat m, struct v3 angles) -{ - f32 angle = deg2rad(angles.x); - f32 cx = cosf(angle); - f32 sx = sinf(angle); - angle = deg2rad(angles.y); - f32 cy = cosf(angle); - f32 sy = sinf(angle); - angle = deg2rad(angles.z); - f32 cz = cosf(angle); - f32 sz = sinf(angle); - struct v3 x = { cy*cz, sx*sy*cz + cx*sz, -cx*sy*cz + sx*sz}; - struct v3 y = {-cy*sz, -sx*sy*sz + cx*cz, cx*sy*sz + sx*cz}; - struct v3 z = { sy, -sx*cy, cx*cy}; - return mulmat(make_rotate(x, y, z), m); -} - -struct v4 mulmatv4(struct mat m, struct v4 v) -{ - return (struct v4){ - m.c0.x*v.x + m.c1.x*v.y + m.c2.x*v.z + m.c3.x*v.w, - m.c0.y*v.x + m.c1.y*v.y + m.c2.y*v.z + m.c3.y*v.w, - m.c0.z*v.x + m.c1.z*v.y + m.c2.z*v.z + m.c3.z*v.w, - m.c0.w*v.x + m.c1.w*v.y + m.c2.w*v.z + m.c3.w*v.w - }; -} - -i32 in_rect(struct v2 pos, struct rect rect) -{ - return (pos.x > rect.start.x) && (pos.x < rect.end.x) && - (pos.y > rect.start.y) && (pos.y < rect.end.y); + return (mat){ + {x.x, x.y, x.z, 0.0f}, + {y.x, y.y, y.z, 0.0f}, + {z.x, z.y, z.z, 0.0f}, + {0.0f, 0.0f, 0.0f, 1.0f} + }; +} + +mat +translate_mat(mat m, v3 v) +{ + return mulmat(make_translate(v), m); +} + +mat +scale_mat(mat m, v3 v) +{ + return mulmat(make_scale(v), m); +} + +mat +rotate_mat(mat m, v3 angles) +{ + f32 angle = deg2rad(angles.x); + f32 cx = cosf(angle); + f32 sx = sinf(angle); + angle = deg2rad(angles.y); + f32 cy = cosf(angle); + f32 sy = sinf(angle); + angle = deg2rad(angles.z); + f32 cz = cosf(angle); + f32 sz = sinf(angle); + v3 x = { cy*cz, sx*sy*cz + cx*sz, -cx*sy*cz + sx*sz}; + v3 y = {-cy*sz, -sx*sy*sz + cx*cz, cx*sy*sz + sx*cz}; + v3 z = { sy, -sx*cy, cx*cy}; + return mulmat(make_rotate(x, y, z), m); +} + +v4 +mulmatv4(mat m, v4 v) +{ + return (v4){ + m.c0.x*v.x + m.c1.x*v.y + m.c2.x*v.z + m.c3.x*v.w, + m.c0.y*v.x + m.c1.y*v.y + m.c2.y*v.z + m.c3.y*v.w, + m.c0.z*v.x + m.c1.z*v.y + m.c2.z*v.z + m.c3.z*v.w, + m.c0.w*v.x + m.c1.w*v.y + m.c2.w*v.z + m.c3.w*v.w + }; +} + +i32 +in_rect(v2 pos, rect r) +{ + return (pos.x > r.start.x) && (pos.x < r.end.x) && + (pos.y > r.start.y) && (pos.y < r.end.y); } diff --git a/prbm.h b/prbm.h index f3932dd..a5432de 100644 --- a/prbm.h +++ b/prbm.h @@ -3,110 +3,111 @@ #include "types.h" -struct v2 { - f32 x; - f32 y; -}; - -struct v3 { - f32 x; - f32 y; - f32 z; -}; - -struct v4 { - f32 x; - f32 y; - f32 z; - f32 w; -}; - -// column-major -struct mat { - struct v4 c0; - struct v4 c1; - struct v4 c2; - struct v4 c3; -}; - -struct rect { - struct v2 start; - struct v2 end; -}; - -#define v2_zero (struct v2){ 0.0f, 0.0f} -#define v2_one (struct v2){ 1.0f, 1.0f} -#define v2_right (struct v2){ 1.0f, 0.0f} -#define v2_up (struct v2){ 0.0f, 1.0f} -#define v2_left (struct v2){-1.0f, 0.0f} -#define v2_down (struct v2){ 0.0f, -1.0f} - -#define v3_zero (struct v3){ 0.0f, 0.0f, 0.0f} -#define v3_one (struct v3){ 1.0f, 1.0f, 1.0f} -#define v3_right (struct v3){ 1.0f, 0.0f, 0.0f} -#define v3_up (struct v3){ 0.0f, 1.0f, 0.0f} -#define v3_left (struct v3){-1.0f, 0.0f, 0.0f} -#define v3_down (struct v3){ 0.0f, -1.0f, 0.0f} -#define v3_forward (struct v3){ 0.0f, 0.0f, 1.0f} -#define v3_backward (struct v3){ 0.0f, 0.0f, -1.0f} - -#define v4_zero (struct v4){0.0f, 0.0f, 0.0f, 0.0f} -#define v4_one (struct v4){1.0f, 1.0f, 1.0f, 1.0f} - -#define mat_identity (struct mat){ \ - {1.0f, 0.0f, 0.0f, 0.0f}, \ - {0.0f, 1.0f, 0.0f, 0.0f}, \ - {0.0f, 0.0f, 1.0f, 0.0f}, \ - {0.0f, 0.0f, 0.0f, 1.0f}} +#define v2_zero (v2){ 0.0f, 0.0f} +#define v2_one (v2){ 1.0f, 1.0f} +#define v2_right (v2){ 1.0f, 0.0f} +#define v2_up (v2){ 0.0f, 1.0f} +#define v2_left (v2){-1.0f, 0.0f} +#define v2_down (v2){ 0.0f, -1.0f} + +#define v3_zero (v3){ 0.0f, 0.0f, 0.0f} +#define v3_one (v3){ 1.0f, 1.0f, 1.0f} +#define v3_right (v3){ 1.0f, 0.0f, 0.0f} +#define v3_up (v3){ 0.0f, 1.0f, 0.0f} +#define v3_left (v3){-1.0f, 0.0f, 0.0f} +#define v3_down (v3){ 0.0f, -1.0f, 0.0f} +#define v3_forward (v3){ 0.0f, 0.0f, 1.0f} +#define v3_backward (v3){ 0.0f, 0.0f, -1.0f} + +#define v4_zero (v4){0.0f, 0.0f, 0.0f, 0.0f} +#define v4_one (v4){1.0f, 1.0f, 1.0f, 1.0f} + +#define mat_identity (mat){ \ + {1.0f, 0.0f, 0.0f, 0.0f}, \ + {0.0f, 1.0f, 0.0f, 0.0f}, \ + {0.0f, 0.0f, 1.0f, 0.0f}, \ + {0.0f, 0.0f, 0.0f, 1.0f}} #define f32pi 3.14159265359f -#define deg2rad(angle) (f32pi/180.0f*(angle)) - -struct v2 fillv2(f32 a); -struct v2 invv2(struct v2 a); -struct v2 addv2(struct v2 a, struct v2 b); -struct v2 subv2(struct v2 a, struct v2 b); -struct v2 scalev2(struct v2 a, f32 s); -struct v2 scalevv2(struct v2 a, struct v2 s); -f32 dotv2(struct v2 a, struct v2 b); -f32 len2v2(struct v2 a); -f32 lenv2(struct v2 a); -struct v2 normv2(struct v2 a); - -struct v3 fillv3(f32 a); -struct v3 invv3(struct v3 a); -struct v3 addv3(struct v3 a, struct v3 b); -struct v3 subv3(struct v3 a, struct v3 b); -struct v3 scalev3(struct v3 a, f32 s); -struct v3 scalevv3(struct v3 a, struct v3 s); -f32 dotv3(struct v3 a, struct v3 b); -struct v3 crossv3(struct v3 l, struct v3 r); -f32 len2v3(struct v3 a); -f32 lenv3(struct v3 a); -struct v3 normv3(struct v3 a); - -struct v4 fillv4(f32 a); -struct v4 invv4(struct v4 a); -struct v4 addv4(struct v4 a, struct v4 b); -struct v4 subv4(struct v4 a, struct v4 b); -struct v4 scalev4(struct v4 a, f32 s); -struct v4 scalevv4(struct v4 a, struct v4 s); -f32 dotv4(struct v4 a, struct v4 b); -f32 len2v4(struct v4 a); -f32 lenv4(struct v4 a); -struct v4 normv4(struct v4 a); - -struct mat transpmat(struct mat m); -struct mat mulmat(struct mat left, struct mat right); -struct mat make_translate(struct v3 v); -struct mat make_scale(struct v3 v); -struct mat make_rotate(struct v3 x, struct v3 y, struct v3 z); -struct mat translate_mat(struct mat m, struct v3 v); -struct mat scale_mat(struct mat m, struct v3 v); -struct mat rotate_mat(struct mat m, struct v3 angles); -struct v4 mulmatv4(struct mat m, struct v4 v); - -i32 in_rect(struct v2 pos, struct rect rect); +#define deg2rad(angle) (f32pi/180.0f*(angle)) + +typedef struct { + f32 x; + f32 y; +} v2; + +typedef struct { + f32 x; + f32 y; + f32 z; +} v3; + +typedef struct { + f32 x; + f32 y; + f32 z; + f32 w; +} v4; + +// column-major +typedef struct { + v4 c0; + v4 c1; + v4 c2; + v4 c3; +} mat; + +typedef struct { + v2 start; + v2 end; +} rect; + +v2 fillv2(f32 a); +v2 invv2(v2 a); +v2 addv2(v2 a, v2 b); +v2 subv2(v2 a, v2 b); +v2 scalev2(v2 a, f32 s); +v2 scalevv2(v2 a, v2 s); +f32 dotv2(v2 a, v2 b); +f32 len2v2(v2 a); +f32 lenv2(v2 a); +v2 normv2(v2 a); + +v3 fillv3(f32 a); +v3 invv3(v3 a); +v3 addv3(v3 a, v3 b); +v3 subv3(v3 a, v3 b); +v3 scalev3(v3 a, f32 s); +v3 scalevv3(v3 a, v3 s); +f32 dotv3(v3 a, v3 b); +v3 crossv3(v3 l, v3 r); +f32 len2v3(v3 a); +f32 lenv3(v3 a); +v3 normv3(v3 a); +v3 maxv3(v3 a, v3 b); + +v4 fillv4(f32 a); +v4 invv4(v4 a); +v4 addv4(v4 a, v4 b); +v4 subv4(v4 a, v4 b); +v4 scalev4(v4 a, f32 s); +v4 scalevv4(v4 a, v4 s); +f32 dotv4(v4 a, v4 b); +f32 len2v4(v4 a); +f32 lenv4(v4 a); +v4 normv4(v4 a); + +mat transpose_mat(mat m); +mat mulmat(mat left, mat right); +mat make_translate(v3 v); +mat make_scale(v3 v); +mat make_rotate(v3 x, v3 y, v3 z); +mat translate_mat(mat m, v3 v); +mat scale_mat(mat m, v3 v); +mat rotate_mat(mat m, v3 angles); +v4 mulmatv4(mat m, v4 v); + +i32 in_rect(v2 pos, rect r); #endif diff --git a/prbs.c b/prbs.c index 818a110..0c6bd8a 100644 --- a/prbs.c +++ b/prbs.c @@ -1,132 +1,179 @@ #include "prbs.h" #include "macros.h" +#include "sys.h" #include #include -i32 cstrings_equal(const char *s1, const char *s2) +i32 +cstrings_equal(const char *s1, const char *s2) { - assert(s1 && s2); - while (*s1++ == *s2++) - if (!*s1) - return 1; - return 0; + assert(s1 && s2); + while (*s1++ == *s2++) + if (!*s1) + return 1; + return 0; } -char *string_to_cstring(struct arena *arena, struct string str) +char * +string_to_cstring(struct arena *arena, struct string str) { - u64 len = str.len + 1; - char *cstr = push_arena(arena, len); - memmove(cstr, str.ptr, len); - cstr[str.len] = 0; - return cstr; + u64 len = str.len + 1; + char *cstr = push_arena(arena, len); + memmove(cstr, str.ptr, len); + cstr[str.len] = 0; + return cstr; } -i32 strings_equal(struct string str1, struct string str2) +struct string +string_from_cstring(struct arena *arena, const char *cstr) { - if (str1.len != str2.len) - return 0; - for (u64 i = 0; i < str1.len; ++i) - if (str1.ptr[i] != str2.ptr[i]) - return 0; - return 1; + u64 len = 0; + while (cstr[len++]) + ; + len--; + u8 *ptr = push_arena(arena, len); + for (i32 i = 0; i < len; i++) + ptr[i] = cstr[i]; + return (struct string){ptr, len}; } -u8 *find_char(struct string str, i32 c) +i32 +strings_equal(struct string str1, struct string str2) { - for (u64 i = 0; i < str.len; ++i) - if (str.ptr[i] == c) - return str.ptr + i; - return 0; + if (str1.len != str2.len) + return 0; + for (u64 i = 0; i < str1.len; ++i) + if (str1.ptr[i] != str2.ptr[i]) + return 0; + return 1; } -u8 *find_last_char(struct string str, i32 c) +u8 * +find_char(struct string str, i32 c) { - for (u64 i = str.len - 1; i > 0; --i) - if (str.ptr[i] == c) - return str.ptr + i; - return 0; + for (u64 i = 0; i < str.len; ++i) + if (str.ptr[i] == c) + return str.ptr + i; + return 0; } -struct string string_from_range(u8 *start, u8 *end) +u8 * +find_last_char(struct string str, i32 c) { - assert(start); - assert(end); - assert(start < end); - return (struct string){start, end - start}; + for (u64 i = str.len - 1; i > 0; --i) + if (str.ptr[i] == c) + return str.ptr + i; + return 0; } -struct string get_dirname(struct string str) +struct string +string_from_range(u8 *start, u8 *end) { - assert(str.len); - u8 *slash = find_last_char(str, '/'); - if (!slash) - return str; - return string_from_range(str.ptr, slash); + assert(start); + assert(end); + assert(start < end); + return (struct string){start, end - start}; } -struct string push_fstringv(struct arena *arena, const char *format, va_list args) +struct string +get_dirname(struct string str) { - char tmp[kilobytes(4) + 1]; - i32 len = vsnprintf(tmp, kilobytes(4), format, args); - struct string str = {0}; - if (len < 0) - return str; - u8 *ptr = push_arena(arena, len); - memmove(ptr, tmp, len); - return (struct string){ptr, len}; + assert(str.len); + u8 *dot = find_char(str, '.'); + u8 *slash = find_last_char(str, '/'); + if (!slash) + return str; + return string_from_range(str.ptr, slash); } -struct string push_fstring(struct arena *arena, const char *format, ...) +struct string +push_string(struct arena *arena, struct string str) { - va_list args; - va_start(args, format); - struct string str = push_fstringv(arena, format, args); - va_end(args); - return str; + assert(str.ptr && str.len); + u8 *ptr = push_arena(arena, str.len); + memmove(ptr, str.ptr, str.len); + return (struct string){ptr, str.len}; } -void print_string(struct string str) +static struct string +push_fstringv(struct arena *arena, const char *format, va_list args) { - assert(str.ptr && str.len); - printf("%.*s", str.len, str.ptr); + char tmp[kilobytes(4) + 1]; + i32 len = vsnprintf(tmp, kilobytes(4), format, args); + struct string str = {0}; + if (len < 0) + return str; + u8 *ptr = push_arena(arena, len); + memmove(ptr, tmp, len); + return (struct string){ptr, len}; } -void push_string_to_list(struct arena *arena, struct string_list *list, i32 front, struct string str) +struct string +push_fstring(struct arena *arena, const char *format, ...) { - struct string_node *node = push_arena(arena, sizeof(struct string_node)); - node->str = str; - if (front) - dllpushfront(list->first, list->last, node); - else - dllpushback(list->first, list->last, node); - list->len += str.len; - list->cnt++; + va_list args; + va_start(args, format); + struct string str = push_fstringv(arena, format, args); + va_end(args); + return str; } -void push_fstring_to_list(struct arena *arena, struct string_list *list, i32 front, const char *format, ...) +void +print_string(struct string str) { - va_list args; - va_start(args, format); - struct string str = push_fstringv(arena, format, args); - va_end(args); - push_string_to_list(arena, list, front, str); + assert(str.ptr && str.len); + printf("%.*s", str.len, str.ptr); } -struct string join_string_list(struct arena *arena, struct string_list *list) +void +print_stringnl(struct string str) { - assert(arena); - assert(list); - struct string str = {arena->memory + arena->used, 0}; - for (struct string_node *node = list->first; node; node = node->next) { - u8 *ptr = push_arena(arena, node->str.len); - memmove(ptr, node->str.ptr, node->str.len); - str.len += node->str.len; - } - return str; + print_string(str); + printf("\n"); } -void print_string_list(struct string_list *list) +void +push_string_to_list(struct arena *arena, struct string_list *list, + i32 front, struct string str) { - for (struct string_node *node = list->first; node; node = node->next) - print_string(node->str); + struct string_node *node = push_arena(arena, sizeof(struct string_node)); + node->str = str; + if (front) + dllpushfront(list->first, list->last, node); + else + dllpushback(list->first, list->last, node); + list->len += str.len; + list->cnt++; +} + +void +push_fstring_to_list(struct arena *arena, struct string_list *list, + i32 front, const char *format, ...) +{ + va_list args; + va_start(args, format); + struct string str = push_fstringv(arena, format, args); + va_end(args); + push_string_to_list(arena, list, front, str); +} + +struct string +join_string_list(struct arena *arena, struct string_list *list) +{ + assert(arena); + assert(list); + struct string str = {arena->memory + arena->used, 0}; + for (struct string_node *node = list->first; node; node = node->next) { + u8 *ptr = push_arena(arena, node->str.len); + memmove(ptr, node->str.ptr, node->str.len); + str.len += node->str.len; + } + return str; +} + +void +print_string_list(struct string_list *list) +{ + for (struct string_node *node = list->first; node; node = node->next) + print_string(node->str); } diff --git a/prbs.h b/prbs.h index 4547d7c..2384c49 100644 --- a/prbs.h +++ b/prbs.h @@ -8,37 +8,41 @@ i32 cstrings_equal(const char *s1, const char *s2); struct string { - u8 *ptr; - u64 len; + u8 *ptr; + u64 len; }; struct string_node { - struct string str; - struct string_node *next; - struct string_node *prev; + struct string str; + struct string_node *next; + struct string_node *prev; }; struct string_list { - i32 cnt; - u64 len; - struct string_node *first; - struct string_node *last; + i32 cnt; + u64 len; + struct string_node *first; + struct string_node *last; }; -#define string_from_cliteral(str) (sturct string){(u8 *)(str), sizeof(str) - 1} -#define expand_string(str) (i32)((str).len), ((str).ptr) +#define string_from_cliteral(str) (struct string){(u8 *)(str), sizeof(str) - 1} +#define expand_string(str) (i32)((str).len), ((str).ptr) char *string_to_cstring(struct arena *arena, struct string str); +struct string string_from_cstring(struct arena *arena, const char *cstr); i32 strings_equal(struct string str1, struct string str2); u8 *find_char(struct string str, i32 c); u8 *find_last_char(struct string str, i32 c); struct string string_from_range(u8 *start, u8 *end); struct string get_dirname(struct string str); -struct string push_fstringv(struct arena *arena, const char *format, va_list args); +struct string push_string(struct arena *arena, struct string str); struct string push_fstring(struct arena *arena, const char *format, ...); void print_string(struct string str); -void push_string_to_list(struct arena *arena, struct string_list *list, i32 front, struct string str); -void push_fstring_to_list(struct arena *arena, struct string_list *list, i32 front, const char *format, ...); +void print_stringnl(struct string str); +void push_string_to_list(struct arena *arena, struct string_list *list, + i32 front, struct string str); +void push_fstring_to_list(struct arena *arena, struct string_list *list, + i32 front, const char *format, ...); struct string join_string_list(struct arena *arena, struct string_list *list); void print_string_list(struct string_list *list); diff --git a/prbwin.c b/prbwin.c index 81ad626..ff81c7f 100644 --- a/prbwin.c +++ b/prbwin.c @@ -1,77 +1,84 @@ #include "sys.h" -void *sys_alloc(u64 length) +void +*sys_alloc(u64 length) { - assert(length); - void *result = VirtualAlloc(0, length, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE); - return result; + assert(length); + void *result = VirtualAlloc(0, length, MEM_COMMIT|MEM_RESERVE, PAGE_READWRITE); + return result; } -void sys_free(void *memory, u64 length) +void +sys_free(void *memory, u64 length) { - assert(memory); - assert(length); - assert(VirtualFree(memory, 0, MEM_RELEASE)); + assert(memory); + assert(length); + assert(VirtualFree(memory, 0, MEM_RELEASE)); } -void die(const char *error_string, ...) +void +die(const char *error_string, ...) { - va_list args; - va_start(args, error_string); - printf("error: "); - vprintf(error_string, args); - printf("\n"); - va_end(args); - exit(1); + va_list args; + va_start(args, error_string); + printf("error: "); + vprintf(error_string, args); + printf("\n"); + va_end(args); + exit(1); } -void info(const char *format, ...) +void +info(const char *format, ...) { - va_list args; - va_start(args, format); - vprintf(format, args); - putchar('\n'); - va_end(args); + va_list args; + va_start(args, format); + vprintf(format, args); + putchar('\n'); + va_end(args); } -char *read_entire_file(struct arena *arena, u64 *len, const char *filename) +char * +read_entire_file(struct arena *arena, u64 *len, const char *filename) { - HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, - 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); - if (file == INVALID_HANDLE_VALUE) - return 0; - LARGE_INTEGER lisize; - if (!GetFileSizeEx(file, &lisize)) - goto error; - *len = lisize.QuadPart; - if (!*len) - goto error; - char *buffer = push_arena(arena, *len + 1); - DWORD bytes_read; - if (!ReadFile(file, *buffer, size, &bytes_read, 0)) { - pop_arena(arena, *len + 1); - goto error; - } - CloseHandle(file); - buffer[*len] = 0; - return buffer; + HANDLE file = CreateFile(filename, GENERIC_READ, FILE_SHARE_READ, + 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); + if (file == INVALID_HANDLE_VALUE) + return 0; + LARGE_INTEGER lisize; + if (!GetFileSizeEx(file, &lisize)) + goto error; + *len = lisize.QuadPart; + if (!*len) + goto error; + char *buffer = push_arena(arena, *len + 1); + DWORD bytes_read; + if (!ReadFile(file, *buffer, size, &bytes_read, 0)) { + pop_arena(arena, *len + 1); + goto error; + } + CloseHandle(file); + buffer[*len] = 0; + return buffer; + error: - CloseHandle(file); - return 0; + CloseHandle(file); + return 0; } -char *get_exe_path(arena_t *arena) +char * +get_exe_path(arena_t *arena) { - char path[max_path_len] = {0}; - u32 len = GetModuleFileName(0, path, max_path_len); - if (!len) - return 0; - char *dir = strrchr(path, '\\'); - if (!dir) - return path; - len = dir - path; - dir = push_arena(arena, len + 1); - memmove(dir, path, len); - dir[len] = 0; - return dir; + char path[max_path_len] = {0}; + u32 len = GetModuleFileName(0, path, max_path_len); + if (!len) + return 0; + char *dir = strrchr(path, '\\'); + if (!dir) + return path; + len = dir - path; + dir = push_arena(arena, len + 1); + memmove(dir, path, len); + dir[len] = 0; + return dir; } diff --git a/sys.h b/sys.h index 81d9f0c..f16c719 100644 --- a/sys.h +++ b/sys.h @@ -10,5 +10,6 @@ void sys_free(void *memory, u64 length); void die(const char *format, ...); void info(const char *format, ...); char *read_entire_file(struct arena *arena, u64 *len, const char *filename); +struct string get_basedir(struct arena *arena); #endif -- cgit v1.2.3-70-g09d2