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
|
// TODO(kard): destroy this file, i don't care about platform layer rn this is some gpt trash
// it flickers so bad
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <bits/time.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#define SPONGE_IMPLEMENTATION
#include "../../sponge.h"
#define STB_IMAGE_IMPLEMENTATION
#include "../vendor/stb_image.h"
#include "../example.h"
#define DEFAULT_WIDTH 1280
#define DEFAULT_HEIGHT 720
#define TARGET_FRAME_TIME_NS 16666667L
static size_t pixels_buffer_count = 0;
static sponge_Texture canvas = { 0 };
static float *depths = NULL;
static Display *dpy;
static XImage *ximg = NULL;
void timespec_add_ns(struct timespec *inout_t, long ns) {
inout_t->tv_nsec += ns;
while (inout_t->tv_nsec >= 1000000000L) {
inout_t->tv_nsec -= 1000000000L;
inout_t->tv_sec += 1;
}
}
long long timespec_sub_ns(struct timespec *a, struct timespec *b) {
long long sec = a->tv_sec - b->tv_sec;
long long result = a->tv_nsec - b->tv_nsec;
result += sec * 1000000000L;
return result;
}
int timespec_cmp(struct timespec *a, struct timespec *b) {
if (a->tv_sec < b->tv_sec) return -1;
if (a->tv_sec > b->tv_sec) return 1;
if (a->tv_nsec < b->tv_nsec) return -1;
if (a->tv_nsec > b->tv_nsec) return 1;
return 0;
}
void init_canvas(uint32_t width, uint32_t height) {
if (ximg) {
ximg->data = NULL;
XDestroyImage(ximg);
ximg = NULL;
}
size_t new_count = width * height;
if (new_count > pixels_buffer_count) {
sponge_Color32 *new_pixels_buffer = malloc(new_count * sizeof(uint32_t));
float *new_depths = malloc(new_count * sizeof(float));
if (!new_pixels_buffer || !new_depths) {
free(new_pixels_buffer);
free(new_depths);
exit(1);
fprintf(stderr, "failed to allocate pixel buffer\n");
}
free(canvas.pixels);
free(depths);
canvas.pixels = new_pixels_buffer;
depths = new_depths;
pixels_buffer_count = new_count;
}
canvas.width = width;
canvas.height = height;
canvas.stride_pixels = width;
// TODO(kard): i assume this can fail
ximg = XCreateImage(
dpy,
DefaultVisual(dpy, 0),
DefaultDepth(dpy, 0),
ZPixmap,
0,
(char*)canvas.pixels,
width,
height,
32,
0
);
}
int main() {
dpy = XOpenDisplay(NULL);
if (!dpy) {
fprintf(stderr, "failed to open display\n");
return 1;
}
int screen = DefaultScreen(dpy);
// TODO(kard): i assume all of the following x functions can fail
// clanker only checked XOpenDisplay for some reason
Window win = XCreateSimpleWindow(
dpy,
RootWindow(dpy, screen),
10, 10, DEFAULT_WIDTH, DEFAULT_HEIGHT,
1,
BlackPixel(dpy, screen),
BlackPixel(dpy, screen)
);
Atom atom_wm_delete_window = XInternAtom(dpy, "WM_DELETE_WINDOW", False);
XSetWMProtocols(dpy, win, &atom_wm_delete_window, 1);
XStoreName(dpy, win, "sponge");
XSelectInput(dpy, win, ExposureMask | PointerMotionMask | StructureNotifyMask);
GC gc = DefaultGC(dpy, screen);
XMapWindow(dpy, win);
init_canvas(DEFAULT_WIDTH, DEFAULT_HEIGHT);
init();
struct timespec ts_next;
if (clock_gettime(CLOCK_MONOTONIC, &ts_next)) {
fprintf(stderr, "clock_gettime fail\n");
return 1;
}
timespec_add_ns(&ts_next, TARGET_FRAME_TIME_NS);
int running = 1;
while (running) {
if (clock_nanosleep(CLOCK_MONOTONIC, TIMER_ABSTIME, &ts_next, NULL)) {
fprintf(stderr, "clock_nanosleep fail\n");
return 1;
}
struct timespec ts_now;
if (clock_gettime(CLOCK_MONOTONIC, &ts_now)) {
fprintf(stderr, "clock_gettime fail\n");
return 1;
}
if (sponge_texture_valid(canvas)) {
draw_frame_3d(canvas, depths);
struct timespec ts_draw;
if (clock_gettime(CLOCK_MONOTONIC, &ts_draw)) {
fprintf(stderr, "clock_gettime fail\n");
return 1;
}
long long draw_time = timespec_sub_ns(&ts_draw, &ts_now);
fprintf(stderr, "draw time: %3lld.%03lldms\n", draw_time / 1000000, (draw_time % 1000000) / 1000);
XPutImage(dpy, win, gc, ximg, 0, 0, 0, 0, canvas.width, canvas.height);
}
while (XPending(dpy)) {
XEvent ev;
XNextEvent(dpy, &ev);
switch (ev.type) {
case Expose:
//draw_frame(canvas);
break;
case ConfigureNotify:
init_canvas(ev.xconfigure.width, ev.xconfigure.height);
break;
case MotionNotify:
mouse_move(ev.xmotion.x, ev.xmotion.y);
break;
case ClientMessage:
if ((Atom)ev.xclient.data.l[0] == atom_wm_delete_window) {
running = 0;
}
break;
}
}
while (timespec_cmp(&ts_next, &ts_now) <= 0) {
timespec_add_ns(&ts_next, TARGET_FRAME_TIME_NS);
if (clock_gettime(CLOCK_MONOTONIC, &ts_now)) {
fprintf(stderr, "clock_gettime fail\n");
return 1;
}
}
}
if (ximg) {
ximg->data = NULL; // prevent double free
XDestroyImage(ximg);
}
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
return 0;
}
|