-
Notifications
You must be signed in to change notification settings - Fork 0
/
k15_x11template.c
201 lines (160 loc) · 4.44 KB
/
k15_x11template.c
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
#define _GNU_SOURCE 1
#include "stdio.h"
#include "time.h"
#include "string.h"
#include "unistd.h"
#include "X11/Xlib.h"
#include "GL/gl.h"
#include "GL/glx.h"
#define K15_FALSE 0
#define K15_TRUE 1
#define GLX_CONTEXT_MAJOR_VERSION_ARB 0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB 0x2092
#define GLX_CONTEXT_FLAGS_ARB 0x2094
#define GLX_CONTEXT_CORE_PROFILE_BIT_ARB 0x00000001
typedef unsigned char bool8;
typedef unsigned char byte;
typedef unsigned int uint32;
typedef unsigned short uint16;
typedef unsigned char uint8;
Display* mainDisplay = 0;
GC mainGC;
Atom deleteMessage = 0;
int nanoSecondsPerFrame = 16000000;
typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
void handleKeyPress(XEvent* p_Event)
{
}
void handleKeyRelease(XEvent* p_Event)
{
}
void handleButtonPress(XEvent* p_Event)
{
}
void handleButtonRelease(XEvent* p_Event)
{
}
void handleMouseMotion(XEvent* p_Event)
{
}
void handleWindowResize(XEvent* p_Event)
{
int width = p_Event->xconfigure.width;
int height = p_Event->xconfigure.height;
}
bool8 filterEvent(XEvent* p_Event)
{
if (p_Event->type == KeyRelease)
{
if (XEventsQueued(mainDisplay, QueuedAfterReading))
{
XEvent tempEvent;
XPeekEvent(mainDisplay, &tempEvent);
if (tempEvent.type == KeyPress && tempEvent.xkey.time == p_Event->xkey.time &&
tempEvent.xkey.keycode == p_Event->xkey.keycode)
{
XNextEvent(mainDisplay, &tempEvent);
return K15_TRUE;
}
}
}
return K15_FALSE;
}
void handleEvent(XEvent* p_Event)
{
if (filterEvent(p_Event))
return;
if (p_Event->type == KeyPress)
handleKeyPress(p_Event);
else if (p_Event->type == KeyRelease)
handleKeyRelease(p_Event);
else if (p_Event->type == ButtonPress)
handleButtonPress(p_Event);
else if (p_Event->type == ButtonRelease)
handleButtonRelease(p_Event);
else if (p_Event->type == MotionNotify)
handleMouseMotion(p_Event);
else if (p_Event->type == ConfigureNotify)
handleWindowResize(p_Event);
}
int errorHandler(Display* p_Display, XErrorEvent* p_Event)
{
uint32 errorCode = p_Event->error_code;
char errorMessage[256];
XGetErrorText(p_Display, errorCode, errorMessage, 256);
printf("Error (%d): '%s'\n", errorCode, errorMessage);
return 0;
}
void setupWindow(Window* p_WindowOut, int p_Width, int p_Height)
{
int borderWidth = 1;
int eventMask = ButtonPressMask | ButtonReleaseMask | PointerMotionMask |
KeyPressMask | KeyReleaseMask | StructureNotifyMask;
Window window = XCreateSimpleWindow(mainDisplay, XRootWindow(mainDisplay, XDefaultScreen(mainDisplay)),
0, 0, p_Width, p_Height, 0, 0, 0);
*p_WindowOut = window;
mainGC = XCreateGC(mainDisplay, window, 0, 0);
XSetForeground(mainDisplay, mainGC, (~0));
XStoreName(mainDisplay, window, "Test Window!");
XSelectInput(mainDisplay, window, eventMask);
XSetWMProtocols(mainDisplay, window, &deleteMessage, 1);
XMapWindow(mainDisplay, window);
}
bool8 setup(Window* p_WindowOut)
{
XSetErrorHandler(errorHandler);
mainDisplay = XOpenDisplay(0);
if (mainDisplay)
{
deleteMessage = XInternAtom(mainDisplay, "WM_DELETE_WINDOW", False);
setupWindow(p_WindowOut, 800, 600);
return K15_TRUE;
}
return K15_FALSE;
}
void drawDeltaTime(Window* p_MainWindow, long p_DeltaTimeInNs)
{
char buffer[256];
sprintf(buffer, "Milliseconds:%ld", p_DeltaTimeInNs / 1000000);
XDrawString(mainDisplay, *p_MainWindow, mainGC, 20, 20, buffer, strlen(buffer));
}
void doFrame(Window* p_MainWindow, long p_DeltaTimeInNs)
{
XClearWindow(mainDisplay, *p_MainWindow);
drawDeltaTime(p_MainWindow, p_DeltaTimeInNs);
XFlush(mainDisplay);
XSync(mainDisplay, *p_MainWindow);
}
int main(int argc, char** argv)
{
Window mainWindow;
setup(&mainWindow);
struct timespec timeFrameStarted = {0};
struct timespec timeFrameEnded = {0};
long deltaNs = 0;
bool8 loopRunning = K15_TRUE;
XEvent event = {0};
while (loopRunning)
{
clock_gettime(CLOCK_MONOTONIC, &timeFrameStarted);
while (XPending(mainDisplay))
{
XNextEvent(mainDisplay, &event);
if (event.type == ClientMessage &&
event.xclient.data.l[0] == deleteMessage)
{
loopRunning = K15_FALSE;
}
handleEvent(&event);
}
doFrame(&mainWindow, deltaNs);
clock_gettime(CLOCK_MONOTONIC, &timeFrameEnded);
deltaNs = timeFrameEnded.tv_nsec - timeFrameStarted.tv_nsec;
if (deltaNs < nanoSecondsPerFrame)
{
struct timespec sleepTime = {0, nanoSecondsPerFrame - deltaNs};
nanosleep(&sleepTime, 0);
}
}
return 0;
}