CFanatic

Go Back   CFanatic > Programming > C# Programming

Join CFanatic Forum Now

Program crashes once recoverd from taskbar topic posted under C# Programming which is a part of Programming category in CFanatic Forum
Reply
 
Thread Tools Display Modes
  #1  
Old 09-02-2010, 06:12 AM
Junior Member
 
Join Date: Sep 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
JBtje is on a distinguished road
| More
Program crashes once recoverd from taskbar

Hello there,

A while ago I created this program, based on tutorials and some code of my own. Thougth once I put this program on the taskbar, and recover it from taskbar, it crashes!
The reason is because m_Device doesn't exist anymore.... but I have no clue how to solve this problem....

I hope anyone here can help me with this, (without creating a memory leak as I did b4, but dont remember how...)

The error should occure somewhere here, since this is the only place I use m_Device

Draw.cs:

Code:
using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Globalization;

using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Diagnostics;

namespace JeaBsy_c3d_viewer
{
    public partial class Form1
    {

                // The Direct3D device.
                private Device m_Device;

                // The mesh.
                Mesh m_SphereMesh;
                Mesh m_CubeMesh;

                // The material used for the sphere.
                Material m_SphereMaterial;
                Material m_CubeMaterial;

                // The Camera position matrix
                Vector3 CameraPosition = new Vector3(0f, 3f, 5f);
                Vector3 CameraTarget = new Vector3(0f, 0f, 0f);
                Vector3 CameraUpVector = new Vector3(0f, 1f, 0f);

                // Initialize the graphics device. Return True if successful.
                public bool InitializeGraphics()
                {
                        if (m_Device == null)
                        {
                                PresentParameters parms = new PresentParameters();
                                parms.Windowed = true;
                                parms.SwapEffect = SwapEffect.Discard;

                                // Best: Hardware device and hardware vertex processing.
                                try
                                {
                                        m_Device = new Device(0, DeviceType.Hardware, pic3d,
                                                CreateFlags.HardwareVertexProcessing, parms);
                                        Debug.WriteLine("Hardware, HardwareVertexProcessing");
                                }
                                catch { }

                                // Good: Hardware device and software vertex processing.
                                if (m_Device == null)
                                {
                                        try
                                        {
                                                m_Device = new Device(0, DeviceType.Hardware, pic3d,
                                                        CreateFlags.SoftwareVertexProcessing, parms);
                                                Debug.WriteLine("Hardware, SoftwareVertexProcessing");
                                        }
                                        catch { }
                                }

                                // Adequate?: Software device and software vertex processing.
                                if (m_Device == null)
                                {
                                        try
                                        {
                                                m_Device = new Device(0, DeviceType.Reference, pic3d,
                                                        CreateFlags.SoftwareVertexProcessing, parms);
                                                Debug.WriteLine("Reference, SoftwareVertexProcessing");
                                        }
                                        catch (Exception ex)
                                        {
                                                // If we still can't make a device, give up.
                                                MessageBox.Show("Error initializing Direct3D\n\n" + ex.Message,
                                                        "Direct3D Error", MessageBoxButtons.OK);
                                                return false;
                                        }
                                }

                                // Define camera position
                                CameraAngle.AnglePhi = -60f;
                                CameraAngle.AngleTau = 0f;
                                CameraAngle.Distance = 5f;
                                CameraAngle.FocusPoint = new Vector3(0f, 0f, 0f);
                                CameraAngle.Radians = (Math.PI / 180);

                                // Calculate camera position
                                CalculateCamera();

                                // Turn on D3D lighting.
                                m_Device.RenderState.Lighting = true;
                                m_Device.RenderState.SpecularEnable = true;

                                m_Device.RenderState.ZBufferEnable = true;
                                m_Device.RenderState.ZBufferFunction = Compare.LessEqual;
                                m_Device.RenderState.ZBufferWriteEnable = true;

                                // Make points bigger so they're easy to see.
                                m_Device.RenderState.PointSize = 4;

                                // Start in solid mode.
                                m_Device.RenderState.FillMode = FillMode.Solid;

                                // Create the vertex data.
                                CreateVertexBuffer();

                                // Make the lights.
                                SetupLights();

                                // We succeeded.
                                return true;
                        }
                        return true;
                }

