Professional Documents
Culture Documents
Lesson 14
Tiled-Based
Scrolling
Backgrounds
Game Programming
Here is the breakdown of the major topics in this chapter:
Introduction to scrolling.
Creating tile-based backgrounds.
Using a single large scroll buffer.
Using dynamically drawn tiles.
Game Programming
The Keyboard
Game Programming
DirectInput Object and Device
LPDIRECTINPUT8 dinput;
LPDIRECTINPUTDEVICE8 dinputdev;
Initialize Directinput:
Game Programming
The first parameter is the instance handler for the current
program.
The second parameter is the DirectInput version, which is
always passed as DIRECTINPUT_VERSION, defined in
dinput.h.
The third parameter is a reference identifier to the version
of DirectInput that you want to use. At present this value is
IID_IDirectInput8.
The fourth parameter is a pointer to the primary =
DirectInput object pointer.
The fifth parameter is always NULL.
Game Programming
To call this function:
After initializing the object, you can then use the object to
create a new DirectInput device by calling the CreateDevice
function:
HRESULT CreateDevice (
REFGUID rguid,
LPDIRECTINPUTDEVICE *lplpDirectInputDevice,
LPUNKNOWN pUnkOuter );
Game Programming
The first parameter is a value that specifies the type of
object you want to create (such as keyboard or mouse). Here
are the values you can use for this parameter:
GUID_SysKeyboard
GUID_SysMouse
Game Programming
Initializing the Keyboard
HRESULT SetDataFormat (
LPCIDDATAFORMAT lpdf
);
Game Programming
Setting the Cooperative Level
HRESULT SetCooperativeLevel (
HWND hWnd,
DWORD dwFlags
);
HRESULT Acquire(VOID);
HRESULT Unacquire(VOID);
Game Programming
Reading Key Presses
HRESULT GetDeviceState (
DWORD cbData,
LPVOID lpvData
);
Game Programming
The Mouse
LPDIRECTINPUTDEVICE8 dimouse;
Game Programming
Initializing the Mouse
HRESULT SetDataFormat (
LPCDIDATFORMAT lpdf
);
Game Programming
Setting the Cooperative Level
Game Programming
Acquiring the Device
Game Programming
Reading the Mouse
HRESULT GetDeviceState (
DWORD cbData,
LPVOID lpvData
);
DIMOUSESTATE mouse_state;
Game Programming
Here is how you would fill the DIMOUSESTATE struct by
calling the GetDeviceState function:
dimouse -> GetDeviceState (sizeof (mouse_state), (LPVOID) &mouse_state);
Alternate struct to support complex mouse with more than four buttons:
Game Programming
After polling the mouse, check the mouse_state struct for x and y
motion and button presses.
You can check the mouse movement, also called mickeys, using
lX and lY member variables.
Mickeys represent motion of the mouse rather than an absolute
position.
rgbButton arrays hold the result of button presses. If you want to
check for a specific button (starting with 0 for button 1) here is how
you might do that:
button_1 = obj.rgbButtons [0] & 0x80;
A more convenient method of detecting button presses is by using
a define:
#define BUTTON_DOWN (obj, button) (obj.rgbButtons[button] & 0x 80)
By using the define, you can check for button presses like so:
button_1 = BUTTON_DOWN (mouse_state, 0);
Game Programming
What You Have Learned
Here are the key points:
Game Programming