Tutorial 5, DirectX using C#
DirectX using C#
Some sort of input is always required to control the main character of the game, or more specifically involve user in the game. Most people play games using keyboard, so it is important to support keyboard in PC games.
Let's see how we'll take input from keyboard.
There's a separate library that is extensively used for taking inputs. You'll need to add this dll as reference from your Solution explorer. After that, add these line somewhere before namespace
I have redefined it with 'DI' to avoid ambiguity between Microsoft.DirectX.Direct3D.Device and Microsoft.DirectX.DirectInput.Device (both contain a class named Device)
Acquire the keyboard
After initializing Direct3d device, we will start listening to the keyboard (hook it up). We will retrieve the keyboard state after each render, which means we will get sort of an array of all keys in byte form. Those which are pressed will contain 'non-zero' value while others '0'.
So, everytime we get a byte array of length 256, and suppose 'A' is pressed at that moment, so the index of the array corresponding to DirectInput.Key.A will have a non-zero value while all others have a '0'.
Using the function
All game logic will go to OnPaint method of the main form.
Note: You can isolate class CosmicX into another file to improve code readability and understanding. You'll see besides framework, required code to create your own game is that small and easy!!