DXUT窗口设置背景

本文主要介绍如何在DXUT框架下设置Direct3D窗口的背景,通过C2DDraw类实现,包括创建Offscreen Plain Surface,加载图片并将其复制到后台缓冲区。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DXUT窗口设置背景

作者:Kagula

日期:2009-3-31

阅读对象

[1]了解DXUT框架的基本使用

[2]能够读懂C源码

环境

[1]Visual Studio 2005

[2]DirectX SDK (November 2008)

内容概要

DXUT框架下,设置当前窗口的背景。

正文

理论方面可以参考《Beginning.DirectX9》这本书,2D部份。

这里仅给出源码,C2DDraw.hC2DDraw.cpp

 

C2DDraw.h

#pragma once

 

#include "DX9Obj.h"

 

/*

  二维绘图类

*/

class C2DDraw :public CDX9Obj //CDX9Obj是个抽象类,它规定了类的接口

{

public:

     C2DDraw(LPCWSTR pFilename) //可以是JPG文件,图片尺寸会自动拉缩

     {

         m_pFilename = pFilename;

         m_pSurface=NULL;

     }

     ~C2DDraw(void)

     {   

         SAFE_RELEASE(m_pSurface);

     }

public:

         //Callback 函数列表

      HRESULT  OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,

         void* pUserContext ) ;

 

      void     OnLostDevice( void* pUserContext ) ;

 

      HRESULT  OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,

         void* pUserContext ) ;

 

      void     OnDestroyDevice( void* pUserContext ) ;

 

     //Render

      void     OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext ) ;

private:

     LPDIRECT3DSURFACE9 m_pSurface;

     LPCWSTR            m_pFilename;

 

     HRESULT createSurfaces( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc); 

};

C2DDraw.cpp

#include "dxut.h"

#include "C2DDraw.h"

 

 

//Callback 函数列表

HRESULT  C2DDraw::OnCreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,

         void* pUserContext )

{

     return createSurfaces(pd3dDevice,pBackBufferSurfaceDesc);

}

 

void     C2DDraw::OnLostDevice( void* pUserContext )

{

     SAFE_RELEASE(m_pSurface);

}

 

HRESULT  C2DDraw::OnResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,

         void* pUserContext )

{  

     SAFE_RELEASE(m_pSurface);

     return createSurfaces(pd3dDevice,pBackBufferSurfaceDesc);

}

 

void   C2DDraw::OnDestroyDevice( void* pUserContext )

{

     SAFE_RELEASE(m_pSurface);

}

 

//Render

void     C2DDraw::OnFrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )

{

     // This will hold the back buffer

     IDirect3DSurface9* backbuffer = NULL;

     // Check to make sure you have a valid Direct3D device

     if( NULL == pd3dDevice )

         return;

 

     // Clear the back buffer to a blue color

     //pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

 

     // Get the back buffer

     pd3dDevice->GetBackBuffer( 0,0,D3DBACKBUFFER_TYPE_MONO,&backbuffer );

 

     // Copy the offscreen surface to the back buffer

     // Note the use of NULL values for the source and destination RECTs

     // This ensures a copy of the entire surface to the back buffer

     //m_surface -> backbuffer

     pd3dDevice->StretchRect( m_pSurface,NULL,backbuffer,NULL,D3DTEXF_NONE );

    

     // Present the back buffer contents to the display

     //pd3dDevice->Present ( NULL, NULL, NULL, NULL );

     SAFE_RELEASE(backbuffer);

}

 

HRESULT C2DDraw::createSurfaces( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc)

{

     HRESULT hr;

    

     V_RETURN( pd3dDevice->CreateOffscreenPlainSurface(

         pBackBufferSurfaceDesc->Width, // the width of the surface to create

         pBackBufferSurfaceDesc->Height, // the height of the surface to create

         D3DFMT_X8R8G8B8, // the surface format

         D3DPOOL_DEFAULT, // the memory pool to use

         &m_pSurface, // holds the resulting surface

         NULL) ); // reserved; should be NULL

 

     V_RETURN( D3DXLoadSurfaceFromFile( m_pSurface,NULL,NULL,m_pFilename,NULL,D3DX_DEFAULT,0,NULL ) );

 

     return hr;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kagula086

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值