• Advertisement
Sign in to follow this  

Joystick Control problems (SDL)

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

Hello everyone. I'm trying to figure out some things with SDL. I've come pretty far with LazyFoo's tutorials but I've hit a snag. I've been struggling with creating a control structure that will get input from a joystick/keyboard and control the character. I've been having quite a few problems, but so far, I've managed to figure these things out. 1) While using the joystick, I can make the character move if the X axis is moved out of the neutral range. 2) The character I'm using (Megaman) continues to face whatever direction he was moving in when the controller is at a neutral state. This may sound weird, but I was stuck on this for 3 days cause I was missing an equal sign in my if statements. 3) When a button is pressed, the character will dash only while the button is held down. 4) If the shot button is pressed, the shot animation is executed, but it's kind of slow. I need to move on to frame independent animation but I'm not going to for now. The problems: 1) I've put in a control structure for jumping, meaning when a certain button is pressed, say, button 2, it will go through the animations, but the character doesnt' actually go up and then down. I'm wondering how to tackle this problem, meaning do I need to make a function or a class that creates gravity and when the user holds the jump button/key down they fight the gravity, or what? 2) The shoot button I'm using is button 1 on the joystick. I know that when programming, I have to tell it button 0 but this doesn't work. I have set it to button 4 (5 on the joystick) instead, but I'd like to know why button 0 doesn't work. ALSO, I tried to create a 'charge' function, that should make the user store energy while the button is held down and then fire it upon button release. NONE of this is working however. The animation doesn't even show up. The following code is the part used for the shot function. It doesn't actually shoot anything out, I'm just working on Character animations for now. I'll figure out how to actually shoot something out later. Before anyone asks: Yes I calibrated the controller. All the buttons are working.
/************************************
 ************************************
 *****  S H O O T I N G *************
 ************************************
 ************************************/
 // The charge part doesn't work...don't know why

    int chargeTimer = 0;

        if(event.type == SDL_JOYBUTTONDOWN)
        {
            if(event.jbutton.button == 4) // This doesn't work when I put 0
            {
                chargeTimer = SDL_GetTicks();

                if(chargeTimer > 500 && chargeTimer <= 1500)
                {
                    status = CHARGE_ONE;
                }

                if(chargeTimer > 1500)
                {
                    status = CHARGE_TWO;
                }
            }
        }

        if(event.type == SDL_JOYBUTTONUP)
        {
            if(event.jbutton.button == 4)
            {
                if(chargeTimer <= 500)
                {
                    status = NORMAL_SHOT; // This part works fine
                    chargeTimer = 0;
                }

                if(chargeTimer > 500 && chargeTimer <= 1250)
                {
                    status = LV1_SHOT;
                    chargeTimer = 0;
                }

                if(chargeTimer > 1250)
                {
                    status = LV2_SHOT;
                    chargeTimer = 0;
                }
            }
        }

This next part is the function that will show the character actually doing the action. Note: I have only clipped the images for CHARGE_ONE, but nonetheless, this should work, right?
if(status == CHARGE_ONE)
    {

        frame++;

        if(lastInput == IDLE_RIGHT ||  WALK_RIGHT || DASH_RIGHT)
        {
            apply_surface(offSet, 300, charge_ani_one, no_armor_x, &x_charge_one_ani[ frame ]);
        }

        if(lastInput == IDLE_LEFT ||  WALK_LEFT || DASH_RIGHT)
        {
            apply_surface(offSet, 300, charge_ani_one, reverse_no_armor_x, &x_charge_one_ani[ frame ]);
        }

        if(frame == 3)
        {
            frame = 0;
        }
    }

    if(status == NORMAL_SHOT)
    {
        frame++;

        apply_surface(offSet, 300, no_armor_x, screen, &x_shot_ani[ frame ]);

        if(frame >= 7)
        {
            status = NEUTRAL;
        }


    }

There are more problems like when I stop doing one thing, and tell it to animate something else, there is a blip in the image, a noticeable delay where the images goes completely blank for a moment before executing the next action. This is QUITE annoying, and I haven't figured out how to make it stop doing that. 3) Any action done with the keyboard doesn't work. I haven't figured out how to make the program check to see if a key is held down. When I tried to use the stuff in the documentation, it didn't work for some reason. I've even tried to use some of the stuff I read on the site, but it just wasn't working for me, so I abandoned the Keyboard for now, and rely on the joystick. Granted, I know the program won't even run if there is no joystick found on the PC, but this is just a beginning program to test input and response. 4) There's a few more things, but I'll try to figure these out on my own before bugging you peoples. :D Any help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
One problem I can see is that you are using SDL_GetTicks() as your counter, SDL_GetTicks returns the time since the program started, You need to get the counter for the difference in time from when you pressed the button down to when it is released


// These values need to be saved over the function calls, if your using a class I would make them member functions
bool firstPress = true;
int chargeTimerStart = 0;
int chargeTimerEnd = 0;

if(event.type == SDL_JOYBUTTONDOWN)
{
if( ( event.jbutton.button == 4 ) && firstPress == true ) // This doesn't work when I put 0
{
chargeTimer = SDL_GetTicks();

}

}

if(event.type == SDL_JOYBUTTONUP)
{
if(event.jbutton.button == 4)
{
chargeTimerStart = 0;
firstPress = false;
chargeTimerEnd = SDL_GetTicks();
int chargeTimer = chargeTimerEnd - chargeTimerStart;
if(chargeTimer <= 500)
{
status = NORMAL_SHOT; // This part works fine
chargeTimer = 0;
}
else if(chargeTimer > 500 && chargeTimer <= 1250)
{
status = LV1_SHOT;
chargeTimer = 0;
}
else if(chargeTimer > 1250)
{
status = LV2_SHOT;
chargeTimer = 0;
}
}
}



This code isn't perfect but it should give you a good starting point on charging.

I can't give any guesses as to why keyboard input is not working for you, or why joystick buttons aren't working right.

- Matt S.

Share this post


Link to post
Share on other sites
Thanks for the help.

Anyone care to tackle the image blitting problem? I guess the question would be can I blit a clipped image, on top of another image?

Share this post


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

  • Advertisement