• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
ultramailman

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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Why don't you just put:

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

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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]

No maddening was done ;o

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

Your mileage may vary.
[/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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0