# Hiding SDL from the rest of the program

This topic is 1927 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, this is my first post in gamedev. I thought asking questions in a forum would be very helpful, so here I am.

I am in the process of making a simple 2D game, with SDL. I am trying to hide the SDL subsystems from the program, and only #include them in the .c files. I was able to hide the graphics part of SDL, but now I am not sure how to hide the SDL event system. The SDL event system has hundreds of constants named SDLK_<key>, but I'd like to assign those constants to KEY_<key> instead. Right now i have something like this:

in event.h:
 #ifndef EVENT_HHH #define EVENT_HHH extern int const KEY_A; extern int const KEY_B; /*....*/ extern int const KEY_SPACE; #endif 

and in event.c:
 #include "event.h" #include <SDL/SDL.h> int const KEY_A = SDLK_a; int const KEY_B = SDLK_b; /*.../* int const KEY_SPACE = SDLK_SPACE; 

Since there are hundreds of these constants, it feels like it's very easy to make errors if i just assign them one by one.
My question is, is there a shorter and better way to do this?

##### Share on other sites
Well, if your programming in C:
Why are you programming in C? (Not that I have anything against it, but what's the point? Is there anything we could know to help you?)
And if your programming in C++:
You define a const by saying:
 const int i = 5; 
Not by saying:
 int const i = 5; 
I'm not sure about a shorter and better way, however a loop might be possible? (Probably not )
Sorry if my answer is terrible, but I don't use SDL, and a little more clarification about what you're trying to do would probably be helpful. Edited by superman3275

##### Share on other sites

Well, if your programming in C:
Why are you programming in C? (Not that I have anything against it, but what's the point? Is there anything we could know to help you?)
And if your programming in C++:
You define a const by saying:
 const int i = 5; 
Not by saying:
 int const i = 5; 
I'm not sure about a shorter and better way, however a loop might be possible? (Probably not )
Sorry if my answer is terrible, but I don't use SDL, and a little more clarification about what you're trying to do would probably be helpful.

Hi superman. A loop sounds interesting. I'll probably have to use of macros though, since they have to be constants. As for I am using C, I am not sure. I am getting more comfortable with C though, so I am sticking with it, at least for the duration of this game. Also, "int const" compiles fine, with gcc at least. Edited by ultramailman

##### Share on other sites
I just looked it up, and in C it's defined as const int too. So I recommend you try doing that, because if it's compiling fine odds are you're going to do something and then your code will break because of it. It would probably be better to use const int, because it's better practice and int const will probably end up giving you some bugs eventually. It's better to get into good habits now then find out in 2 months that no-one understands why you're declaring everything as int const.
However,
I don't know C, and maybe that's how SDL is supposed to work in C. If it's compiling fine try doing it both ways and see if your code breaks when you do it my way. Edited by superman3275

##### Share on other sites
Why don't you just put:

[source lang="java"]#define SDLK KEY[/source] Edited by Captacha

##### Share on other sites
The C or C++ standards accept both const int and int const. The choice between the two versions is a matter of taste and conventions. const int is probably more common, but there are people advocating the other version. Just choose one and use it consistently.

I think the best solution to this problem is to design your application to handle all the inputs in a single place. In this way, the SDL code is limited to a small fraction of your application. However, redefining the input keys with more meaningful names can be very useful to increase the readability of your code. You may for example define things like FIRE_KEY, JUMP_KEY, INVENTORY_KEY (whose values may actually vary based on configuration files or platform)... Edited by apatriarca

##### Share on other sites
In my experience, the ASCII character values are comparable to SDLK enums, for all letter keys. So, assuming that you have a function like int getHitKey(Uint32 sdlk), calling getHitKey('a') should work as expected in many cases, to return the status of that particular key.

The rest of the enums can be manually re-defined, because there's not that many of them, and you usually only need a subset of those.

##### Share on other sites

I just looked it up, and in C it's defined as const int too. So I recommend you try doing that, because if it's compiling fine odds are you're going to do something and then your code will break because of it. It would probably be better to use const int, because it's better practice and int const will probably end up giving you some bugs eventually. It's better to get into good habits now then find out in 2 months that no-one understands why you're declaring everything as int const.
However,
I don't know C, and maybe that's how SDL is supposed to work in C. If it's compiling fine try doing it both ways and see if your code breaks when you do it my way.

I looked it up, now I am pretty sure it's just a style and preference thing.

Why don't you just put:

[source lang="java"]#define SDLK KEY[/source]

I could, but that would also require me to include SDL in event.h, because I was intending to use the KEY_<key> constants in other modules of the game.

... You may for example define things like FIRE_KEY, JUMP_KEY, INVENTORY_KEY (whose values may actually vary based on configuration files or platform)...

Oh, this might be the solution. If I read that right, you mean to define extern FIRE_KEY and JUMP_KEY and etc in the header file, and set their values to some SDL_K's according to some configuration files in the .c file?

In my experience, the ASCII character values are comparable to SDLK enums, for all letter keys. So, assuming that you have a function like int getHitKey(Uint32 sdlk), calling getHitKey('a') should work as expected in many cases, to return the status of that particular key.

The rest of the enums can be manually re-defined, because there's not that many of them, and you usually only need a subset of those.

Oh, thanks for reminding. I was staying away from it because I remember the docs said somewhere that it's not always true for all keyboards. I'll try this too, then.

##### Share on other sites
I was taught const int, and when I looked it up that's how other people defined it. Sorry if I made you mad Edited by superman3275

##### Share on other sites
FWIW, I had the same issue when trying to add a controller mapping class to my game engine (which backends into SDL). Upon spending numerous hours trying to wrap SDL's already well-wrapped event pump functionality, I decided that trying to wrap SDL was pretty silly and not very fruitful, so I just exposed it. I figure SDL has already encapsulated things about as well as I could hope to anyway, and putting any kind of wrapper around SDL's functionality, when not 100% necessary, would only serve to muddle my API.

Your mileage may vary. Edited by akesterson

##### Share on other sites

Why don't you just put:

[source lang="java"]#define SDLK KEY[/source]

This wouldn't work anyways. Preprocessor replacement only works on whole tokens; it won't substitute partial tokens. What you could do is grab a copy of SDL_keysym.h and use your text editor's find and replace "SDLK" with "const int KEY" then replace "," with ";" and copy and paste the resulting modified lines into your .c file.

As for const, in C, whether const goes before or after the type is the same unless you're dealing with a pointer type. For example const int and int const are the same. However const int * and int * const are different. const int * is a pointer to a const int, and could also be written int const *. int * const is a const pointer to a non-const int. And you can double them up with const int * const which is a const pointer to a const int. Since for a pointer type you always need to add the const after the type, some people think it's more consistent to put const after all types. Other people argue that since you stick unsigned in front of types you should also stick const in front of types when you can. Then there's the group I'm in, which just says pick one and it doesn't matter which as long as you're consistent within a code base.

##### Share on other sites

I was taught const int, and when I looked it up that's how other people defined it. Sorry if I made you mad

FWIW, I had the same issue when trying to add a controller mapping class to my game engine (which backends into SDL). Upon spending numerous hours trying to wrap SDL's already well-wrapped event pump functionality, I decided that trying to wrap SDL was pretty silly and not very fruitful, so I just exposed it. I figure SDL has already encapsulated things about as well as I could hope to anyway, and putting any kind of wrapper around SDL's functionality, when not 100% necessary, would only serve to muddle my API.

Yes, I am more inclined towards not making my own names for SDLK's, after reading apatriarca's post. I will not be exposing SDL either though.

...What you could do is grab a copy of SDL_keysym.h and use your text editor's find and replace "SDLK" with "const int KEY" then replace "," with ";" and copy and paste the resulting modified lines into your .c file.
...

Ohhh.... copying directly from SDL headers, I didn't think of that, even though it's so simple.
Rough draft of what I have in mind now is this, after reading apatriarca's post:

in event.h:
 extern int KEY_PICKUP; extern int KEY_LEFT; extern int KEY_RIGHT; int event_init(void); char get_key_state(int); 

in event.c:
 #include "event.h" #include <SDL/SDL.h> char key_state[350]; int KEY_PICKUP; int KEY_LEFT; int KEY_RIGHT; int event_init(void) { SDL_Init(0); if(SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_EVENTTHREAD) != 0) return 1; KEY_PICKUP = file_get_int("config", KEY_PICKUP); KEY_LEFT = file_get_int("config", KEY_LEFT); KEY_RIGHT = file_get_int("config", KEY_RIGHT); return 0; } char get_key_state(int key) { return key_state[key]; } 

This feels all right with me, because it hides SDL pretty well, and it actually hides the keyboard pretty well too. What does everyone think?

##### Share on other sites
Yes, that was basically what I advised. Edited by apatriarca

##### Share on other sites

Yes, that was basically what I advised.

Thanks, this was helpful. I will do it this way. But if anyone has other alternatives, do share it. Edited by ultramailman