Animations DirectInput
Animations DirectInput
DirectInput
Objectives
Understand frame-based animation
Create a simple asset management
framework
Process keyboard events with DirectInput
Animations
What is sprite?
A sprite is a two-dimensional image or
animation that is integrated into a larger
scene. (Wikipedia)
Draw a portion (sprite) of a texture
DrawSpritesImmediate
Draw a portion (sprite) of a texture
0 145 367
R G B A R G B A R G B A
0-255 – 8bits
NOTE: Usually, each color channel use 8-bits but it could be different in
other image format
ALPHA BLENDING
That means: the final color is made up of 70% of the source color and 30% of the
destination color
Animation example
Frame 1
t
Animation example
Frame 2
t
Animation example
Frame 3
t
Animation example
Frame 1
t
Animation example
Frame 2
t
Animation example
Frame 3
t
Animation example
Frame 1
t
What is an animation?
An animation is a set of frames displayed in a
specific order
Animation = array of frames
A frame = a sprite ?
A frame = a sprite + frame time: a time period to
display that sprite before switching to next frame
(frame time, is usually the same on all frames)
A sprite is a portion of an image
top, left, right, bottom
An image is a texture
Important classes
CTexture : warpper to ID3D10TEXTURE
CTextures : to store all textures in our game
CSprite: store a sprite
CSprites: store all sprites in our game
CAnimationFrame: an animation frame =
sprite + frame time
CAnimation = array of frames
CAnimations = all animations in our game
unordered_map
You can think of it as a generic dictionary. You
can find a value given a key
unordered_map<int, LPTEXTURE> textures
Key type is: int
Value type is: LPTEXTURE (texture)
Usage:
textures[100] = texBrick;
texBrick = textures[100];
DirectInput
Keyboard
DirectInput8Create
LPDIRECTINPUT8::CreateDevice
GUID_SysKeyboard
LPDIRECTINPUTDEVICE::SetDataFormat
c_dfDIKeyboard
LPDIRECTINPUTDEVICE::Acquire
GetDeviceState
GetDeviceData
LPDIRECTINPUTDEVICE::Unacquire
Keyboard
result = didev->SetDataFormat(&c_dfDIKeyboard);
result = didev->Acquire();
return 1;
}
Keyboard
...
char keys[256];
didev->GetDeviceState(sizeof(keys),&keys);
if (KEYDOWN(keys, DIK_ESCAPE))
PostMessage(hWnd,WM_DESTROY,0,0);
if (KEYDOWN(keys, DIK_HOME))
...
keys[27]
0 1 2 27 255
A X X X X X X X
AND
1 0 0 0 0 0 0 0
A 0 0 0 0 0 0 0
Keyboard
didev->GetDeviceState(sizeof(keys),&keys);
UP
DOWN
t
GetDeviceState
Buffered input
// Initialize
// The buffer size is a DWORD property associated with the
device.
DIPROPDWORD dipdw;
dipdw.diph.dwSize = sizeof(DIPROPDWORD);
dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
dipdw.diph.dwObj = 0;
dipdw.diph.dwHow = DIPH_DEVICE;
dipdw.dwData = KEYBOARD_BUFFER_SIZE;
_Keyboard->SetProperty( DIPROP_BUFFERSIZE, &dipdw.diph );
Buffered input
// Handle key press event
// NOTES: Buffered input is like an Event
KeyDown
UP
KeyUp
DOWN
t
GetDeviceData
Windows vs DirectX
GetAsyncKeyState vs GetDeviceState
WinProc vs GetDeviceData
Mouse
DirectInput8Create
LPDIRECTINPUT8::CreateDevice
GUID_SysMouse
LPDIRECTINPUTDEVICE::SetDataFormat
c_dfDIMouse
LPDIRECTINPUTDEVICE::Acquire
GetDeviceState
LPDIRECTINPUTDEVICE::Unacquire
Mouse
...
DIMOUSESTATE mouse_state;
dev_mouse->GetDeviceState(sizeof(mouse_state),&mouse_state);
struct DIMOUSESTATE {
LONG lX; // distance movement of X
LONG lY; // distance movement of Y
LONG lZ; // distance movement of mouse
wheel
BYTE rgbButtons[4]; // mouse button state
};