diff options
| author | nasr <nsrddyn@gmail.com> | 2026-04-16 22:01:12 +0200 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-04-16 22:01:12 +0200 |
| commit | 2f4344fe6934b25709cb00a6706fd5d462d9e6f4 (patch) | |
| tree | 7c8e3d5eaa416653253c74ab917506d5a6f48cf7 | |
| parent | 13d9e4df0f4555079d16313a54c6035e22d575a6 (diff) | |
feaature(main): drawing enemys at random positions and storing them as a linked list
| -rwxr-xr-x | Makefile | 2 | ||||
| -rw-r--r-- | source/tb/tb.c | 243 | ||||
| -rw-r--r-- | source/tb/tb.h | 34 |
3 files changed, 186 insertions, 93 deletions
| @@ -1,7 +1,7 @@ | |||
| 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 -Dtb_UNITY | 4 | CFLAGS := -I. -g -o0 -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 |
diff --git a/source/tb/tb.c b/source/tb/tb.c index ed78dd2..e56c0ae 100644 --- a/source/tb/tb.c +++ b/source/tb/tb.c | |||
| @@ -1,56 +1,116 @@ | |||
| 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_PLATFORM | ||
| 5 | #define BASE_PLATFORM_IMPLEMENTATION | ||
| 6 | #include "../base/base_include.h" | 4 | #include "../base/base_include.h" |
| 7 | 5 | ||
| 8 | #define WINDOW_WIDTH 800 | 6 | #include <X11/X.h> |
| 9 | #define WINDOW_HEIGHT 1200 | 7 | #include <X11/Xlib.h> |
| 8 | #include <X11/keysym.h> | ||
| 9 | |||
| 10 | #include "tb.h" | ||
| 11 | |||
| 10 | 12 | ||
| 11 | internal void | 13 | internal void |
| 12 | load_entities(Display *MainDisplay, Window *window, GC *gc, s32 entity_count) | 14 | el_push(enemy_list *el, enemy *en) |
| 13 | { | 15 | { |
| 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 | 16 | ||
| 21 | XDrawRectangle(MainDisplay, | 17 | en->next = NULL; |
| 22 | *window, | ||
| 23 | *gc, | ||
| 24 | x * (1/delta), | ||
| 25 | y * (1/delta), | ||
| 26 | 50, | ||
| 27 | 50); | ||
| 28 | 18 | ||
| 29 | XFillRectangle(MainDisplay, | 19 | if (el->first == NULL) { |
| 30 | *window, | 20 | el->first = en; |
| 31 | *gc, | 21 | el->last = en; |
| 32 | x * (1/delta), | 22 | } else { |
| 33 | y * (1/delta), | 23 | el->last->next = en; |
| 34 | 50, | 24 | el->last = en; |
| 35 | 50); | ||
| 36 | } | 25 | } |
| 26 | el->count++; | ||
| 37 | } | 27 | } |
| 38 | 28 | ||
| 39 | 29 | ||
| 40 | internal void | 30 | internal void |
| 41 | load_user(Display *MainDisplay, Window *window, GC *gc, s32 x, s32 y, u32 width, u32 height) | 31 | pop_enemies(enemy_list *el) |
| 42 | { | 32 | { |
| 43 | XDrawRectangle(MainDisplay, *window, *gc, x, y, width, height); | 33 | unused(el); |
| 44 | XFillRectangle(MainDisplay, *window, *gc, x, y, width, height); | 34 | //- TODO(nasr): implement a stack design |
| 45 | } | 35 | } |
| 46 | 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 | } | ||
| 47 | 106 | ||
| 48 | int main() | 107 | int main() |
| 49 | { | 108 | { |
| 50 | b32 running = 1; | 109 | b32 running = 1; |
| 51 | 110 | ||
| 52 | Display *MainDisplay = XOpenDisplay(0); | 111 | Display *MainDisplay = XOpenDisplay(0); |
| 53 | mem_arena *arena = arena_create(MiB(8)); | 112 | mem_arena *global_arena = arena_create(MiB(8)); |
| 113 | mem_arena *enemy_arena = arena_create(MiB(100)); | ||
| 54 | 114 | ||
| 55 | Window root = XDefaultRootWindow(MainDisplay); | 115 | Window root = XDefaultRootWindow(MainDisplay); |
| 56 | int screen = DefaultScreen(MainDisplay); | 116 | int screen = DefaultScreen(MainDisplay); |
| @@ -62,7 +122,7 @@ int main() | |||
| 62 | .background_pixmap = None, | 122 | .background_pixmap = None, |
| 63 | .background_pixel = BlackPixel(MainDisplay, DefaultScreen(MainDisplay)), | 123 | .background_pixel = BlackPixel(MainDisplay, DefaultScreen(MainDisplay)), |
| 64 | .border_pixmap = CopyFromParent, | 124 | .border_pixmap = CopyFromParent, |
| 65 | .border_pixel = 0, | 125 | .border_pixel= 0, |
| 66 | .bit_gravity = ForgetGravity, | 126 | .bit_gravity = ForgetGravity, |
| 67 | .win_gravity = NorthWestGravity, | 127 | .win_gravity = NorthWestGravity, |
| 68 | .backing_store = NotUseful, | 128 | .backing_store = NotUseful, |
| @@ -79,69 +139,58 @@ int main() | |||
| 79 | s32 dp_heigth = DisplayHeight(MainDisplay, screen); | 139 | s32 dp_heigth = DisplayHeight(MainDisplay, screen); |
| 80 | s32 dp_width = DisplayWidth(MainDisplay, screen); | 140 | s32 dp_width = DisplayWidth(MainDisplay, screen); |
| 81 | 141 | ||
| 142 | s32 WINDOW_WIDTH = 1600; | ||
| 143 | s32 WINDOW_HEIGHT = 800; | ||
| 82 | 144 | ||
| 83 | WindowProperties p = { | 145 | Window window = XCreateWindow( |
| 84 | 146 | MainDisplay, // display | |
| 85 | .x = | 147 | root, // parent |
| 86 | .y = | 148 | (dp_width / 2), // x |
| 87 | .height = | 149 | (dp_heigth / 2), // y |
| 88 | .width = | 150 | (u32)WINDOW_WIDTH, // width |
| 89 | .border_width = 0, | 151 | (u32)WINDOW_HEIGHT, // height |
| 90 | .window_depth = CopyFromParent, | 152 | 0, // border_width |
| 91 | .window_class = | 153 | CopyFromParent, // depth |
| 92 | .value_mask = | 154 | CopyFromParent, // class |
| 93 | 155 | v, // depth | |
| 94 | }; | 156 | CWBackPixel, // visual |
| 95 | 157 | &wa); | |
| 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 | 158 | ||
| 99 | XSetWindowBorder(MainDisplay, window, 60); | 159 | XSetWindowBorder(MainDisplay, window, 60); |
| 100 | XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask | KeyReleaseMask | KeyPressMask); | 160 | XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask | KeyReleaseMask | KeyPressMask); |
| 101 | XMapWindow(MainDisplay, window); | 161 | XMapWindow(MainDisplay, window); |
| 102 | XEvent event; | 162 | XEvent event; |
| 103 | 163 | ||
| 104 | f32 DELTA = 2.5; | ||
| 105 | 164 | ||
| 106 | u32 rect_width = 50; | 165 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); |
| 107 | u32 rect_height = 50; | 166 | XSetForeground(MainDisplay, gc, 0x53f830a2); |
| 108 | 167 | ||
| 109 | s32 rect_x_position = p.width / 2; | 168 | s32 delta = 20; |
| 110 | s32 rect_y_position = p.height / 2; | ||
| 111 | 169 | ||
| 112 | s32 rect_enemy_x_position = (p.width / 2) + 300; | 170 | enemy_list *el = PushStruct(enemy_arena, enemy_list); |
| 113 | s32 rect_enemy_y_position = (p.height / 2) + 300; | ||
| 114 | 171 | ||
| 115 | u64 color = 0x0FF0FF00; | 172 | el->first = NULL; |
| 173 | el->last = NULL; | ||
| 174 | el->count = 0; | ||
| 175 | |||
| 176 | user user = | ||
| 177 | { | ||
| 178 | |||
| 179 | .color = 0x4af333f4ff, | ||
| 180 | .alive = False, | ||
| 181 | .x = 400, | ||
| 182 | .y = 400, | ||
| 183 | .width = 50, | ||
| 184 | .height = 50, | ||
| 185 | |||
| 186 | }; | ||
| 116 | 187 | ||
| 117 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); | ||
| 118 | XSetForeground(MainDisplay, gc, color); | ||
| 119 | 188 | ||
| 120 | for (;running;) | 189 | for (;running;) |
| 121 | { | 190 | { |
| 122 | //- handle collision detection | 191 | //- 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 | 192 | //- 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 | 193 | ||
| 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 | 194 | ||
| 146 | XNextEvent(MainDisplay, &event); | 195 | XNextEvent(MainDisplay, &event); |
| 147 | 196 | ||
| @@ -149,35 +198,45 @@ int main() | |||
| 149 | { | 198 | { |
| 150 | case(KeyPress): | 199 | case(KeyPress): |
| 151 | { | 200 | { |
| 152 | KeySym keysym = XLookupKeysym(&event.xkey, 0); | 201 | KeySym keysym = XLookupKeysym(&event.xkey, 0); //- handle user movement |
| 153 | //- handle user movement | ||
| 154 | { | 202 | { |
| 155 | if(keysym == XK_h) rect_x_position -= DELTA*1.5; | 203 | |
| 156 | else if(keysym == XK_l) rect_x_position += DELTA*1.5; | 204 | if(keysym == XK_h) user.x -= (delta*1.5); |
| 157 | else if(keysym == XK_k) rect_y_position -= DELTA*1.5; | 205 | else if(keysym == XK_l) user.x += (delta*1.5); |
| 158 | else if(keysym == XK_j) rect_y_position += DELTA*1.5; | 206 | else if(keysym == XK_k) user.y -= (delta*1.5); |
| 207 | else if(keysym == XK_j) user.y += (delta*1.5); | ||
| 159 | else if(keysym == XK_s); | 208 | else if(keysym == XK_s); |
| 160 | else if(keysym == XK_Escape || keysym == XK_q) goto exit; | 209 | else if(keysym == XK_Escape || keysym == XK_q) goto exit; |
| 161 | } | 210 | else if(keysym == XK_p) { |
| 162 | 211 | ||
| 212 | enemy *en = PushStruct(enemy_arena, enemy); | ||
| 213 | el_push(el, en); | ||
| 163 | 214 | ||
| 164 | // clear screen before drawing entities | 215 | } |
| 216 | } | ||
| 165 | 217 | ||
| 166 | XClearWindow(MainDisplay, window); | ||
| 167 | 218 | ||
| 168 | //- draw entities | 219 | //- draw entities |
| 169 | { | 220 | } break; |
| 170 | } break; | 221 | default: { |
| 171 | } | 222 | _log("exited"); |
| 172 | default: | ||
| 173 | { | ||
| 174 | 223 | ||
| 175 | } | 224 | } |
| 176 | } | 225 | } |
| 177 | } | ||
| 178 | 226 | ||
| 227 | // clear screen before drawing entities | ||
| 228 | XClearWindow(MainDisplay, window); | ||
| 229 | |||
| 230 | load_enemies(MainDisplay, &window, &gc, el); | ||
| 231 | |||
| 232 | XDrawRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); | ||
| 233 | XFillRectangle(MainDisplay, window, gc, user.x, user.y, user.width, user.height); | ||
| 234 | |||
| 235 | user.color = user.color << 8; | ||
| 236 | |||
| 237 | } | ||
| 179 | 238 | ||
| 180 | exit: | 239 | exit: |
| 181 | arena_destroy(arena); | 240 | arena_destroy(global_arena); |
| 182 | return 0; | 241 | return 0; |
| 183 | } | 242 | } |
diff --git a/source/tb/tb.h b/source/tb/tb.h index e69de29..e03b328 100644 --- a/source/tb/tb.h +++ b/source/tb/tb.h | |||
| @@ -0,0 +1,34 @@ | |||
| 1 | #ifndef TB_H | ||
| 2 | #define TB_H | ||
| 3 | |||
| 4 | typedef struct enemy enemy; | ||
| 5 | struct enemy | ||
| 6 | { | ||
| 7 | enemy *next; | ||
| 8 | enemy *prev; | ||
| 9 | u64 color; | ||
| 10 | s32 x, y; | ||
| 11 | }; | ||
| 12 | |||
| 13 | typedef struct enemy_list enemy_list; | ||
| 14 | struct enemy_list | ||
| 15 | { | ||
| 16 | enemy *first; | ||
| 17 | enemy *last; | ||
| 18 | |||
| 19 | s32 count; | ||
| 20 | }; | ||
| 21 | |||
| 22 | |||
| 23 | typedef struct user user; | ||
| 24 | struct user | ||
| 25 | { | ||
| 26 | u64 color; | ||
| 27 | b32 alive; | ||
| 28 | s32 x; | ||
| 29 | s32 y; | ||
| 30 | u64 width; | ||
| 31 | s32 height; | ||
| 32 | }; | ||
| 33 | |||
| 34 | #endif | ||
