Vous êtes sur la page 1sur 13

1.

Going Fullscreen

Throughout your DirectX experience and in game programming you will come across many functions
and structs that demand to know your screen size. This can become a hassle when you decide to
change the resolution later, and especially when you decide to change it during run-time. For right now,
we will cover a simple method to standardize your screen size across your program.

First, we must add two directives to the top of our program. These represent the screen width and the
screen height.

// define the screen resolution


#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

The next step is to go through your program to where you indicate the width and height of your
window. Up to this point in the tutorial, you only have one, although we will come across another in a
minute. Do this to the code (changes in bold):

hWnd = CreateWindowEx(NULL,
L"WindowClass",
L"Our Direct3D Program",
WS_OVERLAPPEDWINDOW,
300, 300,
SCREEN_WIDTH, SCREEN_HEIGHT, // set window to new resolution
NULL,
NULL,
hInstance,
NULL);
In a later lesson we will cover how to maintain screen size throughout your game after changing it
during runtime.

There are specific resolutions that are available on most PCs, the most common of which can be seen in
this table.

Changing to Fullscreen Mode

When changing to full screen you are doing several things. First, your are telling Windows not to apply
any of the standard Windows borders to your window. Second, you are telling Windows to have your
window overlap all other things on the screen, including the start menu. Third, you are telling DirectX to
change the resolution of the monitor to your set preference. Finally, although less importantly, you are
telling Windows to leave the window background color up to you.

The first two of these are handled by changing some CreateWindowEx() parameters. The changes we
need to make are shown here.

hWnd = CreateWindowEx(NULL,
L"WindowClass",
L"Our Direct3D Program",
WS_EX_TOPMOST | WS_POPUP, // fullscreen values
0, 0, // the starting x and y positions should be 0
SCREEN_WIDTH, SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);

Here we set the starting x and y positions to 0. We also changed the previous parameter to
"WS_EX_TOPMOST | WS_POPUP". The WS_EX_TOPMOST is self-explanatory, and makes the window
overlap everything else. The WS_POPUP is less self-explanatory, but what it does is tell Windows to
remove all borders of any kind, including the rounded-edge top that you see in Windows XP.

There is also a member of the WINDOWCLASSEX struct that we need to take out. This leaves the
background color untouched, which means it won't be visible as window for a second or two before the
game starts (important to making your game look professional).

// wc.hbrBackground = (HBRUSH)COLOR_WINDOW;

Next, we have to tell DirectX about our new screen resolution. We do this by making a few changes to
the d3dpp struct we built in the last lesson. Let's look at what they are before we see what they do.

D3DPRESENT_PARAMETERS d3dpp; // create a struct to hold various device information

ZeroMemory(&d3dpp, sizeof(d3dpp)); // clear out the struct for use

d3dpp.Windowed = FALSE; // program fullscreen, not windowed

d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; // discard old frames

d3dpp.hDeviceWindow = hWnd; // set the window to be used by Direct3D

d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8; // set the back buffer format to 32-bit

d3dpp.BackBufferWidth = SCREEN_WIDTH; // set the width of the buffer

d3dpp.BackBufferHeight = SCREEN_HEIGHT; // set the height of the buffer

Let's examine these new back buffer related variables.

d3dpp.BackBufferFormat

This member is used to tell Direct3D what kind of pixels should be displayed. There are six types that
can be used here, but two of them are older types (16-bit) and not generally used anymore. There are
several 32-bit types that we can use. We'll use the D3DFMT_X8R8G8B8. See the table for a description
along with some other values than can be used here (definitely not all of them).

BackBufferWidth and BackBufferHeight

These two members indicate the width and height of the back buffer. Painfully simple.

2. Drawing Text:

To render text using the ID3DXFont interface


To render text using the CD3DFont class
To create and render 3D text using the D3DXCreateText function

ID3DXFont:

The D3DX library provides the ID3DXFont interface that can be used to draw text in a Direct3D
application.

