summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c345
1 files changed, 184 insertions, 161 deletions
diff --git a/main.c b/main.c
index 0118363..4c43245 100644
--- a/main.c
+++ b/main.c
@@ -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
14typedef uint64_t u64;
15typedef uint32_t u32;
16typedef uint16_t u16;
17typedef uint8_t u8;
18 15
19typedef int8_t i8; 16#define NIL 0
20typedef int16_t i16;
21typedef int32_t i32;
22typedef int64_t i64;
23
24typedef float f32;
25typedef double f64;
26
27typedef i16 b16;
28typedef i32 b32;
29 17
30typedef struct 18typedef struct
31{ 19{
32 i32 x; 20i32 x;
33 i32 y; 21i32 y;
34 u32 height; 22u32 height;
35 u32 width; 23u32 width;
36 u32 border_width; 24u32 border_width;
37 i32 window_depth; 25i32 window_depth;
38 u32 window_class; 26u32 window_class;
39 u64 value_mask; 27u64 value_mask;
40 28
41} WindowProperties; 29} WindowProperties;
42 30
43typedef struct 31typedef struct
44{ 32{
45 i32 x; 33i32 x;
46 i32 y; 34i32 y;
47 i32 z; 35i32 z;
48 36
49} vertex; 37} vertex;
50 38
51void 39void
52sleep_ms(long ms) 40sleep_ms(long ms)
53{ 41{
54 struct timespec ts; 42struct timespec ts;
55 ts.tv_sec = ms / 1000; 43ts.tv_sec = ms / 1000;
56 ts.tv_nsec = (ms % 1000) * 1000000L; 44ts.tv_nsec = (ms % 1000) * 1000000L;
57 45
58 while (nanosleep(&ts, &ts)) 46while (nanosleep(&ts, &ts))
59 { 47{
60 NULL; 48NULL;
61 } 49}
62} 50}
63 51
64void 52void
65move_down(double *y) 53move_down(double *y)
66{ 54{
67 ++*y; 55++*y;
68} 56}
69 57
70void 58void
71move_up(double *y) 59move_up(double *y)
72{ 60{
73 --*y; 61--*y;
62}
63
64void
65move_left(double *x)
66{
67--*x;
68}
69
70void
71move_right(double *x)
72{
73++*x;
74} 74}
75 75
76
77
76typedef struct 78typedef struct
77{ 79{
78 void (*move)(double *a); 80void (*move)(double *a);
79} movement; 81} movement;
80 82
83void
84handle_destroy(Display *display, GC *gc)
85{
86XFreeGC(display, *gc);
87XCloseDisplay(display);
88}
89
90typedef struct
91{
92i32 x;
93i32 y;
94
95} display_pos;
96
97typedef struct
98{
99i32 x;
100i32 y;
101i32 z;
102
103} pos;
104
105
81int 106int
82main() 107main()
83{ 108{
84 Display *MainDisplay = XOpenDisplay(NIL); 109Display *MainDisplay = XOpenDisplay(0);
110mem_arena *arena = arena_create(MiB(8));
111
112Window root = XDefaultRootWindow(MainDisplay);
113int screen = DefaultScreen(MainDisplay);
114
115Visual *v = DefaultVisual(MainDisplay, screen);
85 116
86 Window root = XDefaultRootWindow(MainDisplay); 117XSetWindowAttributes 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); 135i32 dp_heigth = DisplayHeight(MainDisplay, screen);
136i32 dp_width = DisplayWidth(MainDisplay, screen);
90 137
91 XSetWindowAttributes wa = { 138WindowProperties 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 }; 152Window window =
153XCreateWindow(
154MainDisplay,
155root,
156p.x,
157p.y,
158p.width,
159p.height,
160p.border_width,
161p.window_depth,
162p.window_class,
163v,
164p.value_mask,
165&wa);
124 166
125 Window window = 167Pixmap 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); 169XSetWindowBorder(MainDisplay, window, 60);
141 XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask); 170XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask);
142 XMapWindow(MainDisplay, window); 171XMapWindow(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; 174double x = p.width / 2;
175double y = p.height / 2;
150 176
151 u64 color = 0x0000ff00; 177u32 rect_width = 50;
178u32 rect_height = 50;
152 179
153 GC gc = XCreateGC(MainDisplay, window, 0, NIL); 180u64 color = 0x0000ff00;
154 GC textGc = XCreateGC(MainDisplay, window, 0, NIL);
155 XSetForeground(MainDisplay, gc, color);
156 181
157 double *pX = &x; 182GC gc = XCreateGC(MainDisplay, window, 0, NIL);
158 double *pY = &y; 183XSetForeground(MainDisplay, gc, color);
159 184
160 movement m = {
161 .move = move_down
162 };
163 185
164 while (running) 186double *pX = &x;
165 { 187double *pY = &y;
166 if (*pY + rect_height >= p.height) 188
167 { 189movement m = {
168 m.move = move_up; 190.move = move_down
169 } 191};
170 else if (*pY <= 0) 192
171 { 193XEvent event;
172 m.move = move_down; 194XNextEvent(MainDisplay, &event);
173 } 195
196for (;;)
197{
198switch (event.type)
199case KeyPress:
200case KeyRelease:
201{
202
174 203
175 char words[] = "working"; 204}
205default:
206{
207if (*pX + rect_width >= p.width)
208{
209m.move = move_left;
210}
211else if (*pX <= 0)
212{
213m.move = move_right;
214}
176 215
177 XTextItem ti = { 216char words[] = "working";
178 .chars = words,
179 .nchars = (int)strlen(words),
180 .delta = 0,
181 .font = None
182 };
183 217
184 XClearWindow(MainDisplay, window); 218XTextItem ti = {
219.chars = words,
220.nchars = (int)strlen(words),
221.delta = 0,
222.font = None
223};
185 224
186 XDrawText( 225XClearWindow(MainDisplay, window);
187 MainDisplay,
188 window,
189 textGc,
190 50,
191 50,
192 &ti,
193 1);
194 226
195 XFillRectangle( 227pos *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( 229p->z = 10;
205 MainDisplay, 230p->x = ((i32)*pX * 10) / p->z;
206 window, 231p->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( 233XFillRectangle(
214 MainDisplay, 234MainDisplay,
215 window, 235window,
216 gc, 236gc,
217 (i32)*pX - 100, 237(i32)p->x,
218 (i32)*pY, 238(i32)p->y,
219 rect_height, 239rect_height,
220 rect_width); 240rect_width);
221 241
222 m.move(pY); 242m.move(&x);
223 243
224 sleep_ms(10); 244sleep_ms(1);
225 245
226 XFlush(MainDisplay); 246XFlush(MainDisplay);
227 } 247}
228 248
229 XFreeGC(MainDisplay, gc); 249case DestroyNotify:
230 XFreeGC(MainDisplay, textGc); 250{
231 XCloseDisplay(MainDisplay); 251// handle_destroy(MainDisplay, &gc);
252}
253}
232 254
233 return 0; 255arena_clear(arena);
256return 0;
234} 257}