数字时钟DigitalClock尝试--续(1)完美的“8”
效果图:
解说:
代码:
#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
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices 顶点Buffer
// A structure for our custom vertex type 自定义顶点类型
struct CUSTOMVERTEX
{
D3DXVECTOR4 position; // The transformed position for the vertex
DWORD color; // The vertex color
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
// -----------------------------------------------------------------------------
// 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;
}
// Device state would normally be set here
return S_OK;
}
// -----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
// -----------------------------------------------------------------------------
VOID Cleanup()
{
if ( g_pVB != NULL )
g_pVB -> Release();
if ( g_pd3dDevice != NULL )
g_pd3dDevice -> Release();
if ( g_pD3D != NULL )
g_pD3D -> Release();
}
VOID DrawSegment( const CUSTOMVERTEX * pVerticesTemplate, size_t sizeTemplate, D3DXVECTOR4 & vectorOffset)
{
if ( g_pVB != NULL )
g_pVB -> Release();
CUSTOMVERTEX tmpVitices[ 6 ];
memcpy(tmpVitices, pVerticesTemplate, sizeTemplate);
for ( int i = 0 ; i < 6 ; ++ i)
{
tmpVitices[i].position += vectorOffset;
}
g_pd3dDevice -> CreateVertexBuffer( 6 * sizeof (CUSTOMVERTEX),
0 , D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, & g_pVB, NULL );
VOID * pVertices; // 输出参数
g_pVB -> Lock( 0 , sizeof (tmpVitices), ( void ** ) & pVertices, 0 );
memcpy( pVertices, tmpVitices, sizeof (tmpVitices) );
g_pVB -> Unlock();
g_pd3dDevice -> SetStreamSource( 0 , g_pVB, 0 , sizeof (CUSTOMVERTEX) );
g_pd3dDevice -> SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice -> DrawPrimitive( D3DPT_TRIANGLESTRIP, 0 , 4 );
}
// -----------------------------------------------------------------------------
// 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 );
// Begin the scene
if ( SUCCEEDED( g_pd3dDevice -> BeginScene() ) )
{
const static float c_fStartX = 100.0f ;
const static float c_fStartY = 100.0f ;
const static float c_fSharpH = 20.0f ; // 尖头的高度(注意:尖头是直角的)
const static float c_fMid = 150.0f ; // 中段的宽度
// 横着的segment
static CUSTOMVERTEX verticesSegmentHori[] =
{
/* 0 */ { D3DXVECTOR4( c_fStartX, c_fStartY + c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 1 */ { D3DXVECTOR4( c_fStartX + c_fSharpH, c_fStartY, 0.5f , 1.0f ), 0xffff0000 , },
/* 2 */ { D3DXVECTOR4( c_fStartX + c_fSharpH, c_fStartY + 2 * c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 3 */ { D3DXVECTOR4( c_fStartX + c_fSharpH + c_fMid, c_fStartY, 0.5f , 1.0f ), 0xffff0000 , },
/* 4 */ { D3DXVECTOR4( c_fStartX + c_fSharpH + c_fMid, c_fStartY + 2 * c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 5 */ { D3DXVECTOR4( c_fStartX + c_fSharpH * 2 + c_fMid, c_fStartY + c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
};
// 竖着的segment
static CUSTOMVERTEX verticesSegmentVert[] =
{
/* 0 */ { D3DXVECTOR4( c_fStartX + c_fSharpH, c_fStartY, 0.5f , 1.0f ), 0xffff0000 , },
/* 1 */ { D3DXVECTOR4( c_fStartX + 2 * c_fSharpH, c_fStartY + c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 2 */ { D3DXVECTOR4( c_fStartX, c_fStartY + c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 3 */ { D3DXVECTOR4( c_fStartX + 2 * c_fSharpH, c_fStartY + c_fSharpH + c_fMid, 0.5f , 1.0f ), 0xffff0000 , },
/* 4 */ { D3DXVECTOR4( c_fStartX, c_fStartY + c_fSharpH + c_fMid, 0.5f , 1.0f ), 0xffff0000 , },
/* 5 */ { D3DXVECTOR4( c_fStartX + c_fSharpH, c_fStartY + 2 * c_fSharpH + c_fMid, 0.5f , 1.0f ), 0xffff0000 , },
};
const static float c_fStartXOffset = 100.0f ;
const static float c_fOffsetSmall = 5.0f ;
// 画个横着的
DrawSegment(verticesSegmentHori, sizeof (verticesSegmentHori)
, D3DXVECTOR4(c_fStartXOffset, 0.0f , 0.0f , 0.0f ));
// 再画个横着的
DrawSegment(verticesSegmentHori, sizeof (verticesSegmentHori)
, D3DXVECTOR4(c_fStartXOffset, c_fMid + 2 * c_fSharpH + 2 * c_fOffsetSmall, 0.0f , 0.0f ));
// 又画个横着的
DrawSegment(verticesSegmentHori, sizeof (verticesSegmentHori)
, D3DXVECTOR4(c_fStartXOffset, c_fMid * 2 + 4 * c_fSharpH + 4 * c_fOffsetSmall, 0.0f , 0.0f ));
// 左侧竖着的
// 上
DrawSegment(verticesSegmentVert, sizeof (verticesSegmentVert)
, D3DXVECTOR4(c_fStartXOffset - c_fSharpH - c_fOffsetSmall, c_fSharpH + c_fOffsetSmall, 0.0f , 0.0f ));
DrawSegment(verticesSegmentVert, sizeof (verticesSegmentVert)
, D3DXVECTOR4(c_fStartXOffset - c_fSharpH - c_fOffsetSmall, c_fSharpH + c_fOffsetSmall + ( 2 * c_fSharpH + c_fMid + 2 * c_fOffsetSmall), 0.0f , 0.0f ));
// 右侧竖着的
// 下
DrawSegment(verticesSegmentVert, sizeof (verticesSegmentVert)
, D3DXVECTOR4(c_fStartXOffset - c_fSharpH - c_fOffsetSmall + c_fMid + 2 * c_fSharpH + 2 * c_fOffsetSmall, c_fSharpH + c_fOffsetSmall, 0.0f , 0.0f ));
DrawSegment(verticesSegmentVert, sizeof (verticesSegmentVert)
, D3DXVECTOR4(c_fStartXOffset - c_fSharpH - c_fOffsetSmall + c_fMid + 2 * c_fSharpH + 2 * c_fOffsetSmall, c_fSharpH + c_fOffsetSmall + ( 2 * c_fSharpH + c_fMid + 2 * c_fOffsetSmall), 0.0f , 0.0f ));
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 " , " DxDigitalClock " ,
WS_OVERLAPPEDWINDOW, 100 , 100 , 700 , 700 ,
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 ;
}
#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
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices 顶点Buffer
// A structure for our custom vertex type 自定义顶点类型
struct CUSTOMVERTEX
{
D3DXVECTOR4 position; // The transformed position for the vertex
DWORD color; // The vertex color
};
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
// -----------------------------------------------------------------------------
// 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;
}
// Device state would normally be set here
return S_OK;
}
// -----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
// -----------------------------------------------------------------------------
VOID Cleanup()
{
if ( g_pVB != NULL )
g_pVB -> Release();
if ( g_pd3dDevice != NULL )
g_pd3dDevice -> Release();
if ( g_pD3D != NULL )
g_pD3D -> Release();
}
VOID DrawSegment( const CUSTOMVERTEX * pVerticesTemplate, size_t sizeTemplate, D3DXVECTOR4 & vectorOffset)
{
if ( g_pVB != NULL )
g_pVB -> Release();
CUSTOMVERTEX tmpVitices[ 6 ];
memcpy(tmpVitices, pVerticesTemplate, sizeTemplate);
for ( int i = 0 ; i < 6 ; ++ i)
{
tmpVitices[i].position += vectorOffset;
}
g_pd3dDevice -> CreateVertexBuffer( 6 * sizeof (CUSTOMVERTEX),
0 , D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, & g_pVB, NULL );
VOID * pVertices; // 输出参数
g_pVB -> Lock( 0 , sizeof (tmpVitices), ( void ** ) & pVertices, 0 );
memcpy( pVertices, tmpVitices, sizeof (tmpVitices) );
g_pVB -> Unlock();
g_pd3dDevice -> SetStreamSource( 0 , g_pVB, 0 , sizeof (CUSTOMVERTEX) );
g_pd3dDevice -> SetFVF( D3DFVF_CUSTOMVERTEX );
g_pd3dDevice -> DrawPrimitive( D3DPT_TRIANGLESTRIP, 0 , 4 );
}
// -----------------------------------------------------------------------------
// 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 );
// Begin the scene
if ( SUCCEEDED( g_pd3dDevice -> BeginScene() ) )
{
const static float c_fStartX = 100.0f ;
const static float c_fStartY = 100.0f ;
const static float c_fSharpH = 20.0f ; // 尖头的高度(注意:尖头是直角的)
const static float c_fMid = 150.0f ; // 中段的宽度
// 横着的segment
static CUSTOMVERTEX verticesSegmentHori[] =
{
/* 0 */ { D3DXVECTOR4( c_fStartX, c_fStartY + c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 1 */ { D3DXVECTOR4( c_fStartX + c_fSharpH, c_fStartY, 0.5f , 1.0f ), 0xffff0000 , },
/* 2 */ { D3DXVECTOR4( c_fStartX + c_fSharpH, c_fStartY + 2 * c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 3 */ { D3DXVECTOR4( c_fStartX + c_fSharpH + c_fMid, c_fStartY, 0.5f , 1.0f ), 0xffff0000 , },
/* 4 */ { D3DXVECTOR4( c_fStartX + c_fSharpH + c_fMid, c_fStartY + 2 * c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 5 */ { D3DXVECTOR4( c_fStartX + c_fSharpH * 2 + c_fMid, c_fStartY + c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
};
// 竖着的segment
static CUSTOMVERTEX verticesSegmentVert[] =
{
/* 0 */ { D3DXVECTOR4( c_fStartX + c_fSharpH, c_fStartY, 0.5f , 1.0f ), 0xffff0000 , },
/* 1 */ { D3DXVECTOR4( c_fStartX + 2 * c_fSharpH, c_fStartY + c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 2 */ { D3DXVECTOR4( c_fStartX, c_fStartY + c_fSharpH, 0.5f , 1.0f ), 0xffff0000 , },
/* 3 */ { D3DXVECTOR4( c_fStartX + 2 * c_fSharpH, c_fStartY + c_fSharpH + c_fMid, 0.5f , 1.0f ), 0xffff0000 , },
/* 4 */ { D3DXVECTOR4( c_fStartX, c_fStartY + c_fSharpH + c_fMid, 0.5f , 1.0f ), 0xffff0000 , },
/* 5 */ { D3DXVECTOR4( c_fStartX + c_fSharpH, c_fStartY + 2 * c_fSharpH + c_fMid, 0.5f , 1.0f ), 0xffff0000 , },
};
const static float c_fStartXOffset = 100.0f ;
const static float c_fOffsetSmall = 5.0f ;
// 画个横着的
DrawSegment(verticesSegmentHori, sizeof (verticesSegmentHori)
, D3DXVECTOR4(c_fStartXOffset, 0.0f , 0.0f , 0.0f ));
// 再画个横着的
DrawSegment(verticesSegmentHori, sizeof (verticesSegmentHori)
, D3DXVECTOR4(c_fStartXOffset, c_fMid + 2 * c_fSharpH + 2 * c_fOffsetSmall, 0.0f , 0.0f ));
// 又画个横着的
DrawSegment(verticesSegmentHori, sizeof (verticesSegmentHori)
, D3DXVECTOR4(c_fStartXOffset, c_fMid * 2 + 4 * c_fSharpH + 4 * c_fOffsetSmall, 0.0f , 0.0f ));
// 左侧竖着的
// 上
DrawSegment(verticesSegmentVert, sizeof (verticesSegmentVert)
, D3DXVECTOR4(c_fStartXOffset - c_fSharpH - c_fOffsetSmall, c_fSharpH + c_fOffsetSmall, 0.0f , 0.0f ));
DrawSegment(verticesSegmentVert, sizeof (verticesSegmentVert)
, D3DXVECTOR4(c_fStartXOffset - c_fSharpH - c_fOffsetSmall, c_fSharpH + c_fOffsetSmall + ( 2 * c_fSharpH + c_fMid + 2 * c_fOffsetSmall), 0.0f , 0.0f ));
// 右侧竖着的
// 下
DrawSegment(verticesSegmentVert, sizeof (verticesSegmentVert)
, D3DXVECTOR4(c_fStartXOffset - c_fSharpH - c_fOffsetSmall + c_fMid + 2 * c_fSharpH + 2 * c_fOffsetSmall, c_fSharpH + c_fOffsetSmall, 0.0f , 0.0f ));
DrawSegment(verticesSegmentVert, sizeof (verticesSegmentVert)
, D3DXVECTOR4(c_fStartXOffset - c_fSharpH - c_fOffsetSmall + c_fMid + 2 * c_fSharpH + 2 * c_fOffsetSmall, c_fSharpH + c_fOffsetSmall + ( 2 * c_fSharpH + c_fMid + 2 * c_fOffsetSmall), 0.0f , 0.0f ));
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 " , " DxDigitalClock " ,
WS_OVERLAPPEDWINDOW, 100 , 100 , 700 , 700 ,
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 ;
}