summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c234
1 files changed, 234 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..0118363
--- /dev/null
+++ b/main.c
@@ -0,0 +1,234 @@
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
8#define NIL (0)
9
10#define internal static
11#define global static
12#define local static
13
14typedef uint64_t u64;
15typedef uint32_t u32;
16typedef uint16_t u16;
17typedef uint8_t u8;
18
19typedef int8_t i8;
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
30typedef struct
31{
32 i32 x;
33 i32 y;
34 u32 height;
35 u32 width;
36 u32 border_width;
37 i32 window_depth;
38 u32 window_class;
39 u64 value_mask;
40
41} WindowProperties;
42
43typedef struct
44{
45 i32 x;
46 i32 y;
47 i32 z;
48
49} vertex;
50
51void
52sleep_ms(long ms)
53{
54 struct timespec ts;
55 ts.tv_sec = ms / 1000;
56 ts.tv_nsec = (ms % 1000) * 1000000L;
57
58 while (nanosleep(&ts, &ts))
59 {
60 NULL;
61 }
62}
63
64void
65move_down(double *y)
66{
67 ++*y;
68}
69
70void
71move_up(double *y)
72{
73 --*y;
74}
75
76typedef struct
77{
78 void (*move)(double *a);
79} movement;
80
81int
82main()
83{
84 Display *MainDisplay = XOpenDisplay(NIL);
85
86 Window root = XDefaultRootWindow(MainDisplay);
87 int screen = DefaultScreen(MainDisplay);
88
89 Visual *v = DefaultVisual(MainDisplay, screen);
90
91 XSetWindowAttributes wa = {
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
109 i32 dp_heigth = DisplayHeight(MainDisplay, screen);
110 i32 dp_width = DisplayWidth(MainDisplay, screen);
111
112 WindowProperties p = {
113
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
123 };
124
125 Window window =
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
140 XSetWindowBorder(MainDisplay, window, 60);
141 XSelectInput(MainDisplay, window, ExposureMask | StructureNotifyMask);
142 XMapWindow(MainDisplay, window);
143
144 double x = p.width / 2;
145 double y = p.height / 2;
146 u32 rect_width = 50;
147 u32 rect_height = 50;
148
149 b16 running = 1;
150
151 u64 color = 0x0000ff00;
152
153 GC gc = XCreateGC(MainDisplay, window, 0, NIL);
154 GC textGc = XCreateGC(MainDisplay, window, 0, NIL);
155 XSetForeground(MainDisplay, gc, color);
156
157 double *pX = &x;
158 double *pY = &y;
159
160 movement m = {
161 .move = move_down
162 };
163
164 while (running)
165 {
166 if (*pY + rect_height >= p.height)
167 {
168 m.move = move_up;
169 }
170 else if (*pY <= 0)
171 {
172 m.move = move_down;
173 }
174
175 char words[] = "working";
176
177 XTextItem ti = {
178 .chars = words,
179 .nchars = (int)strlen(words),
180 .delta = 0,
181 .font = None
182 };
183
184 XClearWindow(MainDisplay, window);
185
186 XDrawText(
187 MainDisplay,
188 window,
189 textGc,
190 50,
191 50,
192 &ti,
193 1);
194
195 XFillRectangle(
196 MainDisplay,
197 window,
198 gc,
199 (i32)*pX,
200 (i32)*pY,
201 rect_height,
202 rect_width);
203
204 XFillRectangle(
205 MainDisplay,
206 window,
207 gc,
208 (i32)*pX + 100,
209 (i32)*pY,
210 rect_height,
211 rect_width);
212
213 XFillRectangle(
214 MainDisplay,
215 window,
216 gc,
217 (i32)*pX - 100,
218 (i32)*pY,
219 rect_height,
220 rect_width);
221
222 m.move(pY);
223
224 sleep_ms(10);
225
226 XFlush(MainDisplay);
227 }
228
229 XFreeGC(MainDisplay, gc);
230 XFreeGC(MainDisplay, textGc);
231 XCloseDisplay(MainDisplay);
232
233 return 0;
234}