Title: DirectX I
1DirectX I
2Direct3D Interface IDirect3D9
- Header d3d9.h
- Import library d3d9.lib
- IDirect3D9
- Applications use the methods of the IDirect3D9
interface to create Microsoft Direct3D objects
and set up the environment. This interface
includes methods for enumerating and retrieving
capabilities of the device - The IDirect3D9 interface is obtained by calling
the Direct3DCreate9 function - LPDIRECT3D9 is long pointer for IDirect3D9
- typedef struct IDirect3D9 LPDIRECT3D9
- Declare a variable LPDIRECT3D9 d3d NULL
3Direct3D Interface IDirect3DDevice9
- IDirect3DDevice9
- Applications use the methods of the
IDirect3DDevice9 interface to perform
DrawPrimitive-based rendering, create resources,
work with system-level variables, adjust gamma
ramp levels, work with palettes, and create
shaders. - The IDirect3DDevice9 interface is obtained by
calling the IDirect3D9CreateDevice method - LPDirect3DDevice9 is long pointer for
IDirect3DDevice9 - typedef struct IDirect3DDevice9
LPDIRECT3DDEVICE9 - Declare a variable LPDIRECT3DDEVICE9 d3ddev
NULL
4Create the Direct3D Object
- Initialize the main Direct3D object
- D3d Direct3DCreate9(D3D_SDK_VERSION)
- D3D_SDK_VERSION ensure that the header file
version match the runtime DLL version - Create the device upon which Direct3D will
display output - d3d-gtCreateDevice(
- D3DADAPTER_DEFAULT, //use default video
card - D3DDEVTYPE_HAL, // use the
hardware renderer - hwnd, // window handle
- D3DCREATE_SOFTWARE_VERTEXPROCESSING,
- // do not use TL (for
compatibility) - d3dpp, // presentation
parameters - d3ddev) // pointer to the
new device - TL transform and lighting, is handled by video
card itself (since 2002), offload that work from
the CPU
5Set Direct3D presentation parameters
- D3DPRESENT_PARAMETERS d3dpp
- clear out the struct to zero all values before
use ZeroMemory(d3dpp, sizeof(d3dpp)) - Fill in the d3dpp struct with a few values, for
example - d3dpp.Windowed TRUE
- d3dpp.SwapEffect D3DSWAPEFFECT_DISCARD
- d3dpp.BackBufferFormat D3DFMT_UNKNOWN
6Draw on the Direct3D display
- Clears one or more surfaces such as a render
target before each rendering frame
IDirect3DDevice9Clear - Applications must call IDirect3DDevice9BeginScen
e before performing any rendering and must call
IDirect3DDevice9EndScene when rendering is
complete - Presents the contents of the next buffer in the
sequence of back buffers owned by the device
IDirect3DDevice9Present
7Release the Direct3D Object and other things from
memory
- release the Direct3D device
- d3ddev-gtRelease()
- release the Direct3D object
- d3d-gtRelease()
- If not released, Direct3D will keep running in
the memory
8Direct3D in Fullscreen Mode
- hWnd CreateWindow(
- APPTITLE, //window class
- APPTITLE, //title bar
- WS_EX_TOPMOST WS_VISIBLE WS_POPUP,
//window style - CW_USEDEFAULT, //x position of
window - CW_USEDEFAULT, //y position of
window - SCREEN_WIDTH, //width of the
window - SCREEN_HEIGHT, //height of the
window - NULL, //parent window
- NULL, //menu
- hInstance, //application
instance - NULL) //window parameters
- WS_EX_TOPMOST cause the window to take
precedence over all other windows - WS_VISIBLE WS_POPUP ensure that the window has
focus and no longer includes a border or title
bar - main changing the presentation parameters
- d3dpp.Windowed FALSE
- d3dpp.BackBufferFormat D3DFMT_X8R8G8B8
9Surface
- Surfaces are an integral part of DirectX
- Surfaces are areas within memory (residing on the
video card) that are used for the storage of
image information - Surfaces store images and textures and are used
to represent the display buffers - We will use two specific types of surfaces
- Display buffers
- Front buffer is the surface that represents the
viewable area of the game window - Back buffer is where all the drawing is done and
is used to eliminate flickering - Offscreen surfaces
- Areas of video or system memory that hold the
graphics that the game needs
10IDirect3DSurface9
- Applications use the methods of the
IDirect3DSurface9 interface to query and prepare
surfaces - typedef struct IDirect3DSurface9
LPDIRECT3DSURFACE9 - Create a surface
- LPDIRECT3DSURFACE9 surface NULL
11IDirect3DDevice9GetBackBuffer Method
- Retrieves a back buffer from the device's swap
chain - HRESULT GetBackBuffer( UINT iSwapChain,
- UINT BackBuffer,
- D3DBACKBUFFER_TYPE Type,
- IDirect3DSurface9 ppBackBuffer )
- Parameters
- iSwapChain in An unsigned integer specifying
the swap chain. - BackBuffer in Index of the back buffer object
to return. Back buffers are numbered from 0 to
the total number of back buffers minus one. A
value of 0 returns the first back buffer, not the
front buffer. The front buffer is not accessible
through this method. Use IDirect3DDevice9GetFron
tBufferData to retrieve a copy of the front
buffer. - Type in Stereo view is not supported in
Microsoft DirectX 9.0, so the only valid value
for this parameter is D3DBACKBUFFER_TYPE_MONO. - ppBackBuffer out, retval Address of a pointer
to an IDirect3DSurface9 interface, representing
the returned back buffer surface
12IDirect3DDevice9CreateOffscreenPlainSurface
- Create an off-screen surface
- HRESULT CreateOffscreenPlainSurface(
- UINT Width,
- UINT Height,
- D3DFORMAT Format,
- DWORD Pool,
- IDirect3DSurface9 ppSurface,
- HANDLE pSharedHandle )
- Parameters
- Width in Width of the surface
- Height in Height of the surface
- Format in Format of the surface
- Pool in Surface pool type
- ppSurface out, retval Pointer to the
IDirect3DSurface9 interface created - pSharedHandle in Reserved. Set this parameter
to NULL
13IDirect3DDevice9StretchRect
- Copy the contents of the source rectangle to the
destination rectangle. The source rectangle can
be stretched and filtered by the copy. This
function is often used to change the aspect ratio
of a video stream - HRESULT StretchRect(
- IDirect3DSurface9 pSourceSurface,
- CONST RECT pSourceRect,
- IDirect3DSurface9 pDestSurface,
- CONST RECT pDestRect,
- D3DTEXTUREFILTERTYPE Filter )
- Parameters
- pSourceSurface in Pointer to the source
surface. - pSourceRect in Pointer to the source
rectangle. A NULL for this parameter causes the
entire source surface to be used. - pDestSurface in Pointer to the destination
surface. - pDestRect in Pointer to the destination
rectangle. A NULL for this parameter causes the
entire destination surface to be used. - Filter in Filter type. Allowable values are
D3DTEXF_NONE, D3DTEXF_POINT, or D3DTEXF_LINEAR.
14D3DXLoadSurfaceFromFile Function
- Loads a surface from a file
- HRESULT WINAPI D3DXLoadSurfaceFromFile(
LPDIRECT3DSURFACE9 pDestSurface, - CONST PALETTEENTRY pDestPalette,
- CONST RECT pDestRect,
- LPCTSTR pSrcFile,
- CONST RECT pSrcRect,
- DWORD Filter,
- D3DCOLOR ColorKey,
- D3DXIMAGE_INFO pSrcInfo )
- Header d3dx9tex.h or d3dx9.h
- Import library d3dx9.lib
- D3DX stands for Direct3D extensions
15D3DXLoadSurfaceFromFile Function cont.
- Parameters
- pDestSurface in Pointer to an
IDirect3DSurface9 interface. Specifies the
destination surface, which receives the image. - pDestPalette in Pointer to a PALETTEENTRY
structure, the destination palette of 256 colors
or NULL. - pDestRect in Pointer to a RECT structure.
Specifies the destination rectangle. Set this
parameter to NULL to specify the entire surface. - pSrcFile in Pointer to a string that specifies
the filename. If the compiler settings require
Unicode, the data type LPCTSTR resolves to
LPCWSTR. Otherwise, the string data type resolves
to LPCSTR. - pSrcRect in Pointer to a RECT structure.
Specifies the source rectangle. Set this
parameter to NULL to specify the entire image. - Filter in Combination of one or more
D3DX_FILTER controlling how the image is
filtered. Specifying D3DX_DEFAULT for this
parameter is the equivalent of specifying
D3DX_FILTER_TRIANGLE D3DX_FILTER_DITHER. - ColorKey in D3DCOLOR value to replace with
transparent black, or 0 to disable the colorkey.
This is always a 32-bit ARGB color, independent
of the source image format. Alpha is significant
and should usually be set to FF for opaque color
keys Thus, for opaque black, the value would be
equal to 0xFF000000. - pSrcInfo in, out Pointer to a D3DXIMAGE_INFO
structure to be filled with a description of the
data in the source image file, or NULL.
16load surface from file into newly created surface
example
- result D3DXLoadSurfaceFromFile(
- surface, //destination
surface - NULL, //destination
palette - NULL, //destination
rectangle - "legotron.bmp", //source filename
- NULL, //source
rectangle - D3DX_DEFAULT, //controls how image is
filtered - 0, //for
transparency (0 for none) - NULL) //source image info
(usually NULL)