SDL keystates help [SOLVED]

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
corey__cakes
ES Beta Backer
ES Beta Backer
Posts: 23
Joined: Mon Dec 23, 2013 7:56 pm
Current Project: Canadian Simulator 2017
Favorite Gaming Platforms: GBA, DC, iOS, SNES, WS, 360, N64
Programming Language of Choice: C++, Lua
Location: Your VCR.

SDL keystates help [SOLVED]

Post 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
What do you call a cow with no legs?

Ground beef.
User avatar
James Evesque
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Fri Jan 17, 2014 10:20 am
Current Project: Low Level Runtime in C99 for shared code.
Favorite Gaming Platforms: PC, NES, SNES, DMG, GBC, GENISIS, PSX, XBOX
Programming Language of Choice: ASM, C, C++, Lua
Location: Alabama

Re: SDL keystates help.

Post 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
        }
    }
}
Last edited by James Evesque on Sun Jan 19, 2014 9:51 pm, edited 1 time in total.
418 |
419 | #ifndef __cplusplus
420 | #define fucks_given 0
421 | #endif
422 |
User avatar
James Evesque
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Fri Jan 17, 2014 10:20 am
Current Project: Low Level Runtime in C99 for shared code.
Favorite Gaming Platforms: PC, NES, SNES, DMG, GBC, GENISIS, PSX, XBOX
Programming Language of Choice: ASM, C, C++, Lua
Location: Alabama

Re: SDL keystates help.

Post 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
        }
    }
}
418 |
419 | #ifndef __cplusplus
420 | #define fucks_given 0
421 | #endif
422 |
corey__cakes
ES Beta Backer
ES Beta Backer
Posts: 23
Joined: Mon Dec 23, 2013 7:56 pm
Current Project: Canadian Simulator 2017
Favorite Gaming Platforms: GBA, DC, iOS, SNES, WS, 360, N64
Programming Language of Choice: C++, Lua
Location: Your VCR.

Re: SDL keystates help.

Post 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
What do you call a cow with no legs?

Ground beef.
User avatar
James Evesque
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 17
Joined: Fri Jan 17, 2014 10:20 am
Current Project: Low Level Runtime in C99 for shared code.
Favorite Gaming Platforms: PC, NES, SNES, DMG, GBC, GENISIS, PSX, XBOX
Programming Language of Choice: ASM, C, C++, Lua
Location: Alabama

Re: SDL keystates help.

Post by James Evesque »

You are welcome.Goodluck with your editor!
418 |
419 | #ifndef __cplusplus
420 | #define fucks_given 0
421 | #endif
422 |
Post Reply