Hiding SDL from the rest of the program

Started by
12 comments, last by ultramailman 11 years, 6 months ago

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

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


No maddening was done ;o


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.


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?
Yes, that was basically what I advised.

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.

This topic is closed to new replies.

Advertisement