Page 1 of 1

SDL keystates problem.

Posted: Mon Jun 27, 2011 3:51 pm
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;
        }
    }

Re: SDL keystates problem.

Posted: Mon Jun 27, 2011 4:17 pm
by xiphirx
You probably want to trigger this when you release enter, not when its held down, or pressed.

Also, please indent your code.

Re: SDL keystates problem.

Posted: Mon Jun 27, 2011 5:14 pm
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 ->

Re: SDL keystates problem.

Posted: Mon Jun 27, 2011 6:32 pm
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;}

Re: SDL keystates problem.

Posted: Mon Jun 27, 2011 8:08 pm
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 ;)

Re: SDL keystates problem.

Posted: Mon Jun 27, 2011 10:10 pm
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?

Re: SDL keystates problem.

Posted: Mon Jun 27, 2011 10:20 pm
by jakobnator
Read it directly out of my c++ reference book.

Re: SDL keystates problem.

Posted: Tue Jun 28, 2011 5:14 pm
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

Re: SDL keystates problem.

Posted: Wed Jun 29, 2011 10:24 pm
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.

Re: SDL keystates problem.

Posted: Thu Jun 30, 2011 2:02 pm
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.

Re: SDL keystates problem.

Posted: Fri Jul 01, 2011 3:07 pm
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.

Re: SDL keystates problem.

Posted: Sun Jul 03, 2011 3:30 pm
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 ;)

Re: SDL keystates problem.

Posted: Tue Jul 19, 2011 8:21 pm
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?

Re: SDL keystates problem.

Posted: Tue Jul 19, 2011 11:30 pm
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.

Re: SDL keystates problem.

Posted: Fri Jul 29, 2011 10:08 am
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: .