#include #include "windows.h" #include "d3d9.h" #define D3DFVF_MYVERT (D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_SPECULAR) typedef struct _MyVert { float x, y, z; DWORD sColor; DWORD dColor; } MyVert; static IDirect3D9 *pD3d; static IDirect3DDevice9 *pDevice; static IDirect3DVertexBuffer9 *pVertexBuffer; static BOOL Init( HWND hWnd ) { static const MyVert vertices[] = { {-0.5f, -0.5f, 0.5f, D3DCOLOR_XRGB(255,0,0), D3DCOLOR_XRGB(0,0,0)}, {-0.5f, 0.5f, 0.5f, D3DCOLOR_XRGB(0,255,0), D3DCOLOR_XRGB(0,0,0)}, { 0.5f, 0.5f, 0.5f, D3DCOLOR_XRGB(0,0,255), D3DCOLOR_XRGB(0,0,0)}, { 0.5f, 0.5f, 0.5f, D3DCOLOR_XRGB(0,0,0), D3DCOLOR_XRGB(0,0,255)}, { 0.5f, -0.5f, 0.5f, D3DCOLOR_XRGB(0,0,0), D3DCOLOR_XRGB(0,255,0)}, {-0.5f, -0.5f, 0.5f, D3DCOLOR_XRGB(0,0,0), D3DCOLOR_XRGB(255,0,0)} }; if (pD3d = Direct3DCreate9( D3D_SDK_VERSION )) { D3DDISPLAYMODE d3ddm; D3DPRESENT_PARAMETERS d3dpp; VOID *pVertices; if (FAILED(IDirect3D9_GetAdapterDisplayMode( pD3d, D3DADAPTER_DEFAULT, &d3ddm ))) return FALSE; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format; if (FAILED(IDirect3D9_CreateDevice( pD3d, D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &pDevice)) || FAILED(IDirect3DDevice9_CreateVertexBuffer( pDevice, sizeof(vertices), 0, D3DFVF_MYVERT, D3DPOOL_DEFAULT, &pVertexBuffer, NULL )) || FAILED(IDirect3DVertexBuffer9_Lock( pVertexBuffer, 0, sizeof(vertices), &pVertices, 0 ))) return FALSE; memcpy( pVertices, vertices, sizeof(vertices) ); IDirect3DVertexBuffer9_Unlock( pVertexBuffer ); return TRUE; } return FALSE; } static void ReleaseAll() { if(pDevice) IDirect3DDevice9_Release(pDevice); if(pD3d) IDirect3D9_Release(pD3d); if(pVertexBuffer) IDirect3DVertexBuffer9_Release(pVertexBuffer); } static void Render(void) { DWORD dwBlue = D3DCOLOR_XRGB(0,0,128); fprintf(stderr, "render\n"); IDirect3DDevice9_Clear( pDevice, 0, NULL, D3DCLEAR_TARGET, dwBlue, 1.0f, 0 ); IDirect3DDevice9_BeginScene( pDevice ); { IDirect3DDevice9_SetStreamSource( pDevice, 0, pVertexBuffer, 0, sizeof(MyVert) ); IDirect3DDevice9_SetRenderState( pDevice, D3DRS_LIGHTING, FALSE); IDirect3DDevice9_SetFVF( pDevice, D3DFVF_MYVERT ); IDirect3DDevice9_DrawPrimitive( pDevice, D3DPT_TRIANGLELIST, 0, 2 ); } IDirect3DDevice9_EndScene( pDevice ); IDirect3DDevice9_Present( pDevice, 0, 0, 0, 0); } LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: PostQuitMessage( 0 ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(0), 0, 0, 0, 0, "FirstDX_cl", 0 }; RegisterClassEx( &wc ); HWND hWnd = CreateWindow( "FirstDX_cl", "FirstDX", WS_OVERLAPPEDWINDOW, 100, 100, 160, 160, GetDesktopWindow(), NULL, wc.hInstance, NULL ); if (Init( hWnd )) { MSG msg; ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); ZeroMemory( &msg, sizeof(msg) ); while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { if (msg.message == WM_KEYDOWN) Render(); TranslateMessage( &msg ); DispatchMessage( &msg ); } } } ReleaseAll(); UnregisterClass( "FirstDX_cl", wc.hInstance ); return 0; }