PDA

View Full Version : freeGLUT keyboard input now working



CodeOne
01-20-2014, 01:43 AM
I am struggling with keyboard input here,
Would you guys please check my code and tell me what changes should i make?
my keyboard inputs are not getting accepted may be i have messed up my code.


#include <GL/freeglut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <stdio.h>
#include <cmath>
#include <list>
//Headers--------------------
#include "GameObject.h"
#include "Globals.h"
#include "Rollingball.h"
#include "Ground.h"

//Screen Constants
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 400;
const int SCREEN_FPS = 60;

bool* keyStates = new bool[256];
float Cameraposition[2] = {0,0};


//Object list
std::list<GameObject *> objects;
std::list<GameObject *>::iterator iter;
std::list<GameObject *>::iterator iter2;

void update()
{

}
void runMainLoop( int val );
void display (void);
void keyOperations (void);
void keyPressed (unsigned char key, int x, int y);
void keyUp (unsigned char key, int x, int y);


//Globals to main
Rollingball *ball;
Ground *ground;

int main( int argc, char* args[] )
{
glutInit( &argc, args ); //Initialize FreeGLUT
glutInitContextVersion( 2, 1 ); //Create OpenGL 2.1 context
glutInitDisplayMode( GLUT_DOUBLE ); //Create Double Buffered Window
glutInitWindowSize( SCREEN_WIDTH, SCREEN_HEIGHT );
glutCreateWindow( "SuperBall" );


//Do post window/context creation initialization
if( !initGL() )
return 1;

//-----------------------

ball = new Rollingball();
ground = new Ground();

ball->Init();
objects.push_back(ball);
ground->Init();
objects.push_back(ground);

//Set keyboard handler
glutDisplayFunc(display); // Tell GLUT to use the method "display" for rendering
glutKeyboardFunc(keyPressed); // Tell GLUT to use the method "keyPressed" for key presses
glutKeyboardUpFunc(keyUp); // Tell GLUT to use the method "keyUp" for key up events


//Set main loop
glutTimerFunc( 1.0 / SCREEN_FPS, runMainLoop, 0 );

//Start GLUT main loop
glutMainLoop();

return 0;
}

void runMainLoop( int val )
{



glPushMatrix();
ball->Cameraupdate();
glTranslatef( -Cameraposition[0], -Cameraposition[1], 0.f );

ball->Moveball();

update();

// render--------------------------
for(iter = objects.begin(); iter != objects.end(); ++iter )
(*iter)->Render();

glPopMatrix();

//Run frame one more time
glutTimerFunc( 1.0/ SCREEN_FPS, runMainLoop, val );

//Flip Buffer display-------
glutSwapBuffers();
glClear( GL_COLOR_BUFFER_BIT );

}

void keyOperations (void)
{
if (keyStates['a'])
{
ball->Jumpball();
}
}

void display (void)
{
keyOperations();
}

void keyPressed (unsigned char key, int x, int y)
{
keyStates[key] = true; // Set the state of the current key to pressed
}

void keyUp (unsigned char key, int x, int y)
{
keyStates[key] = false; // Set the state of the current key to not pressed
}


Jumpball() in another .cpp file is here:


void Rollingball::Jumpball()
{
if(isonground) // ball is on ground
{
dirX *= 0.9f;
if(keyStates['a'] && mayjumpagain)
{
dirY =-11;
mayjumpagain = false;
isonground = false;
Leftcollision=false;
}

else
{
mayjumpagain=true;
}
}


if(!isonground)// ball is in air
{
dirY += 0.5;
if(dirY>5)
{
dirY = 5;
}
}


if(keyStates['a'] && !isonground && dirY > 0) // long pressing spacebar
{
dirY -= 0.4f;
}

x += dirX;
y += dirY;

if(y + boundY > 330 )
{
y = 300;
dirY = 0;
isonground = true;

}

}

extern bool keyStates[]; is present in globals.h

I have used allegro 5 in past, but I really wanna learn a bit OPENGL stuff. There can be stupid mistakes but am new to this...HELP please!

CarstenT
01-21-2014, 01:13 PM
"input now working" .. a typho?
I suppose that input doesn't work, right?

is it glut that has a sticky-key function or state to asure key-input to be caught? I think so.