Jump to content
  • Advertisement
Sign in to follow this  
jclins

SDL: How do I continuously move while holding down an axis on a joystick?

This topic is 4769 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

Here's a snip of the code:
...
    // message pump
    while (!gameover)
    {
        // handle sprite movement
        keystate = SDL_GetKeyState(NULL);

        // look for an event
        if (SDL_PollEvent(&event)) 
        {
            // handle joystick motion
            case SDL_JOYAXISMOTION:
                if((event.jaxis.value < -3200) || (event.jaxis.value > 3200))
                {
                    // left/right movement
                    if(event.jaxis.axis == 0)
                    {
                        // left
                        if(event.jaxis.value < 0)
                        {
                            rcSprite.x -= 2;
                        }

                        //// right
                         if(event.jaxis.value > 0)
                        {
                            rcSprite.x += 2;
                        }
                    }

                    // up/down movement
                    if(event.jaxis.axis == 1)
                    {
                        // up
                        if(event.jaxis.value < 1)
                        {
                            rcSprite.y -= 2;
                        }

                        // down
                         if(event.jaxis.value > 1)
                        {
                            rcSprite.y += 2;
                        }
                    }
                }
                break;
...



With this code, the sprite will be moved by -2/+2 spaces (pixel) for the direction pressed. What do I have to do to keep the sprite moving in that direction when the corresponding axis is held down? If I hold down the left directional pad, I want the sprite to continuously move to the left (or vise versa). I tried sticking in a while loop but this caused the program to lockup! :( Any help would be appreciated. Thanks.

Share this post


Link to post
Share on other sites
Advertisement
Hmm, why don't you try using your event loop to set booleans. Ie:


if (SDL_PollEvent(&event))
{
// handle joystick motion
case SDL_JOYAXISMOTION:
if((event.jaxis.value < -3200) || (event.jaxis.value > 3200))
{
// left/right movement
if(event.jaxis.axis == 0)
{
// left
if(event.jaxis.value < 0)
{
left_move = true;
}

//// right
if(event.jaxis.value > 0)
{
right_move = true;
}
}

// up/down movement
if(event.jaxis.axis == 1)
{
// up
if(event.jaxis.value < 1)
{
up_move = true;
}

// down
if(event.jaxis.value > 1)
{
down_move = true;
}
}
}
// Else, user is not pressing down joystick anymore
else {
left_move = false;
right_move = false;
up_move = false;
down_move = false;
}
break;





That's how I plan to do it in my event loop (I havent' gotten around to doing joystick support yet, so I'm not 100% sure if the event loop above works like I think it will). In my game I always set boolean variables in a singleton "GameInput" class that monitors events. Each event (like move up, 'confirm', 'cancel', etc.) has three booleans associated with it: state, press, and release. Press and release are only true for one pass through the game loop, where as state will be true as long as the event in question is active. This way, you can easily monitor whether any button has just been pressed, is being pressed down, has just been released, or isn't being pressed down. Then whatever game logic you write can just look at these booleans, instead of bothering with the details of the game event queue loop. [smile]


Hope that makes sense. I can post the source of my game input management if you'd like.

Share this post


Link to post
Share on other sites
Rather than base it on the events, which are triggered on the first time the event happens, use the provided functions to control everything. For example:

int x_move, y_move;
SDL_Joystick *joy1;
joy1=SDL_JoystickOpen(0);
...
// handle sprite movement
keystate = SDL_GetKeyState(NULL);

x_move = SDL_JoystickGetAxis(joy1, 0);
y_move = SDL_JoystickGetAxis(joy1, 1);

if( x_move > 3200 )
{
}

if( x_move < -3200 )
{
}

if( y_move > 3200 )
{
}

if( y_move < -3200 )
{
}

// look for an event
if (SDL_PollEvent(&event))
{
...
}
...




Give that a try and see if it's what you need. More information can be found in the docs/html/joystick.html file as part of the SDL documentation.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
Rather than base it on the events, which are triggered on the first time the event happens, use the provided functions to control everything. For example:
*** Source Snippet Removed ***

Give that a try and see if it's what you need. More information can be found in the docs/html/joystick.html file as part of the SDL documentation.


Horay! Thanks Drew_Benton, that worked! I was lead down the wrong path by this line in the SDL documentation:

"Ideally joysticks should be read using the event system."

BTW, I'm new at SDL (long time game player short time game programmer). I based the code from this page:

http://tonyandpaige.com/tutorials/sdl2.html

A little about myself. I've tinkered with several game-related programming including VB 3.x, Irrlicht 3D engine, darkbasic, 3d game studio, verge, rpg maker 200X, directx (very little), and windows GDI.

I plan to make a castlevania remake to learn SDL much like this:

http://www.castlevania.nzone.it/

The only thing annoying about the above clone is that it's very pixelated because it uses the original sprites for the NES version. I'm hoping to make it less annoying while playing on the PC (adding graphic/pixel filters commonly seen in emulators). Also, it adheres too much to the limitation of the NES version that it's based upon. But this could be that the author wants to be as faithful as possible to the original. I plan to make it more modern like the castlevania series on the GBA.

At any rate, I know I'll have more questions! I haven't put up a web site yet as I've just started coding using SDL (so there's not much to look at the moment).

Share this post


Link to post
Share on other sites
Ah yes,
Quote:
Ideally joysticks should be read using the event system


But then again, ideally you want something that will work [wink]. For the time being, I'd say it's fine to use those functions. I mean I wrote an input library (temporary, but permanent for now link) for the mouse and keyboard a while back. I based it all on the SDL event system and it worked pretty good. However, I could have just used the wrapper functions rather than events in my system, and achieved the *exact*
results. (i.e. use SDL_GetKeyState & etc..).

The only reason the joystick support is not there is because using the Joystick with the events proved to be a royal pain when I tried to add it in. I never got around to adding it in, but if I get time to finish the library, I'll probabally end up using the Joystick subset of functions rather than events. Who knows though, perhaps other have had more success using the joystick with events.

Anyways, good luck with your game, feel free to take a look at that Input Library. While I never got around to adding the joystick as mentioned before, I can say it has some very useful features in it if you are interested in using it or derivations of it for your game (if you just need some examples of how to approach the whole game input issue).

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!