玩玩DirectShow--(3)D3D Tutorial与CMovie的第一次亲密接触
该是自己动手来写的时候了,最简单的代码能突出重点。
自己建个win32项目,选择空项目。
CMovie类来自于
Microsoft Platform SDK for Windows Server 2003 R2\Samples\Multimedia\DirectShow\VMR9\VMRPlayer\
中的vcdplyer.h,做了一点修改。
程序主体代码用了 Direct3D Tutorial 3: Using Matrices的部分代码。
截图:
播放
不播放
项目配置:
#include
<
streams.h
>
位于Microsoft Platform SDK for Windows Server 2003 R2\Samples\Multimedia\DirectShow\BaseClasses
链接:只有这三个d3d9.lib Strmiids.lib Quartz.lib
使用 Unicode 字符集
多线程调试 DLL (/MDd)
核心代码:
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,
L " D3D Tutorial " , NULL };
RegisterClassEx( & wc );
// Create the application's window
g_hwnd = CreateWindow( L " D3D Tutorial " , L " DShow CMovie Test " ,
WS_OVERLAPPEDWINDOW, 100 , 100 , 300 , 300 ,
NULL, NULL, wc.hInstance, NULL );
g_pMovie = new CMovie(g_hwnd);
g_pMovie->OpenMovie(L"C:\\Program Files\\Microsoft Platform SDK for Windows Server 2003 R2\\Samples\\Multimedia\\DirectShow\\Media\\Video\\ruby.avi" );
g_pMovie-> PlayMovie();
// Initialize Direct3D
if ( SUCCEEDED( InitD3D( g_hwnd ) ) )
{
// Show the window
ShowWindow( g_hwnd, SW_SHOWDEFAULT );
UpdateWindow( g_hwnd );
// Enter the message loop
MSG msg;
ZeroMemory( & msg, sizeof (msg));
while (msg.message != WM_QUIT)
{
if ( PeekMessage( & msg, NULL, 0 , 0 , PM_REMOVE ) )
{
TranslateMessage( & msg );
DispatchMessage( & msg );
}
else
{
Render();
}
}
}
UnregisterClass( L " D3D Tutorial " , wc.hInstance );
return 0 ;
}
视频播放时d3d不渲染,不播放的时候才渲染
{
// 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
g_hwnd = CreateWindow( L " D3D Tutorial " , L " DShow CMovie Test " ,
WS_OVERLAPPEDWINDOW, 100 , 100 , 300 , 300 ,
NULL, NULL, wc.hInstance, NULL );
g_pMovie = new CMovie(g_hwnd);
g_pMovie->OpenMovie(L"C:\\Program Files\\Microsoft Platform SDK for Windows Server 2003 R2\\Samples\\Multimedia\\DirectShow\\Media\\Video\\ruby.avi" );
g_pMovie-> PlayMovie();
// Initialize Direct3D
if ( SUCCEEDED( InitD3D( g_hwnd ) ) )
{
// Show the window
ShowWindow( g_hwnd, SW_SHOWDEFAULT );
UpdateWindow( g_hwnd );
// Enter the message loop
MSG msg;
ZeroMemory( & msg, sizeof (msg));
while (msg.message != WM_QUIT)
{
if ( PeekMessage( & msg, NULL, 0 , 0 , PM_REMOVE ) )
{
TranslateMessage( & msg );
DispatchMessage( & msg );
}
else
{
Render();
}
}
}
UnregisterClass( L " D3D Tutorial " , wc.hInstance );
return 0 ;
}
VOID Render()
{
if(g_pMovie-> IsPlaying())
{
RECT rc;
::GetClientRect(g_hwnd, & rc);
g_pMovie->PutMoviePosition(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
g_pMovie-> RepaintVideo(g_hwnd, ::GetDC(g_hwnd));
}
else
{
if ( NULL == g_pd3dDevice )
return ;
// 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() ) )
{
// Rendering of scene objects can happen here
// End the scene
g_pd3dDevice -> EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice -> Present( NULL, NULL, NULL, NULL );
}
}
{
if(g_pMovie-> IsPlaying())
{
RECT rc;
::GetClientRect(g_hwnd, & rc);
g_pMovie->PutMoviePosition(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
g_pMovie-> RepaintVideo(g_hwnd, ::GetDC(g_hwnd));
}
else
{
if ( NULL == g_pd3dDevice )
return ;
// 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() ) )
{
// Rendering of scene objects can happen here
// End the scene
g_pd3dDevice -> EndScene();
}
// Present the backbuffer contents to the display
g_pd3dDevice -> Present( NULL, NULL, NULL, NULL );
}
}
代码下载:
dshow.cmovie.rar
Reference:
DirectShow播放视频