设为首页收藏本站

八达网

 找回密码
 注册
查看: 123|回复: 3
打印 上一主题 下一主题

Directx 达人进!

[复制链接]

0

主题

0

好友

2457

积分

坦克

跳转到指定楼层
1
发表于 2007-12-16 23:34 |只看该作者 |倒序浏览
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.Direct3D;
using Microsoft.DirectX;
using System.Collections;

namespace VisualPoint
{
    public partial class MainForm : Form
    {
        private Device device = null;
        private ArrayList ObstacleList;
        private Camera camera;
      
        //private bool pause=false;
        public MainForm()
        {
            InitializeComponent();
            InitializeGraphics();           
        }
        public bool InitializeGraphics()
        {
            try
            {
                PresentParameters presentParams = new PresentParameters();
                //Now let's setup our D3D stuff               
                presentParams.Windowed = true;
                presentParams.SwapEffect = SwapEffect.Discard;
                presentParams.EnableAutoDepthStencil = true;
                presentParams.AutoDepthStencilFormat = DepthFormat.D16;
                //LoadDefaultOptions();
                // Create the D3DDevice
                device = new Device(0, DeviceType.Hardware, this, CreateFlags.HardwareVertexProcessing, presentParams);
                device.DeviceReset += new System.EventHandler(this.OnResetDevice);              
                OnResetDevice(device, null);
                return true;
            }
            catch (DirectXException)
            {
                return false;
            }
        }
        private void OnResetDevice(object sender, EventArgs e)
        {
            System.IO.Directory.SetCurrentDirectory(Application.StartupPath + @"\..\..\");
            Device dev = (Device)sender;
            // Turn on the zbuffer
            device.RenderState.ZBufferEnable = true;
            //device.RenderState.Ambient = System.Drawing.Color.White;
            ObstacleList = new ArrayList();
            camera = new Camera();
           //SetupMatrices();
            //SetupLights();
            //LoadDefaultOptions();

        }
        private void SetupMatrices()
        {
            //device.Transform.World = Matrix.RotationAxis(new Vector3((float)Math.Cos(Environment.TickCount / 250.0f), 1, (float)Math.Sin(Environment.TickCount / 250.0f)), Environment.TickCount / 1000.0f);
            device.Transform.Projection = Matrix.PerspectiveFovLH((float)Math.PI / 4.0f, 1.0f, 1.0f, 100.0f);
            device.Transform.View = camera.GetViewMatrix();
        }
        private void SetupLights()
        {
            device.Lights[0].Type = LightType.Directional;
            device.Lights[0].Diffuse = System.Drawing.Color.White;
            device.Lights[0].Direction = new Vector3((float)Math.Cos(Environment.TickCount / 1000.0f), 1.0f, (float)Math.Sin(Environment.TickCount / 1000.0f));
            device.Lights[0].Direction = new Vector3(1f, 1f, 1f);
            device.Lights[0].Enabled = true;//turn it on
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            this.Render();
        }
        private void Render()//绘制场景
        {
            //OnFrameUpdate();
            device.Clear(ClearFlags.Target | ClearFlags.ZBuffer, System.Drawing.Color.Blue, 1.0f, 0);
            //Begin the scene
            device.BeginScene();
            SetupMatrices();
            SetupLights();
            foreach (PointObject obj in ObstacleList)
            {
                obj.Draw(this.device);
            }

            //End the scene
            device.EndScene();
            device.Present();           
        }

        private void LoadDefaultOptions()
        {
        }

        private void creatToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            ObstacleList.Add(new PointObject());
                    }  
            
        

    }
}
显示是没有问题,但是移动窗口几下就报错在 device.Present();  在移动窗口的过程中,内存占用不断增大。为什么?有人能帮忙回答下么?

[ 本帖最后由 suisui2006 于 2007-12-16 23:38 编辑 ]

19

主题

0

好友

4万

积分

大和

信义坊第一R

2
发表于 2007-12-16 23:35 |只看该作者
c++已经忘完了
回复

使用道具 举报

19

主题

0

好友

4万

积分

大和

信义坊第一R

3
发表于 2007-12-16 23:36 |只看该作者
你要问什么哦
红色看意思应该是draw list中的所有object啊
回复

使用道具 举报

19

主题

0

好友

4万

积分

大和

信义坊第一R

4
发表于 2007-12-16 23:52 |只看该作者
内存占用不断增大
这肯定是内存泄漏了
有什么东西new了之后没有delete吧
具体原因单步跟踪render的过程啊
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

手机版|Archiver|八达网    

GMT+8, 2026-2-9 05:26

Powered by Discuz! X2.5

© 2001-2012 Comsenz Inc.

回顶部