HRESULT D3DXCreateFontIndirect(
LPDIRECT3DDEVICE9 pDevice,
CONST LOGFONT* pLogFont,
LPD3DXFONT* ppFont
);

LOGFONT Structure:

LOGFONT lf;
ZeroMemory(&lf, sizeof(LOGFONT));

lf.lfHeight = 25; // in logical units


lf.lfWidth = 12; // in logical units
lf.lfWeight = 500; // boldness, range 0(light) - 1000(bold)
lf.lfItalic = false;
lf.lfUnderline = false;
lf.lfStrikeOut = false;
lf.lfCharSet = DEFAULT_CHARSET;
strcpy(lf.lfFaceName, "Times New Roman"); // font style

ID3DXFont* font = 0;
D3DXCreateFontIndirect(d3ddev, &lf, &font);

Drawing Text:

Once we have obtained a pointer to an ID3DXFont interface, drawing text is a simple matter of calling
the ID3DXFont::DrawText method

INT ID3DXFont::DrawText(
LPCSTR pString,
INT Count,
LPRECT pRect,
DWORD Format,
D3DCOLOR Color
);
Font->DrawText(
"Hello World", // String to draw.
-1, // Null terminating string.
&rect, // Rectangle to draw the string in.
DT_TOP | DT_LEFT, // Draw in top-left corner of rect.
0xff000000); // Black.

CD3DFont:

Renders text using textured triangles and Direct3D.


To use the CD3DFont class, you need to add the following files to your application:
d3dfont.h,
d3dfont.cpp,
d3dutil.h,
d3dutil.cpp,
dxutil.h, and
dxutil.cpp.

Constructing a cD3DFont:

CD3DFont(const TCHAR* strFontName, DWORD dwHeight, DWORD dwFlags);

Font = new CD3DFont("Times New Roman", 16, 0); // instantiate


Font->InitDeviceObjects( Device );
Font->RestoreDeviceObjects();

Drawing Text:

HRESULT CD3DFont::DrawText(FLOAT x, FLOAT y, DWORD dwColor, const TCHAR* strText, DWORD


dwFlags);

Font->DrawText(20, 20, 0xff000000, “Hello, World”,0);

3.Drawing a Surface:

Surfaces:

Surfaces are areas within memory that are used for the storage of image information.
They store images and textures and are used to represent the display buffers.
Surfaces are stored internally as a contiguous block of memory, usually residing on the video card, but
occasionally in main system memory.
Types of surfaces - Display buffers

- There are two display buffers that you have to worry about: the front buffer and the back buffer.
- These are the areas of video memory where your game is drawn.
- The front buffer is the surface that represents the viewable area of your game window.
- The second buffer is the back buffer. The back buffer is where you perform all the drawing.
- After the drawing to the back buffer is complete, you use the Present function to display its contents.

Types of surfaces - Offscreen surface

- Offscreen surfaces are areas of video or system memory that hold the graphics that your game needs.
- For instance, if you’re creating an overhead role-playing game, you need an area to store the tiles that
represent the different terrain, as well as the graphics for your characters.
- An offscreen surface would be a perfect choice for this task.

CreateOffscreenPlainSurface:

Offscreen surfaces, represented by the IDirect3DSurface9 interface, are created using the function
CreateOffscreenPlainSurface.

HRESULT CreateOffscreenPlainSurface(
UINT Width,
UINT Height,
D3DFORMAT Format,
DWORD Pool,
IDirect3DSurface9** ppSurface,
HANDLE* pHandle
);

Implementation

hResult = CreateOffscreenPlainSurface(
640,
480,
D3DFMT_X8R8G8B8,
D3DPOOL_DEFAULT,
&surface,
NULL);

Loading a Bitmap to a Surface:

The function D3DXLoadSurfaceFromFile performs the loading of a source bitmap into an offscreen
surface.
HRESULT D3DXLoadSurfaceFromFile(
LPDIRECT3DSURFACE9 pDestSurface,
CONST PALETTEENTRY* pDestPalette,
CONST RECT* pDestRect,
LPCTSTR pSrcFile,
CONST RECT* pSrcRect,
DWORD Filter,
D3DCOLOR ColorKey,
D3DXIMAGE_INFO* pSrcInfo
);

