-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathray_window.cpp
415 lines (359 loc) · 9.11 KB
/
ray_window.cpp
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "ray_window.h"
ray_window::ray_window(int v_XRES, int v_YRES)
{
// Set the XRES & YRES to the visiting variables
XRES = v_XRES;
YRES = v_YRES;
// Initialize the display
initialize();
// Initialize window_buffer
char *image32=(char *)malloc(XRES*YRES*4);
window_buffer = XCreateImage(current_display,
DefaultVisual(current_display,0),
DefaultDepth(current_display,
DefaultScreen(current_display)),
ZPixmap, 0, image32, XRES, YRES, 32, 0);
}
ray_window::~ray_window()
{
// Free up resources
close();
}
void ray_window::initialize()
{
unsigned long black,white;
// Create the X connection to open the current_displayplay
current_display = XOpenDisplay((char *)0);
screen = DefaultScreen(current_display);
black = BlackPixel(current_display,screen),
white = WhitePixel(current_display, screen);
// Create the window after the current_displayplay is initialized
current_window=XCreateSimpleWindow(current_display,
DefaultRootWindow(current_display),
0,0,
XRES, YRES, 5,
black, white);
// Set the properties of the window
XSetStandardProperties(current_display,
current_window,
"Ray Window",
"Ray Window",
None,NULL,0,NULL);
// Set the type of inputs that are allowed
XSelectInput(current_display, current_window,
ExposureMask|KeyPressMask|KeyReleaseMask);
// Create graphics content
graphics_context=XCreateGC(current_display, current_window, 0,0);
// Set the background and foreground colors
XSetBackground(current_display,graphics_context,white);
XSetForeground(current_display,graphics_context,black);
// Clear the window and bring it to the top of the other windows
XClearWindow(current_display, current_window);
XMapRaised(current_display, current_window);
}
void ray_window::close()
{
// Free up system resources
XDestroyImage(window_buffer);
XFreeGC(current_display, graphics_context);
XDestroyWindow(current_display,current_window);
XCloseDisplay(current_display);
}
// Chris if you're reading this this is problem a function to work on.
void ray_window::read_events(keyboard_input &inputs)
{
char keys[25];
int len = 25;
KeySym keysym;
inputs.W_SELECT = false;
/*
// Reset keyboard inputs
inputs.W_FORWARD = false;
inputs.W_BACKWARD = false;
inputs.W_LEFT = false;
inputs.W_RIGHT = false;
inputs.W_QUIT = false;
*/
// If there is an event to be read. Right now either
// key press or key release.
for(int i = 0; i < 10; i++)
if(XCheckMaskEvent(current_display,
KeyPressMask|KeyReleaseMask,
&event))
switch(event.type)
{
// Event a button was pressed
case KeyPress:
// Gets the keys pressed
len = XLookupString(&event.xkey,
keys, 25, &keysym,
NULL);
// Cycle through keys to see
// Which ones were pressed and
// Set the pressed ones to true
for(int i = 0; i < len; i++)
{
if (keys[i] == 'w')
{
inputs.W_FORWARD = true;
}
if (keys[i] == 's')
{
inputs.W_BACKWARD = true;
}
if (keys[i] == 'a')
{
inputs.W_LEFT = true;
}
if (keys[i] == 'd')
{
inputs.W_RIGHT = true;
}
if (keys[i] == 'p')
{
inputs.W_QUIT = true;
}
if (keys[i] == 'k')
{
// inputs.W_SELECT = true;
}
}
break;
case KeyRelease:
// Gets the keys pressed
len = XLookupString(&event.xkey,
keys, 25, &keysym,
NULL);
// Cycle through keys to see
// Which ones were pressed and
// Set the pressed ones to true
for(int i = 0; i < len; i++)
{
if (keys[i] == 'w')
{
inputs.W_FORWARD = false;
}
if (keys[i] == 's')
{
inputs.W_BACKWARD = false;
}
if (keys[i] == 'a')
{
inputs.W_LEFT = false;
}
if (keys[i] == 'd')
{
inputs.W_RIGHT = false;
}
if (keys[i] == 'p')
{
inputs.W_QUIT = false;
}
if (keys[i] == 'k')
{
inputs.W_SELECT = true;
}
}
break;
}
}
/*
Takes an array of wall_objects and displays them across the screen
the line object array must be smaller than the X resolution and
should be equal to it.
*/
void ray_window::line_cast_to_buffer(wall_object line_array[])
{
int current_point;
int draw_size;
// For every line in the X direction..
for(int i = 0; i < XRES; i++)
{
// Start from the center minus half of the size
current_point = (YRES / 2) - (line_array[i].get_size() / 2);
draw_size = (YRES / 2) + (line_array[i].get_size() / 2);
// If the size is off the window less than zero set the draw
// point to the edge
if(current_point < 0)
current_point = 0;
// While there are points that need to be drawn...
while(current_point < draw_size && current_point < YRES)
{
// Make sure we're within the bounds
if((current_point > 0) && (current_point < YRES))
{
// Draw the point on the window buffer
XPutPixel(window_buffer, i, current_point,
line_array[i].get_hex_color());
}
// Proceed to the next point
current_point++;
}
}
}
void ray_window::draw_text(int x, int y, std::string text, int color)
{
XSetForeground(current_display,graphics_context,
color);
XDrawString(current_display, current_window, graphics_context,
x, y, text.c_str(), text.length());
}
/*
Draws a rectangle from the top left point to the bottom right point.
Must be from top left to bottom right.
*/
void ray_window::draw_rectangle_to_buffer(int X1, int Y1,
int X2, int Y2,
int color)
{
// Loop through the X row
for(int i = X1; i < X2; i++)
{
// Make sure we're in bounds of the window buffer
if((i > 0) && (i < XRES))
{
// Loop through the Y row
for(int j = Y1; j < Y2; j++)
{
// Make sure we're still in bounds
if((j > 0) && (j < YRES))
{
// Draw the point on to the
// window buffer
XPutPixel(window_buffer, i, j, color);
}
}
}
}
}
/*
Draws the window buffer to the screen
*/
void ray_window::draw_buffer()
{
// Send window_buffer to the screen
XPutImage(current_display, current_window,
DefaultGC(current_display, 0), window_buffer,
0,0,0,0, XRES, YRES);
}
void ray_window::line_cast(wall_object line_array[])
{
XClearWindow(current_display, current_window);
// Set the draw color to white and draw the ceiling
XSetForeground(current_display,graphics_context,
0xFFFFFF
);
XFillRectangle(current_display,
current_window,
graphics_context,
0, 0,
XRES, (YRES / 2));
// Set the draw color to tan and draw the floor
XSetForeground(current_display,graphics_context,
0xBA924E
);
XFillRectangle(current_display,
current_window,
graphics_context,
0, (YRES / 2),
XRES, YRES);
for(int i = 0; i < XRES; i++)
{
// Set the draw color to the shade of the current
// wall_object
XSetForeground(current_display,graphics_context,
line_array[i].get_hex_color()
);
// If the size of the wall is greater than zero, as in it
// exists, then draw the wall
if(line_array[i].get_size() > 0)
XDrawLine(current_display,
current_window,
graphics_context,
i, ((YRES / 2) - (line_array[i].get_size() / 2)),
i, ((YRES / 2) + (line_array[i].get_size() / 2)));
}
}
void ray_window::clear()
{
// Set the window to white
XClearWindow(current_display, current_window);
}
void ray_window::draw_text_box(int x, int y,
int x_size, int y_size,
std::string text,
int color)
{
XSetForeground(current_display,graphics_context, color);
XDrawRectangle(current_display, current_window, graphics_context,
x, y, x_size, y_size);
draw_text(x + 8, y + (y_size / 2), text, 0x000000);
}
void ray_window::draw_box(int x, int y,
int x_size, int y_size,
int color)
{
XSetForeground(current_display,graphics_context, color);
XDrawRectangle(current_display, current_window, graphics_context,
x, y, x_size, y_size);
}
void ray_window::fill_box(int x, int y,
int x_size, int y_size,
int color)
{
XSetForeground(current_display,graphics_context, color);
XFillRectangle(current_display,
current_window,
graphics_context,
x, y,
x_size, y_size);
}
int ray_window::read_buffered_input()
{
int input = -1;
// Read next event.
XNextEvent(current_display, &event);
switch(event.type)
{
case KeyPress:
char keys[25];
int len = 25;
KeySym keysym;
len = XLookupString(&event.xkey,
keys, 25, &keysym,
NULL);
for(int i = 0; i < len; i++)
{
if (keys[i] == 'w')
{
input = 1;
}
else if (keys[i] == 's')
{
input = 2;
}
else if (keys[i] == 'a')
{
input = 3;
}
else if (keys[i] == 'd')
{
input = 4;
}
else if (keys[i] == 'p')
{
input = 5;
}
else if (keys[i] == 'k')
{
input = 6;
}
else
{
input = -1;
}
}
break;
}
return input;
}