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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378 | diff --git a/window_in_x/Makefile b/window_in_x/Makefile
new file mode 100644
index 0000000..5e9540b
--- /dev/null
+++ b/window_in_x/Makefile
@@ -0,0 +1,2 @@
+install:
+ cc -lX11 -lm -o main main.c
diff --git a/window_in_x/main.c b/window_in_x/main.c
new file mode 100644
index 0000000..df4f257
--- /dev/null
+++ b/window_in_x/main.c
@@ -0,0 +1,364 @@
+#include <X11/X.h>
+#include <X11/Xlib.h>
+#include <X11/Xutil.h>
+#include <math.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+
+#define WIDTH 600
+#define HEIGHT WIDTH
+#define FPS 60
+
+#define STEPS 10
+#define BG_COLOR 0x1d2021
+
+#define RED 0xFB4934
+#define GREEN 0x00FF00
+#define BLUE 0x0000FF
+#define BLACK 0x000000
+#define WHITE 0xffffff
+
+typedef uint32_t Color;
+
+typedef struct {
+ int x;
+ int y;
+}Position;
+
+
+Color image[HEIGHT][WIDTH];
+
+void draw_px(int x, int y, Color c) {
+ if (x >= 0 && x < WIDTH && y >= 0 && y < HEIGHT)
+ image[y][x] = c;
+}
+
+void clear_buffer(Color bg) {
+ for (int y = 0; y < HEIGHT; ++y)
+ for (int x = 0; x < WIDTH; ++x)
+ image[y][x] = bg;
+}
+
+void draw_circlef(int cx, int cy, int radius, Color color) {
+ for (int y = -radius; y <= radius; ++y) {
+ for (int x = -radius; x <= radius; ++x) {
+ int a = x*x + y*y ;
+ if (a<=radius*radius)
+ draw_px(cx + x, cy + y, color);
+ }
+ }
+}
+void draw_circle(int cx, int cy, int radius, Color color) {
+ for (int y = -radius; y <= radius; ++y) {
+ for (int x = -radius; x <= radius; ++x) {
+ int a = x*x + y*y ;
+ if (a<=radius*radius + radius && a>=radius*radius - radius)
+ draw_px(cx + x, cy + y, color);
+ }
+ }
+}
+
+
+void draw_line( float x1,float y1, float x2,float y2, Color color )
+{
+ int ya = y1<y2? y1:y2;
+ int yb = y2<y1? y1:y2;
+ int xa = x1<x2? x1:x2;
+ int xb = x2<x1? x1:x2;
+ int dx =abs(x2-x1);
+ int dy =abs(y2-y1);
+
+ float a,b;
+
+
+ if (x2-x1!=0){
+ a = (y2-y1) / (x2-x1);
+ b = y1 - a * x1;
+ }
+
+
+ if (x1==x2){
+ for (int y = ya;y<=yb;++y)
+ draw_px(x1, y, color);
+ return;
+ }
+ if (dx >=dy){
+ for (int i=xa;i<=xb;++i){
+ int y = a*i + b;
+ draw_px(i, y, color);
+ }
+ }else
+ if (dy > dx){
+ for (int i=ya;i<=yb;++i){
+ int x = (i - b) / a;
+ draw_px(x, i, color);
+ }
+ }
+}
+
+
+void draw_trianglef(int x1,int y1,int x2,int y2,int x3,int y3, Color color) {
+
+ if (y1 > y2) { int tmp=x1; x1=x2; x2=tmp; tmp=y1; y1=y2; y2=tmp; }
+ if (y1 > y3) { int tmp=x1; x1=x3; x3=tmp; tmp=y1; y1=y3; y3=tmp; }
+ if (y2 > y3) { int tmp=x2; x2=x3; x3=tmp; tmp=y2; y2=y3; y3=tmp; }
+
+ int total_height = y3 - y1;
+ for (int i=0; i<=total_height; i++) {
+ int second_half = i > (y2 - y1) || y2 == y1;
+ int segment_height = second_half ? (y3 - y2) : (y2 - y1);
+ float alpha = (float)i / total_height;
+ float beta = (float)(i - (second_half ? y2 - y1 : 0)) / segment_height;
+
+ int ax = x1 + (x3 - x1) * alpha;
+ int bx = second_half ? x2 + (x3 - x2) * beta : x1 + (x2 - x1) * beta;
+
+ if (ax > bx) { int tmp = ax; ax = bx; bx = tmp; }
+ for (int j = ax; j <= bx; j++)
+ draw_px(j, y1 + i, color);
+ }
+}
+void draw_rectanglef(int x,int y,uint width,uint height,Color color)
+{
+ for(int i = x;i<=x+width;i=++i){
+ for(int j = y;j<=y+height;++j){
+ draw_px(i, j,color);
+ }
+ }
+
+}
+void draw_rectangle(int x,int y,uint width,uint height,Color color)
+{
+ draw_line(x,y,x + width,y ,color);
+ draw_line(x, y, x, y+height,color);
+ draw_line(x+width, y, x+width, y+height,color);
+ draw_line(x ,y+ height,x+width ,y+ height,color);
+}
+
+
+
+void draw_triangle(int x1,int y1, int x2,int y2,int x3,int y3,Color color)
+{
+ draw_line(x1, y1, x2, y2, color);
+ draw_line(x2, y2, x3, y3, color);
+ draw_line(x3, y3, x1, y1, color);
+
+}
+
+void draw_ellipse(int x,int y, uint w,uint h, Color color ) {
+ int a = w/2;
+ int b = h/2;
+ for (int i = -a; i<=a;++i){
+ for (int j = -b; j<=b;++j){
+ float eq = (float)(i*i)/(a*a) + (float)(j*j)/(b*b);
+ if (eq <= 1.00 && eq >= 0.90)
+ draw_px(x+i,y+j, color);
+ }
+ }
+}
+
+
+void draw_ellipsef(int x,int y, uint w,uint h, Color color ) {
+ int a = w/2;
+ int b = h/2;
+ for (int i = -a; i<=a;++i){
+ for (int j = -b; j<=b;++j){
+ float eq = (float)(i*i)/(a*a) + (float)(j*j)/(b*b);
+ if (eq <= 1.00 )
+ draw_px(x+i,y+j, color);
+ }
+ }
+}
+
+void cxy(int *x,int *y)
+{
+ *x = WIDTH /2;
+ *y = HEIGHT /2;
+}
+
+// USER Drawing fn's
+
+void game_box(int x, int y, size_t size, size_t border, Color fg_color,Color bg_color){
+ // draw_rectanglef(int x, int y, uint width, uint height, Color color)
+
+ draw_rectanglef(x, y, size, size, bg_color);
+ draw_rectanglef(x+border, y+border, size-10, size-10, fg_color);
+
+
+}
+
+void game_player(int x, int y, size_t size,size_t border,Color fg_color,Color bg_color)
+{
+ int e = (size-border) / 4;
+ draw_rectanglef(x, y, size, size, bg_color);
+ draw_rectanglef(x+border, y+border, size-border*2, size-border*2, fg_color);
+ draw_rectanglef(x+size-e, y+e, 3,7,WHITE);
+ draw_rectanglef(x+e, y+e, 3,7,WHITE);
+}
+
+int main(int argc,char *argv[]) {
+
+ char *t = argv[1];
+ if (t==NULL){
+ printf("./main game\n./main cube\n./main circle\n");
+ exit(1);
+ }
+
+ Display *d = XOpenDisplay(NULL);
+ if (!d) return 1;
+
+ Window w = XCreateSimpleWindow(d, DefaultRootWindow(d),
+ 1920/2 - WIDTH/2, 1080/2 - HEIGHT/2,
+ WIDTH, HEIGHT, 0, 0, 0);
+ XMapWindow(d, w);
+ XSelectInput(d, w, ExposureMask | KeyPressMask);
+
+ XClassHint *h = XAllocClassHint();
+ h->res_class="w_test";
+ XSetClassHint(d, w,h);
+
+
+ GC gc = XCreateGC(d, w, 0, NULL);
+ XSetForeground(d, gc, BG_COLOR);
+
+
+ int boxs = WIDTH / STEPS;
+ Position mv;
+ mv.x = 0;
+ mv.y = 0;
+ int px = boxs+mv.x;
+ int py = boxs+mv.y;
+ Color pc = GREEN;
+
+ while (1) {
+ int cx,cy;
+ cxy(&cx,&cy);
+ clear_buffer(BG_COLOR);
+
+ while (XPending(d)) {
+ XEvent e;
+ XNextEvent(d, &e);
+ if (e.type == KeyPress && XLookupKeysym(&e.xkey, 0) == XK_q)
+ exit(0);
+ }
+ if (strcmp(t,"game") ==0){
+
+ XEvent e;
+ XNextEvent(d, &e);
+ for(int i = 0;i<=WIDTH;i=i+boxs){
+ game_box(i, 0, boxs, 5,RED,WHITE);
+ game_box(0, i, boxs, 5,RED,WHITE);
+ game_box(i, HEIGHT-boxs, boxs, 5,RED,WHITE);
+ game_box(HEIGHT-boxs,i, boxs, 5,RED,WHITE);
+ }
+
+ if (e.type == KeyPress ){
+ int key = XLookupKeysym(&e.xkey,0);
+ if (key == XK_Left){
+ if (px - boxs >=boxs){
+ px = px -boxs;
+ }
+ }else if (key == XK_Right){
+ if (px + boxs <WIDTH - boxs){
+ px = px +boxs;
+ }
+ }else if (key == XK_Down){
+ if (py + boxs <HEIGHT - boxs){
+ py = py +boxs;
+ }
+ }else if (key == XK_v){
+ if (pc == RED)
+ pc = GREEN;
+ else
+ pc = RED;
+ }else if (key == XK_Up){
+ if (py > boxs){
+ py = py -boxs;
+ }
+ }else if (key == XK_q){
+ exit(0);
+ }
+ }
+
+ game_player(px, py, boxs, 3, BLACK, pc);
+
+ }else if(strcmp(t,"cube") == 0){
+ static float ang = 0.0f;
+ const float size = 150.0f;
+ const float dist = 400.0f;
+ const float scale = 250.0f;
+
+ float v[8][3] = {
+ {-size, -size, -size},
+ { size, -size, -size},
+ { size, size, -size},
+ {-size, size, -size},
+ {-size, -size, size},
+ { size, -size, size},
+ { size, size, size},
+ {-size, size, size},
+ };
+
+ float sx[8], sy[8];
+ float cosy = cosf(ang), siny = sinf(ang);
+ float angx = ang * 0.6f;
+ float cosx = cosf(angx), sinx = sinf(angx);
+
+ for (int i = 0; i < 8; ++i) {
+ float x = v[i][0] * cosy + v[i][2] * siny;
+ float z = -v[i][0] * siny + v[i][2] * cosy;
+ float y = v[i][1] * cosx - z * sinx;
+ z = v[i][1] * sinx + z * cosx;
+ float proj = scale / (z + dist);
+ sx[i] = cx + x * proj;
+ sy[i] = cy + y * proj;
+ }
+
+ int edges[12][2] = {
+ {0,1},{1,2},{2,3},{3,0},
+ {4,5},{5,6},{6,7},{7,4},
+ {0,4},{1,5},{2,6},{3,7}
+ };
+
+ Color edge_color = 0xFFFFFF;
+ for (int e = 0; e < 12; ++e) {
+ int a = edges[e][0], b = edges[e][1];
+ draw_line(sx[a], sy[a], sx[b], sy[b], edge_color);
+ }
+
+ for (int i = 0; i < 8; ++i)
+ draw_circlef((int)sx[i], (int)sy[i], 4, edge_color);
+
+ ang += 0.06f;
+ }else if(strcmp(t, "circle" )== 0){
+ static float t = 0.0f;
+ for (int r = 150; r > 0; r--) {
+ int red = (int)(128 + 127 * sin(t + r * 0.1));
+ int green = (int)(128 + 127 * sin(t + r * 0.2));
+ int blue = (int)(128 + 127 * sin(t + r * 0.3));
+ draw_circlef(cx, cy, r, (red << 16) | (green << 8) | blue);
+ }
+ t += 0.08f;
+ }else{
+ printf("./main game\n./main cube\n./main circle\n");
+ exit(1);
+ }
+
+
+ XImage *img = XCreateImage(d, DefaultVisual(d, 0), 24, ZPixmap, 0,
+ (char *)image, WIDTH, HEIGHT, 32, 0);
+ XPutImage(d, w, gc, img, 0, 0, 0, 0, WIDTH, HEIGHT);
+ img->data = NULL;
+ XDestroyImage(img);
+
+ usleep(1.0/FPS * 1000*1000);
+ }
+
+ XCloseDisplay(d);
+ return 0;
+}
+
|