SDL Events in seperated.

This is a discussion on SDL Events in seperated. within the Game Programming forums, part of the General Programming Boards category; Hello Folks, I try to make a little game Pong. Now i have a situation: i have 4 keys defined; ...

  1. #1
    The Programming Dutchman
    Join Date
    Jan 2008
    Posts
    55

    SDL Events in seperated.

    Hello Folks,

    I try to make a little game Pong. Now i have a situation: i have 4 keys defined;

    for player 1: keys w and s. w = UP, S = DOWN.
    for player 2: Keys down arrow, up arrow.

    the problem is that i want the bats of the player keep moving if the player hold their the down/up keys. but it quickly turned out that if both players hold or push their up/down keys only one of them can move (because my SDL_Event Object can only hold one of the pushed keys)

    So i wondered if their is a way to make 2 separated SDL_Event Objects that only handles specific keys of the player so:

    SDL_Event EventsPlayer1 handles s and w keys.
    SDL_Event EventsPlayer2 handles UP and DOWN arrow keys.

    So is their a way to handle multiple keys with SDL?

    EDIT:

    I already tried the SDL function SDL_EnableKeyRepeat(int delay, int interval), but this solves only a part of the problem. it can repeat a key correctly but, it cant manage multiple keys at the time).

    Thanks for helping!

    Jelte.

    if somebody is curious about how i tried to handle this problem, check this code:
    Code:
    					while(SDL_PollEvent(&Event))
            			{
               					if(Event.type == SDL_QUIT)
                				{
                    				Quit = true;
                				}
                				else if(Event.type == SDL_KEYDOWN)
                				{
                    				if(Event.key.keysym.sym == SDLK_ESCAPE)
                    				{
                        					Quit = true;
                    				}
    								else if(Event.key.keysym.sym == SDLK_w || Event.key.keysym.sym == SDLK_UP || 											Player1HoldKeys == true || Player2HoldKeys == true)
    								{
    									if(Event.key.keysym.sym == SDLK_w || (Player1HoldKeys == true &&
    									   this->LastKeyPressed == 2))
    									{
    										this->Player1HoldKeys = true;
    										this->LastKeyPressed = 2;
    										this->ObjPlayer1->MoveBat(true,this->ObjGraphics->ReturnWindowHeigth());
    									}
    									else if(Event.key.keysym.sym == SDLK_UP || (Player2HoldKeys == true && 
    									   this->LastKeyPressed == 4))
    									{
    										this->Player2HoldKeys = true;
    										this->LastKeyPressed = 4;
    										this->ObjPlayer2->MoveBat(true,this->ObjGraphics->ReturnWindowHeigth());
    									}
                					}
    								else if(Event.key.keysym.sym == SDLK_s || Event.key.keysym.sym == SDLK_DOWN ||
    										Player1HoldKeys == true || Player2HoldKeys == true)
    								{
    									if(Event.key.keysym.sym == SDLK_s || (Player1HoldKeys == true && 
    									this->LastKeyPressed == 1))
    									{
    										this->Player1HoldKeys = true;
    										this->LastKeyPressed = 1;
    										this->ObjPlayer1->MoveBat(false,this->ObjGraphics->ReturnWindowHeigth());
    									}
    									else if(Event.key.keysym.sym == SDLK_DOWN || (Player2HoldKeys == true && 
    									   this->LastKeyPressed == 3))
    									{
    										this->Player2HoldKeys = true;
    										this->LastKeyPressed = 3;
    										this->ObjPlayer2->MoveBat(false,this->ObjGraphics->ReturnWindowHeigth());
    									}
    								}
    							}
    							else if(Event.type == SDL_KEYUP)
    							{
    								if(this->LastKeyPressed == 1 || this->LastKeyPressed == 2)
    								{
    									this->Player1HoldKeys = false;
    								}
    								else if(this->LastKeyPressed == 3 || this->LastKeyPressed == 4)
    								{
    									this->Player2HoldKeys = false;
    								}
    								this->LastKeyPressed = 0;
    							}
             			}
    					this->ObjGraphics->MoveBall();	
    					this->ObjGraphics->Draw(this->ObjPlayer1,this->ObjPlayer2);
    Last edited by Jelte; 06-23-2010 at 06:57 AM.
    The Programming Dutchman

  2. #2
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You can use SDL_KEYUP to keep track of when a key is released. For example, if you get a SDL_KEYDOWN event, you can assume that the key is held down until a corresponding SDL_KEYUP event happens.

    EDIT: Added a link to a tutorial.
    http://gpwiki.org/index.php/SDL:Tuto...Keyboard_Input
    Last edited by Memloop; 06-23-2010 at 07:21 AM.

  3. #3
    Registered User
    Join Date
    Aug 2003
    Posts
    1,203
    The way I would do it:
    Declare an array of 256 bools and set all to false at the start of the program
    In your event-loop check for keydown event
    If you get a keydown event set corresponding key in the array to true
    If you get a keyup event set the corresponding key in the array to false
    In your main loop (the one that draws stuff and updates everything and so on) check if array['w'] == true, array['s'] == true and so on and react accordingly.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A little class problem
    By bijan311 in forum C++ Programming
    Replies: 12
    Last Post: 05-23-2010, 11:42 AM
  2. SDL project setup
    By rogster001 in forum C Programming
    Replies: 22
    Last Post: 08-28-2009, 08:05 AM
  3. Problems compiling this SDL app
    By Rider in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2007, 12:22 PM
  4. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  5. sdl in c++
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2002, 07:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21