Page 1 of 1

SDL keystates help [SOLVED]

Posted: Sun Jan 19, 2014 7:09 pm
by corey__cakes
I've been working on a level editor in C++ and SDL. I've run into this terrible problem. Since I'm the only one using it, I'm not trying to add fancy controls, but I mig
ht have to add VERY fancy controls because of this.

The problems I've been having are with the mouse events that SDL handles. I have my two input function in my system class for the mouse. You pass a variable that represents the button you want to check into the function and the function returns true or false. The major inconvenience is that my input checks in my main loop execute what they do multiple times. I want to have my controls set up so that I place tiles with the left mouse button and the right mouses button scrolls through the tiles on the tile sheet. So when I left click, the tile drawn on the screen to represent what and where you're going to place tiles furiously scrolls through each tile and will continuously to flicker for about one second after not pressing . I would need to make my input functions return true once upon being pressed. I've tried to come up with a solution that looks like(in semi-pseudocode).

Code: Select all

bool input = false;
bool CheckInput(button_name) {
if(mouse.button == button_name && !input) {
input = true;
}
if(mouse.button != button_name) {
input = false;
}
return input;
}
Whenever I look through this, I think it should be working. But then I discovered more problems. Whenever I hit a key, any key, I would lay a tile. I didn't code those keys to do anything. Especially not to the what the mouse events should do. Also, whenever I start my program, where the mouse is on the screen initially is where a tile is placed. ALSO, whenever I would drag my mouse on or off the window, it would place a tile on the last tile space the mouse was on.

I need a way to have my input functions return true only once. I can't think of another way to do this. I don't want to map every tile to a different key on the keyboard so I can select it. Is there something in SDL that can fix this? I did learn in it about a week so I can get back into game dev quickly, so I may not be familiar with the slightly more advanced stuffs of SDL.

I would much appreciate any advice or tips that you have. Thank you

Re: SDL keystates help.

Posted: Sun Jan 19, 2014 9:23 pm
by James Evesque
You should be using events, not state. More complicated designs can peek and pump events and pass them around. Without seeing your use of the SDL API I can not help any more then this.

Code: Select all

while(SDL_PollEvent(&event)) // while events waiting, get a new event
{
    if(event.type == SDL_MOUSEBUTTONUP) // check if this event is what we want
    {
        if(event.button.button == SDL_BUTTON_RIGHT)  
        {
           // we pressed right
        }
        if(event.button.button == SDL_BUTTON_LEFT)
        {
           // we pressed left
        }
    }
}

Re: SDL keystates help.

Posted: Sun Jan 19, 2014 9:50 pm
by James Evesque
As for the wheel, you can check to see if it is moved, and do something like..

Code: Select all

current_tile += event.wheel.y; // y is 1 if up and -1 if down.

Code: Select all

while(SDL_PollEvent(&event)) // while events waiting, get a new event
{
    if(event.type == SDL_MOUSEWHEEL) // check if this event is what we want
    {
        if(event.wheel.y == TRUE) 
        {
           // we scrolled up
        }
        else
        {
           // we scrolled down
        }
    }
}

Re: SDL keystates help.

Posted: Mon Jan 20, 2014 7:46 am
by corey__cakes
James Evesque wrote:You should be using events, not state. More complicated designs can peek and pump events and pass them around.
I see the errors of my ways. So I should have more event-drivenness. Thank you

Re: SDL keystates help.

Posted: Mon Jan 20, 2014 8:37 am
by James Evesque
You are welcome.Goodluck with your editor!