Followers 0

# Hiding SDL from the rest of the program

## 13 posts in this topic

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:
[CODE]
#ifndef EVENT_HHH
#define EVENT_HHH
extern int const KEY_A;
extern int const KEY_B;
/*....*/
extern int const KEY_SPACE;
#endif
[/CODE]

and in event.c:
[CODE]
#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;
[/CODE]

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?
0

##### 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:
[CODE]
const int i = 5;
[/CODE]
Not by saying:
[CODE]
int const i = 5;
[/CODE]
I'm not sure about a shorter and better way, however a loop might be possible? (Probably not [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img])
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
0

##### Share on other sites
[quote name='superman3275' timestamp='1350177018' post='4989937']
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:
[CODE]
const int i = 5;
[/CODE]
Not by saying:
[CODE]
int const i = 5;
[/CODE]
I'm not sure about a shorter and better way, however a loop might be possible? (Probably not [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img])
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.
[/quote]

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
0

##### 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
0

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

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

##### 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
1

##### 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.
0

##### Share on other sites
[quote name='superman3275' timestamp='1350178761' post='4989942']
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.
[/quote]

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

[quote name='Captacha' timestamp='1350181009' post='4989948']
Why don't you just put:

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

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.

[quote name='apatriarca' timestamp='1350183668' post='4989958']
... You may for example define things like FIRE_KEY, JUMP_KEY, INVENTORY_KEY (whose values may actually vary based on configuration files or platform)...
[/quote]

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?

[quote name='Goran Milovanovic' timestamp='1350191628' post='4989978']
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.
[/quote]

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.
0

##### 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
0

##### 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
1

##### Share on other sites
[quote name='Captacha' timestamp='1350181009' post='4989948']
Why don't you just put:

[source lang="java"]#define SDLK KEY[/source]
[/quote]
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.
1

##### Share on other sites
[quote name='superman3275' timestamp='1350221253' post='4990030']
I was taught const int, and when I looked it up that's how other people defined it. Sorry if I made you mad [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]
[/quote]

[quote name='akesterson' timestamp='1350221880' post='4990031']
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.

[/quote]

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.

[quote name='SiCrane' timestamp='1350224937' post='4990043']
...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.
...
[/quote]

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:
[CODE]

extern int KEY_PICKUP;
extern int KEY_LEFT;
extern int KEY_RIGHT;
int event_init(void);
char get_key_state(int);
[/CODE]

in event.c:
[CODE]
#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];
}
[/CODE]

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?
0

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

##### Share on other sites
[quote name='apatriarca' timestamp='1350246687' post='4990140']
Yes, that was basically what I advised.
[/quote]

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

## Create an account

Register a new account

Followers 0

• 19
• 14
• 23
• 11
• 28