Jump to content

  • Log In with Google      Sign In   
  • Create Account

SDL getting multiple key imputs at once


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 PauliusM   Members   -  Reputation: 128

Like
0Likes
Like

Posted 23 January 2012 - 01:22 PM

Hey all I am new here, so I have a problem, I am trying to get multiple key imputs in sdl at once, but I am only to in few presses 2 key impust...

I need to get few imputs at once for moving object down + left, or left + up.

Here is the short code for moving an object
while( SDL_PollEvent( &event ) ) {
				
				 if(event.type == SDL_KEYDOWN)
				 {
				  switch(event.key.keysym.sym) {
											   case SDLK_UP: yVel = -1; break;
											   case SDLK_DOWN: yVel = 1; break;
											   case SDLK_RIGHT: xVel = 1; break;
											   case SDLK_LEFT: xVel = -1; break;
											   }
				 }
				 else if(event.type == SDL_KEYUP)
				 {
				  switch(event.key.keysym.sym) {
											   case SDLK_UP: yVel = 0; break;
											   case SDLK_DOWN: yVel = 0; break;
											   case SDLK_RIGHT: xVel = 0; break;
											   case SDLK_LEFT: xVel = 0; break;
											   }
				  }  
				  
				  if( event.type == SDL_QUIT ) {
					  quit = true;
				  }
				
		  }
	y += yVel;
  
	x += xVel;
  
	if (( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH )) {
		  x -= xVel;
	}
  
	if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) ) {
		y -= yVel;
	}	  


Sponsor:

#2 Telios   Members   -  Reputation: 398

Like
0Likes
Like

Posted 23 January 2012 - 01:49 PM


static bool isLeftPressed = false;

static bool isRightPressed = false;

// etc



while(SDL_PollEvent(&event))

{

  if(event.type == SDL_KEYDOWN)

  {

	switch(event.key.keysm.sym)

	{

	  case SDLK_LEFT: isLeftPressed = true; break;

	  case SDLK_RIGHT: isRightPressed = true; break;

	  // etc

	}

  }

  else if(event.type == SDL_KEYUP)

  {

	switch(event.key.keysm.sym)

	{

	  case SDLK_LEFT: isLeftPressed = false; break;

	  case SDLK_RIGHT: isRightPressed = false; break;

	  // etc

	}

  }

}



if(isLeftPressed)

{

  // ...

}

if(isRightPressed)

{

  // ...

}



#3 fastcall22   Crossbones+   -  Reputation: 4330

Like
0Likes
Like

Posted 23 January 2012 - 01:49 PM

Give this a quick read. Or, you can use SDL_GetKeyboardState.
c3RhdGljIGNoYXIgeW91cl9tb21bMVVMTCA8PCA2NF07CnNwcmludGYoeW91cl9tb20sICJpcyBmYXQiKTs=

#4 PauliusM   Members   -  Reputation: 128

Like
0Likes
Like

Posted 24 January 2012 - 12:15 PM

static bool isLeftPressed = false;
static bool isRightPressed = false;
// etc

while(SDL_PollEvent(&event))
{
  if(event.type == SDL_KEYDOWN)
  {
	switch(event.key.keysm.sym)
	{
	  case SDLK_LEFT: isLeftPressed = true; break;
	  case SDLK_RIGHT: isRightPressed = true; break;
	  // etc
	}
  }
  else if(event.type == SDL_KEYUP)
  {
	switch(event.key.keysm.sym)
	{
	  case SDLK_LEFT: isLeftPressed = false; break;
	  case SDLK_RIGHT: isRightPressed = false; break;
	  // etc
	}
  }
}

if(isLeftPressed)
{
  // ...
}
if(isRightPressed)
{
  // ...
}


didnt work maybe I have some problems in my whole code...

#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>
const int SCREEN_WIDTH = 300;
const int SCREEN_HEIGHT = 300;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 10;
const int DOT_WIDTH = 12;
const int DOT_HEIGHT = 12;
SDL_Surface *block = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
			
class Timer
{
    private:
    int startTicks;
    int pausedTicks;
    bool paused;
    bool started;
    public:
    Timer();
    void start();
    void stop();
    void pause();
    void unpause();
    int get_ticks();
    bool is_started();
    bool is_paused();
};

SDL_Surface *load_image( std::string filename ) {
		   
		    SDL_Surface* loadedImage = NULL;
		    SDL_Surface* optimizedImage = NULL;
		   
		    loadedImage = IMG_Load( filename.c_str() );
		   
		    if( loadedImage != NULL ) {
				
			    optimizedImage = SDL_DisplayFormat( loadedImage );
			    SDL_FreeSurface( loadedImage );
			   
			    if( optimizedImage != NULL) {
				    Uint32 colorkey = SDL_MapRGB( optimizedImage->format, 53, 255, 255 );
				    SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, colorkey );
				    }   
		    }
		   
return optimizedImage;
}
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination) {
	 SDL_Rect offset;
	 offset.x = x;
	 offset.y = y;
	 SDL_BlitSurface( source, NULL, destination, &offset );
}
bool init() {
	
	 if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 ) {
		 return false;
	 }
	
	 screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
	
	 if( screen == NULL ) {
		 return false;
	 }
	
	 SDL_WM_SetCaption( "Project", NULL );
	 return true;
}
bool load_files() {
	
	 background = load_image( "Background.png" );
	 block = load_image( "block.png" );
	
	 if( (background == NULL)||(block == NULL)) {
		 return false;
	 }
	
	 return true;
}

void clean_up() {
	 SDL_FreeSurface( background );
	 SDL_FreeSurface( block );
	 SDL_Quit();
}
	  
	
Timer::Timer()
{
    startTicks = 0;
    pausedTicks = 0;
    paused = false;
    started = false;
}
void Timer::start()
{
    started = true;
    paused = false;
    startTicks = SDL_GetTicks();
}
void Timer::stop()
{
    started = false;
    paused = false;
}
void Timer::pause()
{
    if( ( started == true ) && ( paused == false ) )
    {
	    paused = true;
	    pausedTicks = SDL_GetTicks() - startTicks;
    }
}
void Timer::unpause()
{
    if( paused == true )
    {
	    paused = false;
	    startTicks = SDL_GetTicks() - pausedTicks;
	    pausedTicks = 0;
    }
}
int Timer::get_ticks()
{
    if( started == true )
    {
	    if( paused == true )
	    {
		    return pausedTicks;
	    }
	    else
	    {
		    return SDL_GetTicks() - startTicks;
	    }
    }
    return 0;
}
bool Timer::is_started()
{
    return started;
}
bool Timer::is_paused()
{
    return paused;
}
int main( int argc, char* args[] ) {
   
	 int x,y;
	 static bool isLeftPressed = false;
	 static bool isRightPressed = false;
	 static bool isUpPressed = false;
	 static bool isDownPressed = false;
    bool quit = false;
    int frame = 0;
    bool cap = true;
    Timer fps;
   
			   x = 140;
			   y = 140;
			  
	 if( init() == false ) {
	    return 1;
    }
   
    if( load_files() == false ) {
	    return 1;
    }
   
   
    while( quit == false ) {
  
		   fps.start();
		   while( SDL_PollEvent( &event ) ) {
				 
				 if(event.type == SDL_KEYDOWN)
				 {
				  switch(event.key.keysym.sym) {
											   case SDLK_UP: isUpPressed = true; break;
											   case SDLK_DOWN: isDownPressed = true; break;
											   case SDLK_RIGHT: isRightPressed = true; break;
											   case SDLK_LEFT: isLeftPressed = true; break;
											   }
				 }
				 else if(event.type == SDL_KEYUP)
				 {
				  switch(event.key.keysym.sym) {
											   case SDLK_UP: isUpPressed = false; break;
											   case SDLK_DOWN: isDownPressed = false; break;
											   case SDLK_RIGHT: isRightPressed = false; break;
											   case SDLK_LEFT: isLeftPressed = false; break;
											   }
				  }  
				  
				  if( event.type == SDL_QUIT ) {
					  quit = true;
				  }
				 
		  }
    if(isLeftPressed)
    {
	 x -= 1;
	 }
    if(isUpPressed)
    {
	 y -= 1;
	 }
	 if(isRightPressed)
    {
	 x += 1;
	 }
    if(isDownPressed)
    {
	 y += 1;
	 }	 
   
   
   
   
    apply_surface( 0, 0, background, screen );
    apply_surface( x, y, block, screen );
	 
    if( SDL_Flip( screen ) == -1 ) {
	   return 1;
    }
   
   
    frame++;
   
    if( ( cap == true ) && ( fps.get_ticks() < 1000 / FRAMES_PER_SECOND ) )
	    {
		    SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
	    }
}	  
clean_up();
return 0;
} 







Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS