• Advertisement
Sign in to follow this  

SDL getting multiple key imputs at once

This topic is 2188 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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
[CODE]
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;
}
[/CODE]

Share this post


Link to post
Share on other sites
Advertisement
[code]
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)
{
// ...
}
[/code]

Share this post


Link to post
Share on other sites
Give [url="http://www.libsdl.org/docs/html/guideinputkeyboard.html#AEN354"]this[/url] a quick read. Or, you can use [url=http://www.libsdl.org/tmp/SDL-1.3-docs/SDL__keyboard_8h.html#15fe716dd70bd297bc96e111b36f1124]SDL_GetKeyboardState[/url].

Share this post


Link to post
Share on other sites
[quote name='Telios' timestamp='1327348174' post='4905536']
[code]
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)
{
// ...
}
[/code]
[/quote]

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

[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;
}


[/CODE]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement