游戏开发学习 01

ok,继续。现在我有了一个窗口,我要在窗口上画张图片。有很多办法可以在winform上来张图片,但是这次要用DirectX来做。

Step1:添加对Directx程序集的引用(在.net 卡片下,如果没有 C:\WINDOWS\Microsoft.NET\DirectX for Managed Code\ 找找。
Microsoft.DirectX
Microsoft.DirectX.Direct3D
Microsoft.DirectX.Direct3DX
先添这三个就够了,Dirext3DX是个工具集,有很多个版本,这东西现在还用不到。

Step2:用到的东西,使用DirectX来完成这次绘图我需要三个主要的对象 Device(设备),Sprite(精灵),Texture(纹理)。
简单点理解,Device是“墙”,Sprite是“笔”,Texture是“油漆或图纸”,现在可以开始画了。

初始化“墙面”(Device)
DirextX9 Doc:A Microsoft Direct3D device is the rendering component of Direct3D。因此做任何绘制工作前必须创建该对象。
它的创建方法:Device(Int32,DeviceType,IntPtr,CreateFlags,PresentParameters[]) ,看下面(来自DirectX9 sample)。

None.gif // 初始化显示设备
None.gif
         public   bool  InitializeGraphics()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//参数集
InBlock.gif
                PresentParameters presentParams = new PresentParameters();
InBlock.gif                presentParams.Windowed 
= true;
InBlock.gif                presentParams.SwapEffect 
= SwapEffect.Discard;
InBlock.gif
InBlock.gif                device 
= new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (DirectXException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

参数一(int32),为显示设备的索引值,当你有多快显示卡的时候通过该参数来确定使用哪块。
参数二(DeviceType),加速的类型。其他的选项先不管,先用Hardware来完成。
参数三(CreateFlags),一个标签,先不去管它。
参数四(PresentParameters[])显示参数集,通过这个参数的集合我们可以得到我们想要的窗体显示效果。Windowed表示是否为窗口显示,否为全屏。SwapEffect,交换效果,回头再说,照抄。

Step3: 创建一个图纸,这里需要一个纹理(Texture)
DirectX9 Doc:Textures are a powerful tool for creating realism in computer-generated 3-D images. Texture是个强大的工具,他来处理所有的图像素材。
它的重要方法就是Load(载入),使用一个TextureLoader对象来载入各种来源的素材,我现在需要一个来自图片文件的素材,看代码。

None.gif Texture _texture  =  TextureLoader.FromFile(Device,fileName)

Step4:用Sprite把这个纹理画到Device上
Sprite 可以将一些素材绘制出来,目前只需要知道这些,我要使用它的Draw2D方法来绘制纹理,看代码。

None.gif sprite  =   new  Sprite(device);
None.gif
None.gifsprite.Begin(SpriteFlags.AlphaBlend);
None.gifsprite.Draw2D(p.Texture, 
new  PointF( 0 0 ),  0 new  PointF( 0 0 ), Color.FromArgb( 255 255 255 255 ));
None.gifdevice.Transform.World 
=  Matrix.Identity;
None.gifsprite.End();

关于device.Transform 是用于变形设置的,要知道在2维的现实屏中显示3D的效果它可使不可缺少的,现在这个设置是告诉它别动(不变)。运行一下,bingo!(图)
gamehello01.jpg
看起来不错哦。现在,我需要让这些图像动起来,让他们丰富起来...

整体的代码如下:

None.gif using  System.Collections.Generic;
None.gif
using  System.ComponentModel;
None.gif
using  System.Data;
None.gif
using  System.Drawing;
None.gif
using  System.Text;
None.gif
using  System.Windows.Forms;
None.gif
None.gif
using  Microsoft.DirectX;
None.gif
using  Microsoft.DirectX.Direct3D;
None.gif
None.gif
namespace  GameHello01
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
public class GameWindow : Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
//需要的东西
InBlock.gif
        Device device = null;
InBlock.gif        Sprite sprite 
= null;
InBlock.gif        Texture texture 
= null;
InBlock.gif
InBlock.gif        
public GameWindow()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Text = "GameHelloWorld 01";
InBlock.gif            
this.Paint += new PaintEventHandler(GameWindow_Paint);
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 初始化 Device --#region -- 初始化 Device --
InBlock.gif        
public bool InitializeGraphics()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PresentParameters presentParams 
= new PresentParameters();
InBlock.gif                presentParams.Windowed 
= true;
InBlock.gif                presentParams.SwapEffect 
= SwapEffect.Discard;
InBlock.gif                device 
= new Device(0, DeviceType.Hardware, this, CreateFlags.SoftwareVertexProcessing, presentParams);
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (DirectXException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 初始化 Texture --#region -- 初始化 Texture --
InBlock.gif        
public bool InitializeTexture()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                texture 
= TextureLoader.FromFile(device, Application.StartupPath + "\\back.jpg");
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (DirectXException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 初始化 Sprite --#region -- 初始化 Sprite --
InBlock.gif        
public bool InitializeSprite()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                sprite 
= new Sprite(device);
InBlock.gif                
return true;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
catch (DirectXException)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return false;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
-- 画图 Render() --#region -- 画图 Render() --
InBlock.gif        
void Render()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (device == null || sprite == null || texture == null)
InBlock.gif                
return;
InBlock.gif
InBlock.gif            device.Clear(ClearFlags.Target, System.Drawing.Color.Black, 
1.0f0);
InBlock.gif            device.BeginScene();
InBlock.gif
InBlock.gif            sprite.Begin(SpriteFlags.AlphaBlend);
InBlock.gif
InBlock.gif            sprite.Draw2D(texture, 
new PointF(00), 0new PointF(00), Color.FromArgb(255255255255));
InBlock.gif            device.Transform.World 
= Matrix.Identity;
InBlock.gif
InBlock.gif            sprite.End();
InBlock.gif
InBlock.gif            device.EndScene();
InBlock.gif            device.Present();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
InBlock.gif        
void GameWindow_Paint(object sender, PaintEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.Render();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
static void Main()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
using (GameWindow window = new GameWindow())
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (!window.InitializeGraphics())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    MessageBox.Show(
"Direct3D初始化错误!"); 
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (!window.InitializeTexture())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    MessageBox.Show(
"Texture初始化错误!"); 
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                
if (!window.InitializeSprite())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    MessageBox.Show(
"Sprite初始化错误!"); 
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                    
InBlock.gif
InBlock.gif                window.Show();
InBlock.gif
InBlock.gif                
while (window.Created)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    window.Render();
InBlock.gif                    Application.DoEvents();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

InBlock.gif


 

转载于:https://www.cnblogs.com/shichao/archive/2007/02/02/638389.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值