                // Create a vertex buffer for the device.
                public void CreateVertexBuffer()
                {
                        if (m_SphereMesh != null)
                                m_SphereMesh.Dispose();
                        if (m_CubeMesh != null)
                                m_SphereMesh.Dispose();

                        m_SphereMesh = Mesh.Sphere(m_Device, 0.05f, 100, 50);
                        m_CubeMesh = Mesh.Box(m_Device, 1f, 2f, 1f);

                        m_SphereMaterial = new Material();
                        m_SphereMaterial.Ambient = Color.White;
                        m_SphereMaterial.Diffuse = Color.White;
                        m_SphereMaterial.Specular = Color.White;
                        m_SphereMaterial.SpecularSharpness = 100;

                        m_CubeMaterial = new Material();
                        m_CubeMaterial.Ambient = Color.White;
                        m_CubeMaterial.Diffuse = Color.White;
                        m_CubeMaterial.Specular = Color.White;
                        m_CubeMaterial.SpecularSharpness = 100;
                }

                public void Render()
                {
                        // Load the Spheres
                        LoadSpheres();

                        // Clear the back buffer.
                        m_Device.Clear(ClearFlags.Target, Color.Black, 1, 0);
                        // Make a scene.
                        m_Device.BeginScene();

                        // Draw stuff here...
                        // Setup the world, view, and projection matrices.
                        SetupMatrices();
                        UseControls();

                        foreach (GridStruct currentGrid in Grid)
                        {
                                m_Device.Transform.World = Matrix.Scaling(new Vector3(6f, 0.008f, 0.008f)) * Matrix.Translation(new Vector3(0f, 0f, currentGrid.position));
                                m_Device.Material = m_CubeMaterial;
                                m_CubeMesh.DrawSubset(0);

                                m_Device.Transform.World = Matrix.Scaling(new Vector3(0.008f, 0.008f, 6f)) * Matrix.Translation(new Vector3(currentGrid.position, 0f, 0f));
                                m_Device.Material = m_CubeMaterial;
                                m_CubeMesh.DrawSubset(0);
                        }

                        foreach (SphereStruct currentSphere in Spherelist)
                        {
                                m_Device.Transform.World = Matrix.Scaling(
                                                0.1f * (float)SphereSize.SelectedIndex,
                                                0.1f * (float)SphereSize.SelectedIndex,
                                                0.1f * (float)SphereSize.SelectedIndex
                                        ) *
                                        Matrix.Translation(
                                                currentSphere.position
                                        );

                                m_Device.Material = m_SphereMaterial;
                                m_SphereMesh.DrawSubset(0);
                        }

                        float PointX, PointY, PointZ, AngleTau, AnglePhi, VLength, VProjLength;
                        foreach (CubeStruct currentCube in Cubelist)
                        {
                                PointX = (currentCube.P1.X - currentCube.P2.X);
                                PointY = (currentCube.P1.Y - currentCube.P2.Y);
                                PointZ = (currentCube.P1.Z - currentCube.P2.Z);

                                VLength = (float)Math.Sqrt((PointX) * (PointX) + (PointY) * (PointY) + (PointZ) * (PointZ));
                                VProjLength = (float)Math.Sqrt((PointX) * (PointX) + (PointZ) * (PointZ));

                                AngleTau = (float)Math.Atan(PointX / PointZ);
                                AnglePhi = (float)Math.Asin(VProjLength / VLength);

                                if (PointZ < 0)
                                {
                                        AngleTau += (float)Math.PI;
                                }
                                if (PointY < 0)
                                {
                                        AnglePhi = (float)(Math.PI - AnglePhi);
                                }
                                if (float.IsNaN(AngleTau))
                                {
                                        AngleTau = 0f;
                                }

                                m_Device.Transform.World =
                                        Matrix.Scaling(new Vector3(
                                                0.005f * (float)SphereSize.SelectedIndex,
                                                0.5f * VLength,
                                                0.005f * (float)SphereSize.SelectedIndex)
                                        ) *
                                        Matrix.RotationX(AnglePhi) *
                                        Matrix.RotationY(AngleTau) *

                                        Matrix.Translation(
                                                new Vector3(
                                                        (currentCube.P1.X + currentCube.P2.X) / 2,
                                                        (currentCube.P1.Y + currentCube.P2.Y) / 2,
                                                        (currentCube.P1.Z + currentCube.P2.Z) / 2
                                                )

                                        );

                                m_Device.Material = m_CubeMaterial;
                                m_CubeMesh.DrawSubset(0);
                        }

                        m_Device.Transform.World = Matrix.Scaling(0.02f, 0.02f, 0.02f) *
                                        Matrix.Translation(
                                                new Vector3(
                                                        CameraAngle.FocusPoint.X,
                                                        CameraAngle.FocusPoint.Y,
                                                        CameraAngle.FocusPoint.Z
                                                )
                                        );

                        m_Device.Material = m_CubeMaterial;
                        m_CubeMesh.DrawSubset(0);

                        // End the scene and display.
                        m_Device.EndScene();
                        m_Device.Present();
                }

                // Setup the world, view, and projection matrices.
                private void SetupMatrices()
                {
                        if (m_Device == null) return;

                        // World Matrix:
                        m_Device.Transform.World = Matrix.Identity;

                        // View Matrix:
                        // This is defined by giving:
                        //       An eye point            (0, 3, -4)
                        //       A point to look at      (0, 0, 0)
                        //       An "up" direction       

                        m_Device.Transform.View = Matrix.LookAtLH(
                                CameraPosition,
                                CameraTarget,
                                CameraUpVector);

                        // Projection Matrix:
                        // Perspective transformation defined by:
                        //       Field of view           Pi / 4
                        //       Aspect ratio            1
                        //       Near clipping plane     Z = 1
                        //       Far clipping plane      Z = 100
                        m_Device.Transform.Projection =
                                Matrix.PerspectiveFovLH((float)(Math.PI / 4), 1, 1, 100);
                }

                // Make the lights.
                private void SetupLights()
                {
                        // Make a light.
                        // Z-
                        try
                        {
                                m_Device.Lights[0].Type = LightType.Directional;
                                m_Device.Lights[0].DiffuseColor = new ColorValue(0, 0, 256);
                                m_Device.Lights[0].AmbientColor = new ColorValue(10, 10, 20);
                                m_Device.Lights[0].Position = new Vector3(0, 0, 20);
                                m_Device.Lights[0].XDirection = -m_Device.Lights[0].XPosition;
                                m_Device.Lights[0].YDirection = -m_Device.Lights[0].YPosition;
                                m_Device.Lights[0].ZDirection = -m_Device.Lights[0].ZPosition;
                                m_Device.Lights[0].Range = 100;
                                m_Device.Lights[0].Enabled = true;

                                // Z+
                                m_Device.Lights[1].Type = LightType.Directional;
                                m_Device.Lights[1].DiffuseColor = new ColorValue(256, 0, 0);
                                m_Device.Lights[1].AmbientColor = new ColorValue(20, 10, 10);
                                m_Device.Lights[1].Position = new Vector3(0, 0, -20);
                                m_Device.Lights[1].XDirection = -m_Device.Lights[1].XPosition;
                                m_Device.Lights[1].YDirection = -m_Device.Lights[1].YPosition;
                                m_Device.Lights[1].ZDirection = -m_Device.Lights[1].ZPosition;
                                m_Device.Lights[1].Range = 100;
                                m_Device.Lights[1].Enabled = true;

                                // X -
                                m_Device.Lights[2].Type = LightType.Directional;
                                m_Device.Lights[2].DiffuseColor = new ColorValue(256, 256, 256);
                                m_Device.Lights[2].AmbientColor = new ColorValue(20, 20, 20);
                                m_Device.Lights[2].Position = new Vector3(-20, 0, 0);
                                m_Device.Lights[2].XDirection = -m_Device.Lights[2].XPosition;
                                m_Device.Lights[2].YDirection = -m_Device.Lights[2].YPosition;
                                m_Device.Lights[2].ZDirection = -m_Device.Lights[2].ZPosition;
                                m_Device.Lights[2].Range = 100;
                                m_Device.Lights[2].Enabled = true;

                                // X +
                                m_Device.Lights[3].Type = LightType.Directional;
                                m_Device.Lights[3].DiffuseColor = new ColorValue(256, 256, 256);
                                m_Device.Lights[3].AmbientColor = new ColorValue(20, 20, 20);
                                m_Device.Lights[3].Position = new Vector3(20, 0, 0);
                                m_Device.Lights[3].XDirection = -m_Device.Lights[3].XPosition;
                                m_Device.Lights[3].YDirection = -m_Device.Lights[3].YPosition;
                                m_Device.Lights[3].ZDirection = -m_Device.Lights[3].ZPosition;
                                m_Device.Lights[3].Range = 100;
                                m_Device.Lights[3].Enabled = true;

                                /*                        m_Device.Lights[2].Type = LightType.Directional;
                                                        m_Device.Lights[2].Diffuse = Color.White;
                                                        m_Device.Lights[2].AmbientColor = new ColorValue(0, 1, 0);
                                                        m_Device.Lights[2].Position = new Vector3(0, 10, 0);
                                                        m_Device.Lights[2].Attenuation0 = 0;
                                                        m_Device.Lights[2].Attenuation1 = 0.75f;
                                                        m_Device.Lights[2].Attenuation2 = 0;
                                                        m_Device.Lights[2].Range = 100;
                                                        m_Device.Lights[2].Specular = Color.White;
                                                        m_Device.Lights[2].Enabled = true;

                                                        m_Device.Lights[3].Type = LightType.Spot;
                                                        m_Device.Lights[3].Diffuse = Color.Green;
                                                        m_Device.Lights[3].AmbientColor = new ColorValue(0, 1, 0);
                                                        m_Device.Lights[3].Position = new Vector3(0, 0, -2);
                                                        m_Device.Lights[3].XDirection = -m_Device.Lights[3].XPosition;
                                                        m_Device.Lights[3].YDirection = -m_Device.Lights[3].YPosition;
                                                        m_Device.Lights[3].ZDirection = -m_Device.Lights[3].ZPosition;
                                                        m_Device.Lights[3].Attenuation0 = 1;
                                                        m_Device.Lights[3].Range = 100;
                                                        m_Device.Lights[3].Falloff = 1;
                                                        m_Device.Lights[3].InnerConeAngle = (float)(Math.PI / 8);
                                                        m_Device.Lights[3].OuterConeAngle = (float)(Math.PI / 4);
                                                        m_Device.Lights[3].Enabled = true;
                                */
                                // Add some ambient light.
                                m_Device.RenderState.Ambient = Color.FromArgb(255, 20, 20, 20);
                        }
                        catch (Exception ex)
                        {
                                textBox1.Text = ex.ToString();
                        }
                }

                private void FillMode_CheckedChanged(object sender, EventArgs e)
                {
                        if (m_Device == null) return;

                        m_Device.RenderState.FillMode = FillMode.Solid;
                }

                // Turn this light on or off.
                private void chkLight_CheckedChanged(object sender, EventArgs e)
                {
                        if (m_Device == null) return;

                        Control ctl = (Control)sender;
                        int index = Convert.ToInt32(ctl.Tag);
                        m_Device.Lights[index].Enabled = !m_Device.Lights[index].Enabled;
                        m_Device.Lights.Reset();
                        Render();
                }
        }
}
Thanks in advance!
JBtje
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
app written on xp crashes on vista valter C# Programming 0 08-28-2010 03:11 AM
how to make window to not appear on taskbar vidhya Win32 Programming 4 06-06-2007 01:21 PM

 

Advertisement

CFanatic Search
Custom Search

Advertisement

All times are GMT -5. The time now is 04:55 PM.



Powered by vBulletin® Version 3.7.4
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO ©2010, Crawlability, Inc.
Cfanatic.com is a premier member of the IDG TechNetwork. For advertising opportunities contact here
Get Paid for Working on Projects Matching Your Expertise at Go4Expert's Jobs Board