diff options
| author | nasr <nsrddyn@gmail.com> | 2026-01-28 13:13:40 +0100 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-01-28 13:13:40 +0100 |
| commit | 3913d1778318cd0c6bfb871148d38abb33ec7fd3 (patch) | |
| tree | 917728adbf32c877ad591ad9d42f727cc7540b9b /main.c | |
| parent | 7dead79e05e03a71a502ca4e75d05d126ff9f25c (diff) | |
checkpoint
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 345 |
1 files changed, 184 insertions, 161 deletions
| @@ -4,231 +4,254 @@ | |||
| 4 | #include <string.h> | 4 | #include <string.h> |
| 5 | #include <time.h> | 5 | #include <time.h> |
| 6 | #include <unistd.h> | 6 | #include <unistd.h> |
| 7 | #include <stdio.h> | ||
| 7 | 8 | ||
| 8 | #define NIL (0) | 9 | #include "base/base.h" |
| 10 | #include "base/base_arena.h" | ||
| 9 | 11 | ||
| 10 | #define internal static | 12 | #include "base/base.c" |
| 11 | #define global static | 13 | #include "base/base_arena.c" |
| 12 | #define local static | ||
| 13 | 14 | ||
| 14 | typedef uint64_t u64; | ||
| 15 | typedef uint32_t u32; | ||
| 16 | typedef uint16_t u16; | ||
| 17 | typedef uint8_t u8; | ||
| 18 | 15 | ||
| 19 | typedef int8_t i8; | 16 | #define NIL 0 |
| 20 | typedef int16_t i16; | ||
| 21 | typedef int32_t i32; | ||
| 22 | typedef int64_t i64; | ||
| 23 | |||
| 24 | typedef float f32; | ||
| 25 | typedef double f64; | ||
| 26 | |||
| 27 | typedef i16 b16; | ||
| 28 | typedef i32 b32; | ||
| 29 | 17 | ||
| 30 | typedef struct | 18 | typedef struct |
| 31 | { | 19 | { |
| 32 | i32 x; | 20 | i32 x; |
| 33 | i32 y; | 21 | i32 y; |
| 34 | u32 height; | 22 | u32 height; |
| 35 | u32 width; | 23 | u32 width; |
| 36 | u32 border_width; | 24 | u32 border_width; |
| 37 | i32 window_depth; | 25 | i32 window_depth; |
| 38 | u32 window_class; | 26 | u32 window_class; |
| 39 | u64 value_mask; | 27 | u64 value_mask; |
| 40 | 28 | ||
| 41 | } WindowProperties; | 29 | } WindowProperties; |
| 42 | 30 | ||
| 43 | typedef struct | 31 | typedef struct |
| 44 | { | 32 | { |
| 45 | i32 x; | 33 | i32 x; |
| 46 | i32 y; | 34 | i32 y; |
| 47 | i32 z; | 35 | i32 z; |
| 48 | 36 | ||
| 49 | } vertex; | 37 | } vertex; |
| 50 | 38 | ||
| 51 | void | 39 | void |
| 52 | sleep_ms(long ms) | 40 | sleep_ms(long ms) |
| 53 | { | 41 | { |
| 54 | struct timespec ts; | 42 | struct timespec ts; |
| 55 | ts.tv_sec = ms / 1000; | 43 | ts.tv_sec = ms / 1000; |
| 56 | ts.tv_nsec = (ms % 1000) * 1000000L; | 44 | ts.tv_nsec = (ms % 1000) * 1000000L; |
| 57 | 45 | ||
| 58 | while (nanosleep(&ts, &ts)) | 46 | while (nanosleep(&ts, &ts)) |
| 59 | { | 47 | { |
| 60 | NULL; | 48 | NULL; |
| 61 | } | 49 | } |
| 62 | } | 50 | } |
| 63 | 51 | ||
| 64 | void | 52 | void |
| 65 | move_down(double *y) | 53 | move_down(double *y) |
| 66 | { | 54 | { |
| 67 | ++*y; | 55 | ++*y; |
| 68 | } | 56 | } |
| 69 | 57 | ||
| 70 | void | 58 | void |
| 71 | move_up(double *y) | 59 | move_up(double *y) |
| 72 | { | 60 | { |
| 73 | --*y; | 61 | --*y; |
| 62 | } | ||
| 63 | |||
| 64 | void | ||
| 65 | move_left(double *x) | ||
| 66 | { | ||
| 67 | --*x; | ||
| 68 | } | ||
| 69 | |||
| 70 | void | ||
| 71 | move_right(double *x) | ||
| 72 | { | ||
| 73 | ++*x; | ||
| 74 | } | 74 | } |
| 75 | 75 | ||
| 76 | |||
| 77 | |||
| 76 | typedef struct | 78 | typedef struct |
| 77 | { | 79 | { |
| 78 | void (*move)(double *a); | 80 | void (*move)(double *a); |
| 79 | } movement; | 81 | } movement; |
| 80 | 82 | ||
| 83 | void | ||
| 84 | handle_destroy(Display *display, GC *gc) | ||
| 85 | { | ||
| 86 | XFreeGC(display, *gc); | ||
| 87 | XCloseDisplay(display); | ||
| 88 | } | ||
| 89 | |||
| 90 | typedef struct | ||
| 91 | { | ||
| 92 | i32 x; | ||
| 93 | i32 y; | ||
| 94 | |||
| 95 | } display_pos; | ||
| 96 | |||
| 97 | typedef struct | ||
| 98 | { | ||
| 99 | i32 x; | ||
| 100 | i32 y; | ||
| 101 | i32 z; | ||
| 102 | |||
| 103 | } pos; | ||
| 104 | |||
| 105 | |||
| 81 | int | 106 | int |
| 82 | main() | 107 | main() |
| 83 | { | 108 | { |
| 84 | Display *MainDisplay = XOpenDisplay(NIL); | 109 | Display *MainDisplay = XOpenDisplay(0); |
| 110 | mem_arena *arena = arena_create(MiB(8)); | ||
| 111 | |||
| 112 | Window root = XDefaultRootWindow(MainDisplay); | ||
| 113 | int screen = DefaultScreen(MainDisplay); | ||
| 114 | |||
| 115 | Visual *v = DefaultVisual(MainDisplay, screen); | ||
| 85 | 116 | ||
| 86 | Window root = XDefaultRootWindow(MainDisplay); | 117 | XSetWindowAttributes wa = { |
| 87 | int screen = DefaultScreen(MainDisplay); | 118 | .background_pixmap = None, |
| 119 | .background_pixel = BlackPixel(MainDisplay, DefaultScreen(MainDisplay)), | ||
| 120 | .border_pixmap = CopyFromParent, | ||
| 121 | .border_pixel = 0, | ||
| 122 | .bit_gravity = ForgetGravity, | ||
| 123 | .win_gravity = NorthWestGravity, | ||
| 124 | .backing_store = NotUseful, | ||
| 125 | .backing_planes = 1, | ||
| 126 | .backing_pixel = 0, | ||
| 127 | .save_under = False, | ||
| 128 | .event_mask = {}, | ||
| 129 | .do_not_propagate_mask = {}, | ||
| 130 | .override_redirect = False, | ||
| 131 | .colormap = CopyFromParent, | ||
| 132 | .cursor = None | ||
| 133 | }; | ||
| 88 | 134 | ||
| 89 | Visual *v = DefaultVisual(MainDisplay, screen); | 135 | i32 dp_heigth = DisplayHeight(MainDisplay, screen); |
| 136 | i32 dp_width = DisplayWidth(MainDisplay, screen); | ||
| 90 | 137 | ||
| 91 | XSetWindowAttributes wa = { | 138 | WindowProperties p = { |
| 92 | .background_pixmap = None, | ||
| 93 | .background_pixel = BlackPixel(MainDisplay, DefaultScreen(MainDisplay)), | ||
| 94 | .border_pixmap = CopyFromParent, | ||
| 95 | .border_pixel = 0, | ||
| 96 | .bit_gravity = ForgetGravity, | ||
| 97 | .win_gravity = NorthWestGravity, | ||
| 98 | .backing_store = NotUseful, | ||
| 99 | .backing_planes = 1, | ||
| 100 | .backing_pixel = 0, | ||
| 101 | .save_under = False, | ||
| 102 | .event_mask = {}, | ||
| 103 | .do_not_propagate_mask = {}, | ||
| 104 | .override_redirect = False, | ||
| 105 | .colormap = CopyFromParent, | ||
| 106 | .cursor = None | ||
| 107 | }; | ||
| 108 | 139 | ||
| 109 | i32 dp_heigth = DisplayHeight(MainDisplay, screen); | 140 | .x = dp_width / 2, |
| 110 | i32 dp_width = DisplayWidth(MainDisplay, screen); | 141 | .y = dp_heigth / 2, |
| 142 | .height = (u32)800, | ||
| 143 | .width = (u32)1200, | ||
| 144 | .border_width = 0, | ||
| 145 | .window_depth = CopyFromParent, | ||
| 146 | .window_class = CopyFromParent, | ||
| 147 | .value_mask = CWBackPixel, | ||
| 111 | 148 | ||
| 112 | WindowProperties p = { | 149 | }; |
| 113 | 150 | ||
| 114 | .x = dp_width / 2, | ||
| 115 | .y = dp_heigth / 2, | ||
| 116 | .width = (u32)400, | ||
| 117 | .height = (u32)400, | ||
| 118 | .border_width = 0, | ||
| 119 | .window_depth = CopyFromParent, | ||
| 120 | .window_class = CopyFromParent, | ||
| 121 | .value_mask = CWBackPixel, | ||
| 122 | 151 | ||
| 123 | }; | 152 | Window window = |
| 153 | XCreateWindow( | ||
| 154 | MainDisplay, | ||
| 155 | root, | ||
| 156 | p.x, | ||
| 157 | p.y, | ||
| 158 | p.width, | ||
| 159 | p.height, | ||
| 160 | p.border_width, | ||
| 161 | p.window_depth, | ||
| 162 | p.window_class, | ||
| 163 | v, | ||
| 164 | p.value_mask, | ||
| 165 | &wa); | ||
| 124 | 166 | ||
| 125 | Window window = | 167 | Pixmap pixmap = XCreatePixmap(MainDisplay, window, dp_width, dp_heigth, 1); |
| 126 | XCreateWindow( | ||
| 127 | MainDisplay, | ||
| 128 | root, | ||
| 129 | p.x, | ||
| 130 | p.y, | ||
| 131 | p.width, | ||
| 132 | p.height, | ||
| 133 | p.border_width, | ||
| 134 | p.window_depth, | ||
| 135 | p.window_class, | ||
| 136 | v, | ||
| 137 | p.value_mask, | ||
| 138 | &wa); | ||
| 139 | 168 | ||
| 140 | XSetWindowBorder(MainDisplay, window, 60); | 169 | XSetWindowBorder(MainDisplay, window, 60); |
| 141 | XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask); | 170 | XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask); |
| 142 | XMapWindow(MainDisplay, window); | 171 | XMapWindow(MainDisplay, window); |
| 143 | 172 | ||
| 144 | double x = p.width / 2; | ||
| 145 | double y = p.height / 2; | ||
| 146 | u32 rect_width = 50; | ||
| 147 | u32 rect_height = 50; | ||
| 148 | 173 | ||
| 149 | b16 running = 1; | 174 | double x = p.width / 2; |
| 175 | double y = p.height / 2; | ||
| 150 | 176 | ||
| 151 | u64 color = 0x0000ff00; | 177 | u32 rect_width = 50; |
| 178 | u32 rect_height = 50; | ||
| 152 | 179 | ||
| 153 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); | 180 | u64 color = 0x0000ff00; |
| 154 | GC textGc = XCreateGC(MainDisplay, window, 0, NIL); | ||
| 155 | XSetForeground(MainDisplay, gc, color); | ||
| 156 | 181 | ||
| 157 | double *pX = &x; | 182 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); |
| 158 | double *pY = &y; | 183 | XSetForeground(MainDisplay, gc, color); |
| 159 | 184 | ||
| 160 | movement m = { | ||
| 161 | .move = move_down | ||
| 162 | }; | ||
| 163 | 185 | ||
| 164 | while (running) | 186 | double *pX = &x; |
| 165 | { | 187 | double *pY = &y; |
| 166 | if (*pY + rect_height >= p.height) | 188 | |
| 167 | { | 189 | movement m = { |
| 168 | m.move = move_up; | 190 | .move = move_down |
| 169 | } | 191 | }; |
| 170 | else if (*pY <= 0) | 192 | |
| 171 | { | 193 | XEvent event; |
| 172 | m.move = move_down; | 194 | XNextEvent(MainDisplay, &event); |
| 173 | } | 195 | |
| 196 | for (;;) | ||
| 197 | { | ||
| 198 | switch (event.type) | ||
| 199 | case KeyPress: | ||
| 200 | case KeyRelease: | ||
| 201 | { | ||
| 202 | |||
| 174 | 203 | ||
| 175 | char words[] = "working"; | 204 | } |
| 205 | default: | ||
| 206 | { | ||
| 207 | if (*pX + rect_width >= p.width) | ||
| 208 | { | ||
| 209 | m.move = move_left; | ||
| 210 | } | ||
| 211 | else if (*pX <= 0) | ||
| 212 | { | ||
| 213 | m.move = move_right; | ||
| 214 | } | ||
| 176 | 215 | ||
| 177 | XTextItem ti = { | 216 | char words[] = "working"; |
| 178 | .chars = words, | ||
| 179 | .nchars = (int)strlen(words), | ||
| 180 | .delta = 0, | ||
| 181 | .font = None | ||
| 182 | }; | ||
| 183 | 217 | ||
| 184 | XClearWindow(MainDisplay, window); | 218 | XTextItem ti = { |
| 219 | .chars = words, | ||
| 220 | .nchars = (int)strlen(words), | ||
| 221 | .delta = 0, | ||
| 222 | .font = None | ||
| 223 | }; | ||
| 185 | 224 | ||
| 186 | XDrawText( | 225 | XClearWindow(MainDisplay, window); |
| 187 | MainDisplay, | ||
| 188 | window, | ||
| 189 | textGc, | ||
| 190 | 50, | ||
| 191 | 50, | ||
| 192 | &ti, | ||
| 193 | 1); | ||
| 194 | 226 | ||
| 195 | XFillRectangle( | 227 | pos *p = arena_push(arena, sizeof(*p), 0); |
| 196 | MainDisplay, | ||
| 197 | window, | ||
| 198 | gc, | ||
| 199 | (i32)*pX, | ||
| 200 | (i32)*pY, | ||
| 201 | rect_height, | ||
| 202 | rect_width); | ||
| 203 | 228 | ||
| 204 | XFillRectangle( | 229 | p->z = 10; |
| 205 | MainDisplay, | 230 | p->x = ((i32)*pX * 10) / p->z; |
| 206 | window, | 231 | p->y = ((i32)*pY * 10) / p->z; |
| 207 | gc, | ||
| 208 | (i32)*pX + 100, | ||
| 209 | (i32)*pY, | ||
| 210 | rect_height, | ||
| 211 | rect_width); | ||
| 212 | 232 | ||
| 213 | XFillRectangle( | 233 | XFillRectangle( |
| 214 | MainDisplay, | 234 | MainDisplay, |
| 215 | window, | 235 | window, |
| 216 | gc, | 236 | gc, |
| 217 | (i32)*pX - 100, | 237 | (i32)p->x, |
| 218 | (i32)*pY, | 238 | (i32)p->y, |
| 219 | rect_height, | 239 | rect_height, |
| 220 | rect_width); | 240 | rect_width); |
| 221 | 241 | ||
| 222 | m.move(pY); | 242 | m.move(&x); |
| 223 | 243 | ||
| 224 | sleep_ms(10); | 244 | sleep_ms(1); |
| 225 | 245 | ||
| 226 | XFlush(MainDisplay); | 246 | XFlush(MainDisplay); |
| 227 | } | 247 | } |
| 228 | 248 | ||
| 229 | XFreeGC(MainDisplay, gc); | 249 | case DestroyNotify: |
| 230 | XFreeGC(MainDisplay, textGc); | 250 | { |
| 231 | XCloseDisplay(MainDisplay); | 251 | // handle_destroy(MainDisplay, &gc); |
| 252 | } | ||
| 253 | } | ||
| 232 | 254 | ||
| 233 | return 0; | 255 | arena_clear(arena); |
| 256 | return 0; | ||
| 234 | } | 257 | } |
