/
hw1.cpp
443 lines (359 loc) · 10.1 KB
/
hw1.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
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#include <cmath>
#include <X11/Xlib.h>
#include <string>
#include <X11/keysym.h>
#include <unistd.h>
#include <GL/glx.h>
extern "C" {
#include "fonts.h"
}
#define WINDOW_WIDTH 500
#define WINDOW_HEIGHT 360
#define MAX_PARTICLES 30000
#define GRAVITY 0.1
//X varibles used for windows
Display *dpy;
Window win;
GLXContext glc;
//Structure
struct Vec {
float x, y, z;
};
//Structure shape
struct Shape {
float width, height;
float radius;
Vec center;
};
//Particle Structure
struct Particle {
Shape s;
Vec velocity;
};
#define NUM_BOXES 5
struct Game {
Shape box[NUM_BOXES];
Shape circle; // new from class
Particle particle[MAX_PARTICLES];
int n;
int lastMousex, 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();
// game objected declaration
Game game;
game.n=0;
int xOrigin = 100;
int yOrigin = 250;
int width = 80;
int height = 10;
// box shape declaration
game.box[0].width = width;
game.box[0].height = height;
game.box[0].center.x = xOrigin;
game.box[0].center.y = yOrigin;
game.box[1].width = width;
game.box[1].height = height;
game.box[1].center.x = xOrigin + 50;
game.box[1].center.y = yOrigin - 30;
game.box[2].width = width;
game.box[2].height = height;
game.box[2].center.x = xOrigin + 100;
game.box[2].center.y = yOrigin - 60;
game.box[3].width = width;
game.box[3].height = height;
game.box[3].center.x = xOrigin + 150;
game.box[3].center.y = yOrigin - 90;
game.box[4].width = width;
game.box[4].height = height;
game.box[4].center.x = xOrigin + 200;
game.box[4].center.y = yOrigin - 120;
game.circle.center.x = 400;
game.circle.center.y = -20;
game.circle.radius = 100;
//starting 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();
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) {
XDestroyWindow(dpy, win);
XCloseDisplay(dpy);
}
void initXWindows(void) {
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
glOrtho(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT, -1, 1);
//Set screen background color
glClearColor(0.1, 0.1, 0.1, 1.0);
//initialize fonts
glEnable(GL_TEXTURE_2D);
initialize_fonts();
}
//particle function
#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()*.8*+.02;
p->velocity.x = .5 + rnd()*0.5;
game->n++;
}
void check_mouse(XEvent *e, Game *game)
{
static int savex = 0;
static int savey = 0;
//static int n = 0;
if (e->type == ButtonRelease) {
return;
}
if (e->type == ButtonPress) {
if (e->xbutton.button==1) {
//Left button was pressed
for (int i = 0; i <10; i++)
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;
for (int i = 0; i <5; i++){
makeParticle(game, e->xbutton.x, y);
}
//for (++n < 10)
//return;
game->lastMousex = e->xbutton.x;
game->lastMousey = y;
}
}
int check_keys(XEvent *e, Game *game)
{
//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.
int key2 = XLookupKeysym(&e->xkey, 1);
if(key2 == XK_B){
int x = game->lastMousex;
int y = game->lastMousey;
makeParticle(game, x, y);
}
}
return 0;
}
void movement(Game *game)
{
Particle *p;
if (game->n <= 0)
return;
for(int i = 0; i < 10; i++){
makeParticle(game, game->lastMousex, game->lastMousey);
}
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...
for(int j = 0; j < NUM_BOXES; j++){
Shape *s = &game->box[j];
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->s.center.y = s->center.y + s->height;
p->velocity.y *= -0.4;
}
}
//check circle collision
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->s.center.x = game->circle.center.x + (game->circle.radius * d0/dist);
p->s.center.y = game->circle.center.y + (game->circle.radius * d1/dist);
p->velocity.x += d0/dist;
p->velocity.y += d1/dist;
}
//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...
static int firsttime = 1;
static int verts[60][2];
static int n = 60;
glColor3ub(50, 150,50);
if(firsttime){
float angle = 0;
float inc = (3.14459 * 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;
}
glPushMatrix();
glBegin(GL_TRIANGLE_FAN);
for(int i = 0; i < n; i++){
glVertex2i(verts[i][0], verts[i][1]);
}
glEnd();
glPopMatrix();
Rect rect;
rect.bot=WINDOW_HEIGHT-30;
rect.left=0;
rect.center=0;
ggprint16(&rect, 50, 0x00ffffff,"Waterfall Model");
//Draw the boxes here
for(int j = 0; j < NUM_BOXES; j++){
Shape *s;
glColor3ub(44,70,80);
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++){
int red = rand() % 20;
int green = rand() % 100;
int blue = rand() % 255;
glPushMatrix();
glColor3ub(red,green, blue);
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();
}
int bot = 240;
int left = 55;
int center = 0;
Rect rect2;
rect2.bot=bot;
rect2.left=left;
rect2.center=center;
ggprint12(&rect2, 37, 0x00ffffff,"Requirements");
Rect rect3;
rect3.bot=bot-30;
rect3.left=left+75;
rect3.center=center;
ggprint12(&rect3, 37, 0x00ffffff,"Design");
Rect rect4;
rect4.bot=bot - 60;
rect4.left= left + 130;
rect4.center=0;
ggprint12(&rect4, 37, 0x10ffffff,"Coding");
Rect rect5;
rect5.bot= bot -90;
rect5.left= left + 180;
rect5.center=0;
ggprint12(&rect5, 37, 0x00ffffff,"Testing");
Rect rect6;
rect6.bot= bot - 120;
rect6.left= left + 210;
rect6.center=0;
ggprint12(&rect6, 37, 0x00ffffff,"Maintenance");
}