Initialize a directx form instance

Source: Internet
Author: User

# Include "windows. h"
# Include "windowsx. h"
# Include "d3d9. h"
# Include "d3dx9. h"
# Include "tchar. h"

HWND g_hwnd;
LPDIRECT3D9 g_d3d9 = NULL;
LPDIRECT3DDEVICE9 g_d3d9device = NULL;

LRESULT initgame (void );
LRESULT Render (void );

Lresult callback wndproc (HWND hwnd, UINT message, WPARAM wpara, LPARAM lpara)
{
Switch (message)
{
Case WM_DESTROY:
PostQuitMessage (0 );
Break;
}
Return DefWindowProc (hwnd, message, wpara, lpara );
}

Int WINAPI WinMain (_ in HINSTANCE instance, _ in_opt HINSTANCE hPrevInstance, _ in_opt LPSTR lpCmdLine, _ in int nShowCmd)
{
WNDCLASSEX wndclass;
ZeroMemory (& wndclass, sizeof (wndclass ));
Wndclass. cbSize = sizeof (wndclass );
Wndclass. lpszClassName = _ T ("game ");
Wndclass. lpfnWndProc = wndproc;
Wndclass. hInstance = instance;
Wndclass. style = CS_HREDRAW | CS_VREDRAW;
Wndclass. hCursor = LoadCursor (instance, IDC_ARROW );
Wndclass. hbrBackground = GetStockBrush (RGB (0, 0, 0 ));
 
RegisterClassEx (& wndclass );
G_hwnd = CreateWindow (_ T ("game"), _ T ("Directx example"), WS_OVERLAPPEDWINDOW, 100,100,400,600, NULL, NULL, instance, NULL );
ShowWindow (g_hwnd, SW_SHOW );
UpdateWindow (g_hwnd );
MSG msg;
Initgame ();
While (true)
{
If (PeekMessage (& msg, NULL, 0, 0, PM_REMOVE ))
{
If (msg. message = WM_QUIT)
Break;
TranslateMessage (& msg );
DispatchMessage (& msg );
}
Render ();
}

Return 0;

}

LRESULT Render ()
{
G_d3d9device-> Clear (0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB (255,0, 0), 1.0f, 0 );
G_d3d9device-> Present (NULL, NULL );

Return S_ OK;
}
LRESULT initgame ()
{
D3DCAPS9 d3dcaps;
ZeroMemory (& d3dcaps, sizeof (d3dcaps ));
D3DPRESENT_PARAMETERS pa;
ZeroMemory (& pa, sizeof (pa ));
Long v;

G_d3d9 = Direct3DCreate9 (D3D_SDK_VERSION );
G_d3d9-> GetDeviceCaps (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, & d3dcaps );

If (d3dcaps. DevCaps & d3ddevcaps_hwtransformanlight) // whether the hardware supports illumination (hardware vertex processing)
{
V = D3DCREATE_HARDWARE_VERTEXPROCESSING;
}
Else
{
V = D3DCREATE_SOFTWARE_VERTEXPROCESSING;
}
 
Pa. customized wed = true; // whether it is customized
Pa. enableautodepthstenpencil = true; // automatically create depth/template Buffer
Pa. AutoDepthStencilFormat = D3DFMT_D16; // depth/degree format
Pa. BackBufferCount = 1; // Number of backend buffer Surfaces
Pa. BackBufferFormat = D3DFMT_A8R8G8B8; // backup buffer surface pixel format
Pa. SwapEffect = D3DSWAPEFFECT_DISCARD; // specifies the rules of the surface in the switching chain.
Pa. FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; // update rate
Pa. MultiSampleType = D3DMULTISAMPLE_NONE; // full-screen anti-aliasing type
Pa. MultiSampleQuality = 0; // quality grade of full screen anti-aliasing

If FAILED (g_d3d9-> CreateDevice (D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, g_hwnd, v, & pa, & g_d3d9device ))
{
Return E_FAIL;
}

 
 

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.