diff options
| author | nasr <nsrddyn@gmail.com> | 2026-04-17 13:25:41 +0200 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-04-17 13:25:41 +0200 |
| commit | f7bf8f96d3fae8ec28293783c0fb521eb7e99255 (patch) | |
| tree | 0fe2eafd6f38da7c3df1c8f46445b908697249f4 /source | |
| parent | 2f4344fe6934b25709cb00a6706fd5d462d9e6f4 (diff) | |
feature(main): inlined the functions
Diffstat (limited to 'source')
| -rwxr-xr-x | source/base/base_include.h | 1 | ||||
| -rw-r--r-- | source/base/base_os.h | 6 | ||||
| -rw-r--r-- | source/tb/tb.c | 216 |
3 files changed, 116 insertions, 107 deletions
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 @@ | |||
| 12 | #include <math.h> | 12 | #include <math.h> |
| 13 | #include <unistd.h> | 13 | #include <unistd.h> |
| 14 | #include <time.h> | 14 | #include <time.h> |
| 15 | #include <stdarg.h> | ||
| 15 | 16 | ||
| 16 | #include "base.h" | 17 | #include "base.h" |
| 17 | #include "base_mem.h" | 18 | #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) | |||
| 89 | os_write(s.data, s.size); | 89 | os_write(s.data, s.size); |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | #if 1 | ||
| 92 | internal void | 93 | internal void |
| 93 | _log(const char *str) | 94 | _log(const char *str) |
| 94 | { | 95 | { |
| 96 | #ifdef BASE_LOGGING | ||
| 95 | s32 len = 0; | 97 | s32 len = 0; |
| 96 | while (str[len]) len++; | 98 | while (str[len]) len++; |
| 97 | os_write(str, len); | 99 | os_write(str, len); |
| 100 | #else | ||
| 101 | unused(str); | ||
| 102 | #endif | ||
| 98 | 103 | ||
| 99 | } | 104 | } |
| 105 | #endif | ||
| 100 | 106 | ||
| 101 | internal void | 107 | internal void |
| 102 | write_string(const char *str) | 108 | 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 @@ | |||
| 1 | #define BASE_IMPLEMENTATION | 1 | #define BASE_IMPLEMENTATION |
| 2 | #define BASE_RAND_IMPLEMENTATION | 2 | #define BASE_RAND_IMPLEMENTATION |
| 3 | #define BASE_MATH_IMPLEMENTATION | 3 | #define BASE_MATH_IMPLEMENTATION |
| 4 | #define BASE_LOGGING | ||
| 4 | #include "../base/base_include.h" | 5 | #include "../base/base_include.h" |
| 5 | 6 | ||
| 6 | #include <X11/X.h> | 7 | #include <X11/X.h> |
| @@ -10,7 +11,7 @@ | |||
| 10 | #include "tb.h" | 11 | #include "tb.h" |
| 11 | 12 | ||
| 12 | 13 | ||
| 13 | internal void | 14 | internal void |
| 14 | el_push(enemy_list *el, enemy *en) | 15 | el_push(enemy_list *el, enemy *en) |
| 15 | { | 16 | { |
| 16 | 17 | ||
| @@ -27,90 +28,13 @@ el_push(enemy_list *el, enemy *en) | |||
| 27 | } | 28 | } |
| 28 | 29 | ||
| 29 | 30 | ||
| 30 | internal void | ||
| 31 | pop_enemies(enemy_list *el) | ||
| 32 | { | ||
| 33 | unused(el); | ||
| 34 | //- TODO(nasr): implement a stack design | ||
| 35 | } | ||
| 36 | |||
| 37 | internal void | ||
| 38 | load_enemies(Display *MainDisplay, Window *window, GC *gc, enemy_list *el) | ||
| 39 | { | ||
| 40 | s32 x = (s32)generate_random_u64(RAND_CONSTANT) / ( 10 << 8 ); | ||
| 41 | s32 y = (s32)generate_random_u64(RAND_CONSTANT) / ( 10 << 8 ); | ||
| 42 | |||
| 43 | if(el->first == NULL) | ||
| 44 | { | ||
| 45 | return; | ||
| 46 | } | ||
| 47 | enemy *current = el->first; | ||
| 48 | |||
| 49 | for (s32 index = 0; index < el->count; ++index) | ||
| 50 | { | ||
| 51 | |||
| 52 | s32 delta = (s32)generate_random_u64(RAND_CONSTANT); | ||
| 53 | |||
| 54 | //- draw | ||
| 55 | { | ||
| 56 | XDrawRectangle(MainDisplay, *window, *gc, x * (1/delta), y * (1/delta), 50, 50); | ||
| 57 | XFillRectangle(MainDisplay, *window, *gc, x * (1/delta), y * (1/delta), 50, 50); | ||
| 58 | } | ||
| 59 | |||
| 60 | |||
| 61 | |||
| 62 | { | ||
| 63 | s32 dx, dy; | ||
| 64 | |||
| 65 | if(current->next) | ||
| 66 | { | ||
| 67 | dx = current->x - current->next->x; | ||
| 68 | dy = current->y - current->next->y; | ||
| 69 | _log("shawakoemba"); | ||
| 70 | |||
| 71 | } else | ||
| 72 | { | ||
| 73 | dx = 50; | ||
| 74 | dy = 50; | ||
| 75 | } | ||
| 76 | |||
| 77 | //- normalize the distance | why this is needed? no clue | ||
| 78 | f32 distance = sqrtf(dx*dx + dy*dy); | ||
| 79 | |||
| 80 | if(distance > 1.0f) { | ||
| 81 | |||
| 82 | f32 speed = delta * 0.5f; | ||
| 83 | |||
| 84 | current->x += (s32)((dx / distance) * speed); | ||
| 85 | current->y += (s32)((dy / distance) * speed); | ||
| 86 | } | ||
| 87 | |||
| 88 | for (s32 index = 0; index < el->count; ++index) { | ||
| 89 | if(current->x >= 500) delta = -delta; | ||
| 90 | if(current->y >= 500) delta = -delta; | ||
| 91 | |||
| 92 | if(current->x >= 500) delta = -delta; | ||
| 93 | if(current->x >= 500) delta = -delta; | ||
| 94 | } | ||
| 95 | |||
| 96 | } | ||
| 97 | |||
| 98 | if(current->next != NULL) | ||
| 99 | { | ||
| 100 | current = current->next; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | |||
| 104 | // handle enemys | ||
| 105 | } | ||
| 106 | |||
| 107 | int main() | 31 | int main() |
| 108 | { | 32 | { |
| 109 | b32 running = 1; | 33 | b32 running = 1; |
| 110 | 34 | ||
| 111 | Display *MainDisplay = XOpenDisplay(0); | 35 | Display *MainDisplay = XOpenDisplay(0); |
| 112 | mem_arena *global_arena = arena_create(MiB(8)); | 36 | mem_arena *global_arena = arena_create(MiB(8)); |
| 113 | mem_arena *enemy_arena = arena_create(MiB(100)); | 37 | mem_arena *enemy_arena = arena_create(MiB(8)); |
| 114 | 38 | ||
| 115 | Window root = XDefaultRootWindow(MainDisplay); | 39 | Window root = XDefaultRootWindow(MainDisplay); |
| 116 | int screen = DefaultScreen(MainDisplay); | 40 | int screen = DefaultScreen(MainDisplay); |
| @@ -163,7 +87,7 @@ int main() | |||
| 163 | 87 | ||
| 164 | 88 | ||
| 165 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); | 89 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); |
| 166 | XSetForeground(MainDisplay, gc, 0x53f830a2); | 90 | XSetForeground(MainDisplay, gc, 0x55ffaaff); |
| 167 | 91 | ||
| 168 | s32 delta = 20; | 92 | s32 delta = 20; |
| 169 | 93 | ||
| @@ -176,11 +100,11 @@ int main() | |||
| 176 | user user = | 100 | user user = |
| 177 | { | 101 | { |
| 178 | 102 | ||
| 179 | .color = 0x4af333f4ff, | 103 | .color = 0x4af333f4ff, |
| 180 | .alive = False, | 104 | .alive = False, |
| 181 | .x = 400, | 105 | .x = 400, |
| 182 | .y = 400, | 106 | .y = 400, |
| 183 | .width = 50, | 107 | .width = 50, |
| 184 | .height = 50, | 108 | .height = 50, |
| 185 | 109 | ||
| 186 | }; | 110 | }; |
| @@ -191,9 +115,9 @@ int main() | |||
| 191 | //- handle collision detection | 115 | //- handle collision detection |
| 192 | //- handle enemy movement | 116 | //- handle enemy movement |
| 193 | 117 | ||
| 194 | |||
| 195 | XNextEvent(MainDisplay, &event); | 118 | XNextEvent(MainDisplay, &event); |
| 196 | 119 | ||
| 120 | |||
| 197 | switch (event.type) | 121 | switch (event.type) |
| 198 | { | 122 | { |
| 199 | case(KeyPress): | 123 | case(KeyPress): |
| @@ -201,42 +125,120 @@ int main() | |||
| 201 | KeySym keysym = XLookupKeysym(&event.xkey, 0); //- handle user movement | 125 | KeySym keysym = XLookupKeysym(&event.xkey, 0); //- handle user movement |
| 202 | { | 126 | { |
| 203 | 127 | ||
| 204 | if(keysym == XK_h) user.x -= (delta*1.5); | 128 | switch(keysym ) |
| 205 | else if(keysym == XK_l) user.x += (delta*1.5); | 129 | { |
| 206 | else if(keysym == XK_k) user.y -= (delta*1.5); | 130 | case XK_h: user.x -= (delta*1.1); |
| 207 | else if(keysym == XK_j) user.y += (delta*1.5); | 131 | case XK_l: user.x += (delta*1.1); |
| 208 | else if(keysym == XK_s); | 132 | case XK_k: user.y -= (delta*1.1); |
| 133 | case XK_j: user.y += (delta*1.1); | ||
| 134 | default: { | ||
| 135 | XClearArea(MainDisplay, window, user.x, user.y, 50, 50, True); | ||
| 136 | XDrawRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); | ||
| 137 | XFillRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); | ||
| 138 | } | ||
| 139 | } | ||
| 140 | |||
| 141 | |||
| 142 | if(keysym == XK_s) { | ||
| 143 | |||
| 144 | |||
| 145 | |||
| 146 | } | ||
| 209 | else if(keysym == XK_Escape || keysym == XK_q) goto exit; | 147 | else if(keysym == XK_Escape || keysym == XK_q) goto exit; |
| 210 | else if(keysym == XK_p) { | 148 | else if(keysym == XK_p) { |
| 211 | 149 | ||
| 212 | enemy *en = PushStruct(enemy_arena, enemy); | 150 | enemy *en = PushStruct(enemy_arena, enemy); |
| 151 | en->x = (u32)generate_random_u64(RAND_CONSTANT) / ( 10 << 20 ); | ||
| 152 | en->y = (u32)generate_random_u64(RAND_CONSTANT) / ( 10 << 20 ); | ||
| 213 | el_push(el, en); | 153 | el_push(el, en); |
| 214 | 154 | ||
| 215 | } | ||
| 216 | } | ||
| 217 | 155 | ||
| 156 | if(el->first == NULL) | ||
| 157 | { | ||
| 158 | _log("first is NULL\n"); | ||
| 159 | } | ||
| 218 | 160 | ||
| 219 | //- draw entities | 161 | enemy *current = el->first; |
| 220 | } break; | ||
| 221 | default: { | ||
| 222 | _log("exited"); | ||
| 223 | 162 | ||
| 224 | } | 163 | for (s32 index = 0; index < el->count; ++index) |
| 225 | } | 164 | { |
| 165 | |||
| 166 | |||
| 167 | s32 delta = (s32)generate_random_u64(RAND_CONSTANT); | ||
| 168 | |||
| 169 | //- draw | ||
| 170 | { | ||
| 171 | |||
| 172 | XClearArea(MainDisplay, window, current->x, current->y, 50, 50, True); | ||
| 173 | |||
| 174 | XDrawRectangle(MainDisplay, window, gc, current->x , current->y, 50, 50); | ||
| 175 | XFillRectangle(MainDisplay, window, gc, current->x , current->y, 50, 50); | ||
| 176 | } | ||
| 226 | 177 | ||
| 227 | // clear screen before drawing entities | 178 | { |
| 228 | XClearWindow(MainDisplay, window); | 179 | s32 dx, dy; |
| 229 | 180 | ||
| 230 | load_enemies(MainDisplay, &window, &gc, el); | 181 | if(current->next != NULL) |
| 182 | { | ||
| 183 | dx = current->x - current->next->x; | ||
| 184 | dy = current->y - current->next->y; | ||
| 185 | _log("shawakoemba\n"); | ||
| 231 | 186 | ||
| 232 | XDrawRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); | 187 | } else |
| 233 | XFillRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); | 188 | { |
| 189 | dx = 50; | ||
| 190 | dy = 50; | ||
| 191 | } | ||
| 234 | 192 | ||
| 235 | user.color = user.color << 8; | 193 | //- normalize the distance | why this is needed? no clue |
| 194 | f32 distance = sqrtf(dx*dx + dy*dy); | ||
| 236 | 195 | ||
| 196 | if(distance > 1.0f) { | ||
| 197 | |||
| 198 | f32 speed = delta * 0.5f; | ||
| 199 | |||
| 200 | current->x += (s32)((dx / distance) * speed); | ||
| 201 | current->y += (s32)((dy / distance) * speed); | ||
| 202 | } | ||
| 203 | |||
| 204 | for (s32 index = 0; index < el->count; ++index) { | ||
| 205 | if(current->x >= 500) delta = -delta; | ||
| 206 | if(current->y >= 500) delta = -delta; | ||
| 207 | |||
| 208 | if(current->x >= 500) delta = -delta; | ||
| 209 | if(current->x >= 500) delta = -delta; | ||
| 210 | } | ||
| 211 | |||
| 212 | } | ||
| 213 | |||
| 214 | if(current->next != NULL) | ||
| 215 | { | ||
| 216 | current = current->next; | ||
| 217 | } | ||
| 218 | } | ||
| 219 | } | ||
| 220 | else if(keysym == XK_c) { | ||
| 221 | |||
| 222 | XSetForeground(MainDisplay, gc, user.color); | ||
| 223 | |||
| 224 | u64 x = user.color; | ||
| 225 | user.color = (x << 1) | (x >> 63); | ||
| 226 | } | ||
| 227 | |||
| 228 | //- draw entities | ||
| 229 | } break; | ||
| 230 | default: | ||
| 231 | { | ||
| 232 | |||
| 233 | } | ||
| 234 | // TODO(nasr): what do to do on default? | ||
| 235 | |||
| 236 | //- draw user | ||
| 237 | } | ||
| 238 | } | ||
| 237 | } | 239 | } |
| 238 | 240 | ||
| 239 | exit: | 241 | exit: |
| 240 | arena_destroy(global_arena); | 242 | arena_destroy(global_arena); |
| 241 | return 0; | 243 | return 0; |
| 242 | } | 244 | } |
