-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1.cpp
More file actions
290 lines (258 loc) · 5.63 KB
/
lab1.cpp
File metadata and controls
290 lines (258 loc) · 5.63 KB
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
//modified by:
//date:
//
//author: Gordon Griesel
//date: Spring 2022
//purpose: get openGL working on your personal computer
//
#include <iostream>
using namespace std;
#include <stdio.h>
#include <unistd.h>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <GL/glx.h>
//some structures
class Global {
public:
int xres, yres;
Global();
} g;
class X11_wrapper {
private:
Display *dpy;
Window win;
GLXContext glc;
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);
} x11;
//Function prototypes
void init_opengl(void);
void physics(void);
void render(void);
//=====================================
// MAIN FUNCTION IS HERE
//=====================================
int main()
{
init_opengl();
//Main loop
int done = 0;
while (!done) {
//Process external events.
while (x11.getXPending()) {
XEvent e = x11.getXNextEvent();
x11.check_resize(&e);
x11.check_mouse(&e);
done = x11.check_keys(&e);
}
physics();
render();
x11.swapBuffers();
usleep(200);
}
return 0;
}
Global::Global()
{
xres = 400;
yres = 200;
}
X11_wrapper::~X11_wrapper()
{
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
X11_wrapper::X11_wrapper()
{
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);
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, "3350 Lab1");
}
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);
}
}
//-----------------------------------------------------------------------------
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) {
return;
}
if (e->type == ButtonPress) {
if (e->xbutton.button==1) {
//Left button was pressed.
//int y = g.yres - e->xbutton.y;
return;
}
if (e->xbutton.button==3) {
//Right button was pressed.
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.
}
}
}
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_1:
//Key 1 was pressed
break;
case XK_Escape:
//Escape key was pressed
return 1;
}
}
return 0;
}
void init_opengl(void)
{
//OpenGL initialization
glViewport(0, 0, g.xres, g.yres);
//Initialize matrices
glMatrixMode(GL_PROJECTION); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
//Set 2D mode (no perspective)
glOrtho(0, g.xres, 0, g.yres, -1, 1);
//Set the screen background color
glClearColor(0.1, 0.1, 0.1, 1.0);
}
//Starting from here >>> I'm making edit but
//I'm unsure of what I need to do. 1/30/2023
//Copy pics from phone and update program
//on the next class
//These global variables might be wrong
float w = 20.0f;
float dir = 1.0f;
float pos[2] = {0.0f+w, g.yres/2.0f};
float boxr = 1.0f, boxg = 0.0f, boxb = 0.0f;
void physics()
{
//This might be wrong
pos[0] += dir;
if (pos[0] >= (g.xres-w)) {
pos[0] = (g.xres-w);
dir = -dir;
boxr = 0.0f;
boxb = 1.0f;
}
if (pos[0] <= w) {
pos[0] = w;
dir = -dir;
boxr = 1.0f;
boxb = 0.0f;
}
}
void render()
{
glClear(GL_COLOR_BUFFER_BIT);
//Drawbox.
glPushMatrix();
glColor3f(boxr, boxg, boxb);
glTranslatef(pos[0], pos[1], 0.0f);
glBegin(GL_QUADS);
glVertex2f(-w, -w);
glVertex2f(-w, w);
glVertex2f( w, w);
glVertex2f( w, -w);
glEnd();
glPopMatrix();
}