This week in Interactive 3D Graphics, we have been learning how to initialize and use Direct Input, as well as state machines. We were then set the task to implement this into our App framework that we were working on last week.
To have keyboard functionality in my framework, I first made an "Input Manager" class that would hide away all the initialization. This involved;
- Creating the DirectX device interface
- Obtaining the interface to the system Keyboard device
- Setting the data format (using a predefined keyboard format)
- setting the cooperation levels (based of flags that I had declared earlier)
Once the keyboard input was initialised, I then created a "IsKeyDown" function to use to check if a certain key was pressed:
bool InputManager::IsKeyDown(char key)
{
return(mKeyboardState[key] & 0x80) != 0;
}
this could now be used to move my camera on one of the DirectX tutorials I had ported into my framework:
if(gDInput->IsKeyDown(DIK_D))
eyePointX += 0.02f;
The state machine I found a bit more tricky. This meant taking out the tutorials that I had previously ported, and put them into a "State" class. My base state class was very simple:
#pragma once
class State
{
public:
State(void);
virtual ~State(void);
virtual bool OnBegin() { return true; } // Called when the state begins
virtual unsigned int Update() { return 0; } // Called every update
virtual bool OnEnd() { return true; } // Called when the state ends
};
OnBegin() - This was used for initialization, such as initializing the geometry and lights.
Update() - This was called every cycle, for setting up matrices and drawing the scene.
OnEnd() - This was called when the state was closing, and used to release any COM objects.
This was then used with my state machine, mapped to a unique I.D and used my Input manager to navigate through different states.
gStateMachine = new StateMachine();
State* p_LightState = new LightState();
State* p_MeshState = new MeshState();
gStateMachine->AddState(1,p_LightState);
gStateMachine->AddState(2,p_MeshState);
gStateMachine->SetState(1);
Gearing Up For My Final Year
8 months ago
0 comments:
Post a Comment