Hiding SDL from the rest of the program

Started by
12 comments, last by ultramailman 11 years, 6 months ago
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?
Advertisement
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 smile.png)
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.

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !


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 smile.png)
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.
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'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

Why don't you just put:

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

+---------------------------------------------------------------------+

| Game Dev video tutorials -> http://www.youtube.com/goranmilovano | +---------------------------------------------------------------------+

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.
I was taught const int, and when I looked it up that's how other people defined it. Sorry if I made you mad :)

I'm a game programmer and computer science ninja !

Here's my 2D RPG-Ish Platformer Programmed in Python + Pygame, with a Custom Level Editor and Rendering System!

Here's my Custom IDE / Debugger Programmed in Pure Python and Designed from the Ground Up for Programming Education!

Want to ask about Python, Flask, wxPython, Pygame, C++, HTML5, CSS3, Javascript, jQuery, C++, Vimscript, SFML 1.6 / 2.0, or anything else? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

hobohm.business@gmail.com

or Personal-Message me on here !

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.

This topic is closed to new replies.

Advertisement