So my basic 2d game framework in SlimDX is going well. I've created a custom Sprite object, and have some rudimentary mastery of rendering sprites. I just need to move them now.
I have a basic game loop set up, which is based on an old C# game programming book. Having set up all the graphics device bits and bobs, and drawn my sprite on screen, I enter a method in my Game class called Run()
. This has the following content:
public void Run()
{
while( this.Created )
{
// Process one frame of the game
ProcessFrame();
Render();
// Handle all events
Application.DoEvents();
}
}
ProcessFrame()
is supposed to have all the game logic in it, in response to events. Whilst I have a simple event handler (based on an override of OnKeyDown
) set up to detect keypresses, I'm wondering how to collect keypresses such that I can process the responses to them in the ProcessFrame()
method.
Some initial research on this subject suggests creating a HashSet<Keys>
. I've never done this before, so I'm not sure how I would go about it.
There are probably many ways to do this, so I thought I'd see what people would recommend before jumping right in there.
Thanks in advance everyone.
I don't believe you need to manually capture inputs. This typically would be handled by the direct input keyboard class. I personally have not used SlimDX myself yet but as it is a wrapper for DirectX (which I've dabbled in) you should just need to initialize a variable to hold the reference to the keyboard so that you can poll it for changes, and then act upon those changes.
http://slimdx.org/docs/#T_SlimDX_DirectInput_Keyboard
Such that you would code something like
if (myKeyboard.IsKeyDown(Keys.Escape)) { ProgramStatus = GameState.Title; }
assuming my reference variable was called myKeyboard.
Edit: I might also mention that depending on the particular library your using that it can be beneficial to save current, and previous keyboard states. This is very useful when you want to ensure that the program only takes in one keystroke vs many.
XNA as I recall has a KEYDOWN method for controllers, useful to test if the button is being held down. If you need to take in a input for each time the button is hit capture both. IE if your library has a Keypress method, your good, if it captures KEYDOWN only you may need to capture current and previous.
if (myMouse.LeftButton == ButtonState.Pressed && oldMouse.LeftButton == ButtonState.Released)