Results 1 to 2 of 2

Thread: freeGLUT keyboard input now working

  1. #1
    Newbie Newbie
    Join Date
    Jan 2014
    Posts
    1

    freeGLUT keyboard input now working

    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.
    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:

    Code :
    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!

  2. #2
    Senior Member Regular Contributor
    Join Date
    Jul 2013
    Posts
    101
    "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.

Similar Threads

  1. Problem with keyboard interface - freeglut
    By thor36 in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 12-06-2014, 08:41 PM
  2. VB - Keyboard Input
    By Bhaji123Go in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 11-08-2002, 02:34 PM
  3. Keyboard input
    By Questions Burner in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 10-15-2001, 10:17 AM
  4. keyboard input
    By Vlasko in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 09-21-2001, 05:34 AM
  5. get keyboard input
    By vgm2 in forum OpenGL: Basic Coding
    Replies: 8
    Last Post: 03-14-2001, 02:18 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean