0% found this document useful (0 votes)
40 views32 pages

Animations DirectInput

The document discusses frame-based animation and DirectInput for processing keyboard and mouse input. It explains that animations are composed of frames, with each frame containing a sprite and frame time. Keyboard and mouse input can be read using DirectInput by getting the device state and buffered input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views32 pages

Animations DirectInput

The document discusses frame-based animation and DirectInput for processing keyboard and mouse input. It explains that animations are composed of frames, with each frame containing a sprite and frame time. Keyboard and mouse input can be read using DirectInput by getting the device state and buffered input.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Animations &

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

0.0 145 / 367 1.0


Draw a portion (sprite) of a texture

sprite.TexCoord.x = rect->left / (float)tex->getWidth();


sprite.TexCoord.y = rect->top / (float)tex->getHeight();

spriteWidth = (rect->right - rect->left + 1);


spriteHeight = (rect->bottom - rect->top + 1);

sprite.TexSize.x = spriteWidth / (float)tex->getWidth();


sprite.TexSize.y = spriteHeight / (float)tex->getHeight();
Transparent sprites
Pixel format

R G B A R G B A R G B A

Each channel will be mapped to 0.0f – 1.0f instead of integer value

0-255 – 8bits

Alpha : how transparent that pixel should be.


1.0f = completely opaque
0.0f = completely transparent

NOTE: Usually, each color channel use 8-bits but it could be different in
other image format

Most popular format: R8G8B8A8


Transparent sprites
Color blending
Final color = src_color * src_factor OP dest_color * dest_factor

src_color : the color to be draw (r,g,b)


src_factor : the “adjustment” applied to the source color
dest_color: the color already exists at the target
dest_factor : the “adjustment” applied to the destination color
OP : the operation (like + , - , * etc)

ALPHA BLENDING

F = src_color * src_alpha + dest_color * (1 - src_alpha)

For example: src_alpha = 0.7

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

int InitKeyboard(HINSTANCE hInstance, HWND hWnd) {


HRESULT result;
result = DirectInput8Create(hInstance, DIRECTINPUT_VERSION,
IID_IDirectInput8, (void**)&dinput, NULL);
if (result!=DI_OK)
return 0;

result = dinput->CreateDevice(GUID_SysKeyboard, &didev, NULL);


...

result = didev->SetDataFormat(&c_dfDIKeyboard);

result = didev->SetCooperativeLevel(hWnd, DISCL_FOREGROUND |


DISCL_NONEXCLUSIVE);

result = didev->Acquire();

return 1;
}
Keyboard

#define KEYDOWN(name, key) (name[key] & 0x80)


// 0x80 = 1000 0000

...

char keys[256];
didev->GetDeviceState(sizeof(keys),&keys);

if (KEYDOWN(keys, DIK_ESCAPE))
PostMessage(hWnd,WM_DESTROY,0,0);

if (KEYDOWN(keys, DIK_HOME))
...

Where to find all the keyboard constants?

DirectX Input/Direct Input/Reference/Device Constants


Keyboard
didev->GetDeviceState(sizeof(keys),&keys);

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

// Get all events (also clear them from DirectInput buffer)


DWORD dwElements = KEYBOARD_BUFFER_SIZE;
HRESULT hr = _Keyboard-
>GetDeviceData( sizeof(DIDEVICEOBJECTDATA), _KeyEvents,
&dwElements, 0 );

// Scan through all data, check if the key is pressed or


released
for( DWORD i = 0; i < dwElements; i++ )
{
int KeyCode = _KeyEvents[i].dwOfs;
int KeyEvent = _KeyEvents[i].dwData;
if ( (KeyEvent & 0x80) > 0)
//Process key DOWN event
else
//Process key UP event
}
Keyboard
GetDeviceData

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
};

How do I know the CURRENT cursor position?


vy

You might also like