diff options
29 files changed, 832 insertions, 797 deletions
| @@ -0,0 +1,25 @@ | |||
| 1 | CompileFlags: | ||
| 2 | CompilationDatabase: . | ||
| 3 | Add: | ||
| 4 | - -g | ||
| 5 | - -O0 | ||
| 6 | - -I. | ||
| 7 | - -Wall | ||
| 8 | - -Wextra | ||
| 9 | - -Wno-unused-variable | ||
| 10 | - -Wno-unused-function | ||
| 11 | - -Wno-undefined-internal | ||
| 12 | - -Wno-implicit-function-declaration | ||
| 13 | - -lX11 | ||
| 14 | - -lm | ||
| 15 | |||
| 16 | Remove: | ||
| 17 | - -O* | ||
| 18 | - -Ofast | ||
| 19 | Diagnostics: | ||
| 20 | Suppress: | ||
| 21 | - pp_file_not_found | ||
| 22 | - unused-command-line-argument | ||
| 23 | - unknown-warning-option | ||
| 24 | Index: | ||
| 25 | Background: Build | ||
| @@ -1,12 +1,12 @@ | |||
| 1 | .PHONY: all clean | 1 | .PHONY: all clean |
| 2 | 2 | ||
| 3 | CC := gcc | 3 | CC := gcc |
| 4 | CFLAGS := -I. -Wall -Wextra -Wno-unused-function -Wno-unused-variable -DCORE_UNITY | 4 | CFLAGS := -I. -Wall -Wextra -Wno-unused-function -Wno-unused-variable -Dtb_UNITY |
| 5 | LDLIBS := -lX11 -lm | 5 | LDLIBS := -lX11 -lm |
| 6 | 6 | ||
| 7 | BUILD_DIR := build | 7 | BUILD_DIR := build |
| 8 | TARGET := $(BUILD_DIR)/app | 8 | TARGET := $(BUILD_DIR)/app |
| 9 | SOURCE := source/core/core.c | 9 | SOURCE := source/tb/tb.c |
| 10 | 10 | ||
| 11 | all: $(TARGET) | 11 | all: $(TARGET) |
| 12 | 12 | ||
| @@ -1,8 +0,0 @@ | |||
| 1 | #!/bin/sh | ||
| 2 | set -eu | ||
| 3 | |||
| 4 | build_dir=~/projects/tb/build | ||
| 5 | |||
| 6 | # Run | ||
| 7 | export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:-}:$build_dir" | ||
| 8 | "$build_dir/app" | ||
diff --git a/source/base/base.h b/source/base/base.h index 5fcf9e2..3fe9dca 100755 --- a/source/base/base.h +++ b/source/base/base.h | |||
| @@ -1,34 +1,12 @@ | |||
| 1 | #ifndef BASE_H | 1 | #ifndef BASE_H |
| 2 | #define BASE_H | 2 | #define BASE_H |
| 3 | 3 | ||
| 4 | #include <stdint.h> | ||
| 5 | #include <unistd.h> | ||
| 6 | #include <stddef.h> | ||
| 7 | #include <string.h> | ||
| 8 | |||
| 9 | /* assert an expression and output the file and the line */ | 4 | /* assert an expression and output the file and the line */ |
| 10 | 5 | #define internal static | |
| 11 | #define internal static | ||
| 12 | #define global_variable static | 6 | #define global_variable static |
| 13 | #define local_persist static | 7 | #define local_persist static |
| 14 | |||
| 15 | #define ERR_OK 0 | ||
| 16 | #define ERR_IO 1 | ||
| 17 | #define ERR_PARSE 2 | ||
| 18 | #define ERR_PERM 3 | ||
| 19 | #define ERR_INVALID 4 | ||
| 20 | |||
| 21 | #define KiB(n) (((u64)(n)) << 10) | ||
| 22 | #define MiB(n) (((u64)(n)) << 20) | ||
| 23 | #define GiB(n) (((u64)(n)) << 30) | ||
| 24 | 8 | ||
| 25 | #define unused(x) (void)(x) | 9 | #define unused(x) (void)(x) |
| 26 | |||
| 27 | #define PATH_MAX_LEN 128 | ||
| 28 | #define BUFF_SMALL 128 | ||
| 29 | #define BUFF_DEFAULT 256 | ||
| 30 | #define BUFF_LARGE 512 | ||
| 31 | |||
| 32 | #define NIL 0 | 10 | #define NIL 0 |
| 33 | 11 | ||
| 34 | #define DEPRECATED __attribute__((__deprecated__)) | 12 | #define DEPRECATED __attribute__((__deprecated__)) |
| @@ -43,35 +21,44 @@ | |||
| 43 | #define MemCpy(dest, src, len) memcpy((dest), (src), (len)) | 21 | #define MemCpy(dest, src, len) memcpy((dest), (src), (len)) |
| 44 | #define MemSet(dest, len) memset((dest), (0), (len)) | 22 | #define MemSet(dest, len) memset((dest), (0), (len)) |
| 45 | 23 | ||
| 24 | #if COMPILER_MSVC || (COMPILER_CLANG && OS_WINDOWS) | ||
| 25 | #pragma section(".rdata$", read) | ||
| 26 | #define read_only __declspec(allocate(".rdata$")) | ||
| 27 | #elif (COMPILER_CLANG && OS_LINUX) | ||
| 28 | #define read_only __attribute__((section(".rodata"))) | ||
| 29 | #else | ||
| 30 | #define read_only | ||
| 31 | #endif | ||
| 32 | |||
| 46 | typedef uint64_t u64; | 33 | typedef uint64_t u64; |
| 47 | typedef uint32_t u32; | 34 | typedef uint32_t u32; |
| 48 | typedef uint16_t u16; | 35 | typedef uint16_t u16; |
| 49 | typedef uint8_t u8; | 36 | typedef uint8_t u8; |
| 50 | 37 | ||
| 51 | typedef int8_t i8; | 38 | typedef int8_t s8; |
| 52 | typedef int16_t i16; | 39 | typedef int16_t s16; |
| 53 | typedef int32_t i32; | 40 | typedef int32_t s32; |
| 54 | typedef int64_t i64; | 41 | typedef int64_t s64; |
| 55 | 42 | ||
| 56 | typedef float f32; | 43 | typedef float f32; |
| 57 | typedef double f64; | 44 | typedef double f64; |
| 58 | 45 | ||
| 59 | typedef i32 b32; | 46 | typedef s32 b32; |
| 60 | typedef i16 b16; | 47 | typedef s16 b16; |
| 61 | typedef u8 b8; | 48 | typedef s8 b8; |
| 62 | 49 | ||
| 63 | typedef uintptr_t umm; | 50 | typedef uintptr_t umm; |
| 64 | typedef intptr_t smm; | 51 | typedef intptr_t smm; |
| 65 | 52 | ||
| 66 | #define TRUE (0 == 0) | 53 | #define True (1 == 1) |
| 67 | #define FALSE (0 != 0) | 54 | #define False (1 != 1) |
| 68 | 55 | ||
| 69 | typedef struct s8 s8; | 56 | #define Red "\x1b[31m" |
| 57 | #define Green "\x1b[32m" | ||
| 58 | #define Reset "\x1b[0m" | ||
| 59 | #define Blue "\x1b[34m" | ||
| 60 | #define Yellow "\x1b[33m" | ||
| 70 | 61 | ||
| 71 | struct s8 | 62 | #define Len(s) (sizeof(s) - 1) |
| 72 | { | ||
| 73 | char *data; | ||
| 74 | umm size; | ||
| 75 | }; | ||
| 76 | 63 | ||
| 77 | #endif | 64 | #endif |
diff --git a/source/base/base_arena.c b/source/base/base_arena.c index 76938bd..60133e0 100755 --- a/source/base/base_arena.c +++ b/source/base/base_arena.c | |||
| @@ -30,21 +30,17 @@ arena_destroy(mem_arena *arena) | |||
| 30 | { | 30 | { |
| 31 | return; | 31 | return; |
| 32 | } | 32 | } |
| 33 | 33 | munmap(arena, arena->capacity + sizeof(mem_arena)); | |
| 34 | int code = munmap(arena, arena->capacity + sizeof(mem_arena)); | ||
| 35 | } | 34 | } |
| 36 | |||
| 37 | internal void * | 35 | internal void * |
| 38 | arena_alloc(mem_arena *arena, u64 size) | 36 | arena_alloc(mem_arena *arena, u64 size, b32 zero) |
| 39 | { | 37 | { |
| 40 | if (!arena) | 38 | if (!arena) |
| 41 | { | 39 | { |
| 42 | return NULL; | 40 | return NULL; |
| 43 | } | 41 | } |
| 44 | |||
| 45 | u64 aligned = Align(arena->current_position, ARENA_ALIGN); | 42 | u64 aligned = Align(arena->current_position, ARENA_ALIGN); |
| 46 | u64 new_pos = aligned + size; | 43 | u64 new_pos = aligned + size; |
| 47 | |||
| 48 | if (new_pos > arena->capacity) | 44 | if (new_pos > arena->capacity) |
| 49 | { | 45 | { |
| 50 | return NULL; | 46 | return NULL; |
| @@ -55,7 +51,7 @@ arena_alloc(mem_arena *arena, u64 size) | |||
| 55 | arena->previous_position = arena->current_position; | 51 | arena->previous_position = arena->current_position; |
| 56 | arena->current_position = aligned + size; | 52 | arena->current_position = aligned + size; |
| 57 | 53 | ||
| 58 | MemSet(out, size); | 54 | if (zero) MemSet(out, size); |
| 59 | 55 | ||
| 60 | return out; | 56 | return out; |
| 61 | } | 57 | } |
| @@ -92,7 +88,7 @@ arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_siz | |||
| 92 | 88 | ||
| 93 | if (old_memory == NULL || old_size == 0) | 89 | if (old_memory == NULL || old_size == 0) |
| 94 | { | 90 | { |
| 95 | return (mem_arena *)arena_alloc(arena, new_size); | 91 | return (mem_arena *)arena_alloc(arena, new_size, 0); |
| 96 | } | 92 | } |
| 97 | else if ((old_mem >= arena->base_position && old_mem < arena->base_position + arena->capacity)) | 93 | else if ((old_mem >= arena->base_position && old_mem < arena->base_position + arena->capacity)) |
| 98 | { | 94 | { |
| @@ -107,15 +103,16 @@ arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_siz | |||
| 107 | } | 103 | } |
| 108 | else | 104 | else |
| 109 | { | 105 | { |
| 110 | void *new_memory = arena_alloc(arena, new_size); | 106 | void *new_memory = arena_alloc(arena, new_size, 0); |
| 111 | umm copy_size = old_size < new_size ? old_size : new_size; | 107 | umm copy_size = old_size < new_size ? old_size : new_size; |
| 112 | memmove(new_memory, old_mem, copy_size); | 108 | memmove(new_memory, old_mem, copy_size); |
| 113 | } | 109 | } |
| 114 | } | 110 | } |
| 115 | else | 111 | else |
| 116 | { | 112 | { |
| 117 | check(0); | 113 | warn(0); |
| 118 | } | 114 | } |
| 115 | |||
| 119 | return NULL; | 116 | return NULL; |
| 120 | } | 117 | } |
| 121 | 118 | ||
diff --git a/source/base/base_arena.h b/source/base/base_arena.h index 71d9e69..dc6dd73 100755 --- a/source/base/base_arena.h +++ b/source/base/base_arena.h | |||
| @@ -1,14 +1,18 @@ | |||
| 1 | #ifndef ARENA_H | 1 | #ifndef BASE_ARENA_H |
| 2 | #define ARENA_H | 2 | #define BASE_ARENA_H |
| 3 | 3 | ||
| 4 | #define Align(pointer, alignment) align((u64)(pointer), (umm)(alignment)) | 4 | #define Align(pointer, alignment) align((u64)(pointer), (umm)(alignment)) |
| 5 | #define PushStruct(arena, type) (type *)arena_alloc((arena), sizeof(type)) | 5 | #define PushStruct(arena, type) (type *)arena_alloc((arena), sizeof(type), 0) |
| 6 | #define PushArray(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len)) | 6 | #define PushStructZero(arena, type) (type *)arena_alloc((arena), sizeof(type), 1) |
| 7 | #define PushString(arena, len) (s8 *)arena_alloc((arena), sizeof(s8)*len)) | 7 | #define PushArray(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len), 0) |
| 8 | #define PushArrayZero(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len), 1) | ||
| 8 | 9 | ||
| 9 | typedef struct mem_arena mem_arena; | 10 | #define KiB(n) (((u64)(n)) << 10) |
| 10 | typedef struct temp_arena temp_arena; | 11 | #define MiB(n) (((u64)(n)) << 20) |
| 12 | #define GiB(n) (((u64)(n)) << 30) | ||
| 11 | 13 | ||
| 14 | |||
| 15 | typedef struct mem_arena mem_arena; | ||
| 12 | struct mem_arena | 16 | struct mem_arena |
| 13 | { | 17 | { |
| 14 | u64 current_position; | 18 | u64 current_position; |
| @@ -17,25 +21,11 @@ struct mem_arena | |||
| 17 | u8 *base_position; | 21 | u8 *base_position; |
| 18 | }; | 22 | }; |
| 19 | 23 | ||
| 24 | typedef struct temp_arena temp_arena; | ||
| 20 | struct temp_arena | 25 | struct temp_arena |
| 21 | { | 26 | { |
| 22 | mem_arena *arena; | 27 | mem_arena *arena; |
| 23 | u64 start_position; | 28 | u64 start_position; |
| 24 | }; | 29 | }; |
| 25 | 30 | ||
| 26 | internal mem_arena * | 31 | #endif /* BASE_ARENA_H */ |
| 27 | arena_resize_align( | ||
| 28 | mem_arena *arena, | ||
| 29 | void *old_memory, | ||
| 30 | u64 new_size, | ||
| 31 | u64 old_size, | ||
| 32 | umm alignment); | ||
| 33 | |||
| 34 | internal mem_arena * | ||
| 35 | arena_resize( | ||
| 36 | mem_arena *arena, | ||
| 37 | void *old_memory, | ||
| 38 | u64 new_size, | ||
| 39 | u64 old_size); | ||
| 40 | |||
| 41 | #endif | ||
diff --git a/source/base/base_error.h b/source/base/base_error.h new file mode 100644 index 0000000..e9f52b7 --- /dev/null +++ b/source/base/base_error.h | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | /* base library internal error checking system */ | ||
| 2 | #ifndef BASE_ERROR_H | ||
| 3 | #define BASE_ERROR_H | ||
| 4 | |||
| 5 | #define error_at(msg) \ | ||
| 6 | do { \ | ||
| 7 | write_string(STDERR_FD, Red "[ERROR] " __FILE__ ":"); \ | ||
| 8 | write_int(STDERR_FD, __LINE__); \ | ||
| 9 | write_string(STDERR_FD, ":" __func__ ": " Reset); \ | ||
| 10 | write_string(STDERR_FD, (msg)); \ | ||
| 11 | write_string(STDERR_FD, "\n"); \ | ||
| 12 | } while (0) | ||
| 13 | |||
| 14 | #define warn(msg) \ | ||
| 15 | do { \ | ||
| 16 | write_string(STDERR_FD, Yellow "[WARN] " __FILE__ ":"); \ | ||
| 17 | write_int(STDERR_FD, __LINE__); \ | ||
| 18 | write_string(STDERR_FD, ":" __func__ ": " Reset); \ | ||
| 19 | write_string(STDERR_FD, (msg)); \ | ||
| 20 | write_string(STDERR_FD, "\n"); \ | ||
| 21 | } while (0) | ||
| 22 | |||
| 23 | #define assert_msg(expr, msg) \ | ||
| 24 | do { \ | ||
| 25 | if (!(expr)) { \ | ||
| 26 | write_string(STDERR_FD, Red "[ERROR] " __FILE__ ":"); \ | ||
| 27 | write_int(STDERR_FD, __LINE__); \ | ||
| 28 | write_string(STDERR_FD, ":" __func__ ": " Reset); \ | ||
| 29 | write_string(STDERR_FD, (msg)); \ | ||
| 30 | write_string(STDERR_FD, "\n"); \ | ||
| 31 | _exit(1); \ | ||
| 32 | } \ | ||
| 33 | } while (0) | ||
| 34 | |||
| 35 | #define show \ | ||
| 36 | do { \ | ||
| 37 | write_string(STDOUT_FD, __FILE__ ":"); \ | ||
| 38 | write_int(STDOUT_FD, __LINE__); \ | ||
| 39 | write_string(STDOUT_FD, ":" __func__ "\n"); \ | ||
| 40 | } while (0) | ||
| 41 | |||
| 42 | #define test(expr) \ | ||
| 43 | do { \ | ||
| 44 | if ((expr) != 0) { \ | ||
| 45 | write_string(STDERR_FD, "[FAILED] " __FILE__ ":"); \ | ||
| 46 | write_int(STDERR_FD, __LINE__); \ | ||
| 47 | write_string(STDERR_FD, ":" __func__ "\n"); \ | ||
| 48 | _exit(1); \ | ||
| 49 | } \ | ||
| 50 | } while (0) | ||
| 51 | |||
| 52 | #define verify(expr) \ | ||
| 53 | do { \ | ||
| 54 | if ((expr) != 0) { \ | ||
| 55 | write_string(STDERR_FD, Red "[ERROR] " __FILE__ ":"); \ | ||
| 56 | write_int(STDERR_FD, __LINE__); \ | ||
| 57 | write_string(STDERR_FD, ":" __func__ "\n" Reset); \ | ||
| 58 | _exit(1); \ | ||
| 59 | } else { \ | ||
| 60 | write_string(STDERR_FD, Green "[OK] " __FILE__ ":"); \ | ||
| 61 | write_int(STDERR_FD, __LINE__); \ | ||
| 62 | write_string(STDERR_FD, ":" __func__ "\n" Reset); \ | ||
| 63 | } \ | ||
| 64 | } while (0) | ||
| 65 | |||
| 66 | #endif /* BASE_ERROR_H */ | ||
diff --git a/source/base/base_hash.c b/source/base/base_hash.c deleted file mode 100644 index c309a9a..0000000 --- a/source/base/base_hash.c +++ /dev/null | |||
| @@ -1,18 +0,0 @@ | |||
| 1 | |||
| 2 | internal u64 | ||
| 3 | generate_hash() | ||
| 4 | { | ||
| 5 | // TODO(nasr): | ||
| 6 | return 0; | ||
| 7 | } | ||
| 8 | |||
| 9 | internal hash_map | ||
| 10 | make_hash_map | ||
| 11 | { | ||
| 12 | |||
| 13 | // TODO(nasr): | ||
| 14 | |||
| 15 | return {0}; | ||
| 16 | |||
| 17 | } | ||
| 18 | |||
diff --git a/source/base/base_include.h b/source/base/base_include.h index 3f83fd7..55ef0db 100755 --- a/source/base/base_include.h +++ b/source/base/base_include.h | |||
| @@ -4,27 +4,41 @@ | |||
| 4 | #include <dirent.h> | 4 | #include <dirent.h> |
| 5 | #include <sys/mman.h> | 5 | #include <sys/mman.h> |
| 6 | #include <sys/stat.h> | 6 | #include <sys/stat.h> |
| 7 | #include <sys/syscall.h> | ||
| 7 | #include <fcntl.h> | 8 | #include <fcntl.h> |
| 8 | #include <stdint.h> | 9 | #include <stdint.h> |
| 9 | #include <stddef.h> | 10 | #include <stddef.h> |
| 10 | #include <string.h> | 11 | #include <string.h> |
| 12 | #include <math.h> | ||
| 11 | #include <unistd.h> | 13 | #include <unistd.h> |
| 14 | #include <time.h> | ||
| 12 | 15 | ||
| 13 | #include "base.h" | 16 | #include "base.h" |
| 14 | #include "base_mem.h" | 17 | #include "base_mem.h" |
| 18 | |||
| 15 | #include "base_arena.h" | 19 | #include "base_arena.h" |
| 16 | #include "base_parse.h" | ||
| 17 | #include "base_stack.h" | 20 | #include "base_stack.h" |
| 18 | #include "base_test.h" | ||
| 19 | 21 | ||
| 20 | #if defined(TB_IMPLEMENTATION) | 22 | #include "base_string.h" |
| 23 | #include "base_string.c" | ||
| 21 | 24 | ||
| 22 | #include "base_mem.c" | 25 | #ifdef BASE_PLATFORM |
| 23 | #include "base_arena.c" | 26 | #include <X11/X.h> |
| 24 | #include "base_parse.c" | 27 | #include <X11/Xlib.h> |
| 25 | #include "base_stack.c" | 28 | #include <X11/keysym.h> |
| 26 | #include "base_test.c" | 29 | |
| 30 | #include "base_platform.h" | ||
| 27 | 31 | ||
| 28 | #endif | 32 | #endif |
| 29 | 33 | ||
| 34 | #include "base_error.h" | ||
| 35 | #include "base_os.h" | ||
| 36 | |||
| 37 | #include "base_rand.h" | ||
| 38 | |||
| 39 | #ifdef BASE_IMPLEMENTATION | ||
| 40 | #include "base_arena.c" | ||
| 41 | #endif | ||
| 42 | |||
| 43 | |||
| 30 | #endif | 44 | #endif |
diff --git a/source/base/base_mem.c b/source/base/base_mem.c deleted file mode 100644 index f20ba39..0000000 --- a/source/base/base_mem.c +++ /dev/null | |||
| @@ -1,17 +0,0 @@ | |||
| 1 | internal inline b8 | ||
| 2 | is_pow(umm x) | ||
| 3 | { | ||
| 4 | return (x & (x - 1)) == 0; | ||
| 5 | } | ||
| 6 | |||
| 7 | internal inline u64 | ||
| 8 | align(u64 pointer, umm alignment) | ||
| 9 | { | ||
| 10 | if ((alignment & (alignment - 1)) == 0) | ||
| 11 | { | ||
| 12 | return pointer; | ||
| 13 | } | ||
| 14 | |||
| 15 | return (pointer + alignment - 1) & ~(alignment - 1); | ||
| 16 | } | ||
| 17 | |||
diff --git a/source/base/base_mem.h b/source/base/base_mem.h index 945f0ce..2778fce 100644 --- a/source/base/base_mem.h +++ b/source/base/base_mem.h | |||
| @@ -5,4 +5,22 @@ | |||
| 5 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | 5 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) |
| 6 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | 6 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) |
| 7 | 7 | ||
| 8 | internal inline b8 | ||
| 9 | is_pow(umm x) | ||
| 10 | { | ||
| 11 | return (x & (x - 1)) == 0; | ||
| 12 | } | ||
| 13 | |||
| 14 | internal inline u64 | ||
| 15 | align(u64 pointer, umm alignment) | ||
| 16 | { | ||
| 17 | if ((alignment & (alignment - 1)) == 0) | ||
| 18 | { | ||
| 19 | return pointer; | ||
| 20 | } | ||
| 21 | |||
| 22 | return (pointer + alignment - 1) & ~(alignment - 1); | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 8 | #endif | 26 | #endif |
diff --git a/source/base/base_os.h b/source/base/base_os.h new file mode 100644 index 0000000..b87c946 --- /dev/null +++ b/source/base/base_os.h | |||
| @@ -0,0 +1,132 @@ | |||
| 1 | #ifndef BASE_OS_H | ||
| 2 | #define BASE_OS_H | ||
| 3 | |||
| 4 | #ifdef OS_LINUX | ||
| 5 | |||
| 6 | |||
| 7 | #define STDIN_FD 0 | ||
| 8 | #define STDOUT_FD 1 | ||
| 9 | #define STDERR_FD 2 | ||
| 10 | |||
| 11 | #endif /* BASE_OS_H */ | ||
| 12 | |||
| 13 | #ifdef BASE_IMPLEMENTATION | ||
| 14 | |||
| 15 | internal string8 | ||
| 16 | load_file(mem_arena *arena, const char *path) | ||
| 17 | { | ||
| 18 | string8 result = {0}; | ||
| 19 | struct stat sbuf = {0}; | ||
| 20 | |||
| 21 | // TODO(nasr): abstract this to a platform layer | ||
| 22 | s32 file = open(path, O_RDONLY); | ||
| 23 | if(file == -1) | ||
| 24 | { | ||
| 25 | warn("fialed to open file. path could be invalid"); return (string8){0}; | ||
| 26 | } | ||
| 27 | |||
| 28 | if(fstat(file, &sbuf) == -1) | ||
| 29 | { | ||
| 30 | warn("error: fstat failed"); | ||
| 31 | close(file); | ||
| 32 | return (string8){0}; | ||
| 33 | } | ||
| 34 | |||
| 35 | |||
| 36 | result = PushString8(arena, sbuf.st_size); | ||
| 37 | |||
| 38 | result.size = (u64)sbuf.st_size; | ||
| 39 | if(result.size != 0) | ||
| 40 | { | ||
| 41 | result.data = (u8 *)mmap(0, result.size, PROT_READ, MAP_PRIVATE, file, 0); | ||
| 42 | } | ||
| 43 | |||
| 44 | close(file); | ||
| 45 | return result; | ||
| 46 | } | ||
| 47 | |||
| 48 | internal string8 | ||
| 49 | write_file(const char *path, string8 data) | ||
| 50 | { | ||
| 51 | string8 result = {0}; | ||
| 52 | s32 file = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644); | ||
| 53 | if(file == -1) | ||
| 54 | { | ||
| 55 | warn("failed to open file for writing. path could be invalid"); | ||
| 56 | return (string8){0}; | ||
| 57 | } | ||
| 58 | |||
| 59 | u64 written = 0; | ||
| 60 | while(written < data.size) | ||
| 61 | { | ||
| 62 | s64 err = write(file, data.data + written, data.size - written); | ||
| 63 | if(err == -1) | ||
| 64 | { | ||
| 65 | warn("write syscall failed"); | ||
| 66 | close(file); | ||
| 67 | return (string8){0}; | ||
| 68 | } | ||
| 69 | written += err; | ||
| 70 | } | ||
| 71 | |||
| 72 | close(file); | ||
| 73 | result = data; | ||
| 74 | return result; | ||
| 75 | } | ||
| 76 | |||
| 77 | #define os_write(void const *buf, u64 count) _os_write(STDIN, buf, count) | ||
| 78 | #define os_read(void const *buf, u64 count) _os_read(STDIN, buf, count) | ||
| 79 | |||
| 80 | internal s64 | ||
| 81 | _os_write(s32 fd, void const *buf, u64 count) | ||
| 82 | { | ||
| 83 | return syscall(SYS_write, fd, buf, count); | ||
| 84 | } | ||
| 85 | |||
| 86 | internal s64 | ||
| 87 | _os_read(s32 fd, void *buf, u64 count) | ||
| 88 | { | ||
| 89 | return syscall(SYS_read, fd, buf, count); | ||
| 90 | } | ||
| 91 | |||
| 92 | internal void | ||
| 93 | print_s8(string8 s) | ||
| 94 | { | ||
| 95 | os_write(s.data, s.size); | ||
| 96 | } | ||
| 97 | |||
| 98 | internal void | ||
| 99 | print(const char *str) | ||
| 100 | { | ||
| 101 | s32 len = 0; | ||
| 102 | while (str[len]) len++; | ||
| 103 | os_write(STDOUT_FILENO, str, len); | ||
| 104 | |||
| 105 | } | ||
| 106 | |||
| 107 | internal void | ||
| 108 | write_string(s32 fd, const char *str) | ||
| 109 | { | ||
| 110 | s32 len = 0; | ||
| 111 | while (str[len]) len++; | ||
| 112 | os_write(fd, str, len); | ||
| 113 | } | ||
| 114 | |||
| 115 | internal void | ||
| 116 | write_int(s32 num) | ||
| 117 | { | ||
| 118 | |||
| 119 | if (num < 0) | ||
| 120 | { | ||
| 121 | write(STDERR_FILENO, "-", 1); | ||
| 122 | num = -num; | ||
| 123 | } | ||
| 124 | if (num >= 10) write_int(num / 10); | ||
| 125 | |||
| 126 | char digit = '0' + (num % 10); | ||
| 127 | |||
| 128 | write(STDERR_FILENO, &digit, 1); | ||
| 129 | } | ||
| 130 | |||
| 131 | #endif | ||
| 132 | #endif | ||
diff --git a/source/base/base_parse.c b/source/base/base_parse.c deleted file mode 100755 index 4f216bf..0000000 --- a/source/base/base_parse.c +++ /dev/null | |||
| @@ -1,98 +0,0 @@ | |||
| 1 | /* | ||
| 2 | * is_numeric - Check if a string contains only digits | ||
| 3 | * @s: String to check | ||
| 4 | * | ||
| 5 | * Return: 1 if string contains only numeric characters, 0 otherwise | ||
| 6 | */ | ||
| 7 | internal b8 | ||
| 8 | is_numeric(char *s) | ||
| 9 | { | ||
| 10 | for (; *s; ++s) | ||
| 11 | { | ||
| 12 | if (*s < '0' || *s > '9') | ||
| 13 | { | ||
| 14 | return 0; | ||
| 15 | } | ||
| 16 | } | ||
| 17 | return 1; | ||
| 18 | } | ||
| 19 | |||
| 20 | /* | ||
| 21 | * TODO(nasr): checkout i think there is a buffer overflow happening somewhere | ||
| 22 | * */ | ||
| 23 | internal proc_file * | ||
| 24 | parse_proc_files(char *path, mem_arena *arena) | ||
| 25 | { | ||
| 26 | if (!path || !arena) | ||
| 27 | { | ||
| 28 | return NULL; | ||
| 29 | } | ||
| 30 | |||
| 31 | i32 fd = open(path, O_RDONLY); | ||
| 32 | if (fd < 0) | ||
| 33 | { | ||
| 34 | return NULL; | ||
| 35 | } | ||
| 36 | |||
| 37 | char *buffer = PushArray(arena, char, KiB(4)); | ||
| 38 | u64 bytes = read(fd, buffer, KiB(4)); | ||
| 39 | close(fd); | ||
| 40 | |||
| 41 | if (bytes == 0) | ||
| 42 | { | ||
| 43 | return NULL; | ||
| 44 | } | ||
| 45 | |||
| 46 | /* guessing the count to 256 because i dont want to do a double pass of the buffer */ | ||
| 47 | proc_file *pf = PushStruct(arena, proc_file); | ||
| 48 | pf->entries = PushArray(arena, proc_entry, 256); | ||
| 49 | |||
| 50 | u64 line_start = 0; | ||
| 51 | u64 delim = -1; | ||
| 52 | u64 entry_index = 0; | ||
| 53 | |||
| 54 | for (u64 index = 0; index < bytes; ++index) | ||
| 55 | { | ||
| 56 | if (buffer[index] == ':' && delim == (u64)-1) | ||
| 57 | { | ||
| 58 | delim = index; | ||
| 59 | } | ||
| 60 | else if (buffer[index] == '\n') | ||
| 61 | { | ||
| 62 | if (delim != (-1.f)) | ||
| 63 | { | ||
| 64 | u64 key_len = delim - line_start; | ||
| 65 | if (key_len >= sizeof(pf->entries[entry_index].key)) | ||
| 66 | { | ||
| 67 | key_len = sizeof(pf->entries[entry_index].key) - 1; | ||
| 68 | } | ||
| 69 | |||
| 70 | u64 val_start = delim + 1; | ||
| 71 | while (val_start < index && (buffer[val_start] == ' ' || buffer[val_start] == '\t')) | ||
| 72 | { | ||
| 73 | val_start++; | ||
| 74 | } | ||
| 75 | |||
| 76 | u64 val_len = index - val_start; | ||
| 77 | if (val_len >= sizeof(pf->entries[entry_index].value)) | ||
| 78 | { | ||
| 79 | val_len = sizeof(pf->entries[entry_index].value) - 1; | ||
| 80 | } | ||
| 81 | |||
| 82 | MemCpy(pf->entries[entry_index].key, buffer + line_start, key_len - 1); | ||
| 83 | MemCpy(pf->entries[entry_index].value, buffer + val_start, val_len); | ||
| 84 | |||
| 85 | pf->entries[entry_index].key[key_len] = '\0'; | ||
| 86 | pf->entries[entry_index].value[val_len] = '\0'; | ||
| 87 | |||
| 88 | ++pf->count; | ||
| 89 | ++entry_index; | ||
| 90 | } | ||
| 91 | |||
| 92 | line_start = index + 1; | ||
| 93 | delim = (u64)-1; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | return (pf); | ||
| 98 | } | ||
diff --git a/source/base/base_parse.h b/source/base/base_parse.h deleted file mode 100755 index d66fd04..0000000 --- a/source/base/base_parse.h +++ /dev/null | |||
| @@ -1,28 +0,0 @@ | |||
| 1 | #ifndef BASE_PARSE_H | ||
| 2 | #define BASE_PARSE_H | ||
| 3 | |||
| 4 | #define COMPARE_STRING(c1, c2) compare_string((char *)c1, (char *)c2) | ||
| 5 | |||
| 6 | typedef struct proc_entry proc_entry; | ||
| 7 | typedef struct proc_file proc_file; | ||
| 8 | |||
| 9 | struct proc_file | ||
| 10 | { | ||
| 11 | i32 count; | ||
| 12 | proc_entry *entries; | ||
| 13 | }; | ||
| 14 | |||
| 15 | struct proc_entry | ||
| 16 | { | ||
| 17 | char value[16]; | ||
| 18 | char key[16]; | ||
| 19 | }; | ||
| 20 | |||
| 21 | typedef struct | ||
| 22 | { | ||
| 23 | i8 *start; | ||
| 24 | i8 *end; | ||
| 25 | umm len; | ||
| 26 | } Line; | ||
| 27 | |||
| 28 | #endif | ||
diff --git a/source/platform/platform.c b/source/base/base_platform.h index 2e7bbad..91cd55d 100644 --- a/source/platform/platform.c +++ b/source/base/base_platform.h | |||
| @@ -1,3 +1,7 @@ | |||
| 1 | #ifndef BASE_PLATFORM_H | ||
| 2 | #define BASE_PLATFORM_H | ||
| 3 | |||
| 4 | #ifdef BASE_PLATFORM_IMPLEMENTATION | ||
| 1 | internal inline void | 5 | internal inline void |
| 2 | sleep_ms(long ms) | 6 | sleep_ms(long ms) |
| 3 | { | 7 | { |
| @@ -10,3 +14,5 @@ sleep_ms(long ms) | |||
| 10 | NULL; | 14 | NULL; |
| 11 | } | 15 | } |
| 12 | } | 16 | } |
| 17 | #endif /* BASE_PLATFORM_IMPLEMENTATION */ | ||
| 18 | #endif /* BASE_PLATFORM_H */ | ||
diff --git a/source/base/base_rand.h b/source/base/base_rand.h new file mode 100644 index 0000000..86acafe --- /dev/null +++ b/source/base/base_rand.h | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | #ifndef BASE_RAND_H | ||
| 2 | #define BASE_RAND_H | ||
| 3 | |||
| 4 | // source: https://rosettacode.org/wiki/Pseudo-random_numbers/Splitmix64 | ||
| 5 | |||
| 6 | #define RAND_CONSTANT 6364136223846793005 | ||
| 7 | #define GEN_RAND() _generate_random_u64(RAND_CONSTANT) | ||
| 8 | |||
| 9 | #endif /* BASE_RAND_H */ | ||
| 10 | |||
| 11 | #ifdef BASE_RAND_IMPLEMENTATION | ||
| 12 | internal u64 | ||
| 13 | generate_random_u64(u64 constant) | ||
| 14 | { | ||
| 15 | |||
| 16 | time_t current_time = time(NULL); | ||
| 17 | return current_time ^ constant; | ||
| 18 | |||
| 19 | constant += 0x9e3779b97f4a7c15; | ||
| 20 | |||
| 21 | u64 z = constant; | ||
| 22 | z = (z ^ (z >> 30)) * 0xbf58476d1ce4e5b9; | ||
| 23 | z = (z ^ (z >> 27)) * 0x94d049bb133111eb; | ||
| 24 | z = z ^ (z >> 31); | ||
| 25 | |||
| 26 | return z; | ||
| 27 | } | ||
| 28 | |||
| 29 | #endif | ||
diff --git a/source/base/base_stack.c b/source/base/base_stack.c deleted file mode 100755 index 7cf0570..0000000 --- a/source/base/base_stack.c +++ /dev/null | |||
| @@ -1,187 +0,0 @@ | |||
| 1 | internal mem_stack * | ||
| 2 | stack_create(u64 capacity) | ||
| 3 | { | ||
| 4 | mem_stack *stack = (mem_stack *)mmap( | ||
| 5 | 0, | ||
| 6 | capacity + sizeof(mem_stack), | ||
| 7 | PROT_READ | PROT_WRITE, | ||
| 8 | MAP_SHARED | MAP_ANONYMOUS, | ||
| 9 | -1, | ||
| 10 | 0); | ||
| 11 | |||
| 12 | if (stack == MAP_FAILED) | ||
| 13 | { | ||
| 14 | return NULL; | ||
| 15 | } | ||
| 16 | |||
| 17 | stack->capacity = capacity; | ||
| 18 | stack->base_position = (u8 *)stack + sizeof(mem_stack); | ||
| 19 | stack->current_offset = 0; | ||
| 20 | |||
| 21 | return stack; | ||
| 22 | } | ||
| 23 | |||
| 24 | internal umm | ||
| 25 | calculate_padding(umm pointer, umm alignment, umm header_size) | ||
| 26 | { | ||
| 27 | umm modulo, padding; | ||
| 28 | |||
| 29 | if (!is_pow(alignment)) | ||
| 30 | { | ||
| 31 | return 0; | ||
| 32 | } | ||
| 33 | |||
| 34 | modulo = pointer & (alignment - 1); | ||
| 35 | |||
| 36 | padding = 0; | ||
| 37 | |||
| 38 | if (0 == modulo) | ||
| 39 | { | ||
| 40 | padding = alignment - modulo; | ||
| 41 | } | ||
| 42 | |||
| 43 | if (padding < header_size) | ||
| 44 | { | ||
| 45 | header_size -= padding; | ||
| 46 | |||
| 47 | if ((header_size & (alignment - 1)) != 0) | ||
| 48 | { | ||
| 49 | padding += alignment * (1 + (header_size / alignment)); | ||
| 50 | } | ||
| 51 | else | ||
| 52 | { | ||
| 53 | padding += alignment * (header_size / alignment); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | return padding; | ||
| 58 | } | ||
| 59 | |||
| 60 | internal mem_stack * | ||
| 61 | stack_push_align(mem_stack *stack, u64 size, umm alignment) | ||
| 62 | { | ||
| 63 | umm padding = 0; | ||
| 64 | |||
| 65 | if (!is_pow(alignment)) | ||
| 66 | { | ||
| 67 | return (0); | ||
| 68 | } | ||
| 69 | |||
| 70 | if (alignment > 128) | ||
| 71 | { | ||
| 72 | alignment = 128; | ||
| 73 | } | ||
| 74 | |||
| 75 | umm current_address = (umm)stack->base_position + stack->current_offset; | ||
| 76 | padding = calculate_padding(current_address, alignment, sizeof(mem_stack_header)); | ||
| 77 | |||
| 78 | if (stack->current_offset + padding + size > stack->capacity) | ||
| 79 | { | ||
| 80 | return 0; | ||
| 81 | } | ||
| 82 | |||
| 83 | stack->current_offset += padding; | ||
| 84 | |||
| 85 | umm next_address = current_address + (umm)padding; | ||
| 86 | mem_stack_header *header = (mem_stack_header *)(next_address - sizeof(mem_stack_header)); | ||
| 87 | header->padding = padding; | ||
| 88 | |||
| 89 | stack->current_offset += size; | ||
| 90 | |||
| 91 | return MemSet((void *)next_address, size); | ||
| 92 | } | ||
| 93 | internal void * | ||
| 94 | stack_push(mem_stack *stack, umm size) | ||
| 95 | { | ||
| 96 | return stack_push_align(stack, size, ARENA_ALIGN); | ||
| 97 | } | ||
| 98 | |||
| 99 | internal void | ||
| 100 | stack_pop(mem_stack *stack, void *pointer) | ||
| 101 | { | ||
| 102 | if (pointer != NULL) | ||
| 103 | { | ||
| 104 | umm start, end, current_address; | ||
| 105 | mem_stack_header *header; | ||
| 106 | umm prev_offset; | ||
| 107 | |||
| 108 | start = (umm)stack->base_position; | ||
| 109 | end = start + (umm)stack->capacity; | ||
| 110 | current_address = (umm)pointer; | ||
| 111 | |||
| 112 | if (!(start <= current_address && current_address < end)) | ||
| 113 | { | ||
| 114 | if (0 && "Out of bounds memory address passed to stack allocator (free)") | ||
| 115 | { | ||
| 116 | return; | ||
| 117 | } | ||
| 118 | return; | ||
| 119 | } | ||
| 120 | |||
| 121 | if (current_address >= start + (umm)stack->base_position) | ||
| 122 | { | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | header = (mem_stack_header *)(current_address - sizeof(mem_stack_header)); | ||
| 127 | prev_offset = (size_t)(current_address - (umm)header->padding - start); | ||
| 128 | stack->current_offset = prev_offset; | ||
| 129 | } | ||
| 130 | } | ||
| 131 | |||
| 132 | internal mem_stack * | ||
| 133 | stack_resize_align(mem_stack *stack, void *pointer, u64 old_size, u64 new_size, u64 alignment) | ||
| 134 | { | ||
| 135 | if (pointer == NULL) | ||
| 136 | { | ||
| 137 | return stack_push_align(stack, new_size, alignment); | ||
| 138 | } | ||
| 139 | else if (new_size == 0) | ||
| 140 | { | ||
| 141 | stack_pop(stack, pointer); | ||
| 142 | return NULL; | ||
| 143 | } | ||
| 144 | |||
| 145 | umm start, end, current_address; | ||
| 146 | umm min_size = old_size < new_size ? old_size : new_size; | ||
| 147 | void *new_pointer; | ||
| 148 | |||
| 149 | start = (umm)stack->base_position; | ||
| 150 | end = start + (umm)stack->capacity; | ||
| 151 | current_address = (umm)pointer; | ||
| 152 | if (!(start <= current_address && current_address < end)) | ||
| 153 | { | ||
| 154 | return NULL; | ||
| 155 | } | ||
| 156 | |||
| 157 | if (current_address >= start + (umm)stack->current_offset) | ||
| 158 | { | ||
| 159 | return NULL; | ||
| 160 | } | ||
| 161 | |||
| 162 | if (old_size == new_size) | ||
| 163 | { | ||
| 164 | return pointer; | ||
| 165 | } | ||
| 166 | |||
| 167 | new_pointer = stack_push_align(stack, new_size, alignment); | ||
| 168 | memmove(new_pointer, pointer, min_size); | ||
| 169 | return new_pointer; | ||
| 170 | } | ||
| 171 | |||
| 172 | internal void | ||
| 173 | stack_pop_all(mem_stack *stack) | ||
| 174 | { | ||
| 175 | stack->current_offset = 0; | ||
| 176 | } | ||
| 177 | |||
| 178 | internal void | ||
| 179 | stack_destroy(mem_stack *stack) | ||
| 180 | { | ||
| 181 | if (!stack) | ||
| 182 | { | ||
| 183 | return; | ||
| 184 | } | ||
| 185 | |||
| 186 | munmap(stack, stack->capacity + sizeof(mem_stack)); | ||
| 187 | } | ||
diff --git a/source/base/base_stack.h b/source/base/base_stack.h index fc80b20..43a7230 100755 --- a/source/base/base_stack.h +++ b/source/base/base_stack.h | |||
| @@ -1,20 +1,210 @@ | |||
| 1 | #ifndef STACK_H | 1 | #ifndef STACK_H |
| 2 | #define STACK_H | 2 | #define STACK_H |
| 3 | 3 | ||
| 4 | typedef struct mem_stack mem_stack; | ||
| 5 | typedef struct mem_stack_header mem_stack_header; | 4 | typedef struct mem_stack_header mem_stack_header; |
| 5 | struct mem_stack_header | ||
| 6 | { | ||
| 7 | u8 padding; | ||
| 8 | u8 previous_offset; | ||
| 9 | }; | ||
| 10 | |||
| 6 | 11 | ||
| 12 | typedef struct mem_stack mem_stack; | ||
| 7 | struct mem_stack | 13 | struct mem_stack |
| 8 | { | 14 | { |
| 15 | mem_stack_header *header; | ||
| 16 | |||
| 17 | u64 current_offset; | ||
| 18 | u64 capacity; | ||
| 9 | u8 *base_position; | 19 | u8 *base_position; |
| 10 | umm current_offset; | ||
| 11 | umm capacity; | ||
| 12 | }; | 20 | }; |
| 13 | 21 | ||
| 14 | struct mem_stack_header | 22 | internal mem_stack * |
| 23 | stack_create(u64 capacity) | ||
| 15 | { | 24 | { |
| 16 | u8 padding; | 25 | mem_stack *stack = (mem_stack *)mmap( |
| 17 | u8 previous_offset; | 26 | 0, |
| 18 | }; | 27 | capacity + sizeof(mem_stack), |
| 28 | PROT_READ | PROT_WRITE, | ||
| 29 | MAP_SHARED | MAP_ANONYMOUS, | ||
| 30 | -1, | ||
| 31 | 0); | ||
| 32 | |||
| 33 | if (stack == MAP_FAILED) | ||
| 34 | { | ||
| 35 | return NULL; | ||
| 36 | } | ||
| 37 | |||
| 38 | stack->capacity = capacity; | ||
| 39 | stack->base_position = (u8 *)stack + sizeof(mem_stack); | ||
| 40 | stack->current_offset = 0; | ||
| 41 | |||
| 42 | return stack; | ||
| 43 | } | ||
| 44 | |||
| 45 | internal u8 | ||
| 46 | calculate_padding(u64 pointer, u8 alignment, u64 header_size) | ||
| 47 | { | ||
| 48 | u8 modulo, padding; | ||
| 49 | |||
| 50 | if (!is_pow(alignment)) | ||
| 51 | { | ||
| 52 | return 0; | ||
| 53 | } | ||
| 54 | |||
| 55 | modulo = pointer & (u8)(alignment - 1); | ||
| 56 | |||
| 57 | padding = 0; | ||
| 58 | |||
| 59 | if (0 == modulo) | ||
| 60 | { | ||
| 61 | padding = alignment - modulo; | ||
| 62 | } | ||
| 63 | |||
| 64 | if (padding < header_size) | ||
| 65 | { | ||
| 66 | header_size -= padding; | ||
| 67 | |||
| 68 | if ((header_size & (alignment - 1)) != 0) | ||
| 69 | { | ||
| 70 | padding += alignment * (1 + (header_size / alignment)); | ||
| 71 | } | ||
| 72 | else | ||
| 73 | { | ||
| 74 | padding += alignment * (header_size / alignment); | ||
| 75 | } | ||
| 76 | } | ||
| 77 | |||
| 78 | return padding; | ||
| 79 | } | ||
| 80 | |||
| 81 | internal mem_stack * | ||
| 82 | stack_push_align(mem_stack *stack, u64 size, u8 alignment) | ||
| 83 | { | ||
| 84 | u8 padding = 0; | ||
| 85 | |||
| 86 | if (!is_pow(alignment)) | ||
| 87 | { | ||
| 88 | return (0); | ||
| 89 | } | ||
| 90 | |||
| 91 | if (alignment > 128) | ||
| 92 | { | ||
| 93 | alignment = 128; | ||
| 94 | } | ||
| 95 | |||
| 96 | u64 current_address = (u64)stack->base_position + stack->current_offset; | ||
| 97 | padding = calculate_padding(current_address, alignment, sizeof(mem_stack_header)); | ||
| 98 | |||
| 99 | if (stack->current_offset + padding + size > stack->capacity) | ||
| 100 | { | ||
| 101 | return 0; | ||
| 102 | } | ||
| 103 | |||
| 104 | stack->current_offset += padding; | ||
| 105 | |||
| 106 | u64 next_address = current_address + (u64)padding; | ||
| 107 | mem_stack_header *header = (mem_stack_header *)(next_address - sizeof(mem_stack_header)); | ||
| 108 | header->padding = padding; | ||
| 109 | |||
| 110 | stack->current_offset += size; | ||
| 111 | |||
| 112 | return MemSet((void *)next_address, size); | ||
| 113 | } | ||
| 114 | internal void * | ||
| 115 | stack_push(mem_stack *stack, u64 size) | ||
| 116 | { | ||
| 117 | return stack_push_align(stack, size, ARENA_ALIGN); | ||
| 118 | } | ||
| 119 | |||
| 120 | internal void | ||
| 121 | stack_pop(mem_stack *stack, void *pointer) | ||
| 122 | { | ||
| 123 | if (pointer != NULL) | ||
| 124 | { | ||
| 125 | u64 start, end, current_address; | ||
| 126 | mem_stack_header *header; | ||
| 127 | u64 prev_offset; | ||
| 128 | |||
| 129 | start = (u64)stack->base_position; | ||
| 130 | end = start + (u64)stack->capacity; | ||
| 131 | current_address = (u64)pointer; | ||
| 132 | |||
| 133 | if (!(start <= current_address && current_address < end)) | ||
| 134 | { | ||
| 135 | if (0 && "Out of bounds memory address passed to stack allocator (free)") | ||
| 136 | { | ||
| 137 | return; | ||
| 138 | } | ||
| 139 | return; | ||
| 140 | } | ||
| 141 | |||
| 142 | if (current_address >= start + (u64)stack->base_position) | ||
| 143 | { | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | |||
| 147 | header = (mem_stack_header *)(current_address - sizeof(mem_stack_header)); | ||
| 148 | prev_offset = (size_t)(current_address - (u64)header->padding - start); | ||
| 149 | stack->current_offset = prev_offset; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | internal mem_stack * | ||
| 154 | stack_resize_align(mem_stack *stack, void *pointer, u64 old_size, u64 new_size, u8 alignment) | ||
| 155 | { | ||
| 156 | if (pointer == NULL) | ||
| 157 | { | ||
| 158 | return stack_push_align(stack, new_size, alignment); | ||
| 159 | } | ||
| 160 | else if (new_size == 0) | ||
| 161 | { | ||
| 162 | stack_pop(stack, pointer); | ||
| 163 | return NULL; | ||
| 164 | } | ||
| 165 | |||
| 166 | u64 start, end, current_address; | ||
| 167 | u64 min_size = old_size < new_size ? old_size : new_size; | ||
| 168 | void *new_pointer; | ||
| 169 | |||
| 170 | start = (u64)stack->base_position; | ||
| 171 | end = start + (u64)stack->capacity; | ||
| 172 | current_address = (u64)pointer; | ||
| 173 | if (!(start <= current_address && current_address < end)) | ||
| 174 | { | ||
| 175 | return NULL; | ||
| 176 | } | ||
| 177 | |||
| 178 | if (current_address >= start + (u64)stack->current_offset) | ||
| 179 | { | ||
| 180 | return NULL; | ||
| 181 | } | ||
| 182 | |||
| 183 | if (old_size == new_size) | ||
| 184 | { | ||
| 185 | return pointer; | ||
| 186 | } | ||
| 187 | |||
| 188 | new_pointer = stack_push_align(stack, new_size, alignment); | ||
| 189 | memmove(new_pointer, pointer, min_size); | ||
| 190 | return new_pointer; | ||
| 191 | } | ||
| 192 | |||
| 193 | internal void | ||
| 194 | stack_pop_all(mem_stack *stack) | ||
| 195 | { | ||
| 196 | stack->current_offset = 0; | ||
| 197 | } | ||
| 198 | |||
| 199 | internal void | ||
| 200 | stack_destroy(mem_stack *stack) | ||
| 201 | { | ||
| 202 | if (!stack) | ||
| 203 | { | ||
| 204 | return; | ||
| 205 | } | ||
| 206 | |||
| 207 | munmap(stack, stack->capacity + sizeof(mem_stack)); | ||
| 208 | } | ||
| 19 | 209 | ||
| 20 | #endif | 210 | #endif |
diff --git a/source/base/base_string.c b/source/base/base_string.c index cb2d71d..986fde5 100644 --- a/source/base/base_string.c +++ b/source/base/base_string.c | |||
| @@ -1,73 +1,30 @@ | |||
| 1 | internal umm | 1 | internal b32 |
| 2 | skip_whitespaces(string *buffer) | 2 | is_alpha(u8 point) |
| 3 | { | 3 | { |
| 4 | s32 index = 0; | 4 | return ((point >= 'a' && point <= 'z') || (point >= 'A' && point <= 'Z') || (point == '_')); |
| 5 | while (buffer->size > index) | ||
| 6 | { | ||
| 7 | ++index; | ||
| 8 | } | ||
| 9 | } | 5 | } |
| 10 | 6 | ||
| 11 | internal b8 | 7 | internal b32 |
| 12 | compare_string_struct(string c1, string c2) | 8 | is_digit(u8 point) |
| 13 | { | 9 | { |
| 14 | if (c1.size != c2.size) | 10 | return (point >= '0' && point <= '9'); |
| 15 | { | ||
| 16 | return -1; | ||
| 17 | } | ||
| 18 | |||
| 19 | for (s32 index = 0; | ||
| 20 | index < c1.size; | ||
| 21 | ++index) | ||
| 22 | { | ||
| 23 | if (c1.Data[index] != c2.Data[index]) | ||
| 24 | { | ||
| 25 | return -1; | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | return 0; | ||
| 30 | } | 11 | } |
| 31 | 12 | ||
| 32 | internal b8 | 13 | internal b32 |
| 33 | compare_string(char *c1, char *c2) | 14 | is_alpha_num(u8 point) |
| 34 | { | 15 | { |
| 35 | if (sizeof(*c1) != sizeof(*c2)) | 16 | return (is_alpha(point) || is_digit(point)); |
| 36 | { | 17 | } |
| 37 | return -1; | ||
| 38 | } | ||
| 39 | |||
| 40 | for ( | ||
| 41 | u64 word_idx = 0; | ||
| 42 | word_idx <= sizeof(*c1); | ||
| 43 | ++word_idx) | ||
| 44 | { | ||
| 45 | if (*c1 != *c2) | ||
| 46 | { | ||
| 47 | return -1; | ||
| 48 | } | ||
| 49 | ++c1; | ||
| 50 | ++c2; | ||
| 51 | } | ||
| 52 | 18 | ||
| 53 | return 0; | 19 | internal b32 is_whitespace(u8 point) |
| 20 | { | ||
| 21 | return (point == '\n' || point == '\r' || point == ' ' || point == '\t'); | ||
| 54 | } | 22 | } |
| 55 | 23 | ||
| 56 | /** NOTE(nasr): Helper function to parse strings to int using ascii codes **/ | 24 | internal b32 |
| 57 | internal u64 | 25 | is_slash(u8 point) |
| 58 | parse_u64(char *buf, umm len) | ||
| 59 | { | 26 | { |
| 60 | u64 value = 0; | 27 | return (point == '/' || point == '\\'); |
| 28 | } | ||
| 61 | 29 | ||
| 62 | for (umm buffer_idx = 0; buffer_idx < len; ++buffer_idx) | ||
| 63 | { | ||
| 64 | char c = buf[buffer_idx]; | ||
| 65 | if (c < '0' || c > '9') | ||
| 66 | { | ||
| 67 | break; | ||
| 68 | } | ||
| 69 | value = value * 10 + (c - '0'); | ||
| 70 | } | ||
| 71 | 30 | ||
| 72 | return value; | ||
| 73 | } | ||
diff --git a/source/base/base_string.h b/source/base/base_string.h index d879450..eb51e65 100644 --- a/source/base/base_string.h +++ b/source/base/base_string.h | |||
| @@ -1,17 +1,84 @@ | |||
| 1 | #ifndef BASE_STRING_H | 1 | #ifndef BASE_STRING_H |
| 2 | #define BASE_STRING_H | 2 | #define BASE_STRING_H |
| 3 | 3 | ||
| 4 | // let's use this for strings | 4 | #define PushString8(arena, count) (string8){ .data = (PushArrayZero(arena, u8, (count))), .size = (count) } |
| 5 | // to better differentiate between stuff | 5 | #define PushString16(arena, count) (string16){ .data = (PushArrayZero(arena, u16, (count))), .size = (count) } |
| 6 | typedef u8 s8; | 6 | #define PushString32(arena, count) (string32){ .data = (PushArrayZero(arena, u32, (count))), .size = (count) } |
| 7 | typedef struct string string ; | ||
| 8 | 7 | ||
| 9 | struct string | 8 | #define String8(data, size) (string8){(u8 *)(data), (u64)(size) } |
| 9 | #define String16(data, size) (string16){(u16 *)(data), (u64)(size) } | ||
| 10 | #define String32(data, size) (string32){(u32 *)(data), (u64)(size) } | ||
| 11 | |||
| 12 | |||
| 13 | |||
| 14 | #define StringFmt "%.*s" | ||
| 15 | #define ULongFmt "%lu" | ||
| 16 | #define ULLongFmt "%llu" | ||
| 17 | |||
| 18 | typedef struct string8 string8; | ||
| 19 | struct string8 | ||
| 20 | { | ||
| 21 | u8 *data; | ||
| 22 | u64 size; | ||
| 23 | }; | ||
| 24 | |||
| 25 | typedef struct string16 string16; | ||
| 26 | struct string16 | ||
| 27 | { | ||
| 28 | u16 *data; | ||
| 29 | u64 size; | ||
| 30 | }; | ||
| 31 | |||
| 32 | typedef struct string32 string32; | ||
| 33 | struct string32 | ||
| 34 | { | ||
| 35 | u32 *data; | ||
| 36 | u64 size; | ||
| 37 | }; | ||
| 38 | |||
| 39 | //- string linked list implementation | ||
| 40 | typedef struct string8_node string8_node; | ||
| 41 | struct string8_node | ||
| 10 | { | 42 | { |
| 11 | s8 *data; | 43 | string8 *next; |
| 12 | umm size; | 44 | string8 string; |
| 13 | }; | 45 | }; |
| 14 | 46 | ||
| 15 | // TODO(nasr): helper functions for string | 47 | typedef struct string8_list string8_list; |
| 48 | struct string8_list | ||
| 49 | { | ||
| 50 | string8 *first; | ||
| 51 | string8 *last; | ||
| 52 | u64 count; | ||
| 53 | }; | ||
| 54 | |||
| 55 | typedef struct string16_list string16_list; | ||
| 56 | struct string16_list | ||
| 57 | { | ||
| 58 | string16 *next; | ||
| 59 | string16 string; | ||
| 60 | }; | ||
| 61 | |||
| 62 | typedef struct string32_list string32_list; | ||
| 63 | struct string32_list | ||
| 64 | { | ||
| 65 | string32 *first; | ||
| 66 | string32 *last; | ||
| 67 | u64 count; | ||
| 68 | }; | ||
| 69 | |||
| 70 | internal b8 | ||
| 71 | string8_cmp(string8 a, string8 b) | ||
| 72 | { | ||
| 73 | if (a.size != b.size) return 0; | ||
| 74 | return (b8)(memcmp(a.data, b.data, a.size) == 0); | ||
| 75 | } | ||
| 76 | |||
| 77 | internal void | ||
| 78 | string8_appendc(string8 *buf, u8 c) | ||
| 79 | { | ||
| 80 | buf->data[buf->size] = c; | ||
| 81 | buf->size += 1; | ||
| 82 | } | ||
| 16 | 83 | ||
| 17 | #endif /* BASE_STRING_H */ | 84 | #endif /* BASE_STRING_H */ |
diff --git a/source/base/base_test.c b/source/base/base_test.c deleted file mode 100644 index e44aa69..0000000 --- a/source/base/base_test.c +++ /dev/null | |||
| @@ -1,14 +0,0 @@ | |||
| 1 | void | ||
| 2 | write_int(i32 num) | ||
| 3 | { | ||
| 4 | if (num < 0) | ||
| 5 | { | ||
| 6 | write(STDERR_FILENO, "-", 1); | ||
| 7 | num = -num; | ||
| 8 | } | ||
| 9 | if (num >= 10) | ||
| 10 | write_int(num / 10); | ||
| 11 | char digit = '0' + (num % 10); | ||
| 12 | |||
| 13 | write(STDERR_FILENO, &digit, 1); | ||
| 14 | } | ||
diff --git a/source/base/base_test.h b/source/base/base_test.h deleted file mode 100644 index 11028f4..0000000 --- a/source/base/base_test.h +++ /dev/null | |||
| @@ -1,61 +0,0 @@ | |||
| 1 | #ifndef BASE_TEST_H | ||
| 2 | #define BASE_TEST_H | ||
| 3 | |||
| 4 | void | ||
| 5 | write_int(i32 num); | ||
| 6 | |||
| 7 | #define RED "\x1b[31m" | ||
| 8 | #define GREEN "\x1b[32m" | ||
| 9 | #define RESET "\x1b[0m" | ||
| 10 | #define BLUE "\x1b[34m" | ||
| 11 | |||
| 12 | #define LEN(s) (sizeof(s) - 1) | ||
| 13 | |||
| 14 | #define show \ | ||
| 15 | do \ | ||
| 16 | { \ | ||
| 17 | write(STDOUT_FILENO, __FILE__, sizeof(__FILE__) - 1); \ | ||
| 18 | write(STDOUT_FILENO, ":", 1); \ | ||
| 19 | write(STDOUT_FILENO, __func__, sizeof(__func__) - 1); \ | ||
| 20 | write(STDOUT_FILENO, ":", 1); \ | ||
| 21 | write_int(__LINE__); \ | ||
| 22 | write(STDOUT_FILENO, "\n", 1); \ | ||
| 23 | } while (0) | ||
| 24 | |||
| 25 | #define test(expr) \ | ||
| 26 | { \ | ||
| 27 | if ((expr) != 0) \ | ||
| 28 | { \ | ||
| 29 | write(STDERR_FILENO, "[FAILED] ", LEN("[FAILED] ")); \ | ||
| 30 | show; \ | ||
| 31 | _exit(1); \ | ||
| 32 | } \ | ||
| 33 | } | ||
| 34 | |||
| 35 | #define check(expr) \ | ||
| 36 | { \ | ||
| 37 | if ((expr) != 0) \ | ||
| 38 | { \ | ||
| 39 | write(STDERR_FILENO, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ | ||
| 40 | show; \ | ||
| 41 | write(STDERR_FILENO, RESET, LEN(RESET)); \ | ||
| 42 | _exit(1); \ | ||
| 43 | } \ | ||
| 44 | else \ | ||
| 45 | { \ | ||
| 46 | write(STDERR_FILENO, GREEN "[SUCCESS] ", LEN(GREEN "[SUCCESS] ")); \ | ||
| 47 | show; \ | ||
| 48 | write(STDERR_FILENO, RESET, LEN(RESET)); \ | ||
| 49 | } \ | ||
| 50 | } | ||
| 51 | |||
| 52 | #define checkpoint_output "<<CHECKPOINT>>\n" | ||
| 53 | #define checkpoint_end_output "^^^^^^^^^^^^^^\n\n\n" | ||
| 54 | #define checkpoint \ | ||
| 55 | { \ | ||
| 56 | write(STDERR_FILENO, BLUE checkpoint_output, LEN(BLUE checkpoint_output)); \ | ||
| 57 | show; \ | ||
| 58 | write(STDERR_FILENO, BLUE checkpoint_end_output, LEN(BLUE checkpoint_end_output)); \ | ||
| 59 | } | ||
| 60 | |||
| 61 | #endif /* BASE_TEST_H */ | ||
diff --git a/source/base/bash_hash.h b/source/base/bash_hash.h index 2c286a2..758ef72 100644 --- a/source/base/bash_hash.h +++ b/source/base/bash_hash.h | |||
| @@ -1,15 +1,24 @@ | |||
| 1 | ##ifndef HEADER_H | 1 | #ifndef BASH_HASH_H |
| 2 | #define HEADER_H | 2 | #define BASH_HASH_H |
| 3 | 3 | ||
| 4 | typedef struct hash_map hash_map; | 4 | typedef struct hash_map hash_map; |
| 5 | typedef struct hash hash; | 5 | typedef struct hash hash; |
| 6 | 6 | ||
| 7 | 7 | struct map | |
| 8 | struct hash_map | ||
| 9 | { | 8 | { |
| 9 | string8 | ||
| 10 | u64 capacity | ||
| 11 | }; | ||
| 10 | 12 | ||
| 11 | 13 | ||
| 14 | struct index | ||
| 15 | { | ||
| 16 | string8 key; | ||
| 17 | string8 value; | ||
| 12 | }; | 18 | }; |
| 13 | 19 | ||
| 14 | 20 | ||
| 15 | #endif /* HEADER_H */ | 21 | |
| 22 | internal void | ||
| 23 | |||
| 24 | #endif /* BASH_HASH_H */ | ||
diff --git a/source/core/core.c b/source/core/core.c deleted file mode 100644 index f9d062a..0000000 --- a/source/core/core.c +++ /dev/null | |||
| @@ -1,136 +0,0 @@ | |||
| 1 | #define TB_IMPLEMENTATION | ||
| 2 | #include "core.h" | ||
| 3 | #include "../base/base_include.h" | ||
| 4 | #include "../platform/platform_include.h" | ||
| 5 | |||
| 6 | int main() | ||
| 7 | { | ||
| 8 | b32 running = 1; | ||
| 9 | |||
| 10 | Display *MainDisplay = XOpenDisplay(0); | ||
| 11 | mem_arena *arena = arena_create(MiB(8)); | ||
| 12 | |||
| 13 | Window root = XDefaultRootWindow(MainDisplay); | ||
| 14 | int screen = DefaultScreen(MainDisplay); | ||
| 15 | |||
| 16 | Visual *v = DefaultVisual(MainDisplay, screen); | ||
| 17 | |||
| 18 | XSetWindowAttributes wa = { | ||
| 19 | .background_pixmap = None, | ||
| 20 | .background_pixel = BlackPixel(MainDisplay, DefaultScreen(MainDisplay)), | ||
| 21 | .border_pixmap = CopyFromParent, | ||
| 22 | .border_pixel = 0, | ||
| 23 | .bit_gravity = ForgetGravity, | ||
| 24 | .win_gravity = NorthWestGravity, | ||
| 25 | .backing_store = NotUseful, | ||
| 26 | .backing_planes = 1, | ||
| 27 | .backing_pixel = 0, | ||
| 28 | .save_under = False, | ||
| 29 | .event_mask = 0, | ||
| 30 | .do_not_propagate_mask = 0, | ||
| 31 | .override_redirect = False, | ||
| 32 | .colormap = CopyFromParent, | ||
| 33 | .cursor = None | ||
| 34 | }; | ||
| 35 | |||
| 36 | i32 dp_heigth = DisplayHeight(MainDisplay, screen); | ||
| 37 | i32 dp_width = DisplayWidth(MainDisplay, screen); | ||
| 38 | |||
| 39 | WindowProperties p = { | ||
| 40 | |||
| 41 | .x = dp_width / 2, | ||
| 42 | .y = dp_heigth / 2, | ||
| 43 | .height = (u32)800, | ||
| 44 | .width = (u32)1200, | ||
| 45 | .border_width = 0, | ||
| 46 | .window_depth = CopyFromParent, | ||
| 47 | .window_class = CopyFromParent, | ||
| 48 | .value_mask = CWBackPixel, | ||
| 49 | |||
| 50 | }; | ||
| 51 | |||
| 52 | Window window = | ||
| 53 | XCreateWindow( | ||
| 54 | MainDisplay, | ||
| 55 | root, | ||
| 56 | p.x, | ||
| 57 | p.y, | ||
| 58 | p.width, | ||
| 59 | p.height, | ||
| 60 | p.border_width, | ||
| 61 | p.window_depth, | ||
| 62 | p.window_class, | ||
| 63 | v, | ||
| 64 | p.value_mask, | ||
| 65 | &wa); | ||
| 66 | |||
| 67 | XSetWindowBorder(MainDisplay, window, 60); | ||
| 68 | XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask | KeyReleaseMask | KeyPressMask); | ||
| 69 | XMapWindow(MainDisplay, window); | ||
| 70 | XEvent event; | ||
| 71 | |||
| 72 | f32 DELTA = 2.5; | ||
| 73 | |||
| 74 | u32 rect_width = 50; | ||
| 75 | u32 rect_height = 50; | ||
| 76 | |||
| 77 | i32 rect_x_position = p.width / 2; | ||
| 78 | i32 rect_y_position = p.height / 2; | ||
| 79 | |||
| 80 | i32 rect_enemy_x_position = (p.width / 2) + 300; | ||
| 81 | i32 rect_enemy_y_position = (p.height / 2) + 300; | ||
| 82 | |||
| 83 | u64 color = 0x0FF0FF00; | ||
| 84 | |||
| 85 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); | ||
| 86 | XSetForeground(MainDisplay, gc, color); | ||
| 87 | |||
| 88 | for (;running;) | ||
| 89 | { | ||
| 90 | //- handle enemy movement | ||
| 91 | { | ||
| 92 | if(rect_enemy_x_position < rect_x_position) rect_enemy_x_position += DELTA*0.5; | ||
| 93 | if(rect_enemy_y_position < rect_y_position) rect_enemy_y_position += DELTA*0.5; | ||
| 94 | |||
| 95 | if(rect_enemy_x_position > rect_x_position) rect_enemy_x_position -= DELTA*0.5; | ||
| 96 | if(rect_enemy_y_position > rect_y_position) rect_enemy_y_position -= DELTA*0.5; | ||
| 97 | } | ||
| 98 | |||
| 99 | XNextEvent(MainDisplay, &event); | ||
| 100 | |||
| 101 | switch (event.type) | ||
| 102 | { | ||
| 103 | case(KeyPress): | ||
| 104 | { | ||
| 105 | |||
| 106 | KeySym keysym = XLookupKeysym(&event.xkey, 0); | ||
| 107 | //- handle user movement | ||
| 108 | { | ||
| 109 | if(keysym == XK_h) rect_x_position -= DELTA*1.2; | ||
| 110 | else if(keysym == XK_l) rect_x_position += DELTA*1.2; | ||
| 111 | else if(keysym == XK_k) rect_y_position -= DELTA*1.2; | ||
| 112 | else if(keysym == XK_j) rect_y_position += DELTA*1.2; | ||
| 113 | else if(keysym == XK_s); | ||
| 114 | else if(keysym == XK_Escape || keysym == XK_q) goto exit; | ||
| 115 | } | ||
| 116 | |||
| 117 | //- draw entities | ||
| 118 | { | ||
| 119 | XClearWindow(MainDisplay, window); | ||
| 120 | |||
| 121 | //-- | ||
| 122 | XDrawRectangle(MainDisplay, window, gc, rect_x_position, rect_y_position, 50, 50); | ||
| 123 | XFillRectangle(MainDisplay, window, gc, rect_x_position, rect_y_position, 50, 50); | ||
| 124 | |||
| 125 | //-- | ||
| 126 | XDrawRectangle(MainDisplay, window, gc, rect_enemy_x_position, rect_y_position, 50, 50); | ||
| 127 | XFillRectangle(MainDisplay, window, gc, rect_enemy_x_position, rect_y_position, 50, 50); | ||
| 128 | } break; | ||
| 129 | } | ||
| 130 | default: | ||
| 131 | } | ||
| 132 | } | ||
| 133 | exit: | ||
| 134 | arena_clear(arena); | ||
| 135 | return 0; | ||
| 136 | } | ||
diff --git a/source/core/core.h b/source/core/core.h deleted file mode 100644 index c5821c5..0000000 --- a/source/core/core.h +++ /dev/null | |||
| @@ -1,5 +0,0 @@ | |||
| 1 | #ifndef CORE_H | ||
| 2 | #define CORE_H | ||
| 3 | |||
| 4 | |||
| 5 | #endif | ||
diff --git a/source/platform/platform.h b/source/platform/platform.h deleted file mode 100644 index da8e065..0000000 --- a/source/platform/platform.h +++ /dev/null | |||
| @@ -1,56 +0,0 @@ | |||
| 1 | #ifndef PLATFORM_H | ||
| 2 | #define PLATFORM_H | ||
| 3 | |||
| 4 | #define NIL 0 | ||
| 5 | |||
| 6 | #include <X11/X.h> | ||
| 7 | #include <X11/Xlib.h> | ||
| 8 | #include <X11/keysym.h> | ||
| 9 | #include <time.h> | ||
| 10 | #include <unistd.h> | ||
| 11 | |||
| 12 | typedef struct WindowProperties WindowProperties; | ||
| 13 | struct WindowProperties | ||
| 14 | { | ||
| 15 | i32 x; | ||
| 16 | i32 y; | ||
| 17 | u32 height; | ||
| 18 | u32 width; | ||
| 19 | u32 border_width; | ||
| 20 | i32 window_depth; | ||
| 21 | u32 window_class; | ||
| 22 | u64 value_mask; | ||
| 23 | |||
| 24 | }; | ||
| 25 | |||
| 26 | typedef struct vertex vertex; | ||
| 27 | |||
| 28 | struct vertex | ||
| 29 | { | ||
| 30 | i32 x; | ||
| 31 | i32 y; | ||
| 32 | i32 z; | ||
| 33 | }; | ||
| 34 | |||
| 35 | typedef struct display_pos display_pos; | ||
| 36 | |||
| 37 | struct display_pos | ||
| 38 | { | ||
| 39 | i32 x; | ||
| 40 | i32 y; | ||
| 41 | |||
| 42 | }; | ||
| 43 | |||
| 44 | typedef struct pos pos; | ||
| 45 | struct pos | ||
| 46 | { | ||
| 47 | i32 x; | ||
| 48 | i32 y; | ||
| 49 | i32 z; | ||
| 50 | |||
| 51 | } ; | ||
| 52 | |||
| 53 | |||
| 54 | |||
| 55 | |||
| 56 | #endif /* PLATFORM_H */ | ||
diff --git a/source/platform/platform_include.h b/source/platform/platform_include.h deleted file mode 100644 index 886c6b8..0000000 --- a/source/platform/platform_include.h +++ /dev/null | |||
| @@ -1,7 +0,0 @@ | |||
| 1 | #ifndef PLATFORM_INCLUDE_H | ||
| 2 | #define PLATFORM_INCLUDE_H | ||
| 3 | |||
| 4 | #include "platform.h" | ||
| 5 | #include "platform.c" | ||
| 6 | |||
| 7 | #endif /* PLATFORM_INCLUDE_H */ | ||
diff --git a/source/tb/tb.c b/source/tb/tb.c new file mode 100644 index 0000000..ed78dd2 --- /dev/null +++ b/source/tb/tb.c | |||
| @@ -0,0 +1,183 @@ | |||
| 1 | #define BASE_IMPLEMENTATION | ||
| 2 | #define BASE_RAND_IMPLEMENTATION | ||
| 3 | #define BASE_MATH_IMPLEMENTATION | ||
| 4 | #define BASE_PLATFORM | ||
| 5 | #define BASE_PLATFORM_IMPLEMENTATION | ||
| 6 | #include "../base/base_include.h" | ||
| 7 | |||
| 8 | #define WINDOW_WIDTH 800 | ||
| 9 | #define WINDOW_HEIGHT 1200 | ||
| 10 | |||
| 11 | internal void | ||
| 12 | load_entities(Display *MainDisplay, Window *window, GC *gc, s32 entity_count) | ||
| 13 | { | ||
| 14 | s32 x = (s32)generate_random_u64(RAND_CONSTANT); | ||
| 15 | s32 y = (s32)generate_random_u64(RAND_CONSTANT); | ||
| 16 | |||
| 17 | for (s32 index = 0; index < entity_count; ++index) | ||
| 18 | { | ||
| 19 | s32 delta = (s32)generate_random_u64(RAND_CONSTANT); | ||
| 20 | |||
| 21 | XDrawRectangle(MainDisplay, | ||
| 22 | *window, | ||
| 23 | *gc, | ||
| 24 | x * (1/delta), | ||
| 25 | y * (1/delta), | ||
| 26 | 50, | ||
| 27 | 50); | ||
| 28 | |||
| 29 | XFillRectangle(MainDisplay, | ||
| 30 | *window, | ||
| 31 | *gc, | ||
| 32 | x * (1/delta), | ||
| 33 | y * (1/delta), | ||
| 34 | 50, | ||
| 35 | 50); | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | |||
| 40 | internal void | ||
| 41 | load_user(Display *MainDisplay, Window *window, GC *gc, s32 x, s32 y, u32 width, u32 height) | ||
| 42 | { | ||
| 43 | XDrawRectangle(MainDisplay, *window, *gc, x, y, width, height); | ||
| 44 | XFillRectangle(MainDisplay, *window, *gc, x, y, width, height); | ||
| 45 | } | ||
| 46 | |||
| 47 | |||
| 48 | int main() | ||
| 49 | { | ||
| 50 | b32 running = 1; | ||
| 51 | |||
| 52 | Display *MainDisplay = XOpenDisplay(0); | ||
| 53 | mem_arena *arena = arena_create(MiB(8)); | ||
| 54 | |||
| 55 | Window root = XDefaultRootWindow(MainDisplay); | ||
| 56 | int screen = DefaultScreen(MainDisplay); | ||
| 57 | |||
| 58 | Visual *v = DefaultVisual(MainDisplay, screen); | ||
| 59 | |||
| 60 | |||
| 61 | XSetWindowAttributes wa = { | ||
| 62 | .background_pixmap = None, | ||
| 63 | .background_pixel = BlackPixel(MainDisplay, DefaultScreen(MainDisplay)), | ||
| 64 | .border_pixmap = CopyFromParent, | ||
| 65 | .border_pixel = 0, | ||
| 66 | .bit_gravity = ForgetGravity, | ||
| 67 | .win_gravity = NorthWestGravity, | ||
| 68 | .backing_store = NotUseful, | ||
| 69 | .backing_planes = 1, | ||
| 70 | .backing_pixel = 0, | ||
| 71 | .save_under = False, | ||
| 72 | .event_mask = 0, | ||
| 73 | .do_not_propagate_mask = 0, | ||
| 74 | .override_redirect = False, | ||
| 75 | .colormap = CopyFromParent, | ||
| 76 | .cursor = None | ||
| 77 | }; | ||
| 78 | |||
| 79 | s32 dp_heigth = DisplayHeight(MainDisplay, screen); | ||
| 80 | s32 dp_width = DisplayWidth(MainDisplay, screen); | ||
| 81 | |||
| 82 | |||
| 83 | WindowProperties p = { | ||
| 84 | |||
| 85 | .x = | ||
| 86 | .y = | ||
| 87 | .height = | ||
| 88 | .width = | ||
| 89 | .border_width = 0, | ||
| 90 | .window_depth = CopyFromParent, | ||
| 91 | .window_class = | ||
| 92 | .value_mask = | ||
| 93 | |||
| 94 | }; | ||
| 95 | |||
| 96 | Window window = XCreateWindow( MainDisplay, root, dp_width / 2, dp_heigth / 2, | ||
| 97 | (u32)WINDOW_WIDTH, (u32)WINDOW_HEIGHT, 0, CopyFromParent, v, CWBackPixel, &wa); | ||
| 98 | |||
| 99 | XSetWindowBorder(MainDisplay, window, 60); | ||
| 100 | XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask | KeyReleaseMask | KeyPressMask); | ||
| 101 | XMapWindow(MainDisplay, window); | ||
| 102 | XEvent event; | ||
| 103 | |||
| 104 | f32 DELTA = 2.5; | ||
| 105 | |||
| 106 | u32 rect_width = 50; | ||
| 107 | u32 rect_height = 50; | ||
| 108 | |||
| 109 | s32 rect_x_position = p.width / 2; | ||
| 110 | s32 rect_y_position = p.height / 2; | ||
| 111 | |||
| 112 | s32 rect_enemy_x_position = (p.width / 2) + 300; | ||
| 113 | s32 rect_enemy_y_position = (p.height / 2) + 300; | ||
| 114 | |||
| 115 | u64 color = 0x0FF0FF00; | ||
| 116 | |||
| 117 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); | ||
| 118 | XSetForeground(MainDisplay, gc, color); | ||
| 119 | |||
| 120 | for (;running;) | ||
| 121 | { | ||
| 122 | //- handle collision detection | ||
| 123 | { | ||
| 124 | if(rect_y_position >= WINDOW_HEIGHT) DELTA = -DELTA; | ||
| 125 | if(rect_x_position >= WINDOW_WIDTH) DELTA = -DELTA; | ||
| 126 | |||
| 127 | if(rect_enemy_y_position >= WINDOW_HEIGHT) DELTA = -DELTA; | ||
| 128 | if(rect_enemy_x_position >= WINDOW_WIDTH) DELTA = -DELTA; | ||
| 129 | } | ||
| 130 | |||
| 131 | //- handle enemy movement | ||
| 132 | { | ||
| 133 | s32 dy = rect_y_position - rect_enemy_y_position; | ||
| 134 | s32 dx = rect_x_position - rect_enemy_x_position; | ||
| 135 | |||
| 136 | //- normalize the distance | why this is needed? no clue | ||
| 137 | f32 distance = sqrtf(dx*dx + dy*dy); | ||
| 138 | |||
| 139 | if(distance > 1.0f) { | ||
| 140 | f32 speed = DELTA * 0.5f; | ||
| 141 | rect_enemy_x_position += (s32)((dx / distance) * speed); | ||
| 142 | rect_enemy_y_position += (s32)((dy / distance) * speed); | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | XNextEvent(MainDisplay, &event); | ||
| 147 | |||
| 148 | switch (event.type) | ||
| 149 | { | ||
| 150 | case(KeyPress): | ||
| 151 | { | ||
| 152 | KeySym keysym = XLookupKeysym(&event.xkey, 0); | ||
| 153 | //- handle user movement | ||
| 154 | { | ||
| 155 | if(keysym == XK_h) rect_x_position -= DELTA*1.5; | ||
| 156 | else if(keysym == XK_l) rect_x_position += DELTA*1.5; | ||
| 157 | else if(keysym == XK_k) rect_y_position -= DELTA*1.5; | ||
| 158 | else if(keysym == XK_j) rect_y_position += DELTA*1.5; | ||
| 159 | else if(keysym == XK_s); | ||
| 160 | else if(keysym == XK_Escape || keysym == XK_q) goto exit; | ||
| 161 | } | ||
| 162 | |||
| 163 | |||
| 164 | // clear screen before drawing entities | ||
| 165 | |||
| 166 | XClearWindow(MainDisplay, window); | ||
| 167 | |||
| 168 | //- draw entities | ||
| 169 | { | ||
| 170 | } break; | ||
| 171 | } | ||
| 172 | default: | ||
| 173 | { | ||
| 174 | |||
| 175 | } | ||
| 176 | } | ||
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | exit: | ||
| 181 | arena_destroy(arena); | ||
| 182 | return 0; | ||
| 183 | } | ||
diff --git a/todo.db b/todo.db deleted file mode 100644 index e1abef7..0000000 --- a/todo.db +++ /dev/null | |||
| Binary files differ | |||