Implementation:

IDirect3DSurface9* surface;
hResult = D3DXLoadSurfaceFromFile( surface,
NULL,
NULL,
“test.bmp”,
NULL,
D3DX_DEFAULT,
0,
NULL );

Display the image

To get the bitmap shown on the screen, you’ll need to use the StretchRect function.
StretchRect performs rectangular copies between two surfaces.
HRESULT StretchRect(
IDirect3DSurface9 *pSourceSurface,
CONST RECT *pSourceRect,
IDirect3DSurface9 *pDestSurface,
CONST RECT *pDestRect,
D3DTEXTUREFILTERTYPE Filter );

Getting pointer to the back buffer surface

GetBackBuffer( 0,0,D3DBACKBUFFER_TYPE_MONO,&backbuffer);

void Render(void)
{
IDirect3DSurface9* backbuffer = NULL;
d3ddev->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
d3ddev->BeginScene();
d3ddev- >GetBackBuffer( 0, 0, D3DBACKBUFFER_TYPE_MONO, &backbuffer );
d3ddev- >StretchRect( srcSurface, NULL, backbuffer,NULL, D3DTEXF_NONE );
d3ddev->EndScene();
d3ddev- >Present ( NULL, NULL, NULL, NULL );
}
4.To Draw a Rectangle:

#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>

#define SCREEN_WIDTH 300


#define SCREEN_HEIGHT 300

#pragma comment (lib, "d3d9.lib")


LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer = NULL;

void initD3D(HWND hWnd);


void render_frame(void);
void cleanD3D(void);
void init_graphics(void);

struct CUSTOMVERTEX {FLOAT X, Y, Z, RHW; DWORD COLOR;};


#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,


HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
HWND hWnd;
WNDCLASSEX wc;

ZeroMemory(&wc, sizeof(WNDCLASSEX));

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.lpszClassName = "WindowClass";

RegisterClassEx(&wc);

hWnd = CreateWindowEx(NULL,
"WindowClass",
"Our Direct3D Program",
WS_OVERLAPPEDWINDOW,
0, 0,
SCREEN_WIDTH, SCREEN_HEIGHT,
NULL,
NULL,
hInstance,
NULL);

ShowWindow(hWnd, nCmdShow);
// set up and initialize Direct3D
initD3D(hWnd);

// enter the main loop:

MSG msg;

while(TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if(msg.message == WM_QUIT)
break;

render_frame();
}

// clean up DirectX and COM


cleanD3D();

return msg.wParam;
}

// this is the main message handler for the program


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}

return DefWindowProc (hWnd, message, wParam, lParam);


}

// this function initializes and prepares Direct3D for use


void initD3D(HWND hWnd)
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);

D3DPRESENT_PARAMETERS d3dpp;

ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hWnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
// create a device class using this information and the info from the d3dpp stuct
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&d3ddev);

init_graphics(); // call the function to initialize the triangle


}

// this is the function used to render a single frame


void render_frame(void)
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);

d3ddev->BeginScene();

// select which vertex format we are using


d3ddev->SetFVF(CUSTOMFVF);

// select the vertex buffer to display


d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(CUSTOMVERTEX));

// copy the vertex buffer to the back buffer


d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST, 3, 1);

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);


}

// this is the function that cleans up Direct3D and COM


void cleanD3D(void)
{
v_buffer->Release(); // close and release the vertex buffer
d3ddev->Release(); // close and release the 3D device
d3d->Release(); // close and release Direct3D
}

