diff options
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 257 |
1 files changed, 0 insertions, 257 deletions
| @@ -1,257 +0,0 @@ | |||
| 1 | #include <X11/X.h> | ||
| 2 | #include <X11/Xlib.h> | ||
| 3 | #include <stdint.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <time.h> | ||
| 6 | #include <unistd.h> | ||
| 7 | #include <stdio.h> | ||
| 8 | |||
| 9 | #include "base/base.h" | ||
| 10 | #include "base/base_arena.h" | ||
| 11 | |||
| 12 | #include "base/base.c" | ||
| 13 | #include "base/base_arena.c" | ||
| 14 | |||
| 15 | |||
| 16 | #define NIL 0 | ||
| 17 | |||
| 18 | typedef struct | ||
| 19 | { | ||
| 20 | i32 x; | ||
| 21 | i32 y; | ||
| 22 | u32 height; | ||
| 23 | u32 width; | ||
| 24 | u32 border_width; | ||
| 25 | i32 window_depth; | ||
| 26 | u32 window_class; | ||
| 27 | u64 value_mask; | ||
| 28 | |||
| 29 | } WindowProperties; | ||
| 30 | |||
| 31 | typedef struct | ||
| 32 | { | ||
| 33 | i32 x; | ||
| 34 | i32 y; | ||
| 35 | i32 z; | ||
| 36 | |||
| 37 | } vertex; | ||
| 38 | |||
| 39 | void | ||
| 40 | sleep_ms(long ms) | ||
| 41 | { | ||
| 42 | struct timespec ts; | ||
| 43 | ts.tv_sec = ms / 1000; | ||
| 44 | ts.tv_nsec = (ms % 1000) * 1000000L; | ||
| 45 | |||
| 46 | while (nanosleep(&ts, &ts)) | ||
| 47 | { | ||
| 48 | NULL; | ||
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | void | ||
| 53 | move_down(double *y) | ||
| 54 | { | ||
| 55 | ++*y; | ||
| 56 | } | ||
| 57 | |||
| 58 | void | ||
| 59 | move_up(double *y) | ||
| 60 | { | ||
| 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 | } | ||
| 75 | |||
| 76 | |||
| 77 | |||
| 78 | typedef struct | ||
| 79 | { | ||
| 80 | void (*move)(double *a); | ||
| 81 | } movement; | ||
| 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 | |||
| 106 | int | ||
| 107 | main() | ||
| 108 | { | ||
| 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); | ||
| 116 | |||
| 117 | XSetWindowAttributes wa = { | ||
| 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 | }; | ||
| 134 | |||
| 135 | i32 dp_heigth = DisplayHeight(MainDisplay, screen); | ||
| 136 | i32 dp_width = DisplayWidth(MainDisplay, screen); | ||
| 137 | |||
| 138 | WindowProperties p = { | ||
| 139 | |||
| 140 | .x = dp_width / 2, | ||
| 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, | ||
| 148 | |||
| 149 | }; | ||
| 150 | |||
| 151 | |||
| 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); | ||
| 166 | |||
| 167 | Pixmap pixmap = XCreatePixmap(MainDisplay, window, dp_width, dp_heigth, 1); | ||
| 168 | |||
| 169 | XSetWindowBorder(MainDisplay, window, 60); | ||
| 170 | XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask); | ||
| 171 | XMapWindow(MainDisplay, window); | ||
| 172 | |||
| 173 | |||
| 174 | double x = p.width / 2; | ||
| 175 | double y = p.height / 2; | ||
| 176 | |||
| 177 | u32 rect_width = 50; | ||
| 178 | u32 rect_height = 50; | ||
| 179 | |||
| 180 | u64 color = 0x0000ff00; | ||
| 181 | |||
| 182 | GC gc = XCreateGC(MainDisplay, window, 0, NIL); | ||
| 183 | XSetForeground(MainDisplay, gc, color); | ||
| 184 | |||
| 185 | |||
| 186 | double *pX = &x; | ||
| 187 | double *pY = &y; | ||
| 188 | |||
| 189 | movement m = { | ||
| 190 | .move = move_down | ||
| 191 | }; | ||
| 192 | |||
| 193 | XEvent event; | ||
| 194 | XNextEvent(MainDisplay, &event); | ||
| 195 | |||
| 196 | for (;;) | ||
| 197 | { | ||
| 198 | switch (event.type) | ||
| 199 | case KeyPress: | ||
| 200 | case KeyRelease: | ||
| 201 | { | ||
| 202 | |||
| 203 | |||
| 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 | } | ||
| 215 | |||
| 216 | char words[] = "working"; | ||
| 217 | |||
| 218 | XTextItem ti = { | ||
| 219 | .chars = words, | ||
| 220 | .nchars = (int)strlen(words), | ||
| 221 | .delta = 0, | ||
| 222 | .font = None | ||
| 223 | }; | ||
| 224 | |||
| 225 | XClearWindow(MainDisplay, window); | ||
| 226 | |||
| 227 | pos *p = arena_push(arena, sizeof(*p), 0); | ||
| 228 | |||
| 229 | p->z = 10; | ||
| 230 | p->x = ((i32)*pX * 10) / p->z; | ||
| 231 | p->y = ((i32)*pY * 10) / p->z; | ||
| 232 | |||
| 233 | XFillRectangle( | ||
| 234 | MainDisplay, | ||
| 235 | window, | ||
| 236 | gc, | ||
| 237 | (i32)p->x, | ||
| 238 | (i32)p->y, | ||
| 239 | rect_height, | ||
| 240 | rect_width); | ||
| 241 | |||
| 242 | m.move(&x); | ||
| 243 | |||
| 244 | sleep_ms(1); | ||
| 245 | |||
| 246 | XFlush(MainDisplay); | ||
| 247 | } | ||
| 248 | |||
| 249 | case DestroyNotify: | ||
| 250 | { | ||
| 251 | // handle_destroy(MainDisplay, &gc); | ||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | arena_clear(arena); | ||
| 256 | return 0; | ||
| 257 | } | ||
