SDL keystates problem.

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
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

SDL keystates problem.

Post by jakobnator »

Yo, back again to spam your forum up with help problems don't mind me.

Anyways, When I am checking for the enter keystate it triggers the whole switch statement and places all the cards, its supposed place one card a time, when you hit enter.
Basically I just want it to run through the switch once and then exit once it goes through the case, until the next time the keystate is triggered.

Code: Select all

if (keystates[SDLK_RETURN] && spot == false)
    {
        switch (pCards)
        {
            case 2:
            Card3.Exist = true;
            pCards = 3;
            break;
            case 3:
            Card4.Exist = true;
            pCards = 4;
            break;
            case 5:
            Card5.Exist = true;
            pCards = 5;
            break;
        }
    }
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: SDL keystates problem.

Post by xiphirx »

You probably want to trigger this when you release enter, not when its held down, or pressed.

Also, please indent your code.
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: SDL keystates problem.

Post by MadPumpkin »

don't access card.Exist directly, always use Get and Set functions
eg.

Code: Select all

bool getExists(){
        return Exist;
}
void setExist(bool exist){
        Exist = exist;
}
EDIT: I understand that this may not be some big project, but this is still good practice especially when you start using pointers and need ->
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: SDL keystates problem.

Post by short »

MadPumpkin wrote:don't access card.Exist directly, always use Get and Set functions
eg.

Code: Select all

bool getExists(){
        return Exist;
}
void setExist(bool exist){
        Exist = exist;
}
EDIT: I understand that this may not be some big project, but this is still good practice especially when you start using pointers and need ->
If your going to suggest this at least allow the compiler to optimize it

Code: Select all

inline const bool getExists() const { return Exist; }

Code: Select all

inline void setExist(const bool exist) {Exist = exist;}
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

Re: SDL keystates problem.

Post by jakobnator »

Its black jack not a MMORPGFPSRTS, I don't need to make it inline, and yea I probably don't need to use getters and setters but, I am just lazy 8-)
Is there a keystate for a release return key?

EDIT: btw, for class methods you don't put inline if you already have it on a single line it automatically makes it inline ;)
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: SDL keystates problem.

Post by short »

jakobnator wrote: EDIT: btw, for class methods you don't put inline if you already have it on a single line it automatically makes it inline ;)
twat? compiler dependent?
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

Re: SDL keystates problem.

Post by jakobnator »

Read it directly out of my c++ reference book.
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: SDL keystates problem.

Post by dandymcgee »

Keyboard events have a type, there is one for KEY_DOWN and one for KEY_UP.

Code: Select all

while (SDL_PollEvent(&keyevent))   //Poll our SDL key event for any keystrokes.
{
  switch(keyevent.type){
    case SDL_KEYUP:
      switch(keyevent.key.keysym.sym){
        case SDLK_RETURN:
          //Enter key let up
          break;
        default:
          break;
      }
  }
}
Handling keyboard events
http://www.gpwiki.org/index.php/SDL:Tut ... oard_Input

SDL key codes
http://wiki.libsdl.org/moin.cgi/SDL_Keycode
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: SDL keystates problem.

Post by MadPumpkin »

short wrote:
jakobnator wrote: EDIT: btw, for class methods you don't put inline if you already have it on a single line it automatically makes it inline ;)
twat? compiler dependent?
I don't think it is, but I am using Visual so I dunno.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: SDL keystates problem.

Post by Ginto8 »

jakobnator wrote:EDIT: btw, for class methods you don't put inline if you already have it on a single line it automatically makes it inline ;)
...no. The way C++ deals with methods is that, if the method's body is INSIDE the class declaration, it tries to inline it. The amount of lines doesn't matter one bit.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: SDL keystates problem.

Post by MadPumpkin »

Ginto8 wrote:
jakobnator wrote:EDIT: btw, for class methods you don't put inline if you already have it on a single line it automatically makes it inline ;)
...no. The way C++ deals with methods is that, if the method's body is INSIDE the class declaration, it tries to inline it. The amount of lines doesn't matter one bit.
Alright cool, wasn't sure exactly how it works thanks for clearing it.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: SDL keystates problem.

Post by M_D_K »

While polling will work for what you're doing right now, take this as a chance to learn how to dump the entire key state at once, it'll help make things clearer.

As for your immediate problem, it's a key repeat problem, you can solve it by checking to make sure the key hasn't already been pressed or you could disable key repeat.

Code: Select all

SDL_EnableKeyRepeat(0, 0); //disables repeat
(that should work)

You should look at SDL_GetKeyState if you wanna go the dumping the entire keystate route. Makes it really easy to keep 2 copies of the key state (current and last frame's) so you can do things like:

Code: Select all

//index is an SDLK_ define like SDLK_ESCAPE
bool KeyPressed(int index) const { return curr_keys[index] && !prev_keys[index]; }
...
bool KeyHeld(int index) const { return curr_keys[index] && prev_keys[index]; }
And the best part is no more polling ;)
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

Re: SDL keystates problem.

Post by jakobnator »

Odd....it works the first compile, then if I run it again it doesn't do anything but if I delete the line, recompile it, put the line back, then compile again it will work once more. Any ideas?
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
Aleios
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 78
Joined: Mon Feb 21, 2011 2:55 am
Current Project: Aleios Engine
Favorite Gaming Platforms: PC, Dreamcast
Programming Language of Choice: C++
Location: Melbourne, Australia

Re: SDL keystates problem.

Post by Aleios »

Did you change anything else? Trace your steps. Print some sort of debug text to the command line. The function "SDL_EnableKeyRepeat" returns a value of 0 on success, output what it returns. Also, if all you did was add, remove and then re-add the function, you must have done something else, otherwise im confused as fuck.
Image
User avatar
jakobnator
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 20
Joined: Thu Mar 31, 2011 8:14 pm
Current Project: Black Jack
Favorite Gaming Platforms: N64,DC,PC,360
Programming Language of Choice: C++0x
Location: (n): A particle place in physical space.

Re: SDL keystates problem.

Post by jakobnator »

It returns 0, and I don't know what you mean about adding.

EDIT: I have a temporary fix by using a debounce that is reset by pressing the "G" key, so everytime you want to make a move you have to press "G" but oh well :lol: .
Image

Current Games:
Black Jack [WIP]
Tic Tac Toe [SDL]
Tic Tac Toe
Post Reply