FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Key States in SDL

 
Post new topic   Reply to topic     Forum Index -> Derelict
View previous topic :: View next topic  
Author Message
RedShft



Joined: 15 Feb 2012
Posts: 12

PostPosted: Fri Mar 02, 2012 3:04 pm    Post subject: Key States in SDL Reply with quote

I have been messing around with Input and SDL and I am wondering if the approach i'm trying to create is needed:

I am not entirely sure how to properly initialize lastKeyboardState and currentKeyboardState right now, so just assume they are initialized properly.

Code:

class Input
{
    bool IsKeyDown ( ref SDL_Event event )
    {
        SDL_Event lastKeyboardState;
        SDL_Event currentKeyboardState;
        if ( lastKeyboardState.type == SDL_KEYUP && currentKeyboardState == SDL_KEYDOWN )
             return true;
        else
            return false;
    }
}


Is this needed? Right now, the way I have it is that IsKeyDown(...) will return true whenever event.type == SDL_KEYDOWN. Also, with another function IsKeyReleased(...), that just checks for event.type == SDL_KEYUP, it constantly returns true instead of only returning true once.

So to summarize, this is my current approach (which i'm pretty sure is flawed):

Code:

import derelict.sdl.sdl;

class PInput
{
   bool IsKeyDown ( ref SDL_Event event )
   {
      return ( event.type == SDL_KEYDOWN );
   }

   bool IsKeyReleased ( ref SDL_Event event )
   {
      return ( event.type == SDL_KEYUP );
   }

   bool KeyPressed ( ref SDL_Event event, SDLKey Key )
   {
      return ( event.key.keysym.sym == Key );
   }
}


How would I capture the lastKeyboardState and currentKeyboardState properly?
Back to top
View user's profile Send private message
aldacron



Joined: 05 May 2004
Posts: 1322
Location: Seoul, South Korea

PostPosted: Sat Mar 03, 2012 6:08 am    Post subject: Re: Key States in SDL Reply with quote

First, I really recommend you take this sort of question to the SDL mailing list (or the web interface at libsdl.org). For one thing, you'll get a lot more experienced eyes on it. And for another, I can't really let this forum become a support board for every library Derelict binds to. Please try to keep posts here related to problems to Derelict. With that said...

RedShft wrote:
I have been messing around with Input and SDL and I am wondering if the approach i'm trying to create is needed:

I am not entirely sure how to properly initialize lastKeyboardState and currentKeyboardState right now, so just assume they are initialized properly.

Code:

class Input
{
    bool IsKeyDown ( ref SDL_Event event )
    {
        SDL_Event lastKeyboardState;
        SDL_Event currentKeyboardState;
        if ( lastKeyboardState.type == SDL_KEYUP && currentKeyboardState == SDL_KEYDOWN )
             return true;
        else
            return false;
    }
}


Is this needed? Right now, the way I have it is that IsKeyDown(...) will return true whenever event.type == SDL_KEYDOWN. Also, with another function IsKeyReleased(...), that just checks for event.type == SDL_KEYUP, it constantly returns true instead of only returning true once.

So to summarize, this is my current approach (which i'm pretty sure is flawed):

Code:

import derelict.sdl.sdl;

class PInput
{
   bool IsKeyDown ( ref SDL_Event event )
   {
      return ( event.type == SDL_KEYDOWN );
   }

   bool IsKeyReleased ( ref SDL_Event event )
   {
      return ( event.type == SDL_KEYUP );
   }

   bool KeyPressed ( ref SDL_Event event, SDLKey Key )
   {
      return ( event.key.keysym.sym == Key );
   }
}


How would I capture the lastKeyboardState and currentKeyboardState properly?


You can do a simple key state map using a static array of bools, one for each SDLKey value.

Code:

bool[SDLK_LAST] keyStates; // true if pressed, false if not

bool isKeyDown(SDLKey key)
{
    return keyStates[key];
}



You don't need an isKeyReleased -- just call this method with a ! in front of it when you need to know that. In your event loop that pumps SDL events, you should have something like this:

Code:

SDL_Event event;
while(SDL_PollEvent(&event))
{
    switch(event.type)
    {
        case SDL_KEYDOWN:
            keyStates[event.key.keysym.sym] = true;
            break;

        case SDL_KEYUP:
            keyStates[event.key.keysym.sym] = false;
            break;

        ...
    }

}


Then as long as you are testing specific keys each frame, their state will always be up-to-date. And you can also catch multiple key presses.

Personally, I prefer to use callbacks via delegates or function pointers. When an event is received, the appropriate callback is called. For example:

Code:

alias void delegate(ref const(SDL_KeyboardEvent) event) OnKeyDown;

class SomeClass
{
    void onKeyDown(ref const(SDL_KeyboardEvent) event)
    {
        switch(event.keysym.sym)
        { ... }
    }

    void init()
    {
        ...
        MyEventPump.setKeyDownHandler(&onKeyDown);
        ...
    }
}


Or you can go more general, handling all key events in one generic callback, or whatever.
_________________
The One With D | The One With Aldacron | D Bits
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> Derelict All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group