I am starting to learn C programming (though I have a lot of experience using higher-level languages). I want to write a game engine using C and OpenGL. I think I understand the graphics rendering part, but now I'm curious about how to receive and process keyboard/mouse/controller input. How does one implement this sort of thing in C?
|
While you can certainly pick a cross platform library, if you want to write the input functions yourself, you'd need to create some generic code interfacing rules or C++ objects which would cover all platforms to do this neatly. Then you would create implementations that are platform specific, only compiled when the library is being built for that platform. At least that piece, I can assist with. The following link shows how to use preprocessor directives to detect Windows, and it should work in most cases, although there may be some caveat for 32-bit versions. You would still need to find platform specific APIs to manage the input gathering for each target platform, and perhaps manage the project settings for the compiler / IDE to create multiple build targets, each one for a different target environment. In short, it's the complicated method... http://stackoverflow.com/questions/3213037/determine-if-linux-or-windows-in-c If you instead wish to use GLUT, a tutorial is available here that should set you on the right path. I don't think it includes creating Windows, but I hear that GLUT does that as well... http://www.lighthouse3d.com/tutorials/glut-tutorial/keyboard/ If you have any other questions, I can see what can be done to answer them. --- EDIT ---- Since it was requested, I can provide a high level idea of how input gathering works, and with some more direction, I can give something more concrete, although I've never used GLUT personally. There are two methods of gathering input, polling and event based. Polling is preferred for gaming, because you want to be able to get all the input that happened in a frame at once during your update loop. However, in reality, it's all happening in an event based model, even if you go as deep as interrupt handlers in your operating system kernel, interacting with the lowest levels of circuitry. I won't belabor that part, because someone else is probably better licensed to answer that question. In an event based model, some callback function is fired each time a key is pressed. This happens asynchronously, so some form of multitasking is involved, such as threading, or interrupts, at the lower levels. In this model you hand the input engine a function, which is called each time the event happens, in this case, a key press or a mouse movement. Let's say it looks like this, for a fictitious InputObject API...
Obviously, you would want more functionality, like getting modifier keys, or getting mouse button presses and the position, but you get the idea. The best idea for a video game, though, if it's not already done for you, is to create a polling environment. In this environment, the polling mechanism is basically a queue or some other structure into which our fictitious InputObject entries would be logged. Then all at once, we would call some methods on our input poll, which would allow us to dequeue InputObjects and respond to them, one at a time. This has the obvious advantage that all input since you last ran update in your game loop is collected and responded to at the same time, so that the game logic isn't updating at a different timestep than the input gathering. An input polling method might look like the following...
For some input libraries, you may have to convert events into a polling queue of your own design. In this case, the events simply enqueue the objects containing input into some input queue, and you get them one at a time on each update. This is the preferred method, as previously stated. Hopefully, that's clear as mud, but if it's ever so slightly clearer, please feel free to offer points:) |
|||||
|