1st_dx8.cpp
上传用户:hnchaoyang
上传日期:2022-04-10
资源大小:13k
文件大小:8k
- /*------------------------------------------------------------
- MY FIRST C++/DIRECTX PROGRAM - BY SIMON PRICE
- ----------------------------------------------------------*/
- // include files
- #include <d3dx9.h>
- #include <mmsystem.h>
- // global variables
- LPDIRECT3D8 g_pD3D = NULL; // pointer to d3d
- LPDIRECT3DDEVICE8 g_pD3Ddevice = NULL; // pointer to rendering device
- LPDIRECT3DVERTEXBUFFER8 g_pD3Dvb = NULL; // pointer to vertex buffer
- //LPDIRECT3DINDEXBUFFER8 g_pD3Dib = NULL; // pointer to index buffer
- INT g_iCur = 0; // used to show/hide cursor
- // a structure which holds untransformed, lit vertices
- struct LVERTEX
- {
- FLOAT x, y, z; // untransformed position
- DWORD color; // vertex color
- };
- // describes the vertex format
- #define D3DFVF_LVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)
- // INITD3D: INITIALIZES DIRECT3D AND THE RENDERING DEVICE
- HRESULT InitD3D( HWND hWnd )
- {
- // get a pointer to direct3d
- if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
- return E_FAIL;
- // get display mode format
- D3DDISPLAYMODE D3Ddm;
- if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &D3Ddm ) ) )
- return E_FAIL;
- // set d3ddevice parameters
- D3DPRESENT_PARAMETERS D3Dpp;
- ZeroMemory( &D3Dpp, sizeof(D3Dpp) );
- //D3Dpp.Windowed = TRUE ;
- D3Dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
- D3Dpp.BackBufferFormat = D3Ddm.Format;
- D3Dpp.BackBufferWidth = D3Ddm.Width;
- D3Dpp.BackBufferHeight = D3Ddm.Height;
- // create the rendering device
- if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &D3Dpp, &g_pD3Ddevice ) ) )
- {
- return E_FAIL;
- }
- // turn off culling
- //g_pD3Ddevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
- // turn off lighting (vertices will be supplied already lit)
- g_pD3Ddevice->SetRenderState( D3DRS_LIGHTING, FALSE );
- // hide the cursor
- g_iCur = ShowCursor( 0 );
- return S_OK;
- }
- // RGB: CREATES A LONG COLOR FROM RED, GREEN AND BLUE COMPONENTS
- inline long RGB2long(int Red,int Green,int Blue)
- {
- Red*=65536;
- Green*=256;
- return(Red+Green+Blue);
- }
- // INITGEOMETRY: INITIALIZES GEOMETRY AND VERTEX BUFFER
- HRESULT InitGeometry()
- {
- // initialize 8 vertices in a cube shape
- LVERTEX Vertices[] =
- {
- { 0.0f,1.0f,0.0f, RGB2long(255,255,255) },
- { 1.0f,-1.0f,-1.0f, RGB2long(0,255,0) },
- { -1.0f,-1.0f,-1.0f, RGB2long(255,0,0) },
- { -0.0f,1.0f,0.0f, RGB2long(255,255,255) },
- { -1.0f,-1.0f,-1.0f, RGB2long(255,0,0) },
- { 0.0f,-1.0f,1.0f, RGB2long(0,0,255) },
- { 0.0f,1.0f,0.0f, RGB2long(255,255,255) },
- { 0.0f,-1.0f,1.0f, RGB2long(0,0,255) },
- { 1.0f,-1.0f,-1.0f, RGB2long(0,255,0) },
- { 0.0f,-1.0f,1.0f, RGB2long(0,0,255) },
- { -1.0f,-1.0f,-1.0f, RGB2long(255,0,0) },
- { 1.0f,-1.0f,-1.0f, RGB2long(0,255,0) },
- };
- // create the vertex buffer
- if( FAILED( g_pD3Ddevice->CreateVertexBuffer( 12*sizeof(LVERTEX), 0, D3DFVF_LVERTEX, D3DPOOL_DEFAULT, &g_pD3Dvb ) ) )
- {
- return E_FAIL;
- }
- // fill the vertex buffer
- VOID* pVertices;
- if( FAILED( g_pD3Dvb->Lock( 0, sizeof(Vertices), (BYTE**)&pVertices, 0 ) ) )
- return E_FAIL;
- memcpy( pVertices, Vertices, sizeof(Vertices) );
- g_pD3Dvb->Unlock();
- /* // initialize indices
- DWORD Indices[] =
- {
- 0,1,2,2,1,3,
- 4,5,0,0,5,3,
- 4,0,6,6,0,2,
- 1,2,3,3,5,7,
- 5,4,7,7,4,6,
- 2,3,6,6,3,7,
- };
- // create index buffer
- if( FAILED( g_pD3Ddevice->CreateIndexBuffer( 36*sizeof(DWORD), D3DUSAGE_SOFTWAREPROCESSING , D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pD3Dib ) ) )
- return E_FAIL;
- // fill index buffer
- VOID* pIndices;
- if( FAILED( g_pD3Dib->Lock( 0, sizeof(Indices), (BYTE**)&pIndices, 0 ) ) )
- return E_FAIL;
- memcpy( pIndices, Indices, sizeof(Vertices) );
- g_pD3Dib->Unlock();*/
- return S_OK;
- }
- // CLEANUP: RELEASES D3D POINTERS
- VOID Cleanup()
- {
- // show the cursor again
- g_iCur = ShowCursor( g_iCur );
- // release vertex buffer
- if( g_pD3Dvb != NULL )
- g_pD3Dvb->Release();
- // release rendering device
- if( g_pD3Ddevice != NULL )
- g_pD3Ddevice->Release();
- // release direct3d
- if( g_pD3D != NULL )
- g_pD3D->Release();
- }
- // SETUPMATRICES: SETS WORLD, VIEW AND PROJECTION MATRICES
- VOID SetupMatrices()
- {
- // rotate the world about the y axis
- D3DXMATRIX matWorld;
- D3DXMatrixRotationYawPitchRoll( &matWorld, timeGetTime()/450.0f, sin(timeGetTime()/850.0f), 0.0f );
- g_pD3Ddevice->SetTransform( D3DTS_WORLD, &matWorld );
- // set camera position
- D3DXMATRIX matView;
- D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 3.0f,-2.0f ),
- &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
- &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
- g_pD3Ddevice->SetTransform( D3DTS_VIEW, &matView );
- // set projection (includes clip distances, field of view, perspective ratio)
- D3DXMATRIX matProj;
- D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
- g_pD3Ddevice->SetTransform( D3DTS_PROJECTION, &matProj );
- }
- // RENDER: DRAWS THE GEOMETRY IN THE VERTEX BUFFER
- VOID Render()
- {
- // clear the backbuffer
- g_pD3Ddevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
- // begin rendering
- g_pD3Ddevice->BeginScene();
- // setup the world, view, and projection matrices
- SetupMatrices();
- // set vertex buffer source for rendering device
- g_pD3Ddevice->SetStreamSource( 0, g_pD3Dvb, sizeof(LVERTEX) );
- // set vertex description
- g_pD3Ddevice->SetVertexShader( D3DFVF_LVERTEX );
- // set index buffer source for rendering device
- //g_pD3Ddevice->SetIndices( g_pD3Dib, 0 );
- // draw contents of vertex buffer
- //g_pD3Ddevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 4, 0, 4);
- g_pD3Ddevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 4 );
- // end rendering
- g_pD3Ddevice->EndScene();
- // present backbuffer contents to the window
- g_pD3Ddevice->Present( NULL, NULL, NULL, NULL );
- }
- // MSGHANDLER: HANDLES MESSAGES FROM WINDOWS
- LRESULT WINAPI MsgHandler( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
- {
- // check which message has been recieved
- switch( msg )
- {
- // quit application
- case WM_DESTROY:
- PostQuitMessage( 0 );
- return 0;
- case WM_KEYDOWN:
- PostQuitMessage( 0 );
- return 0;
- }
- // message not handled
- return DefWindowProc( hWnd, msg, wParam, lParam );
- }
- // WINMAIN: THE START OF THE PROGRAM
- INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
- {
- // Register the window class
- WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgHandler, 0L, 0L, GetModuleHandle(NULL), NULL, NULL, NULL, NULL, "1st_dx8", NULL };
- RegisterClassEx( &wc );
- // create a window
- HWND hWnd = CreateWindow( "1st_dx8", "Si's First C++/DX8 Program!", WS_OVERLAPPEDWINDOW, 100, 100, 500, 500, GetDesktopWindow(), NULL, wc.hInstance, NULL );
- // initialize direct3d
- if( SUCCEEDED( InitD3D( hWnd ) ) )
- {
- // initialize geometry
- if( SUCCEEDED( InitGeometry() ) )
- {
- // show the window
- ShowWindow( hWnd, SW_SHOWDEFAULT );
- UpdateWindow( hWnd );
- // enter the message loop
- MSG msg;
- ZeroMemory( &msg, sizeof(msg) );
- while( msg.message!=WM_QUIT )
- {
- // check for message
- if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
- {
- TranslateMessage( &msg );
- DispatchMessage( &msg );
- }
- else // no messages in queue so render
- Render();
- }
- }
- }
- // clean up and exit the app
- Cleanup();
- UnregisterClass( "1st_dx8", wc.hInstance );
- return 0;
- }