Jump to content

  • Log In with Google      Sign In   
  • Create Account


Hiding SDL from the rest of the program


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 ultramailman   Prime Members   -  Reputation: 1557

Like
0Likes
Like

Posted 13 October 2012 - 06:52 PM

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?

Sponsor:

#2 superman3275   Crossbones+   -  Reputation: 1976

Like
0Likes
Like

Posted 13 October 2012 - 07:10 PM

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 Posted Image)
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, 13 October 2012 - 07:11 PM.

I'm a game programmer and computer science ninja ph34r.png!

Here's Breakout:
Breakout!

Want to ask about Python and / or Pygame? What about HTML5 / CSS3 / JavaScript? What about C++ and / or SFML 2 (and 1.6)? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

Superman3275@Gmail.com

or Personal-Message me on here smile.png!


#3 ultramailman   Prime Members   -  Reputation: 1557

Like
0Likes
Like

Posted 13 October 2012 - 07:34 PM

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 Posted Image)
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, 13 October 2012 - 07:35 PM.


#4 superman3275   Crossbones+   -  Reputation: 1976

Like
0Likes
Like

Posted 13 October 2012 - 07:39 PM

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, 13 October 2012 - 07:41 PM.

I'm a game programmer and computer science ninja ph34r.png!

Here's Breakout:
Breakout!

Want to ask about Python and / or Pygame? What about HTML5 / CSS3 / JavaScript? What about C++ and / or SFML 2 (and 1.6)? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

Superman3275@Gmail.com

or Personal-Message me on here smile.png!


#5 Captacha   Members   -  Reputation: 141

Like
0Likes
Like

Posted 13 October 2012 - 08:16 PM

Why don't you just put:

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

Edited by Captacha, 13 October 2012 - 08:17 PM.


#6 apatriarca   Crossbones+   -  Reputation: 1601

Like
1Likes
Like

Posted 13 October 2012 - 09:01 PM

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, 13 October 2012 - 09:01 PM.


#7 Goran Milovanovic   Members   -  Reputation: 1103

Like
0Likes
Like

Posted 13 October 2012 - 11:13 PM

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.

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

| Need a programmer?        ->   http://www.nilunder.com/protoblend   |

| Want to become one?       ->   http://www.nilunder.com/tutoring     |
| Game Dev video tutorials  ->   http://www.youtube.com/goranmilovano |
+---------------------------------------------------------------------+

#8 ultramailman   Prime Members   -  Reputation: 1557

Like
0Likes
Like

Posted 14 October 2012 - 02:39 AM

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.

#9 superman3275   Crossbones+   -  Reputation: 1976

Like
0Likes
Like

Posted 14 October 2012 - 07:27 AM

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, 14 October 2012 - 07:28 AM.

I'm a game programmer and computer science ninja ph34r.png!

Here's Breakout:
Breakout!

Want to ask about Python and / or Pygame? What about HTML5 / CSS3 / JavaScript? What about C++ and / or SFML 2 (and 1.6)? Recruiting for a game development team and need a passionate programmer? Just want to talk about programming? Email me here:

Superman3275@Gmail.com

or Personal-Message me on here smile.png!


#10 akesterson   Members   -  Reputation: 150

Like
1Likes
Like

Posted 14 October 2012 - 07:38 AM

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, 14 October 2012 - 07:38 AM.


#11 SiCrane   Moderators   -  Reputation: 9387

Like
1Likes
Like

Posted 14 October 2012 - 08:28 AM

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.

#12 ultramailman   Prime Members   -  Reputation: 1557

Like
0Likes
Like

Posted 14 October 2012 - 11:30 AM

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


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?

#13 apatriarca   Crossbones+   -  Reputation: 1601

Like
0Likes
Like

Posted 14 October 2012 - 02:31 PM

Yes, that was basically what I advised.

Edited by apatriarca, 14 October 2012 - 02:31 PM.


#14 ultramailman   Prime Members   -  Reputation: 1557

Like
0Likes
Like

Posted 14 October 2012 - 07:46 PM

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, 14 October 2012 - 08:19 PM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS