-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
257 lines (214 loc) · 5.65 KB
/
Copy pathmain.cpp
File metadata and controls
257 lines (214 loc) · 5.65 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
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include "Textured.h"
#include <iostream>
#include <iomanip>
#include <cmath>
// Video Parameters
#define WINDOW_TITLE "Video"
#define FULL_SCREEN 1
#define FRAME_TIME 10
// Camera Parameters
#define VIEW_W 160
#define VIEW_H 90
// Keyboard Commands
#define EXIT 27
#define MOVE_FORTH 'w'
#define MOVE_BACK 's'
#define MOVE_LEFT 'a'
#define MOVE_RIGHT 'd'
using namespace std;
Textured arara;
Textured checkIcon;
double mouse_x = 0;
double mouse_y = 0;
// Screen Resolution
int window_h = 0;
int window_v = 0;
// Camera position/motion
double view_x = 0;
double view_y = 0;
double x_min = 0;
double x_max = 0;
double y_min = 0;
double y_max = 0;
double cam_speed = 0.5;
// User input flags
int moveLeft = 0;
int moveRight = 0;
int moveBack = 0;
int moveForth = 0;
int mouse_l = 0;
int mouse_m = 0;
int mouse_r = 0;
void getScreenResolution(int& h, int& v);
void iniGl();
void mouseButton(int b,int s,int x,int y);
void mouseMove(int x, int y);
void mouseAction(int x, int y);
void glutMouseFunc(int button, int state, int x, int y);
void keyPressed(unsigned char key, int x, int y);
void keyReleased(unsigned char key, int x, int y);
void updateValues(int n);
void RenderScene();
int main(int argc, char **argv){
getScreenResolution(window_h,window_v);
glutInit(&argc, argv);
iniGl();
arara.init(0,0,15,15,0,"arara.png");
checkIcon.init(0,0,15,15,0,"checkIcon.png");
glutMainLoop();
return 0;
}
// Get the horizontal and vertical screen sizes in pixel
void getScreenResolution(int& h, int& v){
RECT desktop;
// Get a handle to the desktop window
const HWND hDesktop = GetDesktopWindow();
// Get the size of screen to the variable desktop
GetWindowRect(hDesktop, &desktop);
h = desktop.right;
v = desktop.bottom;
}
void iniGl(){
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutInitWindowSize(window_h,window_v);
glutCreateWindow(WINDOW_TITLE);
glutMouseFunc(&mouseButton);
glutMotionFunc(&mouseAction);
glutPassiveMotionFunc(&mouseMove);
glutKeyboardFunc(&keyPressed);
glutKeyboardUpFunc(&keyReleased);
glutDisplayFunc(&RenderScene);
glutIdleFunc(&RenderScene);
glutTimerFunc(1,updateValues,0);
glMatrixMode(GL_PROJECTION);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glDisable(GL_ALPHA_TEST);
gluOrtho2D(x_min,x_max,y_min,y_max);
if (FULL_SCREEN)
glutFullScreen();
}
void mouseButton(int b,int s,int x,int y){
switch (b){
case GLUT_LEFT_BUTTON:
if(s == GLUT_DOWN)
mouse_l = 1;
else
mouse_l = 0;
break;
case GLUT_MIDDLE_BUTTON:
if(s == GLUT_DOWN)
mouse_m = 1;
else
mouse_m = 0;
break;
case GLUT_RIGHT_BUTTON:
if(s == GLUT_DOWN)
mouse_r = 1;
else
mouse_r = 0;
break;
default:
break;
}
}
void mouseMove(int x,int y){
mouse_x = x;
mouse_y = y;
}
void mouseAction(int x,int y){
if(mouse_m){
view_x += VIEW_W*((double)(mouse_x-x)/window_h);
view_y += -VIEW_H*((double)(mouse_y-y)/window_v);
}
if(mouse_l){
arara.x += VIEW_W*((double)(mouse_x-x)/window_h);
arara.y += -VIEW_H*((double)(mouse_y-y)/window_v);
}
if(mouse_r)
arara.r += VIEW_W*((double)(mouse_x-x)/window_h);
mouse_x = x;
mouse_y = y;
}
void keyPressed(unsigned char key, int x, int y){
switch (key){
case EXIT:
exit(0);
break;
case MOVE_LEFT:
moveLeft = 1;
break;
case MOVE_RIGHT:
moveRight = 1;
break;
case MOVE_BACK:
moveBack = 1;
break;
case MOVE_FORTH:
moveForth = 1;
break;
default:
break;
}
}
void keyReleased(unsigned char key, int x, int y){
switch (key){
case MOVE_LEFT:
moveLeft = 0;
break;
case MOVE_RIGHT:
moveRight = 0;
break;
case MOVE_BACK:
moveBack = 0;
break;
case MOVE_FORTH:
moveForth = 0;
break;
default:
break;
}
}
void updateValues(int n){
// Frame limiter
glutTimerFunc(FRAME_TIME,updateValues,0);
// Moving the camera given the camera speed
if(moveLeft)
view_x -= cam_speed;
if(moveRight)
view_x += cam_speed;
if(moveBack)
view_y -= cam_speed;
if(moveForth)
view_y += cam_speed;
// Updating camera projection parameters
x_min = view_x - VIEW_W/2;
x_max = view_x + VIEW_W/2;
y_min = view_y - VIEW_H/2;
y_max = view_y + VIEW_H/2;
}
void RenderScene(){
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(x_min,x_max,y_min,y_max);
glMatrixMode(GL_MODELVIEW);
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glColor3f(0.0,1.0,1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(-VIEW_W/4,-VIEW_H/4);
glVertex2f( VIEW_W/4,-VIEW_H/4);
glVertex2f( VIEW_W/4, VIEW_H/4);
glVertex2f(-VIEW_W/4, VIEW_H/4);
glEnd();
checkIcon.render();
arara.render();
glPopMatrix();
glutSwapBuffers();
}