This is the d3d getting started learning,
Effect:
# Include <d3d8. h>
Lpdirect3d8 g_pd3d = NULL; // used to create a d3d Device
Lpdirect3ddevice8 g_pd3ddevice = NULL; // d3d Device
// Initialize d3d
Hresult initd3d (hwnd)
{
// Create a d3d object
If (null = (g_pd3d = direct3dcreate8 (d3d_sdk_version )))
Return e_fail;
// Obtain the current display mode
D3ddisplaymode d3ddm;
If (failed (g_pd3d-> getadapterdisplaymode (d3dadapter_default, & d3ddm )))
Return e_fail;
D3dpresent_parameters d3dpp;
Zeromemory (& d3dpp, sizeof (d3dpp ));
D3dpp. cmdwed = true; // window mode
D3dpp. swapeffect = d3dswapeffect_discard; // you can specify the switch mode.
D3dpp. backbufferformat = d3ddm. format; // set the background buffer format to the current left-side format.
// Create a d3d Device
// The first parameter: use the default graphics Adapter
// The second parameter: The hardware abstraction layer (HAL) is requested)
// The third parameter is the window handle.
// Fourth parameter: use software to process vertices
// Fifth parameter: The created Parameter
// The sixth parameter is the d3d device pointer created.
If (failed (g_pd3d-> createdevice (d3dadapter_default,
D3ddevtype_hal,
Hwnd,
D3dcreate_software_vertexprocessing,
& D3dpp,
& G_pd3ddevice )))
{
Return e_fail;
}
Return s_ OK;
}
// Release d3d
Void cleanup ()
{
If (g_pd3ddevice! = NULL)
G_pd3ddevice-> release ();
If (g_pd3d! = NULL)
G_pd3d-> release ();
}
// Rendering
Void render ()
{
If (null = g_pd3ddevice)
Return;
// Clear the blue background
G_pd3ddevice-> clear (0, null, d3dclear_target, d3dcolor_xrgb (0, 0, 255), 1.0f, 0 );
// Start to draw the scenario
G_pd3ddevice-> beginscene ();
// Draw what you need here
// Draw the end scenario
G_pd3ddevice-> endscene ();
// Display to the screen
G_pd3ddevice-> present (null, null );
}
// Message Processing
Lresult winapi msgproc (hwnd, uint MSG, wparam, lparam)
{
Switch (MSG)
{
Case wm_destroy: // Exit message
Postquitmessage (0 );
Return 0;
Case wm_paint: // draw
Render ();
Validaterect (hwnd, null );
Return 0;
}
Return defwindowproc (hwnd, MSG, wparam, lparam );
}
// Winmain program entry
Int winapi winmain (hinstance hinst, hinstance, lpstr, INT)
{
// Register the window class
Wndclassex WC = {sizeof (wndclassex), cs_classdc, msgproc, 0l, 0l,
Getmodulehandle (null), null,
"D3d tutorial", null };
Registerclassex (& WC );
// Create a window
Hwnd = createwindow ("d3d tutorial", "d3d tutorial 01: createdevice ",
Ws_overlappedwindow, 100,100,300,300,
Getasktopwindow (), null, WC. hinstance, null );
// Initialize d3d
If (succeeded (initd3d (hwnd )))
{
// Display window
Showwindow (hwnd, sw_showdefault );
Updatewindow (hwnd );
// Message loop
MSG;
While (getmessage (& MSG, null, 0, 0 ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
}
// End Processing
Cleanup ();
Unregisterclass ("d3d tutorial", WC. hinstance );
Return 0;
}