-
Notifications
You must be signed in to change notification settings - Fork 1
/
X11wrapper.h
executable file
·273 lines (255 loc) · 6.33 KB
/
X11wrapper.h
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
#ifndef _X11wrapper_H_
#define _X11wrapper_H_
/*
This code was originally written by Gordon Griesel
of California State Univ, Bakersfield
Modifed by Douglas Rank and Kenneth wood in Spring of 2022
*/
#include <iostream>
#include <X11/Xlib.h>
#include <X11/extensions/Xdbe.h>
#include <GL/glx.h>
#include "global.h"
#include "player.h"
using namespace std;
// X11 setup --------------------------------------------------------------------------------------
class X11_wrapper {
private:
Display *dpy;
Window win;
GLXContext glc;
GC gc;
XdbeBackBuffer backBuffer;
XdbeSwapInfo swapInfo;
public:
~X11_wrapper();
X11_wrapper();
void set_title();
bool getXPending();
XEvent getXNextEvent();
void swapBuffers();
void reshape_window(int width, int height);
void check_resize(XEvent *e);
void check_mouse(XEvent *e);
int check_keys(XEvent *e);
void drawText(int x, int y, char *text);
unsigned long set_color_3i(int r, int g, int b);
} x11;
X11_wrapper::~X11_wrapper()
{
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
X11_wrapper::X11_wrapper()
{
int major, minor;
XdbeBackBufferAttributes *backAttr;
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
int w = g.xres, h = g.yres;
dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
cout << "\n\tcannot connect to X server\n" << endl;
exit(EXIT_FAILURE);
}
Window root = DefaultRootWindow(dpy);
XVisualInfo *vi = glXChooseVisual(dpy, 0, att);
if (vi == NULL) {
cout << "\n\tno appropriate visual found\n" << endl;
exit(EXIT_FAILURE);
}
Colormap cmap = XCreateColormap(dpy, root, vi->visual, AllocNone);
XSetWindowAttributes swa;
swa.colormap = cmap;
swa.event_mask =
ExposureMask | KeyPressMask | KeyReleaseMask |
ButtonPress | ButtonReleaseMask |
PointerMotionMask |
StructureNotifyMask | SubstructureNotifyMask;
win = XCreateWindow(dpy, root, 0, 0, w, h, 0, vi->depth,
InputOutput, vi->visual, CWColormap | CWEventMask, &swa);
//Create gc
gc = XCreateGC(dpy, win, 0, NULL);
//Get DBE version
if (!XdbeQueryExtension(dpy, &major, &minor)) {
fprintf(stderr, "Error : unable to fetch Xdbe Version.\n");
XFreeGC(dpy, gc);
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
//printf("Xdbe version %d.%d\n",major,minor);
//Get back buffer and attributes (used for swapping)
backBuffer = XdbeAllocateBackBufferName(dpy, win, XdbeUndefined);
backAttr = XdbeGetBackBufferAttributes(dpy, backBuffer);
swapInfo.swap_window = backAttr->window;
swapInfo.swap_action = XdbeUndefined;
XFree(backAttr);
//Map and raise window
set_title();
glc = glXCreateContext(dpy, vi, NULL, GL_TRUE);
glXMakeCurrent(dpy, win, glc);
}
void X11_wrapper::set_title()
{
//Set the window title bar.
XMapWindow(dpy, win);
XStoreName(dpy, win, "Tower Defense Project");
}
bool X11_wrapper::getXPending()
{
//See if there are pending events.
return XPending(dpy);
}
XEvent X11_wrapper::getXNextEvent()
{
//Get a pending event.
XEvent e;
XNextEvent(dpy, &e);
return e;
}
void X11_wrapper::swapBuffers()
{
glXSwapBuffers(dpy, win);
}
void X11_wrapper::reshape_window(int width, int height)
{
//window has been resized.
g.xres = width;
g.yres = height;
//
glViewport(0, 0, (GLint)width, (GLint)height);
glMatrixMode(GL_PROJECTION); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
glOrtho(0, g.xres, 0, g.yres, -1, 1);
}
void X11_wrapper::check_resize(XEvent *e)
{
//The ConfigureNotify is sent by the
//server if the window is resized.
if (e->type != ConfigureNotify)
return;
XConfigureEvent xce = e->xconfigure;
if (xce.width != g.xres || xce.height != g.yres) {
//Window size did change.
reshape_window(xce.width, xce.height);
}
}
// end x11 setup --------------------------------------------------------------------------------
//
void X11_wrapper::check_mouse(XEvent *e)
{
static int savex = 0;
static int savey = 0;
//Weed out non-mouse events
if (e->type != ButtonRelease &&
e->type != ButtonPress &&
e->type != MotionNotify) {
//This is not a mouse event that we care about.
return;
}
//
if (e->type == ButtonRelease) {
if(g.showTowerRange)
g.showTowerRange = 0;
return;
}
if (e->type == ButtonPress) {
if (e->xbutton.button==1) {
//Left button was pressed.
if (g.gameState == START) {
if (g.xMousePos >= 270 && g.xMousePos <= 270+startButton.width) {
if (g.yMousePos <= 420 && g.yMousePos >= 420-startButton.height) {
g.gameState = PLAYING;
}
}
if (g.xMousePos >= 278 && g.xMousePos <= 278+ctrlButton.width) {
if (g.yMousePos <= 475 && g.yMousePos >= 475-startButton.height) {
g.showControls = 1;
}
}
}
else if (g.buildState == BUY) {
player.addTower(g.xMousePos, g.yMousePos);
}
else if (g.buildState == SELL) {
player.removeTower(g.xMousePos, g.yMousePos);
}
return;
}
if (e->xbutton.button==3) {
//Right button was pressed.
int mapi = g.xMousePos/g.tile_pxSize;
int mapj = 9-g.yMousePos/g.tile_pxSize;
int key = mapi*10 + mapj;
if (player.towerHash[key])
g.showTowerRange = 1;
return;
}
}
if (e->type == MotionNotify) {
//The mouse moved!
if (savex != e->xbutton.x || savey != e->xbutton.y) {
savex = e->xbutton.x;
savey = e->xbutton.y;
//Code placed here will execute whenever the mouse moves.
//printf("mouse moved\n");
g.xMousePos = (int)savex;
g.yMousePos = (int)savey;
}
}
}
int X11_wrapper::check_keys(XEvent *e)
{
if (e->type != KeyPress && e->type != KeyRelease)
return 0;
int key = XLookupKeysym(&e->xkey, 0);
if (e->type == KeyPress) {
switch (key) {
case XK_b:
if(g.buildState != BUY) {
g.buildState = BUY;
break;
}
g.buildState = NONE;
break;
case XK_d:
if(!g.debug) {
g.debug = 1;
break;
}
g.debug = 0;
break;
case XK_p:
if(g.gameState != PAUSE) {
g.gameState = PAUSE;
break;
}
g.gameState = PLAYING;
break;
case XK_s:
if(!g.spawnWave) {
if(!game.saveWaveSpawn) { game.saveWaveSpawn = 1; }
g.spawnWave = 1;
break;
}
g.waitForPlayer = 0;
break;
case XK_x:
if(g.buildState != SELL) {
g.buildState = SELL;
break;
}
g.buildState = NONE;
break;
case XK_t:
g.gameState = END;
break;
case XK_Escape:
if (g.gameState == START && g.showControls == 1)
g.showControls = 0;
else
return 1;
}
}
return 0;
}
#endif //_X11wrapper_H_