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
|
/***********************************************************************
* Source File:
* UI INTERACT
* Author:
* Br. Helfrich
* Description:
* Implement the interfaces specified in uiInterface.h. This handles
* all the interfaces and events necessary to work with OpenGL. Your
* program will interface with this thorough the callback function
* pointer towards the bottom of the file.
************************************************************************/
#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
#include <cstdlib> // for rand()
#ifdef __APPLE__
#include <openGL/gl.h> // Main OpenGL library
#include <GLUT/glut.h> // Second OpenGL library
#endif // __APPLE__
#ifdef __linux__
#include <GL/gl.h> // Main OpenGL library
#include <GL/glut.h> // Second OpenGL library
#endif // __linux__
#ifdef _WIN32
#include <stdio.h>
#include <stdlib.h>
#include <Gl/glut.h> // OpenGL library we copied
#include <ctime> // for ::Sleep();
#include <Windows.h>
#define _USE_MATH_DEFINES
#include <math.h>
#endif // _WIN32
#include "uiInteract.h"
#include "point.h"
using namespace std;
/*********************************************************************
* SLEEP
* Pause for a while. We want to put the program to sleep until it
* is time to draw again. Note that this requires us to tell the OS
* that we are idle. the nanosleep function performs this task for us
* INPUT: msSleep: sleep time in milliseconds
*********************************************************************/
void sleep(unsigned long msSleep)
{
// Windows handles sleep one way
#ifdef _WIN32
::Sleep(msSleep + 35);
// Unix-based operating systems (OS-X, Linux) do it another
#else // LINUX, XCODE
timespec req = {};
time_t sec = (int)(msSleep / 1000);
msSleep -= (sec * 1000);
req.tv_sec = sec;
req.tv_nsec = msSleep * 1000000L;
while (nanosleep(&req, &req) == -1)
;
#endif // LINUX, XCODE
return;
}
/************************************************************************
* DRAW CALLBACK
* This is the main callback from OpenGL. It gets called constantly by
* the graphics engine to refresh and draw the window. Here we will
* clear the background buffer, draw on it, and send it to the forefront
* when the appropriate time period has passsed.
*
* Note: This and all other callbacks can't be member functions, they must
* have global scope for OpenGL to see them.
*************************************************************************/
void drawCallback()
{
// even though this is a local variable, all the members are static
Interface ui;
// Prepare the background buffer for drawing
glClear(GL_COLOR_BUFFER_BIT); //clear the screen
glColor3f(1,1,1);
//calls the client's display function
assert(ui.callBack != NULL);
ui.callBack(&ui, ui.p);
//loop until the timer runs out
if (!ui.isTimeToDraw())
sleep((unsigned long)((ui.getNextTick() - clock()) / 1000));
// from this point, set the next draw time
ui.setNextDrawTime();
// bring forth the background buffer
glutSwapBuffers();
// clear the space at the end
ui.keyEvent();
}
/************************************************************************
* KEY DOWN CALLBACK
* When a key on the keyboard has been pressed, we need to pass that
* on to the client. Currently, we are only registering the arrow keys
* INPUT key: the key we pressed according to the GLUT_KEY_ prefix
* x y: the position in the window, which we ignore
*************************************************************************/
void keyDownCallback(int key, int x, int y)
{
// Even though this is a local variable, all the members are static
// so we are actually getting the same version as in the constructor.
Interface ui;
ui.keyEvent(key, true /*fDown*/);
}
/************************************************************************
* KEY UP CALLBACK
* When the user has released the key, we need to reset the pressed flag
* INPUT key: the key we pressed according to the GLUT_KEY_ prefix
* x y: the position in the window, which we ignore
*************************************************************************/
void keyUpCallback(int key, int x, int y)
{
// Even though this is a local variable, all the members are static
// so we are actually getting the same version as in the constructor.
Interface ui;
ui.keyEvent(key, false /*fDown*/);
}
/***************************************************************
* KEYBOARD CALLBACK
* Generic callback to a regular ascii keyboard event, such as
* the space bar or the letter 'q'
***************************************************************/
void keyboardCallback(unsigned char key, int x, int y)
{
// Even though this is a local variable, all the members are static
// so we are actually getting the same version as in the constructor.
Interface ui;
ui.keyEvent(key, true /*fDown*/);
}
/***************************************************************
* INTERFACE : KEY EVENT
* Either set the up or down event for a given key
* INPUT key which key is pressed
* fDown down or brown
****************************************************************/
void Interface::keyEvent(int key, bool fDown)
{
switch(key)
{
case GLUT_KEY_DOWN:
isDownPress = fDown;
break;
case GLUT_KEY_UP:
isUpPress = fDown;
break;
case GLUT_KEY_RIGHT:
isRightPress = fDown;
break;
case GLUT_KEY_LEFT:
isLeftPress = fDown;
break;
case GLUT_KEY_HOME:
case ' ':
isSpacePress = fDown;
break;
}
}
/***************************************************************
* INTERFACE : KEY EVENT
* Either set the up or down event for a given key
* INPUT key which key is pressed
* fDown down or brown
****************************************************************/
void Interface::keyEvent()
{
if (isDownPress)
isDownPress++;
if (isUpPress)
isUpPress++;
if (isLeftPress)
isLeftPress++;
if (isRightPress)
isRightPress++;
isSpacePress = false;
}
/************************************************************************
* INTEFACE : IS TIME TO DRAW
* Have we waited long enough to draw swap the background buffer with
* the foreground buffer?
*************************************************************************/
bool Interface::isTimeToDraw()
{
return ((unsigned int)clock() >= nextTick);
}
/************************************************************************
* INTERFACE : SET NEXT DRAW TIME
* What time should we draw the buffer again? This is a function of
* the current time and the frames per second.
*************************************************************************/
void Interface::setNextDrawTime()
{
nextTick = clock() + static_cast<int> (timePeriod * CLOCKS_PER_SEC);
}
/************************************************************************
* INTERFACE : SET FRAMES PER SECOND
* The frames per second dictates the speed of the game. The more frames
* per second, the quicker the game will appear to the user. We will default
* to 30 frames/second but the client can set this at will.
* INPUT value The number of frames per second. 30 is default
*************************************************************************/
void Interface::setFramesPerSecond(double value)
{
timePeriod = (1 / value);
}
/***************************************************
* STATICS
* All the static member variables need to be initialized
* Somewhere globally. This is a good spot
**************************************************/
int Interface::isDownPress = 0;
int Interface::isUpPress = 0;
int Interface::isLeftPress = 0;
int Interface::isRightPress = 0;
bool Interface::isSpacePress = false;
bool Interface::initialized = false;
double Interface::timePeriod = 1.0 / 30; // default to 30 frames/second
unsigned int Interface::nextTick = 0; // redraw now please
void * Interface::p = NULL;
void (*Interface::callBack)(const Interface *, void *) = NULL;
/************************************************************************
* INTERFACE : DESTRUCTOR
* Nothing here!
***********************************************************************/
Interface::~Interface()
{
}
/************************************************************************
* INTEFACE : INITIALIZE
* Initialize our drawing window. This will set the size and position,
* get ready for drawing, set up the colors, and everything else ready to
* draw the window. All these are part of initializing Open GL.
* INPUT argc: Count of command-line arguments from main
* argv: The actual command-line parameters
* title: The text for the titlebar of the window
*************************************************************************/
void Interface::initialize(int argc, char ** argv, const char * title, Point topLeft, Point bottomRight)
{
if (initialized)
return;
// set up the random number generator
srand((unsigned int)time(NULL));
// create the window
glutInit(&argc, argv);
Point point;
glutInitWindowSize( // size of the window
(int)(bottomRight.getX() - topLeft.getX()),
(int)(topLeft.getY() - bottomRight.getY()));
glutInitWindowPosition( 10, 10); // initial position
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); // double buffering
glutCreateWindow(title); // text on titlebar
glutIgnoreKeyRepeat(true);
// set up the drawing style: B/W and 2D
glClearColor(0, 0, 0, 0); // Black is the background color
gluOrtho2D((int)topLeft.getX(), (int)bottomRight.getX(),
(int)bottomRight.getY(), (int)topLeft.getY()); // 2D environment
// register the callbacks so OpenGL knows how to call us
glutDisplayFunc( drawCallback );
glutIdleFunc( drawCallback );
glutKeyboardFunc( keyboardCallback);
glutSpecialFunc( keyDownCallback );
glutSpecialUpFunc( keyUpCallback );
initialized = true;
// done
return;
}
/************************************************************************
* INTERFACE : RUN
* Start the main graphics loop and play the game
* INPUT callBack: Callback function. Every time we are beginning
* to draw a new frame, we first callback to the client
* to see if he wants to do anything, such as move
* the game pieces or respond to input
* p: Void point to whatever the caller wants. You
* will need to cast this back to your own data
* type before using it.
*************************************************************************/
void Interface::run(void (*callBack)(const Interface *, void *), void *p)
{
// setup the callbacks
this->p = p;
this->callBack = callBack;
glutMainLoop();
return;
}
|