|
早些年写过一篇《一篇文章搞定3DXSpriet !! 》知识还是一样的,就是代码用新的SDK编译不能通过了,老的文章讲述的比较细致,也比较全面在这里权作抛砖引玉,希望大家参与讨论!
A. 什么是ID 3DXSpriet
我们有了创建win窗口的基础下来我们谈谈2D游戏中最关键的函数 3DSprite,通过对函数的运用,你会马上发现这是一个多么简单易用的2D绘图函数。还是老办法,在你的Microsoft DirectX 9.0_c SDK 环境里运行以下下列代码(以下代码来自通过ID3DXSprite来实现DirectX 9.0C绘制2D动画)。
找一张图片(或者自己找一个不要小于200*200像素的图片文件)考到你的程序目录下作为纹理的加载。
//----------------------------------------------------------------------------- // File: Vertices.cpp // // Desc: In this tutorial, we are rendering some vertices. This introduces the // concept of the vertex buffer, a Direct3D object used to store // vertices. Vertices can be defined any way we want by defining a // custom structure and a custom FVF (flexible vertex format). In this // tutorial, we are using vertices that are transformed (meaning they // are already in 2D window coordinates) and lit (meaning we are not // using Direct3D lighting, but are supplying our own colors). // // Copyright (c) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #include #pragma warning( disable : 4996 ) // disable deprecated warning #include #pragma warning( default : 4996 )
//----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device LPDIRECT3DTEXTURE9 g_pTexture = NULL; LPD3DXSPRITE g_pSprite = NULL;
//----------------------------------------------------------------------------- // Name: InitD3D() // Desc: Initializes Direct3D //----------------------------------------------------------------------------- HRESULT InitD3D( HWND hWnd ) { // Create the D3D object. if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) return E_FAIL;
// Set up the structure used to create the D3DDevice D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
// Create the D3DDevice if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; }
D3DXCreateSprite( g_pd3dDevice, &g_pSprite );
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "lid9P0ADGHTJA.jpg", &g_pTexture ) ) )
{
if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "..\\lid9P0ADGHTJA.jpg", &g_pTexture ) ) )
{
MessageBox(NULL, "Could not find pic", "Textures.exe", MB_OK);
return E_FAIL;
}
}
// Device state would normally be set here
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(); }
//----------------------------------------------------------------------------- // Name: Render() // Desc: Draws the scene //----------------------------------------------------------------------------- VOID Render() { // Clear the backbuffer to a blue color g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
RECT rct = {0,0,512,512}; // Begin the scene if( SUCCEEDED( g_pd3dDevice->BeginScene() ) ) {
if ( SUCCEEDED( g_pSprite->Begin(D3DXSPRITE_ALPHABLEND) ) ) { g_pSprite->Draw(g_pTexture, &rct, NULL, NULL, 0xffffffff); g_pSprite->End(); } // 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: Cleanup(); PostQuitMessage( 0 ); return 0; }
return DefWindowProc( hWnd, msg, wParam, lParam ); }
//----------------------------------------------------------------------------- // Name: WinMain() // Desc: The application's entry point //----------------------------------------------------------------------------- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT ) { // Register the window class WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "D3D Tutorial", NULL }; RegisterClassEx( &wc );
// Create the application's window HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 02: Vertices", WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, 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; ZeroMemory( &msg, sizeof(msg) ); while( msg.message!=WM_QUIT ) { if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) ) { TranslateMessage( &msg ); DispatchMessage( &msg ); } else Render(); } }
UnregisterClass( "D3D Tutorial", wc.hInstance ); return 0; }
如过以上程序没有运行通过的话,请到 通过ID3DXSprite来实现DirectX 9.0C绘制2D动画 里查找原因或者提出疑问。
我们初步接触到3DSprite,有一个总概性的了解就可以了,下面的内容才是应该仔细的学习的部分。
|
|