Skip to content

Instantly share code, notes, and snippets.

@ferreirasc
Created October 4, 2015 23:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ferreirasc/3a066e9576f2001527a9 to your computer and use it in GitHub Desktop.
Save ferreirasc/3a066e9576f2001527a9 to your computer and use it in GitHub Desktop.
dpas
#include <stdio.h>
#include <string>
#include "utils.h"
#include "helicoptero.h"
#include <cmath>
#include <GL/glut.h>
#include <iostream>
#include <vector>
using namespace std;
int window_height, window_width;
vector <no> objects;
vector <tiro> tiros;
char vetor_estados[256];
string file, path;
Helicoptero heli;
double raio_jogador;
double velHelicoptero, velTiro;
int mouseX, mouseY;
int flag = 1;
int incr = 10;
int flag_tiro = 1;
void init()
{
glClearColor(1.0, 1.0, 1.0, 1.0);
glPointSize(2.5);
glLineWidth(2.5);
glShadeModel (GL_FLAT);
//50->0 pro Y pra adequar com a window.
glOrtho (0, window_width, window_height, 0, 0, 50);
}
void captureX_Y(int x, int y)
{
if(heli.gX < x)
{
heli.giraCanhao(3);
}
else
{
if(heli.gX > x)
{
heli.giraCanhao(-3);
}
}
}
void OnMouseClick(int button, int state, int x, int y)
{
if (button == GLUT_RIGHT_BUTTON)
{
if(state==GLUT_DOWN)
{
if(flag)
{
heli.modScale(raio_jogador/60);
flag = 0;
}
else
{
heli.modScale(-raio_jogador/60);
flag = 1;
}
}
}
else
{
if(button==GLUT_LEFT_BUTTON)
{
if(state==GLUT_DOWN)
{
tiro aux;
aux.x = heli.gX;
aux.y = heli.gY;
double dif;
if(heli.angCanhao<0 && heli.angCanhao>=-45)dif = heli.angCanhao*-25/-45;
else if (heli.angCanhao>0 && heli.angCanhao <=45)dif = heli.angCanhao*25/45;
else dif = 0;
aux.x -= (baseWidth/2*heli.scale + canhaoHeight*heli.scale)*cos((heli.ang+90+heli.angCanhao-dif)*M_PI/180);
aux.y -= (baseWidth/2*heli.scale + canhaoHeight*heli.scale)*sin((heli.ang+90+heli.angCanhao-dif)*M_PI/180);
aux.velTiro = velTiro;
aux.scale = heli.scale*3;
aux.ang_canhao = heli.angCanhao;
aux.ang_heli = heli.ang;
tiros.push_back(aux);
}
}
}
}
void display(void)
{
int tam = objects.size();
glClear(GL_COLOR_BUFFER_BIT);
//printf("FUCK THIS\n");
for(int i=0;i<tam;i++)
{
if(objects[i].fill == "red")
{
glColor3f(1,0,0);
DrawCircle(objects[i].x, objects[i].y, objects[i].r, 2000);
}
else
{
if(objects[i].fill == "blue")
{
glColor3f(0,0,1);
DrawCircle(objects[i].x, objects[i].y, objects[i].r, 2000);
}
else
{
if(objects[i].fill == "gray")
{
glColor3f(0.5,0.5,0.5);
DrawRect(objects[i].x, objects[i].y, objects[i].width, objects[i].height, objects[i].stroke_width);
}
}
}
}
heli.Draw(objects);
tam = tiros.size();
static GLdouble previousTime = 0;
GLdouble currentTime;
GLdouble timeDiference;
currentTime = glutGet(GLUT_ELAPSED_TIME);
timeDiference = currentTime - previousTime; // Elapsed time from the previous frame.
previousTime = currentTime; //Update previous time
for(int i=0;i<tam;i++)
{
if(tiros[i].x <= 800 && tiros[i].y <= 700)
{
//glPushMatrix();
//glTranslatef(0,-baseWidth/2,0);
glColor3f(1,0,0);
DrawCircle(tiros[i].x, tiros[i].y, tiros[i].scale, 20);
//glPopMatrix();
tiros[i].x+=tiros[i].velTiro*timeDiference*cos((tiros[i].ang_canhao-90+tiros[i].ang_heli)*M_PI/180);
tiros[i].y+=tiros[i].velTiro*timeDiference*sin((tiros[i].ang_canhao-90+tiros[i].ang_heli)*M_PI/180);
}
else
{
tiros.erase(tiros.begin()+i);
}
}
heli.aumVelHelice(incr);
glutSwapBuffers();
}
void keypress(unsigned char key, int x, int y){
switch (key)
{
case '+':
vetor_estados['+'] = 1;
break;
case '-':
vetor_estados['-'] = 1;
break;
case 'w':
vetor_estados['w'] = 1;
break;
case 'W':
vetor_estados['W'] = 1;
break;
case 'S':
vetor_estados['S'] = 1;
break;
case 's':
vetor_estados['s'] = 1;
break;
case 'A':
vetor_estados['A'] = 1;
break;
case 'd':
vetor_estados['d'] = 1;
break;
case 'a':
vetor_estados['a'] = 1;
break;
case 'D':
vetor_estados['D'] = 1;
break;
}
glutPostRedisplay();
}
void keyup (unsigned char key, int x, int y){
vetor_estados[key]=0;
glutPostRedisplay();
}
void idle(){
static GLdouble previousTime = 0;
GLdouble currentTime;
GLdouble timeDiference;
currentTime = glutGet(GLUT_ELAPSED_TIME);
timeDiference = currentTime - previousTime; // Elapsed time from the previous frame.
previousTime = currentTime; //Update previous time
if (vetor_estados['+'] == 1)
{
incr+=1;
}
if (vetor_estados['-'] == 1)
{
incr-=1;
}
if ((vetor_estados['w'] == 1 || vetor_estados['W'] == 1) && !flag)
{
heli.MoveInY(-velHelicoptero*timeDiference);
}
if ((vetor_estados['D'] == 1 || vetor_estados['d'] == 1) && !flag)
{
heli.giraHor(+7.0);
}
if ((vetor_estados['A'] == 1 || vetor_estados['a'] == 1) && !flag)
{
heli.giraHor(-7.0);
}
if ((vetor_estados['s'] == 1 || vetor_estados['S'] == 1) && !flag)
{
heli.MoveInY(+velHelicoptero*timeDiference);
}
glutPostRedisplay();
}
int main(int argc, char **argv)
{
TiXmlDocument doc;
string buffer = argv[1];
buffer+="/config.xml";
doc.LoadFile(buffer.c_str());
TiXmlHandle hDoc(&doc);
TiXmlElement* pRoot = hDoc.FirstChildElement().Element();
if(!pRoot)return 0;
searchXMLData(pRoot, objects, file, path, &velHelicoptero, &velTiro);
path+='/';
path+=file;
doc.LoadFile(path.c_str());
pRoot = hDoc.FirstChildElement().Element();
if(!pRoot)return 0;
searchXMLData(pRoot, objects, file, path, &velHelicoptero, &velTiro);
int tam = objects.size();
for(int i=0;i<tam;i++)
{
if(objects[i].fill == "green")
{
raio_jogador = objects[i].r;
heli = Helicoptero(objects[i].x, objects[i].y, objects[i].r);
break;
}
}
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
//Tamanho da janela que vai ser criada
for(int i=0;i<tam;i++)
{
if(objects[i].fill == "white")
{
window_width = objects[i].width;
window_height = objects[i].height;
break;
}
}
glutInitWindowSize (window_width, window_height);
//Onde ela vai aparecer quando for criada
glutInitWindowPosition (100, 100);
//Cria o nome da janela
glutCreateWindow ("My window - TC1");
init();
glutDisplayFunc(display);
glutKeyboardFunc(keypress);
glutKeyboardUpFunc(keyup);
glutIdleFunc(idle);
glutMouseFunc(OnMouseClick);
glutPassiveMotionFunc(captureX_Y);
glutMainLoop();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment