3 Replies - 644 Views - Last Post: 12 February 2013 - 07:11 PM Rate Topic: -----

#1 CountBale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-January 13

Unresponsive SDL Buttons

Posted 12 February 2013 - 04:09 AM

I am making a game using SDL, on the main menu all my buttons work fine but in the game itself you need to click the buttons a lot (20+ times) before they respond. My button class looks like this:

#include "button.h"

Button::Button( int x, int y, int h, int w)
{
    this->x = x;
    this->y = y;
    this->h = h;
    this->w = w;

}

bool Button::isPressed(SDL_Event *event)
{
    int mouseX = 0, mouseY = 0;

    if(( event->type == SDL_MOUSEBUTTONDOWN )&&( event->button.button == SDL_BUTTON_LEFT ))
    {
        mouseX = event->button.x;
        mouseY = event->button.y;

        if( ( mouseX > x ) && ( mouseX < x + w ) && ( mouseY > y ) && ( mouseY < y + h ) )
        {
            return true;
        }
    }
    return false;
}



and the place I am using it is here:

while(SDL_PollEvent(&event))
            {
                if(option1.isPressed(&event) == true)
                {
                    playerHealth = playerHealth - 5;
                    strengthStat = strengthStat - 1;
                    hadEvent = true;
                    eventRandom = false;
                }

                if(option2.isPressed(&event) == true)
                {
                    testResult = playerCharacter.charismaTest();
                    if(testResult >= 20)
                    {
                        hadEvent = true;
                        eventRandom = false;
                    }
                    else if(testResult > 1)
                    {
                        playerHealth = playerHealth - 5;
                        hadEvent = true;
                        eventRandom = false;
                    }
                    else
                    {
                        playerHealth = playerHealth - 10;
                        playerMoney = playerMoney - 10;
                        hadEvent = true;
                        eventRandom = false;
                    }
                }

                if((option3.isPressed(&event) == true)&&(classChoice == engineer))
                {
                    testResult = playerCharacter.intelligenceTest(5);
                    if(testResult > 15)
                    {
                        hadEvent = true;
                        eventRandom = false;
                    }
                    else
                    {
                        playerHealth = playerHealth - 10;
                        hadEvent = true;
                        eventRandom = false;
                    }
                }
            }



the other places that I use it have the same structure.

Is This A Good Question/Topic? 0
  • +

Replies To: Unresponsive SDL Buttons

#2 stayscrisp  Icon User is offline

  • フカユ
  • member icon

Reputation: 998
  • View blog
  • Posts: 4,173
  • Joined: 14-February 08

Re: Unresponsive SDL Buttons

Posted 12 February 2013 - 06:32 AM

You are doing far too much for this to be responsive. Your button shouldn't be doing the event checking, you should have a centralised location for using events and go from there.
Was This Post Helpful? 1
  • +
  • -

#3 CountBale  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 22-January 13

Re: Unresponsive SDL Buttons

Posted 12 February 2013 - 04:04 PM

View Poststayscrisp, on 12 February 2013 - 06:32 AM, said:

You are doing far too much for this to be responsive. Your button shouldn't be doing the event checking, you should have a centralised location for using events and go from there.


My main problem is that needs to be finished with evidence of testing in 3 days, is there a solution that doesn't involve rewriting a large amount of code? I know this is probably asking a lot.
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Unresponsive SDL Buttons

Posted 12 February 2013 - 07:11 PM

stayscrisp has already said what you need to do. You get the key that is pressed, then hand off a reference to the key pressed to your event handler. The way that you are doing it is that you are expecting an immediate response from a system that is busy processing new positions when the option is selected.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1