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