About keyboard input


#1

Hey everyone, another beginner looking for advices :p! This time about keyboard input for games using SDL. Context: wrinting a (very simple) game in C. Yes, I know, C.

I want to use a simple and to-the-point style, and basically use and refer to SDL only in my main.c file, similar to writing a simple Win32 platform layer. In this main.c should be all platform specific code, that is : creation of window, call to an game_init function, an outer main loop that keeps track of time using SDL_GetTicks() and calls game_update function at regular time intervals using SDL_Delay(FIXED_DT- elasped_time) style, and a call to a game_cleanup function before returning to OS.

Basically, all game related stuff would be in other C modules (game.h + game.c, keyboard.c + keyboard.h…) and I’d like to have little to no references to SDL structs or functions outside of main.c.

How do you guys feel about this approach ? Does it even makes sense ? Is it stupid ? What are the things that come to your mind when reading this ? I’ve started doing this and experienced both questions/difficulties but I’d like to first hear all you have to say without being influenced by my noobish way of seeing/doing stuff first.

If i’m just being too plain stupid/intolerably noob for you to make sense, please just ignore my post. In any case, thank you for taking the time for reading this!


#2

After thinking about what I expressed here think I should at least give a high level overview of the strategy that I thought about, otherwise this post could be perceived as “hey, I want to do keyboard input, code it for me!”. What I have in mind is creating a struct that represents the state of the keyboard, something like:

typedef struct _keyboard_state {
bool last[KEYBOARD_KEY_MAX];
bool current[KEYBOARD_KEY_MAX];
} keyboard_state;

This structure could be updated / filled in using information provided by SDL_event queue and/or SDL own keyboard state table in main.c. Then I would have a SDL independant keyboard structure representing last/current keyboard state that I could pass to my game update function, thus making all game code unrelated to SDL functions / data structures / enums! No reference to SDL outside of main!


#3

I think you may be limiting yourself by only using SDL.h in the main and will probably lose intrest in what you wanted to achieve by trying to figure all this out so soon. Maybe get a basic prototype of your game running and then go back and see how you might achieve what you want.

I think you will soon see that this task can get difficult and messy.

What I say to people learning or begining is don’t try write the perfect code,
#1 It doesn’t exist “Everyone has different opinions on whats best”
#2 You will find it difficult to finish anything.


#4

To add to Smiles’ good reply: SDL is a lot more than just something to use for basic graphics and input events. SDL2 is capable of doing both of those as well as: Audio, Threading (Use with caution), Force Feedback, Timers, File I/O, and a bit more. I believe one of its larger goals is to be so platform independent that you can take your code and compile it for multiple OSs without any changes, (my current one works like that right now).

I really don’t see any reason to use SDL in only your main.c file, and then have platform specific code in other files when SDL will do that for you… Unless you absolutely need platform specific functions.

Nothing wrong with C, and SDL is coded in C with bindings to other languages.


#5

Thanks to both of you for your replies! Sounds like words of wisdom to me. I think I’ll just take the time to think about what you said and just build stuff with SDL the simplest way I can think of and see what happens!