Sign in to follow this  
siliconsurfer

SDL Keys problem.. Loading from file

Recommended Posts

Hi I have Unit8 *keys = SDL_GetKeyState(NULL); At the moment I can do something like
if(keys[SDLK_UP])
{
    //move the player up
}


My problem is I'd like the player to be able to change the keys they use. So for example If I had a text file with UP: SDLK_UP DOWN: SDLK_DOWN LEFT: SDLK_LEFT RIGHT: SDLK_RIGHT The values on the right would be used for the respective actions. If I read SDLK_UP into a char array such as char *key[20]. How cn I compare that with keys which is assigned to GetKeyState?? Any ideas

Share this post


Link to post
Share on other sites
Right. Although, you may want to translate the user specified keys to their SDL values at load time and store the ints instead of the strings. Then you can use integer comparisons instead of string comparisons. Unfortunately, there isn't a function to go from the string to the SDLKey, so you'd have to create some kind of look-up table to do the translations. Do-able, but a bit of work.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dave Hunt
Right. Although, you may want to translate the user specified keys to their SDL values at load time and store the ints instead of the strings. Then you can use integer comparisons instead of string comparisons.


I like this idea - and it is not that hard to do either [wink]. Here is a very quick example of one way to approach this problem -


// Have a map of the keys and translations
std::map< std::string, int > KeyMap;
// Have a function to translate
void LoadKeyConfig( std::string filename );
...
// Open the file and read in the string name then int assigned
void LoadKeyConfig( std::string filename )
{
std::ifstream IF;
IF.open( filename.c_str() );
if( !IF.is_open() )
{
abort();
}
int var;
std::string tempstr;
while( IF >> tempstr )
{
IF >> var;
KeyMap[ tempstr ] = var;
}
IF.close();
}
...
// If we press escape
if( g_keys[ KeyMap["SDLK_ESCAPE"] ] )
{
g_done = 1;
}

// If we press right
if( g_keys[ KeyMap["SDLK_RIGHT"] ] )
{
// Add 5 to the X
X += 5;

// If we have this line, it will only move once per key release
g_keys[ KeyMap["SDLK_RIGHT"] ] = 0;
}
...




Now that example is by no means the *best* thing to do, but it is just so you can get an idea of how you can go about this. Here is the sample project that you can run to see it in action. If you open up the Test.txt file, you will see two keys defined. Try changing the escape from 27 to 97 ('a'). You will see when you now hit 'A' it will escape, but when you hit escape it does nothing.

One thing to keep in mind is that you must use 'a'-'z' values for the alpha keys, not 'A'-'Z'. That is just the way SDL works offhand with this process. Take a look at sdl_keysym.h in order to see what the translate values are for each key. Good luck!

- Drew

Share this post


Link to post
Share on other sites
The other thing you could do is simply force the user to use the SDL key numbers, then translate that to an enum number in the programm. The user is going to have to look up what you call [insert key] anyway, so might as well make it easy on yourself.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this