iDevGames Forums
Keyboard Input - C++ OpenGL - Printable Version

+- iDevGames Forums (http://www.idevgames.com/forums)
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Keyboard Input - C++ OpenGL (/thread-1515.html)



Keyboard Input - C++ OpenGL - EugeneSong2000 - Apr 2, 2009 03:10 PM

Hello there,

I've just started learning how to program in C++ and OpenGL on XCODE 2.5. I've been having trouble figuring out how to get multiple simultaneous inputs to move objects on the screen. I tried to develop a keydown type program, by using the glKeyboardfunc and glKeyboardUpfunc.

Basically the logic I used was, to set a variable to true/false (using boolean), or to a certain value (using either int, float, or glFloat). I'd used glKeyboardfunc to set the value to true, and then glKeyboardUpfunc to set it to false.

Then in the main loop, I set up:

if (a = true)
{
move = move + 1;
}

For some reason the block automatically moves , even if i set the value of 'a' to false right before it.

a = false;

if (a = true )
{move = move +1;
}

I'm not sure what is up. If anyone could help that would be great. Here's the actual code that I have so far.

Code:
(C++, GLUT, XCODE 2.5)
/***************Headers*********************/
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <GLUT/glut.h>
#include <OpenGL/glext.h>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
using namespace std;

/*******************Variables********************/

GLfloat angle = 0.0;

GLfloat tX = 0, tY = 0, tZ = 0;




bool ignoreRepeats = false;

/******************Prototypes********************/

void display (void);
void reshape (int w, int h);
void cube ();
void keyboard (unsigned char key, int x, int y);

void keyboardUp (unsigned char key, int x, int y);
//void moveOnOff (void);


/********************Main**************************/

int main (int argc, char**argv)
{

glutInit (&argc, argv); //initialize the program.
glutInitDisplayMode (GLUT_SINGLE); //set up a basic display buffer (only singular for now)
glutInitWindowSize (600, 500); //set whe width and height of the window
glutInitWindowPosition (100, 100); //set the position of the window
glutCreateWindow ("A basic OpenGL Window"); //set the caption for the window

glutIgnoreKeyRepeat(ignoreRepeats);
glutKeyboardUpFunc (keyboardUp);

glutKeyboardFunc (keyboard);

//moveOnOff ();

glutIdleFunc (display); //change any idle values
glutDisplayFunc (display); //call the display function to draw our world



glutReshapeFunc (reshape);
glutMainLoop (); //initialize the OpenGL loop cycle




return 0;
}

/**********************Functions******************/

/*************Display Function****************/

void display (void) {
glClearColor (0.0,0.0,0.0,1.0);
glClear (GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);

//moveOnOff ();

cube ();



bool j = false;  // The keydown variable
if (j = true){
angle = angle + .1;
}

glFlush();

}//End Function

/*****************Reshape*****************/


void reshape (int w, int h) {
glViewport (0, 0, (GLsizei)w, (GLsizei)h);
glMatrixMode (GL_PROJECTION); //set it so we can play with the 'camera'
glLoadIdentity (); //replace the current matrix with the Identity Matrix
gluPerspective (60, (GLfloat)w / (GLfloat)h, 1.0, 100.0); //set the angle of view,
//the ratio of sight, the near and far factors
glMatrixMode (GL_MODELVIEW); //switch back the the model editing mode.
}

void cube ()
{
glTranslatef (tX, tY, tZ);
glRotatef (angle, 1.0, 0.0, 0.0); //rotate on x axis
glRotatef (angle, 0.0, 1.0, 0.0); //rotate on y axis
glRotatef (angle, 0.0, 0.0, 1.0); //rotate on z axis
glColor3f (1.0, 0.0, 0.0);
glutWireCube (2);
}

/**************Keyboard Down Functions******************/

void keyboard (unsigned char key, int x, int y)
{

//rotate

if (key == 'a')
{
angle = angle + 2;
}

else if (key == 'u')
{
j = true;
}

else if (key == 'j')
{
tZ = tZ + 1;
}






//Exit Program
if (key == 27)
{
exit (0);
}


}//end of keyboard funtion

/******************Keyboard up*****************/

void keyboardUp (unsigned char key, int x, int y)
{


}//End of Function



Keyboard Input - C++ OpenGL - maximile - Apr 2, 2009 05:20 PM

In C and C++ you use two equals signs to test for equality. So it should be:

Code:
if (a==true) { // etc.

At the moment, you're setting a to true and testing the result of that operation.


Keyboard Input - C++ OpenGL - EugeneSong2000 - Apr 13, 2009 01:14 PM

Thanks a Bunch. Its been driving me crazy, but that definitely helped. Thanks again