diff options
| author | nasr <nsrddyn@gmail.com> | 2026-04-16 17:10:02 +0200 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-04-16 17:10:02 +0200 |
| commit | 8ea6a3c8621287d11296b8300029f32a27743d9a (patch) | |
| tree | cd12aa5fcd3e058fa74b45705c7b82524658d444 /source/tb/tb.c | |
| parent | f430bfe8f71430032bec689bf0bbdc94ac409c22 (diff) | |
feature(checkpoint): checkpoint cleaning up base library
Diffstat (limited to 'source/tb/tb.c')
| -rw-r--r-- | source/tb/tb.c | 183 |
1 files changed, 183 insertions, 0 deletions
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 | } | ||
