PDA

View Full Version : flying jet



openglNewbie
11-27-2003, 07:55 AM
I have noticed that other posts have been made on this topic but i am not able to understand how to implement it. I am trying to make a simple 2d game with a flying jet; at the moment it is just a square block. The problem is I would like to be able to able to press multiple keys at once.
Below is the code, i would graetly appreciate if anyone could give any ideas, advice or anything which would start the ball rolling.

Many Thanks in advance

#include <windows.h>
#include <GL/gl.h>
#include <GL/glut.h>
#include <gl/glaux.h>
#include <stdio.h>
#include <conio.h>
#include <math.h>

static int room = 0, rotb = 0, rota = 0;
int rotc = 0;
static float scale = 1.0, test = 0;
float currentx,x,currenty,y;


void init(void)
{
glClearColor(0.0, 0.0, 0.0, 0.0);
glShadeModel(GL_SMOOTH);
glEnable(GL_DEPTH_TEST);
}

void plane(void)
{
if (rotc>360) rotc=1;
if (rotc<1) rotc=360;
x=(cos(rotc/57.2957795));
y=(sin(rotc/57.2957795));
printf("%d %f %f\n",rotc,x,y);
glColor3f(1.0f, 1.0f, 0.0f);
glRotatef(rotc, 0.0, 0.0, 0.1);
glutSolidCube(0.1);
}


void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glColor3f(1.0f, 1.0f, 1.0f);
glPushMatrix();
glScalef(3.0, 2.0, 1.0);
plane();
glPopMatrix();
glutSwapBuffers();
}

void reshape(int w, int h)
{
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(65.0, (GLfloat) w/(GLfloat) h, 1.0, 20.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
gluLookAt(0.0, -0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}

void keyboard(unsigned char key, int u, int v)
{
switch (key) {

case 'y':
glTranslatef(x/25,y/25, 0.0);
glutPostRedisplay();
break;

case 'u':
rotc = (rotc -3);
glutPostRedisplay();
break;

case 't':
rotc = (rotc +3);
glutPostRedisplay();
break;

default:
break;
}
}

int main(int argc, char**argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(0, 0);
glutCreateWindow(argv[0]);
init();
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
glutMainLoop();
return 0();
}

Vlasko
11-27-2003, 08:29 AM
This wont't work.Use some variables as key state changes- something like bool keystate[number]
number is how many keys you want to use.
Then do this in key()

void keyboard(unsigned char key, int u, int v)
{
switch (key) {
case 'y':
if(keystate[0]==true)
keystate[0]==false
else keystate[0]==true;
break;
case 'u':
if(keystate[1]==true)
keystate[1]==false
else keystate[1]==true;
break;
.
.
.

default:
break;
}
}
and you check for the key in display func
void display(void)
{
..do something.....
if(keystate[0])
{
....some code....
}
if(keystate[1]}
{
.....some code.....
}
....do something...
}