From 444bfa2f41143aff7490e4fa21565947565b7d30 Mon Sep 17 00:00:00 2001 From: nasr Date: Fri, 13 Mar 2026 22:31:21 +0100 Subject: cleanup: generalisation --- source/base/base.h | 77 +++++++++++++++ source/base/base_arena.c | 142 ++++++++++++++++++++++++++++ source/base/base_arena.h | 41 ++++++++ source/base/base_hash.c | 18 ++++ source/base/base_include.h | 30 ++++++ source/base/base_mem.c | 17 ++++ source/base/base_mem.h | 8 ++ source/base/base_parse.c | 98 +++++++++++++++++++ source/base/base_parse.h | 28 ++++++ source/base/base_stack.c | 187 +++++++++++++++++++++++++++++++++++++ source/base/base_stack.h | 20 ++++ source/base/base_string.c | 73 +++++++++++++++ source/base/base_string.h | 17 ++++ source/base/base_test.c | 14 +++ source/base/base_test.h | 61 ++++++++++++ source/base/bash_hash.h | 15 +++ source/core/core.c | 126 +++++++++++++++++++++++++ source/core/core.h | 5 + source/platform/platform.c | 12 +++ source/platform/platform.h | 56 +++++++++++ source/platform/platform_include.h | 7 ++ 21 files changed, 1052 insertions(+) create mode 100755 source/base/base.h create mode 100755 source/base/base_arena.c create mode 100755 source/base/base_arena.h create mode 100644 source/base/base_hash.c create mode 100755 source/base/base_include.h create mode 100644 source/base/base_mem.c create mode 100644 source/base/base_mem.h create mode 100755 source/base/base_parse.c create mode 100755 source/base/base_parse.h create mode 100755 source/base/base_stack.c create mode 100755 source/base/base_stack.h create mode 100644 source/base/base_string.c create mode 100644 source/base/base_string.h create mode 100644 source/base/base_test.c create mode 100644 source/base/base_test.h create mode 100644 source/base/bash_hash.h create mode 100644 source/core/core.c create mode 100644 source/core/core.h create mode 100644 source/platform/platform.c create mode 100644 source/platform/platform.h create mode 100644 source/platform/platform_include.h (limited to 'source') diff --git a/source/base/base.h b/source/base/base.h new file mode 100755 index 0000000..5fcf9e2 --- /dev/null +++ b/source/base/base.h @@ -0,0 +1,77 @@ +#ifndef BASE_H +#define BASE_H + +#include +#include +#include +#include + +/* assert an expression and output the file and the line */ + +#define internal static +#define global_variable static +#define local_persist static + +#define ERR_OK 0 +#define ERR_IO 1 +#define ERR_PARSE 2 +#define ERR_PERM 3 +#define ERR_INVALID 4 + +#define KiB(n) (((u64)(n)) << 10) +#define MiB(n) (((u64)(n)) << 20) +#define GiB(n) (((u64)(n)) << 30) + +#define unused(x) (void)(x) + +#define PATH_MAX_LEN 128 +#define BUFF_SMALL 128 +#define BUFF_DEFAULT 256 +#define BUFF_LARGE 512 + +#define NIL 0 + +#define DEPRECATED __attribute__((__deprecated__)) + +#if defined(__arm__) || defined(__aarch64__) +#define breakpoint __asm__ volatile("brk #0"); +#define temp_breakpoint __asm__ volatile("udf #0"); +#elif defined(__i386__) || defined(__x86_64__) +#define breakpoint __asm__ volatile("int3"); +#endif + +#define MemCpy(dest, src, len) memcpy((dest), (src), (len)) +#define MemSet(dest, len) memset((dest), (0), (len)) + +typedef uint64_t u64; +typedef uint32_t u32; +typedef uint16_t u16; +typedef uint8_t u8; + +typedef int8_t i8; +typedef int16_t i16; +typedef int32_t i32; +typedef int64_t i64; + +typedef float f32; +typedef double f64; + +typedef i32 b32; +typedef i16 b16; +typedef u8 b8; + +typedef uintptr_t umm; +typedef intptr_t smm; + +#define TRUE (0 == 0) +#define FALSE (0 != 0) + +typedef struct s8 s8; + +struct s8 +{ + char *data; + umm size; +}; + +#endif diff --git a/source/base/base_arena.c b/source/base/base_arena.c new file mode 100755 index 0000000..76938bd --- /dev/null +++ b/source/base/base_arena.c @@ -0,0 +1,142 @@ +internal mem_arena * +arena_create(u64 capacity) +{ + mem_arena *arena = (mem_arena *)mmap( + /* kernel decides where to throw the arena */ + NULL, + capacity + sizeof(mem_arena), + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, + -1, + 0); + + if (arena == MAP_FAILED) + { + return NULL; + } + + arena->capacity = capacity; + arena->base_position = (u8 *)arena + sizeof(mem_arena); + arena->current_position = 0; + arena->previous_position = 0; + + return arena; +} + +internal void +arena_destroy(mem_arena *arena) +{ + if (!arena) + { + return; + } + + int code = munmap(arena, arena->capacity + sizeof(mem_arena)); +} + +internal void * +arena_alloc(mem_arena *arena, u64 size) +{ + if (!arena) + { + return NULL; + } + + u64 aligned = Align(arena->current_position, ARENA_ALIGN); + u64 new_pos = aligned + size; + + if (new_pos > arena->capacity) + { + return NULL; + } + + void *out = arena->base_position + aligned; + + arena->previous_position = arena->current_position; + arena->current_position = aligned + size; + + MemSet(out, size); + + return out; +} + +internal void +arena_pop(mem_arena *arena, u64 size) +{ + size = MIN(size, arena->current_position); + arena->current_position -= size; +} + +internal void +arena_pop_to(mem_arena *arena, u64 pos) +{ + u64 size = pos < arena->current_position ? arena->current_position - pos : 0; + arena_pop(arena, size); +} + +internal void +arena_clear(mem_arena *arena) +{ + arena->current_position = 0; +} + +internal mem_arena * +arena_resize_align(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size, umm alignment) +{ + u8 *old_mem = (u8 *)old_memory; + + if (!is_pow(alignment)) + { + Align(arena->current_position, alignment); + } + + if (old_memory == NULL || old_size == 0) + { + return (mem_arena *)arena_alloc(arena, new_size); + } + else if ((old_mem >= arena->base_position && old_mem < arena->base_position + arena->capacity)) + { + if ((arena->base_position + arena->previous_position) == old_memory) + { + arena->current_position = arena->previous_position + new_size; + if (new_size > old_size) + { + MemSet(&arena->current_position, new_size - old_size); + } + return (mem_arena *)old_memory; + } + else + { + void *new_memory = arena_alloc(arena, new_size); + umm copy_size = old_size < new_size ? old_size : new_size; + memmove(new_memory, old_mem, copy_size); + } + } + else + { + check(0); + } + return NULL; +} + +internal mem_arena * +arena_resize(mem_arena *arena, void *old_memory, u64 new_size, u64 old_size) +{ + return arena_resize_align(arena, old_memory, new_size, old_size, ARENA_ALIGN); +} + +internal temp_arena +temp_arena_begin(mem_arena *arena) +{ + temp_arena t; + t.arena = arena; + t.start_position = arena->current_position; + + return t; +} + +internal void +temp_arena_end(temp_arena temp) +{ + temp.arena->current_position = temp.start_position; +} diff --git a/source/base/base_arena.h b/source/base/base_arena.h new file mode 100755 index 0000000..71d9e69 --- /dev/null +++ b/source/base/base_arena.h @@ -0,0 +1,41 @@ +#ifndef ARENA_H +#define ARENA_H + +#define Align(pointer, alignment) align((u64)(pointer), (umm)(alignment)) +#define PushStruct(arena, type) (type *)arena_alloc((arena), sizeof(type)) +#define PushArray(arena, type, len) (type *)arena_alloc((arena), sizeof(type) * (len)) +#define PushString(arena, len) (s8 *)arena_alloc((arena), sizeof(s8)*len)) + +typedef struct mem_arena mem_arena; +typedef struct temp_arena temp_arena; + +struct mem_arena +{ + u64 current_position; + u64 previous_position; + u64 capacity; + u8 *base_position; +}; + +struct temp_arena +{ + mem_arena *arena; + u64 start_position; +}; + +internal mem_arena * +arena_resize_align( + mem_arena *arena, + void *old_memory, + u64 new_size, + u64 old_size, + umm alignment); + +internal mem_arena * +arena_resize( + mem_arena *arena, + void *old_memory, + u64 new_size, + u64 old_size); + +#endif diff --git a/source/base/base_hash.c b/source/base/base_hash.c new file mode 100644 index 0000000..c309a9a --- /dev/null +++ b/source/base/base_hash.c @@ -0,0 +1,18 @@ + +internal u64 +generate_hash() +{ + // TODO(nasr): + return 0; +} + +internal hash_map +make_hash_map +{ + + // TODO(nasr): + + return {0}; + +} + diff --git a/source/base/base_include.h b/source/base/base_include.h new file mode 100755 index 0000000..3f83fd7 --- /dev/null +++ b/source/base/base_include.h @@ -0,0 +1,30 @@ +#ifndef BASE_INCLUDE_H +#define BASE_INCLUDE_H + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "base.h" +#include "base_mem.h" +#include "base_arena.h" +#include "base_parse.h" +#include "base_stack.h" +#include "base_test.h" + +#if defined(TB_IMPLEMENTATION) + +#include "base_mem.c" +#include "base_arena.c" +#include "base_parse.c" +#include "base_stack.c" +#include "base_test.c" + +#endif + +#endif diff --git a/source/base/base_mem.c b/source/base/base_mem.c new file mode 100644 index 0000000..f20ba39 --- /dev/null +++ b/source/base/base_mem.c @@ -0,0 +1,17 @@ +internal inline b8 +is_pow(umm x) +{ + return (x & (x - 1)) == 0; +} + +internal inline u64 +align(u64 pointer, umm alignment) +{ + if ((alignment & (alignment - 1)) == 0) + { + return pointer; + } + + return (pointer + alignment - 1) & ~(alignment - 1); +} + diff --git a/source/base/base_mem.h b/source/base/base_mem.h new file mode 100644 index 0000000..945f0ce --- /dev/null +++ b/source/base/base_mem.h @@ -0,0 +1,8 @@ +#ifndef BASE_MEM_H +#define BASE_MEM_H + +#define ARENA_ALIGN (2 * sizeof(void *)) +#define MIN(a, b) (((a) < (b)) ? (a) : (b)) +#define MAX(a, b) (((a) > (b)) ? (a) : (b)) + +#endif diff --git a/source/base/base_parse.c b/source/base/base_parse.c new file mode 100755 index 0000000..4f216bf --- /dev/null +++ b/source/base/base_parse.c @@ -0,0 +1,98 @@ +/* + * is_numeric - Check if a string contains only digits + * @s: String to check + * + * Return: 1 if string contains only numeric characters, 0 otherwise + */ +internal b8 +is_numeric(char *s) +{ + for (; *s; ++s) + { + if (*s < '0' || *s > '9') + { + return 0; + } + } + return 1; +} + +/* + * TODO(nasr): checkout i think there is a buffer overflow happening somewhere + * */ +internal proc_file * +parse_proc_files(char *path, mem_arena *arena) +{ + if (!path || !arena) + { + return NULL; + } + + i32 fd = open(path, O_RDONLY); + if (fd < 0) + { + return NULL; + } + + char *buffer = PushArray(arena, char, KiB(4)); + u64 bytes = read(fd, buffer, KiB(4)); + close(fd); + + if (bytes == 0) + { + return NULL; + } + + /* guessing the count to 256 because i dont want to do a double pass of the buffer */ + proc_file *pf = PushStruct(arena, proc_file); + pf->entries = PushArray(arena, proc_entry, 256); + + u64 line_start = 0; + u64 delim = -1; + u64 entry_index = 0; + + for (u64 index = 0; index < bytes; ++index) + { + if (buffer[index] == ':' && delim == (u64)-1) + { + delim = index; + } + else if (buffer[index] == '\n') + { + if (delim != (-1.f)) + { + u64 key_len = delim - line_start; + if (key_len >= sizeof(pf->entries[entry_index].key)) + { + key_len = sizeof(pf->entries[entry_index].key) - 1; + } + + u64 val_start = delim + 1; + while (val_start < index && (buffer[val_start] == ' ' || buffer[val_start] == '\t')) + { + val_start++; + } + + u64 val_len = index - val_start; + if (val_len >= sizeof(pf->entries[entry_index].value)) + { + val_len = sizeof(pf->entries[entry_index].value) - 1; + } + + MemCpy(pf->entries[entry_index].key, buffer + line_start, key_len - 1); + MemCpy(pf->entries[entry_index].value, buffer + val_start, val_len); + + pf->entries[entry_index].key[key_len] = '\0'; + pf->entries[entry_index].value[val_len] = '\0'; + + ++pf->count; + ++entry_index; + } + + line_start = index + 1; + delim = (u64)-1; + } + } + + return (pf); +} diff --git a/source/base/base_parse.h b/source/base/base_parse.h new file mode 100755 index 0000000..d66fd04 --- /dev/null +++ b/source/base/base_parse.h @@ -0,0 +1,28 @@ +#ifndef BASE_PARSE_H +#define BASE_PARSE_H + +#define COMPARE_STRING(c1, c2) compare_string((char *)c1, (char *)c2) + +typedef struct proc_entry proc_entry; +typedef struct proc_file proc_file; + +struct proc_file +{ + i32 count; + proc_entry *entries; +}; + +struct proc_entry +{ + char value[16]; + char key[16]; +}; + +typedef struct +{ + i8 *start; + i8 *end; + umm len; +} Line; + +#endif diff --git a/source/base/base_stack.c b/source/base/base_stack.c new file mode 100755 index 0000000..7cf0570 --- /dev/null +++ b/source/base/base_stack.c @@ -0,0 +1,187 @@ +internal mem_stack * +stack_create(u64 capacity) +{ + mem_stack *stack = (mem_stack *)mmap( + 0, + capacity + sizeof(mem_stack), + PROT_READ | PROT_WRITE, + MAP_SHARED | MAP_ANONYMOUS, + -1, + 0); + + if (stack == MAP_FAILED) + { + return NULL; + } + + stack->capacity = capacity; + stack->base_position = (u8 *)stack + sizeof(mem_stack); + stack->current_offset = 0; + + return stack; +} + +internal umm +calculate_padding(umm pointer, umm alignment, umm header_size) +{ + umm modulo, padding; + + if (!is_pow(alignment)) + { + return 0; + } + + modulo = pointer & (alignment - 1); + + padding = 0; + + if (0 == modulo) + { + padding = alignment - modulo; + } + + if (padding < header_size) + { + header_size -= padding; + + if ((header_size & (alignment - 1)) != 0) + { + padding += alignment * (1 + (header_size / alignment)); + } + else + { + padding += alignment * (header_size / alignment); + } + } + + return padding; +} + +internal mem_stack * +stack_push_align(mem_stack *stack, u64 size, umm alignment) +{ + umm padding = 0; + + if (!is_pow(alignment)) + { + return (0); + } + + if (alignment > 128) + { + alignment = 128; + } + + umm current_address = (umm)stack->base_position + stack->current_offset; + padding = calculate_padding(current_address, alignment, sizeof(mem_stack_header)); + + if (stack->current_offset + padding + size > stack->capacity) + { + return 0; + } + + stack->current_offset += padding; + + umm next_address = current_address + (umm)padding; + mem_stack_header *header = (mem_stack_header *)(next_address - sizeof(mem_stack_header)); + header->padding = padding; + + stack->current_offset += size; + + return MemSet((void *)next_address, size); +} +internal void * +stack_push(mem_stack *stack, umm size) +{ + return stack_push_align(stack, size, ARENA_ALIGN); +} + +internal void +stack_pop(mem_stack *stack, void *pointer) +{ + if (pointer != NULL) + { + umm start, end, current_address; + mem_stack_header *header; + umm prev_offset; + + start = (umm)stack->base_position; + end = start + (umm)stack->capacity; + current_address = (umm)pointer; + + if (!(start <= current_address && current_address < end)) + { + if (0 && "Out of bounds memory address passed to stack allocator (free)") + { + return; + } + return; + } + + if (current_address >= start + (umm)stack->base_position) + { + return; + } + + header = (mem_stack_header *)(current_address - sizeof(mem_stack_header)); + prev_offset = (size_t)(current_address - (umm)header->padding - start); + stack->current_offset = prev_offset; + } +} + +internal mem_stack * +stack_resize_align(mem_stack *stack, void *pointer, u64 old_size, u64 new_size, u64 alignment) +{ + if (pointer == NULL) + { + return stack_push_align(stack, new_size, alignment); + } + else if (new_size == 0) + { + stack_pop(stack, pointer); + return NULL; + } + + umm start, end, current_address; + umm min_size = old_size < new_size ? old_size : new_size; + void *new_pointer; + + start = (umm)stack->base_position; + end = start + (umm)stack->capacity; + current_address = (umm)pointer; + if (!(start <= current_address && current_address < end)) + { + return NULL; + } + + if (current_address >= start + (umm)stack->current_offset) + { + return NULL; + } + + if (old_size == new_size) + { + return pointer; + } + + new_pointer = stack_push_align(stack, new_size, alignment); + memmove(new_pointer, pointer, min_size); + return new_pointer; +} + +internal void +stack_pop_all(mem_stack *stack) +{ + stack->current_offset = 0; +} + +internal void +stack_destroy(mem_stack *stack) +{ + if (!stack) + { + return; + } + + munmap(stack, stack->capacity + sizeof(mem_stack)); +} diff --git a/source/base/base_stack.h b/source/base/base_stack.h new file mode 100755 index 0000000..fc80b20 --- /dev/null +++ b/source/base/base_stack.h @@ -0,0 +1,20 @@ +#ifndef STACK_H +#define STACK_H + +typedef struct mem_stack mem_stack; +typedef struct mem_stack_header mem_stack_header; + +struct mem_stack +{ + u8 *base_position; + umm current_offset; + umm capacity; +}; + +struct mem_stack_header +{ + u8 padding; + u8 previous_offset; +}; + +#endif diff --git a/source/base/base_string.c b/source/base/base_string.c new file mode 100644 index 0000000..cb2d71d --- /dev/null +++ b/source/base/base_string.c @@ -0,0 +1,73 @@ +internal umm +skip_whitespaces(string *buffer) +{ + s32 index = 0; + while (buffer->size > index) + { + ++index; + } +} + +internal b8 +compare_string_struct(string c1, string c2) +{ + if (c1.size != c2.size) + { + return -1; + } + + for (s32 index = 0; + index < c1.size; + ++index) + { + if (c1.Data[index] != c2.Data[index]) + { + return -1; + } + } + + return 0; +} + +internal b8 +compare_string(char *c1, char *c2) +{ + if (sizeof(*c1) != sizeof(*c2)) + { + return -1; + } + + for ( + u64 word_idx = 0; + word_idx <= sizeof(*c1); + ++word_idx) + { + if (*c1 != *c2) + { + return -1; + } + ++c1; + ++c2; + } + + return 0; +} + +/** NOTE(nasr): Helper function to parse strings to int using ascii codes **/ +internal u64 +parse_u64(char *buf, umm len) +{ + u64 value = 0; + + for (umm buffer_idx = 0; buffer_idx < len; ++buffer_idx) + { + char c = buf[buffer_idx]; + if (c < '0' || c > '9') + { + break; + } + value = value * 10 + (c - '0'); + } + + return value; +} diff --git a/source/base/base_string.h b/source/base/base_string.h new file mode 100644 index 0000000..d879450 --- /dev/null +++ b/source/base/base_string.h @@ -0,0 +1,17 @@ +#ifndef BASE_STRING_H +#define BASE_STRING_H + +// let's use this for strings +// to better differentiate between stuff +typedef u8 s8; +typedef struct string string ; + +struct string +{ + s8 *data; + umm size; +}; + +// TODO(nasr): helper functions for string + +#endif /* BASE_STRING_H */ diff --git a/source/base/base_test.c b/source/base/base_test.c new file mode 100644 index 0000000..e44aa69 --- /dev/null +++ b/source/base/base_test.c @@ -0,0 +1,14 @@ +void +write_int(i32 num) +{ + if (num < 0) + { + write(STDERR_FILENO, "-", 1); + num = -num; + } + if (num >= 10) + write_int(num / 10); + char digit = '0' + (num % 10); + + write(STDERR_FILENO, &digit, 1); +} diff --git a/source/base/base_test.h b/source/base/base_test.h new file mode 100644 index 0000000..11028f4 --- /dev/null +++ b/source/base/base_test.h @@ -0,0 +1,61 @@ +#ifndef BASE_TEST_H +#define BASE_TEST_H + +void +write_int(i32 num); + +#define RED "\x1b[31m" +#define GREEN "\x1b[32m" +#define RESET "\x1b[0m" +#define BLUE "\x1b[34m" + +#define LEN(s) (sizeof(s) - 1) + +#define show \ + do \ + { \ + write(STDOUT_FILENO, __FILE__, sizeof(__FILE__) - 1); \ + write(STDOUT_FILENO, ":", 1); \ + write(STDOUT_FILENO, __func__, sizeof(__func__) - 1); \ + write(STDOUT_FILENO, ":", 1); \ + write_int(__LINE__); \ + write(STDOUT_FILENO, "\n", 1); \ + } while (0) + +#define test(expr) \ + { \ + if ((expr) != 0) \ + { \ + write(STDERR_FILENO, "[FAILED] ", LEN("[FAILED] ")); \ + show; \ + _exit(1); \ + } \ + } + +#define check(expr) \ + { \ + if ((expr) != 0) \ + { \ + write(STDERR_FILENO, RED "[ERROR] ", LEN(RED "[ERROR] ")); \ + show; \ + write(STDERR_FILENO, RESET, LEN(RESET)); \ + _exit(1); \ + } \ + else \ + { \ + write(STDERR_FILENO, GREEN "[SUCCESS] ", LEN(GREEN "[SUCCESS] ")); \ + show; \ + write(STDERR_FILENO, RESET, LEN(RESET)); \ + } \ + } + +#define checkpoint_output "<>\n" +#define checkpoint_end_output "^^^^^^^^^^^^^^\n\n\n" +#define checkpoint \ + { \ + write(STDERR_FILENO, BLUE checkpoint_output, LEN(BLUE checkpoint_output)); \ + show; \ + write(STDERR_FILENO, BLUE checkpoint_end_output, LEN(BLUE checkpoint_end_output)); \ + } + +#endif /* BASE_TEST_H */ diff --git a/source/base/bash_hash.h b/source/base/bash_hash.h new file mode 100644 index 0000000..2c286a2 --- /dev/null +++ b/source/base/bash_hash.h @@ -0,0 +1,15 @@ +##ifndef HEADER_H +#define HEADER_H + +typedef struct hash_map hash_map; +typedef struct hash hash; + + +struct hash_map +{ + + +}; + + +#endif /* HEADER_H */ diff --git a/source/core/core.c b/source/core/core.c new file mode 100644 index 0000000..43322de --- /dev/null +++ b/source/core/core.c @@ -0,0 +1,126 @@ +#define TB_IMPLEMENTATION +#include "core.h" +#include "../base/base_include.h" +#include "../platform/platform_include.h" + +int main() +{ + b32 running = 1; + + Display *MainDisplay = XOpenDisplay(0); + mem_arena *arena = arena_create(MiB(8)); + + Window root = XDefaultRootWindow(MainDisplay); + int screen = DefaultScreen(MainDisplay); + + Visual *v = DefaultVisual(MainDisplay, screen); + + XSetWindowAttributes wa = { + .background_pixmap = None, + .background_pixel = BlackPixel(MainDisplay, DefaultScreen(MainDisplay)), + .border_pixmap = CopyFromParent, + .border_pixel = 0, + .bit_gravity = ForgetGravity, + .win_gravity = NorthWestGravity, + .backing_store = NotUseful, + .backing_planes = 1, + .backing_pixel = 0, + .save_under = False, + .event_mask = 0, + .do_not_propagate_mask = 0, + .override_redirect = False, + .colormap = CopyFromParent, + .cursor = None + }; + + i32 dp_heigth = DisplayHeight(MainDisplay, screen); + i32 dp_width = DisplayWidth(MainDisplay, screen); + + WindowProperties p = { + + .x = dp_width / 2, + .y = dp_heigth / 2, + .height = (u32)800, + .width = (u32)1200, + .border_width = 0, + .window_depth = CopyFromParent, + .window_class = CopyFromParent, + .value_mask = CWBackPixel, + + }; + + Window window = + XCreateWindow( + MainDisplay, + root, + p.x, + p.y, + p.width, + p.height, + p.border_width, + p.window_depth, + p.window_class, + v, + p.value_mask, + &wa); + + Pixmap pixmap = XCreatePixmap(MainDisplay, window, dp_width, dp_heigth, 1); + + XSetWindowBorder(MainDisplay, window, 60); + + // NOTE(nasr): type of input we want to handle + XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask | KeyReleaseMask); + + XMapWindow(MainDisplay, window); + + double x = p.width / 2; + double y = p.height / 2; + + u32 rect_width = 50; + u32 rect_height = 50; + + u64 color = 0x0000ff00; + + GC gc = XCreateGC(MainDisplay, window, 0, NIL); + XSetForeground(MainDisplay, gc, color); + + double *pX = &x; + double *pY = &y; + + XEvent event; + XNextEvent(MainDisplay, &event); + + for (;running;) + { + KeySym keysym = XLookupKeysym(event, 0); + + switch (event.type) + { + case (KeyPress): + { + if (keysym == XK_p || keysym == XK_P) + { + XDrawRectangle(MainDisplay, screen, gc, 50, 50, 50, 50); + } + + break; + } + case (KeyRelease): + { + if (keysym == XK_p || keysym == XK_P) + { + XDrawRectangle(MainDisplay, screen, gc, 50, 50, 50, 50); + } + + break; + } + default: + { + + } + + } + } + arena_clear(arena); + return 0; +} diff --git a/source/core/core.h b/source/core/core.h new file mode 100644 index 0000000..c5821c5 --- /dev/null +++ b/source/core/core.h @@ -0,0 +1,5 @@ +#ifndef CORE_H +#define CORE_H + + + #endif diff --git a/source/platform/platform.c b/source/platform/platform.c new file mode 100644 index 0000000..2e7bbad --- /dev/null +++ b/source/platform/platform.c @@ -0,0 +1,12 @@ +internal inline void +sleep_ms(long ms) +{ + struct timespec ts; + ts.tv_sec = ms / 1000; + ts.tv_nsec = (ms % 1000) * 1000000L; + + while (nanosleep(&ts, &ts)) + { + NULL; + } +} diff --git a/source/platform/platform.h b/source/platform/platform.h new file mode 100644 index 0000000..da8e065 --- /dev/null +++ b/source/platform/platform.h @@ -0,0 +1,56 @@ +#ifndef PLATFORM_H +#define PLATFORM_H + +#define NIL 0 + +#include +#include + #include +#include +#include + +typedef struct WindowProperties WindowProperties; +struct WindowProperties +{ + i32 x; + i32 y; + u32 height; + u32 width; + u32 border_width; + i32 window_depth; + u32 window_class; + u64 value_mask; + +}; + +typedef struct vertex vertex; + +struct vertex +{ + i32 x; + i32 y; + i32 z; +}; + +typedef struct display_pos display_pos; + +struct display_pos +{ + i32 x; + i32 y; + +}; + +typedef struct pos pos; +struct pos +{ + i32 x; + i32 y; + i32 z; + +} ; + + + + +#endif /* PLATFORM_H */ diff --git a/source/platform/platform_include.h b/source/platform/platform_include.h new file mode 100644 index 0000000..886c6b8 --- /dev/null +++ b/source/platform/platform_include.h @@ -0,0 +1,7 @@ +#ifndef PLATFORM_INCLUDE_H +#define PLATFORM_INCLUDE_H + +#include "platform.h" +#include "platform.c" + +#endif /* PLATFORM_INCLUDE_H */ -- cgit v1.3