转自 : http://hi.baidu.com/welflau/blog/item/1b17032320bb05499358075e.html
http://hi.baidu.com/welflau/希望能保留原创作者链接,谢谢!
2008-12-02
第1讲【创建框架】
1 新建MFC exe / dialog based
2 删除dialog类的显示代码
3 添加WelflGameFrm类
4 在app类的InitInstance 函数中 添加http://hi.baidu.com/welflau/希望能保留原创作者链接,谢谢!
m_pMainWnd = new CWelflGameFrm;
m_pMainWnd->ShowWindow( m_nCmdShow );
m_pMainWnd->UpdateWindow();
5 将CWelflGameFrm()构造函数改为public 成员
6 在构造函数CWelflGameFrm中添加:
RECT rect;
Create(NULL,"ch07-1: ミDirectGraphics");
CClientDC dc(this);
int width = dc.GetDeviceCaps(HORZRES);
int height = dc.GetDeviceCaps(VERTRES);
GetWindowRect( &rect );
width = ( width - ( rect.right - rect.left ))/2 ;
height = (height - (rect.bottom - rect.top ))/2 ;
MoveWindow( width , height , (rect.right - rect.left ) , (rect.bottom - rect.top ) ,true);
7 为CWelfGameFrm类添加WindowProc函数,并加入代码:http://hi.baidu.com/welflau/希望能保留原创作者链接,谢谢!
switch( message )
{
case WM_CREATE :
if( !d3dCreate( m_hWnd , 640 , 480 , true ))
PostMessage( WM_CLOSE );
return 0 ;
case WM_DESTROY :
d3dRelease();
return 0 ;
}
8 为工程添加两个文件
myd3d.cpp, myd3d.h
9 编译出现
fatal error C1010: unexpected end of file while looking for precompiled header directive
解决方案:在Project Settings里C++页面的Precomplie Header里把出错源文件设置为不使用预编译头就可以了,详见视频
10 将BOOL CWelfGameApp::InitInstance()中的
return FALSE;改为 return TRUE;
11 为CWelfGameFrm类添加OnPaint响应
添加如下代码:
d3dClear(0);
//
d3d_Device->Present( NULL , NULL , NULL , NULL );
第2讲【绘制文字和图像】
目录
【一】、从DirectGraphic中获取DC(GDI)
【二】、2D图像绘制
【一】、从DirectGraphic中获取DC(GDI)
1 添加类d3dHdc 在 myd3d.h文件中
class d3dHdc
{
private :
HDC m_hdc ;
LPDIRECT3DSURFACE9 m_Surface ;
public :
void Release();
inline operator HDC(){ return m_hdc ;};
public :
d3dHdc();
~d3dHdc();
};
2在myd3d.cpp中添加函数定义
d3dHdc::d3dHdc()
{
m_hdc = 0 ;
m_Surface = 0 ;
// 获得设备
if( !d3d_Device )
return ;
if( d3d_Device->GetBackBuffer( 0 , 0 , D3DBACKBUFFER_TYPE_MONO , &m_Surface ) != D3D_OK )
return ;
m_Surface->GetDC( &m_hdc );
}
d3dHdc::~d3dHdc()
{
Release();
}
void d3dHdc::Release()
{
if( m_Surface )
{
if( m_hdc )
m_Surface->ReleaseDC( m_hdc );
m_Surface->Release();
m_hdc = NULL ;
m_Surface = NULL ;
}
}
3 现在就可以使用他了
在绘制函数中添加:
d3dHdc hdc ;
SetTextColor( hdc , RGB( 255 , 255 , 255 ));
SetBkMode( hdc , 1 );
TextOut( hdc , 0 , 0 , str , strlen( str ));
hdc.Release();
【二】、2D图像绘制
1 新建d3dTexture类
class d3dTexture
{
private :
int m_Width ;
int m_Height ;
LPDIRECT3DTEXTURE9 m_Texture ;
public :
void BltFast( int x , int y );
void BltFast( int l , int t , int r , int b );
public :
BOOL Create( LPCTSTR file );
void Release();
inline operator LPDIRECT3DTEXTURE9(){ return m_Texture ;};
public :
d3dTexture();
~d3dTexture();
};
2 并添加定义:
/*////////////////////////////////////////////////
3D 纹理类d3dTexture 函数定义 2008-12-02
/*////////////////////////////////////////////////
d3dTexture::d3dTexture()
{
m_Texture = NULL ;
}
d3dTexture::~d3dTexture()
{
Release();
}
void d3dTexture::Release()
{
if( m_Texture )
m_Texture->Release();
m_Texture = NULL ;
}
BOOL d3dTexture::Create( LPCTSTR file )
{
D3DXIMAGE_INFO in ;
memset( &in , 0 , sizeof( in ));
// ﹍て
Release();
//更
D3DXCreateTextureFromFileEx( d3d_Device ,
file , D3DX_DEFAULT , D3DX_DEFAULT ,
0 , 0 , D3DFMT_UNKNOWN , D3DPOOL_MANAGED ,
D3DX_DEFAULT ,
D3DX_DEFAULT , 0 , &in , NULL , &m_Texture );
if( m_Texture == NULL )
return false ;
// 眔戈
m_Width = in.Width ;
m_Height = in.Height ;
return true ;
}
void d3dTexture::BltFast(int x, int y)
{
BltFast( x , y , x + m_Width , y + m_Height );
}
void d3dTexture::BltFast(int l , int t , int r , int b )
{
D3DTLVERTEX v[4] ;
//郴翴 挡篶
memset( v , 0 , sizeof( v ));
v[0].x = v[3].x = (float)(l) ;
v[1].x = v[2].x = (float)(r);
v[0].y = v[1].y = (float)(t);
v[2].y = v[3].y = (float)(b);
v[0].rhw = v[1].rhw = v[2].rhw = v[3].rhw =
v[0].z = v[1].z = v[2].z = v[3].z = 0.5f ;
v[0].diffuse = v[1].diffuse = v[2].diffuse = v[3].diffuse = -1 ;
v[1].tu = v[2].tu = 1.0f ;
v[2].tv = v[3].tv = 1.0f ;
//砞酶瓜家Α
d3d_Device->SetTexture( 0 , m_Texture );
d3d_Device->SetFVF( D3DFVF_TLVERTEX );
d3d_Device->DrawPrimitiveUP( D3DPT_TRIANGLEFAN , 2 , (LPVOID)v , sizeof( D3DTLVERTEX ));
}
3 编译时会出错
解决方法:
在myd3d.h文件中添加
#include "d3dx9tex.h"
和
#pragma comment(lib,"d3dx9.lib")
以及
const DWORD D3DFVF_TLVERTEX = (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX1 );
typedef struct _D3DTLVERTEX
{
float x , y , z , rhw ;
D3DCOLOR diffuse , specular;
float tu, tv;
}D3DTLVERTEX ;
4 在WelfGameFrm中添加两个成员
d3dTexture m_Bk ;
d3dTexture m_Role ;
5 在WelfGameFrm的Create响应添加:
m_Bk.Create( "背景.tga" );
m_Role.Create( "娃娃.tga" );
6 调用BltFast进行绘制
//开始绘制
d3d_Device->BeginScene();
d3d_Device->SetRenderState( D3DRS_CULLMODE , D3DCULL_NONE );
d3d_Device->SetRenderState( D3DRS_ZENABLE , D3DZB_FALSE );
m_Bk.BltFast( 0 , 0 );
m_Role.BltFast( 0 , 0 );
d3d_Device->EndScene();
7 将BltFast函数改进
第3讲 WelfGame【图像透明及颜色】
http://hi.baidu.com/welflau/希望能保留原创作者链接,
【一】、实现透明图像
1 在RenderScene()中添加设置代码
在背景绘制前添加:
d3d_Device->SetRenderState( D3DRS_CULLMODE , D3DCULL_NONE );
d3d_Device->SetRenderState( D3DRS_ZENABLE , D3DZB_FALSE );
d3d_Device->SetRenderState( D3DRS_SHADEMODE , D3DSHADE_FLAT );
d3d_Device->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
在前景绘制的前面添加
d3d_Device->SetRenderState( D3DRS_SRCBLEND , D3DBLEND_SRCALPHA );
d3d_Device->SetRenderState( D3DRS_DESTBLEND , D3DBLEND_INVSRCALPHA );
d3d_Device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );//开启混色
效果
2 为d3dTexture添加BltFast函数
void d3dTexture::BltFast(int l , int t , int r , int b , DWORD diffuse )
{
D3DTLVERTEX v[4] ;
//郴翴 挡篶
memset( v , 0 , sizeof( v ));
v[0].x = v[3].x = (float)(l) ;
v[1].x = v[2].x = (float)(r);
v[0].y = v[1].y = (float)(t);
v[2].y = v[3].y = (float)(b);
v[0].rhw = v[1].rhw = v[2].rhw = v[3].rhw =
v[0].z = v[1].z = v[2].z = v[3].z = 0.5f ;
//肅︹
v[0].diffuse = v[1].diffuse = v[2].diffuse = v[3].diffuse = diffuse ;
v[1].tu = v[2].tu = 1.0f ;
v[2].tv = v[3].tv = 1.0f ;
//砞酶瓜家Α
d3d_Device->SetTexture( 0 , m_Texture );
d3d_Device->SetFVF( D3DFVF_TLVERTEX );
d3d_Device->DrawPrimitiveUP( D3DPT_TRIANGLEFAN , 2 , (LPVOID)v , sizeof( D3DTLVERTEX ));
}
添加以下几句:
d3d_Device->SetTextureStageState( 0 , D3DTSS_ALPHAARG1 , D3DTA_TEXTURE );
d3d_Device->SetTextureStageState( 0 , D3DTSS_ALPHAARG2 , D3DTA_DIFFUSE );
d3d_Device->SetTextureStageState( 0 , D3DTSS_COLORARG1 , D3DTA_TEXTURE );
d3d_Device->SetTextureStageState( 0 , D3DTSS_COLORARG2 , D3DTA_DIFFUSE );
d3d_Device->SetTextureStageState( 0 , D3DTSS_COLOROP , D3DTOP_SELECTARG1 );
d3d_Device->SetTextureStageState( 0 , D3DTSS_ALPHAOP , D3DTOP_MODULATE );
//以下为前景
m_Role.BltFast( 100, 100, 250 ,250 ,D3DCOLOR_ARGB( 128 , 255 , 255 , 255 ));//最后一个参数为颜色,包括透明度
效果图
FW:
http://www.wowwiki.com/Making_Draggable_Frames
XML Declarations
First, the XML tags movable="true" and enableMouse="true" must be in the frames declaration.
Note: Some frame templates like 'button' already include enableMouse="true".
Example:
<Frame name="TellTrackFrame" enableMouse="true" movable="true" resizable="true" parent="UIParent" hidden="true">
Simple Dragging
One simple way to detect drag is to add OnDragStart and OnDragStop script elements to the frame:
<Scripts>
<OnLoad>
this:RegisterForDrag("LeftButton");
</OnLoad>
<OnDragStart>
this:StartMoving();
this.isMoving = true;
</OnDragStart>
<OnDragStop>
this:StopMovingOrSizing();
this.isMoving = false;
</OnDragStop>
</Scripts>
Advanced Dragging
Another way, which is more responsive but requires an onhide element so that the frame wont get stuck to the mouse:
<Scripts>
<OnMouseUp>
if ( this.isMoving ) then
this:StopMovingOrSizing();
this.isMoving = false;
end
</OnMouseUp>
<OnMouseDown>
if ( ( ( not this.isLocked ) or ( this.isLocked == 0 ) ) and ( arg1 == "LeftButton" ) ) then
this:StartMoving();
this.isMoving = true;
end
</OnMouseDown>
<OnHide>
if ( this.isMoving ) then
this:StopMovingOrSizing();
this.isMoving = false;
end
</OnHide>
</Scripts>
Note: this method also demonstrates an optional isLocked parameter to determine whether you can drag the frame or not.
Parent Dragging
Some advanced dragging addons use overlays that make default Blizzard
frames draggable. This is possible by using GetParent when starting and
stopping drag. To do this, one must make the parent frame movable
through the use of the SetMovable widget function, i.e.
frame:SetMovable(true).
One drawback with overlay frames that are mouse enabled is that they
will prevent the parent frame's click script tags from being called so
you often have to simulate their click events.
Quick Dragging Code
While somewhat untested there is an easier and more automatic way to
activate dragging. If you have your <Frame> delcaration
attributes "enableMouse" and "movable" set to true, dragging may be
accomplished by adding a <TitleRegion> tag inside of your
<Frame>
<Frame name="myname" frameStrata="HIGH" toplevel="true" enableMouse="true" movable="true" parent="UIParent">
<TitleRegion setAllPoints="true"/>
</Frame>
I haven't discovered any adverse side effects to doing this yet, I am not even sure if this is the intended use for it.
Using this method can result in the frame not responding to other mouse events, also both mouse buttons will drag the frame.
You can also specify <Size> and <Anchors> within <TitleRegion>, e.g.
<Frame name="myname" frameStrata="HIGH" toplevel="true" enableMouse="true" movable="true" parent="UIParent">
<TitleRegion>
<Size>
<AbsDimension x="200" y="20"/>
</Size>
<Anchors>
<Anchor point="TOP"/>
</Anchors>
</TitleRegion>
</Frame>
This way, your <Frame> can still receive mouse events, and you
can only drag it by clicking within its <TitleRegion>.
Lua Only Approach
If your frame is called MyFrame -
MyFrame:SetMovable(true)
MyFrame:EnableMouse(true)
MyFrame:SetScript("OnMouseDown",function()
MyFrame:StartMoving()
end)
MyFrame:SetScript("OnMouseUp",function()
MyFrame:StopMovingOrSizing()
end)