-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_window.cpp
More file actions
executable file
·274 lines (222 loc) · 6.57 KB
/
Copy pathapp_window.cpp
File metadata and controls
executable file
·274 lines (222 loc) · 6.57 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
# include <iostream>
# include "app_window.h"
# include <vector>
# include "GameLogic.h"
# include<string.h>
# include<sstream>
#include <chrono>
#include <thread>
#define BACKGROUND_PATH "Images/background.png"
float newH;
float newW;
bool resizeCalledAlready = false;
GameLogic * gLogic;
Animation *testAnim;
GLuint * background = new GLuint;
AppWindow::AppWindow ( const char* label, int x, int y, int w, int h )
:GlutWindow ( label, x, y, w, h )
{
_markx = 0;
_marky = 0;
addMenuEntry ( "Option 0", evOption0 );
addMenuEntry ( "Option 1", evOption1 );
scor = 0;
s = 0;
time = "";
score = "";
*background = SOIL_load_OGL_texture
(
BACKGROUND_PATH,
SOIL_LOAD_AUTO,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_MIPMAPS | SOIL_FLAG_INVERT_Y | SOIL_FLAG_COMPRESS_TO_DXT | SOIL_FLAG_NTSC_SAFE_RGB
);
}
// mouse events are in window coordinates, but your scene is in [0,1]x[0,1],
// so make here the conversion when needed
void AppWindow::windowToScene ( float& x, float &y )
{
x = (2.0f*(x/float(_w))) - 1.0f;
y = 1.0f - (2.0f*(y/float(_h)));
}
// Called every time there is a window event
void AppWindow::handle ( const Event& e )
{
bool rd=true;
if ( e.type==Keyboard )
switch ( e.key )
{ case ' ': // space bar
//std::cout << "Space pressed.\n";
_markx = 1.5;
_marky = 1.5;
redraw();
break;
case 27: // Esc was pressed
exit(1);
}
if ( e.type==MouseDown || e.type==Motion)
{ _markx=(float)e.mx;
_marky=(float)e.my;
windowToScene(_markx,_marky);
}
if ( e.type==Menu )
{ //std::cout<<"Menu Event: "<<e.menuev<<std::endl;
rd=false; // no need to redraw
}
// Considering checking if the user will move out of bound. Have a function to check and return true if not out of bound and false if out of bound?
// Currently it is possible for user to move to the edge and move past where we can see the user.
if ( e.type==SpecialKey )
switch ( e.key )
{
case GLUT_KEY_LEFT:
break;
case GLUT_KEY_RIGHT:
break;
case GLUT_KEY_UP:
break;
case GLUT_KEY_DOWN:
break;
default: rd=false; // no redraw
}
gLogic->eventQueue.push(e);
if (rd) redraw(); // ask the window to be rendered when possible
}
void AppWindow::resize ( int w, int h )
{
// Define that OpenGL should use the whole window for rendering
//cout << "resize called: " << endl;
//cout << "Resize to this width: " << w << endl;
//cout << "Resize to this height: " << h << endl;
glViewport( 0, 0, w, h );
_w=w; _h=h;
newW = w;
newH = h;
if(!resizeCalledAlready)
{
gLogic = new GameLogic();
resizeCalledAlready = true;
}
}
// here we will redraw the scene according to the current state of the application.
void AppWindow::draw ()
{
// Clear the rendering window
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
// Clear the trasnformation stack
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
/*
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glBindTexture(GL_TEXTURE_2D, *(imgPtr->at((it-imgPtr->begin()))));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// Drawing the Texture
glBegin(GL_POLYGON); // texCoord for tex, vertex for where to map
// texCoord is set to non-stretching, 1 to 1 mapping.
glTexCoord2f(0.0f, 1.0f); glVertex2d(x, y+h);
glTexCoord2f(1.0f, 1.0f); glVertex2d(x+w, y+h);
glTexCoord2f(1.0f, 0.0f); glVertex2d(x+w, y);
glTexCoord2f(0.0f, 0.0f); glVertex2d(x, y);
glEnd();
it = imgPtr->begin() + ((it-imgPtr->begin()+1) % imgPtr->size());
glDisable(GL_TEXTURE_2D);
*/
/*
vector<GLuint *> * test = testAnim->imgPtr;
test->push_back(&texture);
float x = 0.0f;
float y = 0.0f;
float w = 1.0f;
float h = 1.0f;
testAnim->animate(x, y, w, h);
*/
/* you may use GL_POLYGON for generic *convex* polygons, like this:
glBegin( GL_POLYGON );
glColor3f ( ... );
glVertex2d ( ... );
glVertex2d ( ... );
glVertex2d ( ... );
glVertex2d ( ... );
glEnd();*/
// Swap buffers
//
if(gLogic->currentStatus == GameLogic::status::RUNNING)
{
stringstream ts, ss;
ts<<"Time : ";
ts<<s;
time = ts.str();
drawText(time.data(), time.size(),1,590); //580
s = gLogic->getTime();
ss<<"Score : ";
ss<<scor;
score = ss.str();
drawText(score.data(), score.size(),500,590);
scor = gLogic->getScore();
glBegin( GL_POLYGON );
glColor3f (0,0,1);
glVertex2f (-1,1);
glVertex2f (1,1);
glVertex2f (1,.95);
glVertex2f (-1,.95);
glEnd();
glColor3f (1,1,1);
/*if(s > 60)
{
m += 1;
s -= 60;
}
if(m > 0 && s <= 0)
{
m -=1;
s += 60;
}*/
}
if(gLogic)
{
gLogic->update();
}
if(gLogic->currentStatus == GameLogic::status::RUNNING)
{
glBindTexture(GL_TEXTURE_2D, *background);
glEnable(GL_TEXTURE_2D);
// Drawing the Texture
glBegin(GL_POLYGON); // texCoord for tex, vertex for where to map
glTexCoord2f(0.0f, 1.0f); glVertex2d(-1, 1);
glTexCoord2f(1.0f, 1.0f); glVertex2d(1, 1);
glTexCoord2f(1.0f, 0.0f); glVertex2d(1, -1);
glTexCoord2f(0.0f, 0.0f); glVertex2d(-1, -1);
glEnd();
// Fixing this code so there's no possibility of overflow
//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0.1);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glDisable(GL_TEXTURE_2D);
}
glFlush(); // flush the pipeline (usually not necessary)
glutSwapBuffers(); // we were drawing to the back buffer, now bring it to the front
}
void AppWindow::drawText(const char * text, int length, int x, int y)
{
glMatrixMode(GL_PROJECTION);
double * matrix = new double[16];
glGetDoublev(GL_PROJECTION_MATRIX, matrix);
glLoadIdentity();
glOrtho(0,800,0,600, -5,5);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
glLoadIdentity();
glRasterPos2i(x,y);
for(int i = 0; i < length; i++)
glutBitmapCharacter(GLUT_BITMAP_9_BY_15, (int)text[i]);
glPopMatrix();
glMatrixMode(GL_PROJECTION);
glLoadMatrixd(matrix);
glMatrixMode(GL_MODELVIEW);
}