Пример рисования в си используя directx
Категория: C/C++
2012-02-16 16:21:12
code: #cpp
#include <windows.h> #include <stdio.h> #include <d3d9.h> struct CUSTOMVERTEX { FLOAT x, y, z, rhw; // The transformed position for the vertex. DWORD color; // The vertex color. }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE) IDirect3D9* g_pD3D; IDirect3DDevice9* g_pd3dDevice; IDirect3DVertexBuffer9 *g_pVB; MSG msg; HDC hdc; VOID* pVertices; FLOAT z=0.5f; FLOAT strhw=1.0f; DWORD stcolor=0xff00ffff; int vn; CUSTOMVERTEX* vertices; INT WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, INT ); LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ); VOID Cleanup(); VOID Render(); VOID create_vertices(); int main(int argc, char *argv[]) { vn = 49*49; vertices = new CUSTOMVERTEX[vn]; HINSTANCE hInst = GetModuleHandle(NULL); create_vertices(); int a = WinMain(hInst,NULL,NULL,0); Cleanup(); delete []vertices; } VOID create_vertices() { FLOAT x,y; int i; for(i=0,x=2.0f;x<100.0f;x+=2.0f) { for(y=2.0f;y<100.0f;y+=2.0f,i++) {//f(i); vertices[i].x = x; vertices[i].y = y; vertices[i].z = z; vertices[i].rhw = strhw; vertices[i].color = stcolor; } } } INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE phI, LPSTR lp, INT ig) { WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_HREDRAW|CS_VREDRAW, MsgProc, 0L, 0L, hInst, NULL, NULL, NULL, NULL, "Direct3D Tutorial", NULL }; RegisterClassEx( &wc ); HWND hWnd = CreateWindow( "Direct3D Tutorial", "Direct3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, GetDesktopWindow(), NULL, wc.hInstance, NULL ); SetFocus(hWnd); ShowWindow(hWnd,SW_SHOWDEFAULT); if( ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) )== NULL){ return E_FAIL;} D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN; if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ){} if( FAILED( g_pd3dDevice->CreateVertexBuffer( vn*sizeof(CUSTOMVERTEX), 0 /*Usage*/, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB, NULL ) ) ) return E_FAIL; if( FAILED( g_pVB->Lock( 0, vn*sizeof(CUSTOMVERTEX), (void**)&pVertices, 0 ) ) ) return E_FAIL; memcpy( pVertices, vertices, vn*sizeof(CUSTOMVERTEX) ); g_pVB->Unlock(); while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: PostQuitMessage( 0 ); return 0; case WM_PAINT: Render(); ValidateRect( hWnd, NULL ); return 0; } return DefWindowProc( hWnd, msg, wParam, lParam ); } VOID Render() { // Clear the back buffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 ); // Begin the scene g_pd3dDevice->BeginScene(); // Rendering of scene objects happens here g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX) ); g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_POINTLIST, 0, vn ); // End the scene g_pd3dDevice->EndScene(); g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } VOID Cleanup() { if( g_pd3dDevice != NULL) g_pd3dDevice->Release(); if( g_pD3D != NULL) g_pD3D->Release(); }
автор: challengerr
Поделиться: