PDA

View Full Version : [Solved] Object with single x,y (C++, XCode)



toothyXdip
07-28-2010, 08:13 PM
........I had to use floats instead of ints in the class

I'm trying to create an object made of lines with a single x and y. I figured making a class would be the easiest way, but considering my 5 hours of openGL experience I'm having some trouble. The boxes only move to the left and down. What am I doing wrong? Is this the right way to do it in openGL?



#include <stdlib.h>

#include <GLUT/glut.h>

typedef unsigned char Bool;

typedef enum
{
ENTER = 3,
TAB = 9,
RETURN = 13,
ESC = 27,
SPACE = 32,
DEL = 127,
UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW,
NUM_KEY_CODES
} KeyCode;

Bool key[NUM_KEY_CODES], keyDown[NUM_KEY_CODES], keyUp[NUM_KEY_CODES];
int lastFrameTime = 0;
int aframe = glutGet(GLUT_ELAPSED_TIME);
float dt;

class mShip {
public:
int PosX;
int PosY;
void sDisplay();
mShip(int, int);
};

mShip ship(300, 300);

mShip::mShip(int x, int y) {
PosX = x;
PosY = y;
}

void mShip::sDisplay() {

glColor3f(0, 0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex3f(PosX-40,PosY-40, 0.0f);
glVertex3f(PosX+40,PosY-40, 0.0f);
glVertex3f(PosX+40, PosY+40, 0.0f);
glVertex3f(PosX-40,PosY+40,0.0f);


glEnd();

glBegin(GL_LINE_LOOP);
glVertex3f(PosX-30,PosY-30, 0.0f);
glVertex3f(PosX+30,PosY-30, 0.0f);
glVertex3f(PosX+30, PosY+30, 0.0f);
glVertex3f(PosX-30,PosY+30,0.0f);



glEnd();

// That's the ship drawn
glBegin(GL_LINE_LOOP);
glVertex3f(PosX-20,PosY-20, 0.0f);
glVertex3f(PosX+20,PosY-20, 0.0f);
glVertex3f(PosX+20, PosY+20, 0.0f);
glVertex3f(PosX-20,PosY+20,0.0f);

glEnd();
}

void resetKeyboardInput(void)
{
int i;

for (i = 0; i < NUM_KEY_CODES; i++)
{
keyDown[i] = false;
keyUp[i] = false;
}
}

void keyboard(unsigned char rawKeyCode, int x, int y)
{
if (rawKeyCode < NUM_KEY_CODES)
{
key[rawKeyCode] = true;
keyDown[rawKeyCode] = true;
}
}

void keyboardUp(unsigned char rawKeyCode, int x, int y)
{
if (rawKeyCode < NUM_KEY_CODES)
{
key[rawKeyCode] = false;
keyUp[rawKeyCode] = false;
}
}

void keyboardSpecial(int rawKeyCode, int x, int y)
{
switch (rawKeyCode)
{
case GLUT_KEY_LEFT:
key[LEFT_ARROW] = true;
keyDown[LEFT_ARROW] = true;
break;
case GLUT_KEY_UP:
key[UP_ARROW] = true;
keyDown[UP_ARROW] = true;
break;
case GLUT_KEY_RIGHT:
key[RIGHT_ARROW] = true;
keyDown[RIGHT_ARROW] = true;
break;
case GLUT_KEY_DOWN:
key[DOWN_ARROW] = true;
keyDown[DOWN_ARROW] = true;
break;
}
}

void keyboardSpecialUp(int rawKeyCode, int x, int y)
{
switch (rawKeyCode)
{
case GLUT_KEY_LEFT:
key[LEFT_ARROW] = false;
keyUp[LEFT_ARROW] = true;
break;
case GLUT_KEY_UP:
key[UP_ARROW] = false;
keyUp[UP_ARROW] = true;
break;
case GLUT_KEY_RIGHT:
key[RIGHT_ARROW] = false;
keyUp[RIGHT_ARROW] = true;
break;
case GLUT_KEY_DOWN:
key[DOWN_ARROW] = false;
keyUp[DOWN_ARROW] = true;
break;
}
}

void display(void)
{
if (lastFrameTime == 0)
{
lastFrameTime = glutGet(GLUT_ELAPSED_TIME);
}
int now = glutGet(GLUT_ELAPSED_TIME);
int elapsedMilliseconds = now - lastFrameTime;
float dt = elapsedMilliseconds / 1000.0f;
lastFrameTime = now;


if (key[LEFT_ARROW])
ship.PosX -= 200.0f * dt;
if (key[RIGHT_ARROW])
ship.PosX += 200.0f * dt;
if (key[UP_ARROW])
ship.PosY += 200.0f * dt;
if (key[DOWN_ARROW])
ship.PosY -= 200.0f * dt;


if (keyDown['a'])
ship.PosX -= 20.0f;
if (keyDown['d'])
ship.PosX += 20.0f;
if (keyDown['w'])
ship.PosY += 20.0f;
if (keyDown['s'])
ship.PosY -= 20.0f;

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

ship.sDisplay();
// draw the ship @ the position


glutSwapBuffers();


resetKeyboardInput();
}

void reshape(int width, int height)
{
glViewport(0, 0, width, height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, width, 0, height);
glMatrixMode(GL_MODELVIEW);
}


void idle(void)
{
glutPostRedisplay();
}

int main(int argc, char** argv)
{
glutInit(&amp;argc, argv);

glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
//glutInitWindowSize(1680,1050);
glutInitWindowSize(640, 480);

glutCreateWindow("GLUT Program");

glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutIdleFunc(idle);

glutKeyboardFunc(keyboard);
glutKeyboardUpFunc(keyboardUp);
glutSpecialFunc(keyboardSpecial);
glutSpecialUpFunc(keyboardSpecialUp);



//glutFullScreen();
glutMainLoop();
return EXIT_SUCCESS;
}