PDA

View Full Version : Button not working and object not shown



Blossom8222
01-26-2014, 12:30 PM
I have been trying to add a firing button for my spaceship but it did not shown once I try to compile it.

Code:


static GLsizei width, height;

static int shipX = 0, shipY = 0; // Ship Position

static bool isShoot = false; // Shooting bullet
static int isAnimate = 0; // Animated?
static int animationPeriod = 100; // Time interval between frames
static float t = 0; // Time parameter

class Bullet
{
public:
Bullet(int x, int y)
{
xPos = x;
yPos = y;
}
Bullet(){};

void DrawBullet(void);

void SetPosition(int x, int y)
{
xPos = x;
yPos = y;
}

int xPos;
int yPos;


};

// Draw Bullet
void Bullet::DrawBullet()
{
glColor3f(0, 0, 1);

glPushMatrix();

glPointSize(3);
glBegin(GL_POINTS);
glVertex3f(xPos, yPos, 0);

glPopMatrix();
}

vector<Bullet> bullets;
vector<Bullet>::iterator bulletsIt;
Bullet bulletsCur;

// Draw Ship
void DrawShip()
{
glPushMatrix();

glColor3f(0.0, 1.0, 0.0);

glBegin(GL_TRIANGLE_STRIP);
glVertex3f(6, 0, 0.0);
glVertex3f(0, -1.5, 0.0);
glVertex3f(0, 1.5, 0.0);
glVertex3f(6, 0, 0.0);
glEnd();

glPopMatrix();
}




void drawScene(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f );
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

////// SHIP ////////////////////////////////////////
glPushMatrix();

glTranslatef(shipX, shipY, 0);

DrawShip();

glPopMatrix();
/////////////////////////////////////////////////////

////// Bullet ////////////////////////////////////////
if (isShoot)
{
bulletsIt = bullets.begin();
while (bulletsIt != bullets.end())
{
bulletsIt->DrawBullet();
bulletsIt++;
}
bulletsCur.DrawBullet();
}
/////////////////////////////////////////////////////


glutSwapBuffers();

glutPostRedisplay();
}



void moveBullet()
{
bullets.back().xPos = shipX + t;


glutPostRedisplay();
}

void animate(int value)
{
if (isAnimate)
{
isShoot = true;
t += 1;
}
moveBullet();
glutTimerFunc(animationPeriod, animate, 1);
glutPostRedisplay();
}


void keyInput(unsigned char key, int x, int y)
{
switch (key)
{
case 32:
if (!isShoot)
{
if (isAnimate)
{
isAnimate = 0;
bulletsCur = Bullet(x, y);
}
else
{
isAnimate = 1;
bullets.push_back(bulletsCur);
}
}
break;
case 27:
exit(0);
break;

default:
break;
}
glutPostRedisplay();
}

// Callback routine for non-ASCII key entry.
void specialKeyInput(int key, int x, int y)
{
float tempxPos = shipX, tempyPos = shipY;

switch(key)
{
case GLUT_KEY_DOWN :


tempyPos = shipY - 1;

break;
case GLUT_KEY_UP :


tempyPos = shipY + 1;

break;
case GLUT_KEY_LEFT:

tempxPos = shipX - 1;

break;
case GLUT_KEY_RIGHT:

tempxPos = shipX + 1;

break;
default:
break;
}

shipX = tempxPos;
shipY = tempyPos;

glutPostRedisplay();
}

void setup(void)
{
glClearColor (0.0, 0.0, 0.0, 0.0);
}

void resize(int w, int h)
{
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();

glOrtho(-90.0, 90.0, -50.0, 50.0, -1.0, 1.0);

width = w;
height = h;

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void printInteraction(void)
{
cout << "Help" << endl;
cout << "Use the arrow keys to hover over the button and press enter to select the button" << endl;
//cout << "Click on the left mouse button behind the line to shoot the bullet" << endl;
//cout << "Click on the object to get score" << endl;
}

int main(int argc, char **argv)
{
printInteraction();
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DOUBLE);
glutInitWindowSize(1000, 600);
glutInitWindowPosition(150, 100);
glutCreateWindow("Defender.cpp");
setup();
glutDisplayFunc(drawScene);
glutReshapeFunc(resize);
glutKeyboardFunc(keyInput);
//glutKeyboardUpFunc(keyboardUp);
//glutMouseFunc(mouseControl);
glutSpecialFunc(specialKeyInput);
//glutTimerFunc(5, animate, 1);

glutMainLoop();

return 0;
}