From f7bf8f96d3fae8ec28293783c0fb521eb7e99255 Mon Sep 17 00:00:00 2001 From: nasr Date: Fri, 17 Apr 2026 13:25:41 +0200 Subject: feature(main): inlined the functions --- source/base/base_include.h | 1 + source/base/base_os.h | 6 ++ source/tb/tb.c | 216 +++++++++++++++++++++++---------------------- 3 files changed, 116 insertions(+), 107 deletions(-) (limited to 'source') diff --git a/source/base/base_include.h b/source/base/base_include.h index e70ccf2..a376f29 100755 --- a/source/base/base_include.h +++ b/source/base/base_include.h @@ -12,6 +12,7 @@ #include #include #include +#include #include "base.h" #include "base_mem.h" diff --git a/source/base/base_os.h b/source/base/base_os.h index 536119e..82aa70d 100644 --- a/source/base/base_os.h +++ b/source/base/base_os.h @@ -89,14 +89,20 @@ log_s8(string8 s) os_write(s.data, s.size); } +#if 1 internal void _log(const char *str) { +#ifdef BASE_LOGGING s32 len = 0; while (str[len]) len++; os_write(str, len); +#else + unused(str); +#endif } +#endif internal void write_string(const char *str) diff --git a/source/tb/tb.c b/source/tb/tb.c index e56c0ae..0457e89 100644 --- a/source/tb/tb.c +++ b/source/tb/tb.c @@ -1,6 +1,7 @@ #define BASE_IMPLEMENTATION #define BASE_RAND_IMPLEMENTATION #define BASE_MATH_IMPLEMENTATION +#define BASE_LOGGING #include "../base/base_include.h" #include @@ -10,7 +11,7 @@ #include "tb.h" -internal void + internal void el_push(enemy_list *el, enemy *en) { @@ -27,90 +28,13 @@ el_push(enemy_list *el, enemy *en) } -internal void -pop_enemies(enemy_list *el) -{ - unused(el); - //- TODO(nasr): implement a stack design -} - -internal void -load_enemies(Display *MainDisplay, Window *window, GC *gc, enemy_list *el) -{ - s32 x = (s32)generate_random_u64(RAND_CONSTANT) / ( 10 << 8 ); - s32 y = (s32)generate_random_u64(RAND_CONSTANT) / ( 10 << 8 ); - - if(el->first == NULL) - { - return; - } - enemy *current = el->first; - - for (s32 index = 0; index < el->count; ++index) - { - - s32 delta = (s32)generate_random_u64(RAND_CONSTANT); - - //- draw - { - XDrawRectangle(MainDisplay, *window, *gc, x * (1/delta), y * (1/delta), 50, 50); - XFillRectangle(MainDisplay, *window, *gc, x * (1/delta), y * (1/delta), 50, 50); - } - - - - { - s32 dx, dy; - - if(current->next) - { - dx = current->x - current->next->x; - dy = current->y - current->next->y; - _log("shawakoemba"); - - } else - { - dx = 50; - dy = 50; - } - - //- normalize the distance | why this is needed? no clue - f32 distance = sqrtf(dx*dx + dy*dy); - - if(distance > 1.0f) { - - f32 speed = delta * 0.5f; - - current->x += (s32)((dx / distance) * speed); - current->y += (s32)((dy / distance) * speed); - } - - for (s32 index = 0; index < el->count; ++index) { - if(current->x >= 500) delta = -delta; - if(current->y >= 500) delta = -delta; - - if(current->x >= 500) delta = -delta; - if(current->x >= 500) delta = -delta; - } - - } - - if(current->next != NULL) - { - current = current->next; - } - } - - // handle enemys -} - int main() { b32 running = 1; Display *MainDisplay = XOpenDisplay(0); mem_arena *global_arena = arena_create(MiB(8)); - mem_arena *enemy_arena = arena_create(MiB(100)); + mem_arena *enemy_arena = arena_create(MiB(8)); Window root = XDefaultRootWindow(MainDisplay); int screen = DefaultScreen(MainDisplay); @@ -163,7 +87,7 @@ int main() GC gc = XCreateGC(MainDisplay, window, 0, NIL); - XSetForeground(MainDisplay, gc, 0x53f830a2); + XSetForeground(MainDisplay, gc, 0x55ffaaff); s32 delta = 20; @@ -176,11 +100,11 @@ int main() user user = { - .color = 0x4af333f4ff, - .alive = False, - .x = 400, - .y = 400, - .width = 50, + .color = 0x4af333f4ff, + .alive = False, + .x = 400, + .y = 400, + .width = 50, .height = 50, }; @@ -191,9 +115,9 @@ int main() //- handle collision detection //- handle enemy movement - XNextEvent(MainDisplay, &event); + switch (event.type) { case(KeyPress): @@ -201,42 +125,120 @@ int main() KeySym keysym = XLookupKeysym(&event.xkey, 0); //- handle user movement { - if(keysym == XK_h) user.x -= (delta*1.5); - else if(keysym == XK_l) user.x += (delta*1.5); - else if(keysym == XK_k) user.y -= (delta*1.5); - else if(keysym == XK_j) user.y += (delta*1.5); - else if(keysym == XK_s); + switch(keysym ) + { + case XK_h: user.x -= (delta*1.1); + case XK_l: user.x += (delta*1.1); + case XK_k: user.y -= (delta*1.1); + case XK_j: user.y += (delta*1.1); + default: { + XClearArea(MainDisplay, window, user.x, user.y, 50, 50, True); + XDrawRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); + XFillRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); + } + } + + + if(keysym == XK_s) { + + + + } else if(keysym == XK_Escape || keysym == XK_q) goto exit; else if(keysym == XK_p) { enemy *en = PushStruct(enemy_arena, enemy); + en->x = (u32)generate_random_u64(RAND_CONSTANT) / ( 10 << 20 ); + en->y = (u32)generate_random_u64(RAND_CONSTANT) / ( 10 << 20 ); el_push(el, en); - } - } + if(el->first == NULL) + { + _log("first is NULL\n"); + } - //- draw entities - } break; - default: { - _log("exited"); + enemy *current = el->first; - } - } + for (s32 index = 0; index < el->count; ++index) + { + + + s32 delta = (s32)generate_random_u64(RAND_CONSTANT); + + //- draw + { + + XClearArea(MainDisplay, window, current->x, current->y, 50, 50, True); + + XDrawRectangle(MainDisplay, window, gc, current->x , current->y, 50, 50); + XFillRectangle(MainDisplay, window, gc, current->x , current->y, 50, 50); + } - // clear screen before drawing entities - XClearWindow(MainDisplay, window); + { + s32 dx, dy; - load_enemies(MainDisplay, &window, &gc, el); + if(current->next != NULL) + { + dx = current->x - current->next->x; + dy = current->y - current->next->y; + _log("shawakoemba\n"); - XDrawRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); - XFillRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); + } else + { + dx = 50; + dy = 50; + } - user.color = user.color << 8; + //- normalize the distance | why this is needed? no clue + f32 distance = sqrtf(dx*dx + dy*dy); + if(distance > 1.0f) { + + f32 speed = delta * 0.5f; + + current->x += (s32)((dx / distance) * speed); + current->y += (s32)((dy / distance) * speed); + } + + for (s32 index = 0; index < el->count; ++index) { + if(current->x >= 500) delta = -delta; + if(current->y >= 500) delta = -delta; + + if(current->x >= 500) delta = -delta; + if(current->x >= 500) delta = -delta; + } + + } + + if(current->next != NULL) + { + current = current->next; + } + } + } + else if(keysym == XK_c) { + + XSetForeground(MainDisplay, gc, user.color); + + u64 x = user.color; + user.color = (x << 1) | (x >> 63); + } + + //- draw entities + } break; + default: + { + + } + // TODO(nasr): what do to do on default? + + //- draw user + } + } } exit: - arena_destroy(global_arena); - return 0; + arena_destroy(global_arena); + return 0; } -- cgit v1.3