现在有一个远期目标:写一个带融合的播放器。播放器已经用mpc-hc编译了出来,但融合部分没有。打算中重新学习D3D的知识,在深化学习后,实现融合。
学习Dxdirect SDK版本为Microsoft DirectX SDK (June 2010),SDK中的所有例子已经全部用VS2010重新编译跑过一次。然后,自己尝试实现一些例子。我的目标是实现播放器,用不到3D场景,就想着用LPDIRECT3DSURFACE9来显示一张图片。在网上也很容易找到了相应的例子,但编译后发现并没有能正常的显示出图片。(图片大小为1280*800,窗口创建为1280*800)
D3DXIMAGE_INFO info;
if( FAILED(D3DXGetImageInfoFromFile(TEX_FILE, &info)))
{
return S_FALSE;
}
D3DPOOL pool = D3DPOOL_SYSTEMMEM;
result =g_pd3dDevice->CreateOffscreenPlainSurface(info.Width, info.Height, info.Format, pool, &g_surface, NULL);
if( result == S_FALSE )
{
return result;
}
//
继续查找资料并实现调试后发现,窗口创建后,GetBackBuffer获取到的surface远小于1280*800,所以CreateOffscreenPlainSurface传入的宽高参数应当是GetBackBuffer中获取的surface的实际大小
实现代码如下:
//-----------------------------------------------------------------------------
// File: Surface.cpp
//
// Desc: 演示LPDIRECT3DSURFACE9的运用
// 在调用CreateOffscreenPlainSurface宽和高并不能是窗口的大小,也不能是图片的大小,而应当是BackSurface的大小
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.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
LPDIRECT3DSURFACE9 g_surface=NULL;
#define TEX_FILE L"./无标题.png"
//-----------------------------------------------------------------------------
// 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. Since we are now
// using more complex geometry, we will create a device with a zbuffer.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof( d3dpp ) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
// Create the D3DDevice
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 D3D lighting
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
// Turn on the zbuffer
g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
return S_OK;
}
//-----------------------------------------------------------------------------
// Name: InitGeometry()
// Desc: Create the textures and vertex buffers
//-----------------------------------------------------------------------------
HRESULT InitGeometry()
{
D3DXIMAGE_INFO info;
if( FAILED(D3DXGetImageInfoFromFile(TEX_FILE, &info)))
{
return S_FALSE;
}
HRESULT result;
LPDIRECT3DSURFACE9 win_surface=NULL;
result = g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &win_surface);
if( result == S_FALSE )
{
return result;
}
D3DSURFACE_DESC desc;
win_surface->GetDesc(&desc); //win_surface大小并不是创建窗口时的大小
win_surface->Release();
D3DPOOL pool = D3DPOOL_SYSTEMMEM;
result =g_pd3dDevice->CreateOffscreenPlainSurface(desc.Width, desc.Height, info.Format, pool, &g_surface, NULL);
if( result == S_FALSE )
{
return result;
}
result = D3DXLoadSurfaceFromFile(g_surface, NULL, NULL, TEX_FILE, NULL, D3DX_FILTER_NONE, 0xFF000000, NULL);
if( result == S_FALSE )
{
return result;
}
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_surface != NULL )
{
g_surface->Release();
}
}
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
// Clear the backbuffer and the zbuffer
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
// Begin the scene
//if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
{
LPDIRECT3DSURFACE9 backBuffer = NULL;
HRESULT result = g_pd3dDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &backBuffer);
if( result == S_FALSE )
{
return ;
}
D3DSURFACE_DESC desc;
backBuffer->GetDesc(&desc);
result = g_pd3dDevice->UpdateSurface(g_surface, NULL, backBuffer, NULL);
if( result == S_FALSE )
{
return;
}
backBuffer->Release();
// 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 wWinMain( HINSTANCE hInst, HINSTANCE, LPWSTR, INT )
{
UNREFERENCED_PARAMETER( hInst );
// Register the window class
WNDCLASSEX wc =
{
sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
L"D3D Surface", NULL
};
RegisterClassEx( &wc );
// Create the application's window
HWND hWnd = CreateWindow( L"D3D Surface", L"D3D Surface",
WS_OVERLAPPEDWINDOW, 100, 100, 1280, 800,
NULL, NULL, wc.hInstance, NULL );
// Initialize Direct3D
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
// Create the scene 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 )
{
if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
else
Render();
}
}
}
UnregisterClass( L"D3D Surface", wc.hInstance );
return 0;
}