Repository navigation
Expand file tree
/
Copy pathuiDraw.cpp
More file actions
440 lines (384 loc) · 14.6 KB
/
Copy pathuiDraw.cpp
File metadata and controls
440 lines (384 loc) · 14.6 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
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
/***********************************************************************
* Source File:
* User Interface Draw : put pixels on the screen
* Author:
* Br. Helfrich
* Summary:
* This is the code necessary to draw on the screen. We have a collection
* of procedural functions here because each draw function does not
* retain state. In other words, they are verbs (functions), not nouns
* (variables) or a mixture (objects)
************************************************************************/
#include <string> // need you ask?
#include <sstream> // convert an integer into text
#include <cassert> // I feel the need... the need for asserts
#include <time.h> // for clock
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#include <openGL/gl.h> // Main OpenGL library
#include <GLUT/glut.h> // Second OpenGL library
#define GLUT_TEXT GLUT_BITMAP_HELVETICA_18
#endif // __APPLE__
#ifdef __linux__
#include <GL/gl.h> // Main OpenGL library
#include <GL/glut.h> // Second OpenGL library
#define GLUT_TEXT GLUT_BITMAP_HELVETICA_12
#endif // __linux__
#ifdef _WIN32
#include <stdio.h>
#include <stdlib.h>
#include <GL/glut.h> // OpenGL library we copied
#define _USE_MATH_DEFINES
#include <math.h>
#define GLUT_TEXT GLUT_BITMAP_HELVETICA_12
#endif // _WIN32
#include "point.h"
#include "uiDraw.h"
using namespace std;
#define deg2rad(value) ((M_PI / 180) * (value))
/********************************************************
* PT
* Ultra-simple point used to draw shapes
********************************************************/
struct PT
{
int x;
int y;
};
/*************************************************************************
* GL VERTEXT POINT
* Just a more convenient format of glVertext2f
*************************************************************************/
inline void glVertexPoint(const Point& point)
{
glVertex2f((GLfloat)point.getX(), (GLfloat)point.getY());
}
/*************************************************************************
* DISPLAY the text in the buffer on the screen
*************************************************************************/
void ogstream::flush()
{
string sOut;
string sIn = str();
// copy everything but the newlines
for (string::iterator it = sIn.begin(); it != sIn.end(); ++it)
// newline triggers an buffer flush and a move down
if (*it == '\n')
{
drawText(pt, sOut.c_str());
sOut.clear();
pt.addY(-18);
}
// othewise append
else
sOut += *it;
// put the text on the screen
if (!sOut.empty())
{
drawText(pt, sOut.c_str());
pt.addY(-18);
}
// reset the buffer
str("");
}
/*************************************************************************
* DRAW TEXT
* Draw text using a simple bitmap font
* INPUT topLeft The top left corner of the text
* text The text to be displayed
************************************************************************/
void ogstream::drawText(const Point& topLeft, const char* text) const
{
void* pFont = GLUT_TEXT;
// prepare to draw the text from the top-left corner
glRasterPos2f((GLfloat)topLeft.getX(), (GLfloat)topLeft.getY());
// loop through the text
for (const char* p = text; *p; p++)
glutBitmapCharacter(pFont, *p);
}
/************************************************************************
* DRAW STAR
* Draw a star that twinkles
* INPUT POINT The position of the beginning of the star
* PHASE The phase of the twinkling
*************************************************************************/
void ogstream::drawStar(const Point& point, unsigned char phase)
{
// use the current point if the default parameter is used
if (point != Point())
*this = point;
// Get ready...
// most of the time, it is just a pale yellow dot
if (phase < 128)
{
glBegin(GL_POINTS);
glColor3f((GLfloat)0.5 /* red % */, (GLfloat)0.5 /* green % */, (GLfloat)0.0 /* blue % */);
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() + 0.0));
}
// transitions to a bright yellow dot
else if (phase < 160 || phase > 224)
{
glBegin(GL_POINTS);
glColor3f((GLfloat)1.0 /* red % */, (GLfloat)1.0 /* green % */, (GLfloat)0.0 /* blue % */);
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() + 0.0));
}
// transitions to a bright yellow dot with pale yellow corners
else if (phase < 176 || phase > 208)
{
glBegin(GL_LINES);
glColor3f((GLfloat)0.5 /* red % */, (GLfloat)0.5 /* green % */, (GLfloat)0.0 /* blue % */);
glVertex2f((GLfloat)(pt.getX() + 1.0), (GLfloat)(pt.getY() + 0.0));
glVertex2f((GLfloat)(pt.getX() - 1.0), (GLfloat)(pt.getY() + 0.0));
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() + 1.0));
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() - 1.0));
glEnd();
glBegin(GL_POINTS);
glColor3f((GLfloat)1.0 /* red % */, (GLfloat)1.0 /* green % */, (GLfloat)0.0 /* blue % */);
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() + 0.0));
}
// the biggest yet
else
{
glBegin(GL_LINES);
glColor3f((GLfloat)0.5 /* red % */, (GLfloat)0.5 /* green % */, (GLfloat)0.0 /* blue % */);
glVertex2f((GLfloat)(pt.getX() + 2.0), (GLfloat)(pt.getY() + 0.0));
glVertex2f((GLfloat)(pt.getX() - 2.0), (GLfloat)(pt.getY() + 0.0));
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() + 2.0));
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() - 2.0));
glEnd();
glBegin(GL_LINES);
glColor3f((GLfloat)0.7 /* red % */, (GLfloat)0.7 /* green % */, (GLfloat)0.0 /* blue % */);
glVertex2f((GLfloat)(pt.getX() + 1.0), (GLfloat)(pt.getY() + 0.0));
glVertex2f((GLfloat)(pt.getX() - 1.0), (GLfloat)(pt.getY() + 0.0));
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() + 1.0));
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() - 1.0));
glEnd();
glBegin(GL_POINTS);
glColor3f((GLfloat)1.0 /* red % */, (GLfloat)1.0 /* green % */, (GLfloat)0.0 /* blue % */);
glVertex2f((GLfloat)(pt.getX() + 0.0), (GLfloat)(pt.getY() + 0.0));
}
// Complete drawing
glColor3f((GLfloat)1.0 /* red % */, (GLfloat)1.0 /* green % */, (GLfloat)1.0 /* blue % */);
glEnd();
}
/************************************************************************
* DRAW LINE
* Draw a line on the screen from the beginning to the end.
* INPUT begin The position of the beginning of the line
* end The position of the end of the line
*************************************************************************/
void ogstream::drawLine(const Point& begin, const Point& end,
double red, double green, double blue) const
{
// Get ready...
glBegin(GL_LINES);
glColor3f((GLfloat)red, (GLfloat)green, (GLfloat)blue);
// Draw the actual line
glVertexPoint(begin);
glVertexPoint(end);
// Complete drawing
glColor3f((GLfloat)1.0 /* red % */, (GLfloat)1.0 /* green % */, (GLfloat)1.0 /* blue % */);
glEnd();
}
/************************************************************************
* DRAW RECTANGLE
* Draw a rectangle on the screen from the beginning to the end.
* INPUT begin The position of the beginning of the rectangle
* end The position of the end of the rectangle
*************************************************************************/
void ogstream::drawRectangle(const Point& begin, const Point& end,
double red, double green, double blue) const
{
// Get ready...
glBegin(GL_QUADS);
glColor3f((GLfloat)red, (GLfloat)green, (GLfloat)blue);
// Draw the actual line
glVertex2f((GLfloat)begin.getX(), (GLfloat)begin.getY());
glVertex2f((GLfloat)begin.getX(), (GLfloat)end.getY());
glVertex2f((GLfloat)end.getX(), (GLfloat)end.getY());
glVertex2f((GLfloat)end.getX(), (GLfloat)begin.getY());
// Complete drawing
glColor3f((GLfloat)1.0 /* red % */, (GLfloat)1.0 /* green % */, (GLfloat)1.0 /* blue % */);
glEnd();
}
/***********************************************************************
* DRAW GameState
* Draw a moon-EnvironmentalForces spaceship on the screen at a given point
***********************************************************************/
void ogstream::drawLander(const Point& point, double angle)
{
// use the current point if the default parameter is used
if (point != Point())
*this = point;
//
// Landing legs
//
glBegin(GL_LINE_STRIP);
PT ptsLegs[] =
{
{-10,0}, {-6,0}, {-9,1}, {-9,8}, {-5,3}, {-9,8}, {-5,6},
{5,6}, {9,8}, {5,3}, {9,8}, {9,1}, {6,0}, {10,0}
};
glColor3f((GLfloat)1.0, (GLfloat)1.0, (GLfloat)1.0);
for (int i = 0; i < sizeof(ptsLegs) / sizeof(PT); i++)
glVertexPoint(rotate(pt, ptsLegs[i].x, ptsLegs[i].y, angle));
glEnd();
//
// Habitat module
//
// gold engine unit
glBegin(GL_QUADS);
glColor3f((GLfloat)0.8, (GLfloat)0.8, (GLfloat)0.0);
glVertexPoint(rotate(pt, -5, 3, angle));
glVertexPoint(rotate(pt, -5, 7, angle));
glVertexPoint(rotate(pt, 5, 7, angle));
glVertexPoint(rotate(pt, 5, 3, angle));
// engine
glColor3f((GLfloat)0.4, (GLfloat)0.4, (GLfloat)0.4);
glVertexPoint(rotate(pt, -4, 1, angle));
glVertexPoint(rotate(pt, -2, 3, angle));
glVertexPoint(rotate(pt, 2, 3, angle));
glVertexPoint(rotate(pt, 4, 1, angle));
// horizontal thrusters
glVertexPoint(rotate(pt, -8, 12, angle));
glVertexPoint(rotate(pt, -8, 11, angle));
glVertexPoint(rotate(pt, 8, 11, angle));
glVertexPoint(rotate(pt, 9, 12, angle));
glEnd();
// main habitat
glBegin(GL_TRIANGLE_FAN);
PT ptsCenter[] =
{
{0,10},
{3,7}, {-3, 7}, {-5,9}, {-5,12}, {-3, 16},
{3,16}, {5,12}, {5,9}, {3,7}
};
glColor3f((GLfloat)0.7, (GLfloat)0.7, (GLfloat)0.7);
for (int i = 0; i < sizeof(ptsCenter) / sizeof(PT); i++)
glVertexPoint(rotate(pt, ptsCenter[i].x, ptsCenter[i].y, angle));
glEnd();
// window
glBegin(GL_TRIANGLES);
glColor3f((GLfloat)0.2, (GLfloat)0.2, (GLfloat)0.2);
glVertexPoint(rotate(pt, 3, 15, angle));
glVertexPoint(rotate(pt, 4, 11, angle));
glVertexPoint(rotate(pt, 0, 12, angle));
glEnd();
// storage units
glColor3f((GLfloat)0.92, (GLfloat)0.92, (GLfloat)0.92);
glBegin(GL_QUADS);
glVertexPoint(rotate(pt, -1, 7, angle));
glVertexPoint(rotate(pt, -5, 10, angle));
glVertexPoint(rotate(pt, -5, 12, angle));
glVertexPoint(rotate(pt, -1, 12, angle));
glEnd();
glColor3f((GLfloat)1.0 /* red % */, (GLfloat)1.0 /* green % */, (GLfloat)1.0 /* blue % */);
}
/***********************************************************************
* DRAW GameState Flame
* Draw the flames coming out of a moonlander for thrust
***********************************************************************/
void ogstream::drawLanderFlames(const Point& point, double angle,
bool bottom,
bool clockwise,
bool counterClockwise)
{
// use the current point if the default parameter is used
if (point != Point())
*this = point;
// bottom thrust
if (bottom)
{
glBegin(GL_TRIANGLES);
glColor3f(1.0 /* red % */, 0.0 /* green % */, 0.0 /* blue % */);
glVertexPoint(rotate(pt, -3, 1, angle));
glVertexPoint(rotate(pt, random(-5.0, 5.0), random(-15.0, -5.0), angle));
glVertexPoint(rotate(pt, 3, 1, angle));
glVertexPoint(rotate(pt, -3, 1, angle));
glVertexPoint(rotate(pt, random(-5.0, 5.0), random(-15.0, -5.0), angle));
glVertexPoint(rotate(pt, 3, 1, angle));
glEnd();
}
// right thrust
if (counterClockwise)
{
glBegin(GL_LINES);
glColor3f(1.0 /* red % */, 0.0 /* green % */, 0.0 /* blue % */);
glVertexPoint(rotate(pt, 6, 12, angle));
glVertexPoint(rotate(pt, random(6.0, 8.0), random(15.0, 18.0), angle));
glVertexPoint(rotate(pt, 8, 12, angle));
glVertexPoint(rotate(pt, -6, 11, angle));
glVertexPoint(rotate(pt, random(-8.0, -6.0), random(7.0, 10.0), angle));
glVertexPoint(rotate(pt, -8, 11, angle));
glEnd();
}
// left thrust
if (clockwise)
{
glBegin(GL_LINES);
glColor3f(1.0 /* red % */, 0.0 /* green % */, 0.0 /* blue % */);
glVertexPoint(rotate(pt, 6, 11, angle));
glVertexPoint(rotate(pt, random(6.0, 8.0), random(7.0, 10.0), angle));
glVertexPoint(rotate(pt, 8, 11, angle));
glVertexPoint(rotate(pt, -6, 12, angle));
glVertexPoint(rotate(pt, random(-8.0, -6.0), random(15.0, 18.0), angle));
glVertexPoint(rotate(pt, -8, 12, angle));
glEnd();
}
// reset the color back to white when done
if (bottom || clockwise || counterClockwise)
glColor3f((GLfloat)1.0 /* red % */, (GLfloat)1.0 /* green % */, (GLfloat)1.0 /* blue % */);
}
/************************************************************************
* ROTATE
* Rotate a given point (point) around a given origin (center) by a given
* number of degrees (angle).
* INPUT origin The center point we will rotate around
* x,y Offset from center that we will be rotating
* rotation Rotation in degrees
* OUTPUT point The new position
*************************************************************************/
Point ogstream::rotate(const Point& origin,
double x, double y, double rotation) const
{
// the only element that rotates is the lunar landar
// and it has the center of rotation at (0, 8)
y -= 8;
// because sine and cosine are expensive, we want to call them only once
double cosA = cos(rotation);
double sinA = sin(rotation);
// start with our original point
Point ptReturn(origin);
// find the new values
ptReturn.addX(x * cosA - y * sinA);
ptReturn.addY(y * cosA + x * sinA + 8 /*center of rotation*/);
return ptReturn;
}
/******************************************************************
* RANDOM
* This function generates a random number.
*
* INPUT: min, max : The number of values (min <= num <= max)
* OUTPUT <return> : Return the integer
****************************************************************/
int random(int min, int max)
{
assert(min < max);
int num = (rand() % (max - min)) + min;
assert(min <= num && num <= max);
return num;
}
/******************************************************************
* RANDOM
* This function generates a random number.
*
* INPUT: min, max : The number of values (min <= num <= max)
* OUTPUT <return> : Return the double
****************************************************************/
double random(double min, double max)
{
assert(min <= max);
double num = min + ((double)rand() / (double)RAND_MAX * (max - min));
assert(min <= num && num <= max);
return num;
}