You are on page 1of 20

DirectX

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

The keyboard is the standard device for all games.


The primary DirectInput object is called IDirectInput8; you
can reference it directly or using the LPDIRECTINPUT8
pointer data type.
The DirectInput library file is called dinput8.lib

Game Programming
DirectInput Object and Device

Define the variables:

LPDIRECTINPUT8 dinput;
LPDIRECTINPUTDEVICE8 dinputdev;

Initialize Directinput:

HRESULT WINAPI DirectInput8Create (


HINSTANCE hinst,
DWORD dwVersion,
REFID riidltf,
LPVOID *ppvout,
LPUNKNOWN punkouter
);

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:

HRESULT result = DirectInput8Create (


GetModuleHandle (NULL),
DIRECTINPUT_VERSION,
IID_IDirectInput8,
(void**) &dinput,
NULL);

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

The second parameter is your device pointer that receives


the address of the DirectInput device handler.
The third parameter is always NULL. Here is how you might
call this function:

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

Game Programming
Initializing the Keyboard

Setting the Data Format


The SetDataFormat specifies the data format is set:

HRESULT SetDataFormat (
LPCIDDATAFORMAT lpdf
);

The single parameter to this function specifies the device


type.
For the keyboard you want to pass the value
c_dfDIKeyboard and for the mouse would be c_dfDIMouse.
Here is a sample function call:

HRESULT result = dikeyboard -> SetDataFormat (&c_dfDIKeyboard);

Game Programming
Setting the Cooperative Level

This determines how much of the keyboard DirectInput will


give your program the way of priority.
To set the cooperative level you can call
SetCooperativeLevel function:

HRESULT SetCooperativeLevel (
HWND hWnd,
DWORD dwFlags
);

The first parameter is the window handle.


The second parameter specifies the priority that your
program will have over the keyboard.
The most common value to pass are
DISCL_NONEXCLUSIVE, and DISCL_FOREGROUND.
Game Programming
Heres how you might call the function:

HRESULT result = dikeyboard -> SetCooperativeLevel


(hWnd, DISCL_NONEXCLUSIVE |
DISCL_FOREGROUND);

Acquiring the Device

HRESULT Acquire(VOID);

If the function returns a positive value (DI_OK) then you


have successfully acquire the keyboard.
you must unacquire the keyboard before your game ends.

HRESULT Unacquire(VOID);

Game Programming
Reading Key Presses

Call the GetDeviceState function to poll the keyboard. This


function is used for all devices regardless of type, it is
standard for all input devices:

HRESULT GetDeviceState (
DWORD cbData,
LPVOID lpvData
);

The first parameter is the size of the device state buffer to


be filled with data
The second parameter is a pointer to the data. In the case
of the keyboard, here is how you would call this function:

dikeyboard -> GetDeviceState (sizeof(keys), (LPVOID) &keys);


Game Programming
Here is how you would check for the ESCAPE key:

if (keys [DIK_ESCAPE] & 0x80)


{
//ESCAPE key was pressed, do something
}

Game Programming
The Mouse

First define the mouse device

LPDIRECTINPUTDEVICE8 dimouse;

Next create the mouse device:

result = dinput -> CreateDevice (GUID_SysMouse,


&dimouse, NULL);

Game Programming
Initializing the Mouse

Setting the Data Format

HRESULT SetDataFormat (
LPCDIDATFORMAT lpdf
);

The single parameter to this function specifies the device


type. The constant for mouse is c_dfDIMouse.
To call the function:

HRESULT result = dimouse -> SetDataFormat (&c_dfDIMouse);

Game Programming
Setting the Cooperative Level

call the SetCooperativeLevel function:


HRESULT SetCooperativeLevel (
HWND hWnd,
DWORD dwFlags
);

The first parameter is the window handle.


The second parameter specifies the priority that your
program will have over the mouse.
The most common value to pass when working on with
mouse are DISCL_EXCLUSIVE, and
DISCL_FOREGROUND. To call this function:
HRESULT result = dimouse -> SetCooperativeLevel (hWnd,
DISCL_EXCLUSIVE | DISCL_FOREGROUND);

Game Programming
Acquiring the Device

Use the Acquire function. If it return DI_OK, then you have


successfully acquired the mouse.
As with the keyboard device you must also unacquire the
mouse device after you are done using it.

HRESULT Unacquire (VOID);

Game Programming
Reading the Mouse

You need to poll the mouse in order to update the mouse


position and button status. Use the GetDeviceState function.

HRESULT GetDeviceState (
DWORD cbData,
LPVOID lpvData
);

The first parameter is the size of the device state buffer to


be filled with data.
The second parameter is the pointer to the data.
The struct available to poll the mouse:

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

The struct look like this:

typedef struct DIMOUSESTATE (


LONG lX;
LONG lY;
LONG lZ;
BYTE rgbButtons[4];
)DIMOUSESTATE;

Alternate struct to support complex mouse with more than four buttons:

typedef struct DIMOUSESTATE2 (


LONG lX;
LONG lY;
LONG lZ;
BYTE rgbButtons[8];
)DIMOUSESTATE2;

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:

You learned how to create a virtual scroll buffer.


You learned how to use Mappy to create a tile map.
You learned how to dynamically draw tiles onto the screen.

Game Programming

You might also like