D3D绘制按钮

D3D绘制按钮_第1张图片 //----------------------------------------------------------------------------- // File: CreateDevice.cpp // // Desc: This is the first tutorial for using Direct3D. In this tutorial, all // we are doing is creating a Direct3D device and using it to clear the // window. // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #include <d3d9.h> #include <d3dx9.h> #pragma warning( disable : 4996 ) // disable deprecated warning #include <strsafe.h> #pragma warning( default : 4996 ) //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device LPDIRECT3DTEXTURE9 g_upTex, g_downTex, g_overTex; LPDIRECT3DVERTEXBUFFER9 g_vertexBuffers; bool LMBDown = false; int mouseX = 0, mouseY = 0; // A structure for our custom vertex type struct stGUIVertex { float x, y, z, rhw; unsigned long color; float tu, tv; }; // Our custom FVF, which describes our custom vertex structure #define D3DFVF_GUI (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1) //----------------------------------------------------------------------------- // Name: InitializeObjects() // Desc: Initializes Object //----------------------------------------------------------------------------- bool InitializeObjects() { // Load the texture image from file. if(D3DXCreateTextureFromFile(g_pd3dDevice, L"startUp.png", &g_upTex) != D3D_OK) { return false; } if(D3DXCreateTextureFromFile(g_pd3dDevice, L"startOver.png", &g_overTex) != D3D_OK) { return false; } if(D3DXCreateTextureFromFile(g_pd3dDevice, L"startDown.png", &g_downTex) != D3D_OK) { return false; } unsigned long white = D3DCOLOR_XRGB(255,255,255); // Get width and height of the image so we use that size. D3DSURFACE_DESC desc; g_downTex->GetLevelDesc(0,&desc); float w = (float)desc.Width; float h = (float)desc.Height; int x = 50; int y = 200; stGUIVertex obj[] = { {w + x, 0 + y, 0.0f, 1, white, 1.0f, 0.0f}, {w + x, h + y, 0.0f, 1, white, 1.0f, 1.0f}, {0 + x, 0 + y, 0.0f, 1, white, 0.0f, 0.0f}, {0 + x, h + y, 0.0f, 1, white, 0.0f, 1.0f}, }; if(FAILED(g_pd3dDevice->CreateVertexBuffer(sizeof(obj), 0, D3DFVF_GUI, D3DPOOL_DEFAULT, &g_vertexBuffers, NULL))) { return false; } // Fill the vertex buffer. void *ptr; if(FAILED(g_vertexBuffers->Lock(0, sizeof(obj), (void**)&ptr, 0))) { return false; } memcpy(ptr, obj, sizeof(obj)); g_vertexBuffers->Unlock(); return true; } //----------------------------------------------------------------------------- // Name: InitD3D() // Desc: Initializes Direct3D //----------------------------------------------------------------------------- HRESULT InitD3D( HWND hWnd ) { // Create the D3D object, which is needed to create the D3DDevice. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL; D3DDISPLAYMODE displayMode; g_pD3D->GetAdapterDisplayMode(0,&displayMode); // Set up the structure used to create the D3DDevice. Most parameters are // zeroed out. We set Windowed to TRUE, since we want to do D3D in a // window, and then set the SwapEffect to "discard", which is the most // efficient method of presenting the back buffer to the display. And // we request a back buffer format that matches the current desktop display // format. D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof( d3dpp ) ); d3dpp.Windowed = TRUE; d3dpp.hDeviceWindow = hWnd; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = displayMode.Format; // Create the Direct3D device. Here we are using the default adapter (most // systems only have one, unless they have multiple graphics hardware cards // installed) and requesting the HAL (which is saying we want the hardware // device rather than a software one). Software vertex processing is // specified since we know it will work on all cards. On cards that support // hardware vertex processing, though, we would see a big performance gain // by specifying hardware vertex processing. if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } // Device state would normally be set here // Initialize any objects we will be displaying. if(!InitializeObjects()) { return false; } return S_OK; } //----------------------------------------------------------------------------- // Name: Cleanup() // Desc: Releases all previously initialized objects //----------------------------------------------------------------------------- VOID Cleanup() { if( g_pd3dDevice != NULL ) g_pd3dDevice->Release(); if( g_pD3D != NULL ) g_pD3D->Release(); if(g_vertexBuffers) g_vertexBuffers->Release(); } //----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { if( NULL == g_pd3dDevice ) return; // Clear the backbuffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 ); // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) { // Rendering of scene objects can happen here // Set alpha transparency on for the texture image. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); g_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); g_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); g_pd3dDevice->SetTexture(0,g_downTex); // Render button. g_pd3dDevice->SetStreamSource(0, g_vertexBuffers, 0, sizeof(stGUIVertex)); g_pd3dDevice->SetFVF(D3DFVF_GUI); g_pd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, 2); // Turn off alpha. g_pd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE); // End the scene g_pd3dDevice->EndScene(); } // Present the backbuffer contents to the display g_pd3dDevice->Present( NULL, NULL, NULL, NULL ); } //----------------------------------------------------------------------------- // Name: MsgProc() // Desc: The window's message handler //----------------------------------------------------------------------------- LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam ) { switch( msg ) { case WM_DESTROY: PostQuitMessage(0); return 0; break; case WM_KEYUP: if(wParam == VK_ESCAPE) PostQuitMessage(0); break; case WM_LBUTTONDOWN: LMBDown = true; break; case WM_LBUTTONUP: LMBDown = false; break; case WM_MOUSEMOVE: mouseX = LOWORD (lParam); mouseY = HIWORD (lParam); break; case WM_PAINT: Render(); ValidateRect( hWnd, NULL ); break; } return DefWindowProc( hWnd, msg, wParam, lParam ); } //----------------------------------------------------------------------------- // Name: wWinMain() // Desc: The application's entry point //----------------------------------------------------------------------------- INT WINAPI wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT ) { // Register the window class WNDCLASSEX wc = { sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle( NULL ), NULL, NULL, NULL, NULL, L"D3D Tutorial", NULL }; RegisterClassEx( &wc ); // Create the application's window HWND hWnd = CreateWindow( L"D3D Tutorial", L"D3D Tutorial 01: CreateDevice", WS_OVERLAPPEDWINDOW, 100, 100, 640, 480, NULL, NULL, wc.hInstance, NULL ); // Initialize Direct3D if( SUCCEEDED( InitD3D( hWnd ) ) ) { // Show the window ShowWindow( hWnd, SW_SHOWDEFAULT ); UpdateWindow( hWnd ); // Enter the message loop MSG msg; while( GetMessage( &msg, NULL, 0, 0 ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } } UnregisterClass( L"D3D Tutorial", wc.hInstance ); return 0; }

你可能感兴趣的:(D3D绘制按钮)