// this is the function that puts the 3D models into video RAM
void init_graphics(void)
{
// create the vertices using the CUSTOMVERTEX struct
CUSTOMVERTEX vertices[] =
{
{ 100.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
{ 200.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 200.0f, 200.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 200.0f, 200.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(255, 0, 0), },
{ 100.0f, 200.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 255, 0), },
{ 100.0f, 100.0f, 0.5f, 1.0f, D3DCOLOR_XRGB(0, 0, 255), },
};
// create a vertex buffer interface called v_buffer
d3ddev->CreateVertexBuffer(6*sizeof(CUSTOMVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);

VOID* pVoid; // a void pointer

// lock v_buffer and load the vertices into it


v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertices, sizeof(vertices));
v_buffer->Unlock();
}

/*OUTPUT

5. To Capture Key board and mouse events (Already done)

6. Adding a Texture:
Texturing:

· In Direct3D a texture is represented with the IDirect3DTexture9 interface.


· A texture is a matrix of pixels similar to a surface but can be mapped to triangles.
· Direct3D uses a texture coordinate system that consists of a u-axis that runs horizontally and a
v-axis that runs vertically.
· A pair of u, v coordinates identifies an element on the texture called a texel.
· For each 3D triangle, we want to define a corresponding triangle on the texture that is to be
mapped to the 3D triangle

Texture coordinates:

• To do this, we have to modify our vertex structure by adding a pair of texture coordinates that
identifies a vertex on the texture.

struct Vertex
{
float _x, _y, _z;
float _nx, _ny, _nz;
float _u, _v; // texture coordinates
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;

Creating and Enabling a Texture:

Texture data is usually read from an image file stored on disk and loaded into an IDirect3DTexture9
object.
To do this, we can use the following D3DX function:
HRESULT D3DXCreateTextureFromFile(
LPDIRECT3DDEVICE9 pDevice,
LPCSTR pSrcFile,
LPDIRECT3DTEXTURE9* ppTexture
);

Set the texture:

To set the current texture, we use the following method:


HRESULT IDirect3DDevice9::SetTexture(
DWORD Stage,
IDirect3DBaseTexture9* pTexture
);

Implementation

IDirect3Dtexture9* texstonewall;
D3DXCreateTextureFromFile(d3ddev, "stonewall.bmp", &texstonewall);

Device->SetTexture(0, texstonewall);

Filters:

· Texture filters are set with the IDirect3DDevice9::SetSamplerState method.


· HRESULT SetSamplerState(DWORD Sampler, D3DSAMPLERSTATETYPE Type, DWORD Value );
· Direct3D provides three different types of filters; each one provides a different level of quality.

Nearest point sampling

This is the default filtering method and produces the worst-looking results, but it is also the fastest to
compute.
The following code sets nearest point sampling as the minification and magnification filter:
Device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_POINT);
Device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_POINT);

Linear filtering

D3DTEXF_LINEAR

Anisotropic filtering

D3DTEXF_ANISOTROPIC

Mipmap Filter:

The mipmap filter is used to control how Direct3D uses the mipmaps.
Device->SetSamplerState(0, D3DSAMP_MIPFILTER, Filter);
where Filter is one of the following three options:
D3DTEXF_NONE
D3DTEXF_POINT
D3DTEXF_LINEAR

· D3DTEXF_NONE—Disables mipmapping

· D3DTEXF_POINT—By using this filter, Direct3D will choose the mipmap level that is closest in
size to the screen triangle. Once that level is chosen, Direct3D will filter that level based on the
specified min and mag filters.
· D3DTEXF_LINEAR—By using this filter, Direct3D will take the two closest mipmap levels,filter
each level with the min and mag filters, and linearly combine these two levels to form the final
color values.

Transparent Textures

· To use transparent textures in Direct3D we create a texture in a format that supports alpha and
load the texture.
· You then need to turn on transparency by enabling alpha blending before rendering.
· You should then get transparent textures.
· SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);

Adding a Texture - Summary

· Construct the vertices of the objects with the texture coordinates specified.
· Load a texture into an IDirect3DTexture9 interface using the D3DXCreateTextureFromFile
function.
· Set the minification, magnification, and mipmap filters.
· Before you draw an object, set the texture that is associated with the object with
IDirect3DDevice9::SetTexture

Vous aimerez peut-être aussi