/
lab1.cpp
executable file
·473 lines (421 loc) · 11.3 KB
/
lab1.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
//cs335 Spring 2015 Lab-1
//This program demonstrates the use of OpenGL and XWindows
//
//Assignment is to modify this program.
//You will follow along with your instructor.
//
//Elements to be learned in this lab...
//
//. general animation framework
//. animation loop
//. object definition and movement
//. collision detection
//. mouse/keyboard interaction
//. object constructor
//. coding style
//. defined constants
//. use of static variables
//. dynamic memory allocation
//. simple opengl components
//. git
//
//elements we will add to program...
//. Game constructor
//. multiple particles
//. gravity
//. collision detection
//. more objects
//
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <GL/glx.h>
extern"C" {
#include "fonts.h"
}
#define WINDOW_WIDTH 800
#define WINDOW_HEIGHT 600
#define MAX_PARTICLES 8000
#define GRAVITY 0.1
//for check keys
int bubbler=0;
//int keys[65536];
//X Windows variables
Display *dpy;
Window win;
GLXContext glc;
//Structures
struct Vec {
float x, y, z;
};
struct Shape {
float width, height;
float radius;
Vec center;
};
struct Particle {
Shape s;
Vec velocity;
};
struct Game {
Shape box[5];
Shape circle;
Particle particle[MAX_PARTICLES];
int n;
int lastMousex;
int lastMousey;
};
//Function prototypes
void initXWindows(void);
void init_opengl(void);
void cleanupXWindows(void);
void check_mouse(XEvent *e, Game *game);
int check_keys(XEvent *e, Game *game);
void movement(Game *game);
void render(Game *game);
int main(void)
{
int done=0;
srand(time(NULL));
initXWindows();
init_opengl();
//declare game object
Game game;
game.n=0;
//declare a box shape
game.box[0].width = 100;
game.box[0].height = 10;
game.box[0].center.x = 270 + 5*65;
game.box[0].center.y = 500 - 5*60;
game.box[1].width = 100;
game.box[1].height = 10;
game.box[1].center.x = 270 + 5*65 - 75;
game.box[1].center.y = 500 - 5*60 + 45;
game.box[2].width = 100;
game.box[2].height = 10;
game.box[2].center.x = 270 + 5*65 - 150;
game.box[2].center.y = 500 - 5*60 + 90;
game.box[3].width = 100;
game.box[3].height = 10;
game.box[3].center.x = 270 + 5*65 - 225;
game.box[3].center.y = 500 - 5*60 + 135;
game.box[4].width = 100;
game.box[4].height = 10;
game.box[4].center.x = 270 + 5*65 - 300;
game.box[4].center.y = 500 - 5*60 + 180;
game.circle.center.x = 760;
game.circle.center.y = -25;
game.circle.radius = 100;
//start animation
while(!done) {
while(XPending(dpy)) {
XEvent e;
XNextEvent(dpy, &e);
check_mouse(&e, &game);
done = check_keys(&e, &game);
}
movement(&game);
render(&game);
glXSwapBuffers(dpy, win);
}
cleanupXWindows();
cleanup_fonts();
return 0;
}
void set_title(void)
{
//Set the window title bar.
XMapWindow(dpy, win);
XStoreName(dpy, win, "335 Lab1 LMB for particle");
}
void cleanupXWindows(void) {
//do not change
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
void initXWindows(void) {
//do not change
GLint att[] = { GLX_RGBA, GLX_DEPTH_SIZE, 24, GLX_DOUBLEBUFFER, None };
int w=WINDOW_WIDTH, h=WINDOW_HEIGHT;
dpy = XOpenDisplay(NULL);
if (dpy == NULL) {
std::cout << "\n\tcannot connect to X server\n" << std::endl;
exit(EXIT_FAILURE);
}
Window root = DefaultRootWindow(dpy);
XVisualInfo *vi = glXChooseVisual(dpy, 0, att);
if(vi == NULL) {
std::cout << "\n\tno appropriate visual found\n" << std::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 init_opengl(void)
{
//OpenGL initialization
glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
//Initialize matrices
glMatrixMode(GL_PROJECTION); glLoadIdentity();
glMatrixMode(GL_MODELVIEW); glLoadIdentity();
//Set 2D mode (no perspective)
glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, -1, 1);
//Set the screen background color
glClearColor(0.1, 0.1, 0.1, 1.0);
//Do this to allow fonts
glEnable(GL_TEXTURE_2D);
initialize_fonts();
}
#define rnd() (float)rand() / (float)RAND_MAX
void makeParticle(Game *game, int x, int y) {
if (game->n >= MAX_PARTICLES)
return;
std::cout << "makeParticle() " << x << " " << y << std::endl;
//position of particle
Particle *p = &game->particle[game->n];
p->s.center.x = x;
p->s.center.y = y;
p->velocity.y = rnd()*0.1 - 0.5;
p->velocity.x = 0 + rnd()*1; /*1.0 + rnd()*0.1;*/
game->n++;
}
void check_mouse(XEvent *e, Game *game)
{
static int savex = 0;
static int savey = 0;
//static int n = 0; took out
if (e->type == ButtonRelease) {
return;
}
if (e->type == ButtonPress) {
if (e->xbutton.button==1) {
//Left button was pressed
int y = WINDOW_HEIGHT - e->xbutton.y;
for(int i=0; i<10; i++)
makeParticle(game, e->xbutton.x, y);
return;
}
if (e->xbutton.button==3) {
//Right button was pressed
return;
}
}
//Did the mouse move?
if (savex != e->xbutton.x || savey != e->xbutton.y) {
savex = e->xbutton.x;
savey = e->xbutton.y;
int y = WINDOW_HEIGHT - e->xbutton.y;
/*if(bubbler){
for (int i=0; i<10; i++)
makeParticle(game, e->xbutton.x, y); //make particle if mouse moves
}*/
//if (n++ < 10;
//return;
game->lastMousex = e->xbutton.x; //added
game->lastMousey = y; //added
}
}
int check_keys(XEvent *e, Game *game)
{
/* static int shift=0;
int key = XLookupKeysym(&e->xkey, 0);
if(e->type == KeyRelease) {
if (key == XK_Shift_L || key == XK_Shift_R)
shift=0;
return;
}
if (e->type == KeyPress) {
if(key == XK_Shift_L || key == XK_Shift_R) {
shift=1;
return;
}
}else {
return;
}
switch(key) {
case XK_Escape:
return 1;
case XK_d:
bubbler ^= 1;
}
}
*/
//Was there input from the keyboard?
if (e->type == KeyPress) {
int key = XLookupKeysym(&e->xkey, 0);
if (key == XK_Escape) {
return 1;
}
//You may check other keys here
if (key == XK_b) {
bubbler ^= 1 ;
std::cout << "b works " << bubbler << std::endl;
return 0;
}
}
return 0;
}
void movement(Game *game)
{
Particle *p;
if (game->n <= 0)
return;
/*if(bubbler){
for(int i=0; i<10; i++)
makeParticle(game, game->lastMousex, game->lastMousey);
}*/ //make constant particles
for(int i=0; i<game->n; i++){
p = &game->particle[i];
p->s.center.x += p->velocity.x;
p->s.center.y += p->velocity.y;
p->velocity.y -= GRAVITY;
//check for collision with shapes...
//add for loop using j and number of boxes
for (int j=0; j<5; j++) {
Shape *s = &game->box[j]; //make &game->box[]
if (p->s.center.y < s->center.y + s->height &&
p->s.center.y > s->center.y - s->height &&
p->s.center.x >= s->center.x - s->width &&
p->s.center.x <= s->center.x + s->width){
p->velocity.y *= -0.45;
p->s.center.y = s->center.y + s->height + 0.01;
p->velocity.x = 1.0;
// p->s.center.y = s->center.y + s->height;
// p->velocity.y *= -.5;
// p->velocity.x = 2; /*1.0 + rnd()*0.1;*/
}
}
//check circle collison
float d0,d1,dist;
d0 = p->s.center.x - game->circle.center.x;
d1 = p->s.center.y - game->circle.center.y;
dist = sqrt(d0*d0 + d1*d1);
if(dist <= game->circle.radius) {
//p->velocity.y = -p->velocity.y;
//float v[2];
d0 /= dist;
d1 /= dist;
d0 *= game->circle.radius * 1.01;
d1 *= game->circle.radius * 1.01;
p->s.center.x = game->circle.center.x + d0;
p->s.center.y = game->circle.center.y + d1;
p->velocity.x += d0 * 0.02;
p->velocity.y += d0 * 0.02;
}
//end for loop
//check for off-screen
if (p->s.center.y < 0.0 || p->s.center.y > WINDOW_HEIGHT ) {
//std::cout << "off screen" << std::endl;
memcpy(&game->particle[i], &game->particle[game->n-1], sizeof(Particle));
game->n--;
}
}
}
void render(Game *game)
{
float w, h;
glClear(GL_COLOR_BUFFER_BIT);
//Draw shapes...
//
//circle
static int firsttime=1;
static int verts[60][2];
static int n = 60;
if (firsttime) {
float angle = 0.0;
float inc = (3.14159 * 2.0) / (float)n;
for (int i=0; i<n; i++) {
verts[i][0] = cos(angle) * game->circle.radius + game->circle.center.x;
verts[i][1] = sin(angle) * game->circle.radius + game->circle.center.y;
angle += inc;
}
firsttime=0;
}
glColor3ub(90,140,90);
glPushMatrix();
glBegin(GL_TRIANGLE_FAN);
for (int i=0; i<n; i++)
glVertex2i(verts[i][0], verts[i][1]);
glEnd();
glPopMatrix();
//press b to turn on bubbler
if(bubbler){
for(int i=0; i<10; i++)
makeParticle(game, game->lastMousex, game->lastMousey);
}
//draw box
Shape *s;
glColor3ub(90,140,90);
for (int j=0; j<5; j++){
s = &game->box[j];
glPushMatrix();
glTranslatef(s->center.x, s->center.y, s->center.z);
w = s->width;
h = s->height;
glBegin(GL_QUADS);
glVertex2i(-w,-h);
glVertex2i(-w, h);
glVertex2i( w, h);
glVertex2i( w,-h);
glEnd();
glPopMatrix();
}
//draw all particles here
for(int i=0; i<game->n; i++){
glPushMatrix();
glColor3ub(rnd()*150,rnd()*160,220);
Vec *c = &game->particle[i].s.center;
w = 2;
h = 2;
glBegin(GL_QUADS);
glVertex2i(c->x-w, c->y-h);
glVertex2i(c->x-w, c->y+h);
glVertex2i(c->x+w, c->y+h);
glVertex2i(c->x+w, c->y-h);
glEnd();
glPopMatrix();
}
Rect r[4];
Rect re;
Rect rec;
glBindTexture(GL_TEXTURE_2D, 0);
r[0].bot = 495 - 5 * 60;
r[0].left = 270 + 5 * 65;
r[0].center = -100;
r[1].bot = 495 - 5 * 60 + 45;
r[1].left = 270 + 5 * 65 - 75;
r[1].center = -100;
r[2].bot = 495 - 5 * 60 + 90;
r[2].left = 270 + 5 * 65 - 150;
r[2].center = -100;
r[3].bot = 495 - 5 * 60 + 135;
r[3].left = 270 + 5 * 65 - 225;
r[3].center = -100;
re.bot = 495 - 5 * 60 + 180;
re.left = 270 + 5 * 65 - 300;
re.center = -100;
rec.bot = 495 - 5 * 60 + 225;
rec.left = 270 + 5 * 65 - 375;
rec.center = -100;
unsigned int cref = 0x00ffff;
ggprint8b(&r[0], 16, cref, "Maintenance");
ggprint8b(&r[1], 16, cref, "Testing");
ggprint8b(&r[2], 16, cref, "Coding");
ggprint8b(&r[3], 16, cref, "Design");
ggprint8b(&re, 16, cref, "Requirements");
ggprint16(&rec, 16, 0x00ffffff, "Waterfall Model");
}