-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
367 lines (310 loc) · 9.67 KB
/
main.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
#include <stdlib.h>
#include <GL/glut.h>
#include <iostream>
#include <sstream>
#include "Player.h"
#include "PVector.h"
#include <vector>
#include <math.h>
#include <string.h>
#include <stdio.h>
using namespace std;
//float WINDOW_WIDTH=800.0f, WINDOW_HEIGHT=800.0f;
float WINDOW_WIDTH=800, WINDOW_HEIGHT=768.0f;
float lleft, rright, ttop, bbottom, panX, panY;
bool fired = true;
PVector *pi = NULL;
PVector *pf = NULL;
PVector *arrow = NULL;
float gravity;
float cx, cy, radius = 70;
const float PI_F=3.14159265358979f;
vector<Player> players;
int currentPlayer = 0;
float speedx;
float speedy;
char texto[30];
bool winner = false;
void draw_text(char *string){
glPushMatrix();
glColor3f(0,0,0);
glRasterPos2f(lleft + (rright-lleft)/2-50, ttop + bbottom/2);
while(*string){
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24,*string++);
}
glPopMatrix();
}
void draw_circle(float x, float y, float r){
cx = x;
cy = y;
bool filled = false;
int subdivs = 100;
glPushMatrix();
if(currentPlayer == 0) glColor3f(0,0,0);
else glColor3f(0,0,1);
glLineWidth(3);
if(filled){
glBegin(GL_TRIANGLE_FAN);
glVertex2f(x, y);
}else{
glBegin(GL_LINE_STRIP);
}
for(int i = 0; i <= subdivs; i++){
float angle = i * ((2.0f * PI_F) / subdivs);
glVertex2f(x + r*cos(angle), y + r*sin(angle));
}
glEnd();
glPopMatrix();
}
void draw_floor(){
glPushMatrix();
glLineWidth(3);
glColor3f(0,0,0);
glBegin(GL_LINES);
glVertex2f(-5000, WINDOW_HEIGHT);
glVertex2f(5000, WINDOW_HEIGHT);
glEnd();
glPopMatrix();
}
void draw_player(float w, float h, int player) {
glPushMatrix();
glLineWidth(3);
if(player == 0) glColor3f(0,0,0);
else glColor3f(0,0,1);
glBegin(GL_LINE_LOOP);
glVertex2f(0.0f, 0.0f);
glVertex2f(w , 0.0f);
glVertex2f(w , h );
glVertex2f(0.0f, h );
glEnd();
glPopMatrix();
}
void draw_vector(float x, float y, float xf, float yf){
glPushMatrix();
glLineWidth(2);
glBegin(GL_LINES);
glVertex2f(x, y);
glVertex2f(xf, yf);
glEnd();
glPopMatrix();
}
void updateCameraPlayer(){
lleft = players[currentPlayer].getX() - WINDOW_WIDTH/2 + players[currentPlayer].getW()/2;
rright = lleft + WINDOW_WIDTH;
ttop = 0;
bbottom = WINDOW_HEIGHT;
}
void updateCameraArrow(){
lleft += speedx;
rright = rright + speedx;
ttop +=speedy;
bbottom = bbottom+ speedy;
}
bool inside(float x, float y, float w, float h){
float xx = arrow->getX();
float yy = arrow->getY();
float px = arrow->getX() + xx;
float py = arrow->getY() + yy;
if(px >= x && px <= (x+w) && py >= y && py <= (y+h))
return true;
if(xx >= x && xx <= (x+w) && yy >= y && yy <= (y+h))
return true;
return false;
}
bool checkPlayerCollision(){
int p;
if(currentPlayer == 0) p = 1;
else p = 0;
float x = players[p].getX();
float y = players[p].getY();
float w = players[p].getW();
float h = players[p].getH();
return inside(x, y, w, h);
}
bool checkWallCollision(){
float x = arrow->getX() + arrow->getSizeX();
float y = arrow->getY() + arrow->getSizeY();
return y >= WINDOW_HEIGHT;
}
void swap_current_player(){
if(currentPlayer == 1) currentPlayer = 0;
else currentPlayer = 1;
updateCameraPlayer();
}
void Timer(int value){
if(arrow != NULL && fired){
arrow->addSum(speedx, speedy);
float x = arrow->getSizeX();
float y = arrow->getSizeY();
arrow->rotate(arrow->getAngle(x+speedx, y+speedy));
speedy += gravity;
if(checkWallCollision()){
swap_current_player();
arrow = NULL;
}else if(checkPlayerCollision()){
arrow = NULL;
updateCameraPlayer();
sprintf(texto, "Player %d ganhou!", currentPlayer+1);
draw_text(texto);
winner = true;
}else{
updateCameraArrow();
}
}
glutPostRedisplay();
glutTimerFunc(30, Timer, 1);
}
void slow_camera(){
float init_pointx = arrow->getX();
float init_pointy = arrow->getY();
float media_pointx = lleft + (rright - lleft)/2;
float media_pointy = ttop + (bbottom - ttop)/2;
// cout << "initx=" << init_pointx << endl;
// cout << "inity=" << init_pointy << endl;
// cout << "mediax=" << media_pointx<< endl;
// cout << "mediay=" << media_pointy << endl;
}
void Draw(void)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D (lleft+panX, rright+panX, bbottom+panY, ttop+panY);
glMatrixMode(GL_MODELVIEW);
// Limpa a janela de visualização com a cor preta
glClearColor(1,1,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0,0,0);
for(int i =0; i < players.size(); i++){
glPushMatrix();
glTranslatef(players[i].getX(), players[i].getY(), 0.0f);
draw_player(players[i].getW(), players[i].getH(), i);
glPopMatrix();
}
// desenha a linha do chao
draw_floor();
// desenha o vetor quando clika
if(pi != NULL && pf != NULL){
float width = rright - lleft;
float height = ttop - bbottom;
draw_vector(lleft + pi->getSizeX(), ttop + pi->getSizeY(), lleft + pf->getSizeX(), ttop + pf->getSizeY());
}
float centerx = players[currentPlayer].getX() + players[currentPlayer].getW()/2;
float centery = players[currentPlayer].getY() + players[currentPlayer].getH()/2;
// desenha o circulo da mira
draw_circle(centerx, centery, radius);
if(arrow != NULL && !fired){
centerx = centerx + arrow->getUnitarioX() * radius;
centery = centery + arrow->getUnitarioY() * radius;
draw_vector(centerx, centery, centerx + arrow->getSizeX(), centery + arrow->getSizeY());
arrow->setXY(centerx, centery);
}else if(arrow != NULL){
draw_vector(arrow->getX(), arrow->getY(), arrow->getX() + arrow->getSizeX(), arrow->getY()+ arrow->getSizeY());
}
if(winner == true)
draw_text(texto);
glFlush();
}
void mouse_drag (int x, int y){
pf = new PVector(x, y);
arrow->setSizeXY(pi->getSizeX() - pf->getSizeX(), pi->getSizeY() - pf->getSizeY());
glutPostRedisplay();
}
void mouse_click(int button, int state, int x, int y){
if(state == 0){ // clickou
arrow = new PVector(x, y);
pi = new PVector(x, y);
fired = !fired;
}else if(state == 1){ // soltou o click
if(pf != NULL)
arrow->setSizeXY(pi->getSizeX() - pf->getSizeX(), pi->getSizeY() - pf->getSizeY());
speedx = arrow->getSizeX() / 5;
speedy = arrow->getSizeY() / 5;
pi = NULL;
pf = NULL;
gravity = 0.3;
slow_camera();
fired = !fired;
}
}
// Função callback chamada para gerenciar eventos de teclas
void Teclado (unsigned char key, int x, int y)
{
if (key == 27)
exit(0);
}
void TeclasEspecias(int key, int x, int y)
{
if(key == GLUT_KEY_LEFT){
players[currentPlayer].translate(-10.0f, 0.0f);
}
if(key == GLUT_KEY_RIGHT){
players[currentPlayer].translate(10.0f, 0.0f);
}
if(key == GLUT_KEY_UP){
players[currentPlayer].translate(0.0f, -10.0f);
}
if(key == GLUT_KEY_DOWN){
players[currentPlayer].translate(0.0f, 10.0f);
}
if(key == GLUT_KEY_END){
swap_current_player();
}
if(key == GLUT_KEY_HOME){
winner = false;
updateCameraPlayer();
}
glutPostRedisplay();
}
// Função responsável por inicializar parâmetros e variáveis
void Inicializa(void)
{
// Define a janela de visualização 2D
glMatrixMode(GL_PROJECTION);
lleft=0.0;
rright=WINDOW_WIDTH;
ttop=0.0;
bbottom=WINDOW_HEIGHT;
updateCameraPlayer();
gluOrtho2D(lleft+panX,rright+panX,bbottom+panY,ttop+panY);
glMatrixMode(GL_MODELVIEW);
}
// Programa Principal
int main(void)
{
int argc = 0;
char *argv[] = { (char *)"gl", 0 };
srand ( time(NULL) );
int num1 = rand() % 2000;
int num2 = rand() % 2000;
cout << "play1 = "<< num1 << endl;
cout << "play2 = "<< num2 << endl;
Player p1(-num1, WINDOW_HEIGHT);
Player p2(num2, WINDOW_HEIGHT);
p1.translate(0.0f, -p1.getH()-radius/2);
p2.translate(-p2.getW(), -p2.getH()-radius/2);
players.push_back(p1);
players.push_back(p2);
glutInit(&argc,argv);
// Define do modo de operação da GLUT
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
// Especifica o tamanho inicial em pixels da janela GLUT
glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
// Cria a janela passando como argumento o título da mesma
glutCreateWindow("MEU GAME!");
// Registra a função callback de redesenho da janela de visualização
glutDisplayFunc(Draw);
// Registra a função callback para tratamento das teclas ASCII
glutKeyboardFunc (Teclado);
// Registra a função callback para tratamento das teclas especiais
glutSpecialFunc (TeclasEspecias);
// Registra a função callback para drag de mouse
glutMotionFunc(mouse_drag);
// Registra a função callback para click de mouse
glutMouseFunc(mouse_click);
glutTimerFunc(33, Timer, 1);
// Chama a função responsável por fazer as inicializações
Inicializa();
// Inicia o processamento e aguarda interações do usuário
glutMainLoop();
return 0;
}