public ParticleEditor()
        {
            InitializeComponent();

            SGP.ManagedDirect3D d3d = SGP.ManagedDirect3D.Instance;
            d3d.InitManagedDirect3D(panel1);
            SGP.ManagedTextureManager mtm = SGP.ManagedTextureManager.Instance;
            mtm.InitManagedTextureManager(d3d.Device, d3d.Sprite);

            XElement xRoot;
            if (File.Exists("Config.xml"))
            {
                xRoot = XElement.Load("Config.xml");

                XAttribute xPath = xRoot.Attribute("Path");
                filepath = xPath.Value;
            }

            if (filepath == null)
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                fbd.ShowDialog();
                filepath = fbd.SelectedPath;
            }

            Alive = true;
            emitter.Circlejpg = tm.LoadTexture("../../circle.png", Color.FromArgb(255, 255, 255, 255).ToArgb());
            Vector2D pos = new Vector2D();
            pos.Init(230, 270);
            emitter.Pos = pos;
            emitter.Type = Shape.POINT;
            cbShape.SelectedIndex = 0;
        }
        private void btPreview_Click(object sender, EventArgs e)
        {
            int R = int.Parse(btColorStart.BackColor.R.ToString());
            int G = int.Parse(btColorStart.BackColor.G.ToString());
            int B = int.Parse(btColorStart.BackColor.B.ToString());
            emitter.ColorStart = Color.FromArgb(int.Parse(nudStartAlpha.Value.ToString()), R, G, B);

            R = int.Parse(btColorEnd.BackColor.R.ToString());
            G = int.Parse(btColorEnd.BackColor.G.ToString());
            B = int.Parse(btColorEnd.BackColor.B.ToString());
            emitter.ColorEnd = Color.FromArgb(int.Parse(nudEndAlpha.Value.ToString()), R, G, B);

            emitter.ParticleCount = (int)nudParticleNum.Value;
            float pi = float.Parse(Math.PI.ToString());
            double rad = Math.PI / 180;
            double r = (double)nudRotStart.Value * rad;
            emitter.RotationStart = float.Parse(r.ToString());
            r = (double)nudRotEnd.Value * rad;
            emitter.RotationEnd = float.Parse(r.ToString());

            emitter.ScaleStart = float.Parse(nudScaleStart.Value.ToString());
            emitter.ScaleEnd = float.Parse(nudScaleEnd.Value.ToString());
            emitter.SpawnMax = float.Parse(nudSpawnMax.Value.ToString());
            emitter.SpawnMin = float.Parse(nudSpawnMin.Value.ToString());
            emitter.LifeMax = float.Parse(nudLifeMax.Value.ToString());
            emitter.LifeMin = float.Parse(nudLifeMin.Value.ToString());

            Vector2D tmp = new Vector2D();
            tmp.Init((float)nudVelMaxStartX.Value, (float)nudVelMaxStartY.Value);
            emitter.VelStartMax = tmp;
            tmp.Init((float)nudVelMaxEndX.Value, (float)nudVelMaxEndY.Value);
            emitter.VelEndMax = tmp;
            tmp.Init((float)nudVelMinStartX.Value, (float)nudVelMinStartY.Value);
            emitter.VelStartMin = tmp;
            tmp.Init((float)nudVelMinEndX.Value, (float)nudVelMinEndY.Value);
            emitter.VelEndMin = tmp;

            emitter.Looping = cbLooping.Checked;
            int i = cbShape.SelectedIndex;

            if (i == 0)
                emitter.Type = Shape.POINT;
            else if( i == 1 )
                emitter.Type = Shape.CIRCLE;
            else if( i == 2 )
                emitter.Type = Shape.SQUARE;
            else if( i == 3 )
                emitter.Type = Shape.LINE;

            Vector2D pos = new Vector2D();
            pos.Init(230, 270);
            emitter.Pos = pos;

            if (emitter.Imgpath == null)
            {
                string s = "No Image Selected";
                DialogResult mb = MessageBox.Show(s);
                return;
            }

            emitter.InitParticle();
        }
        public void Update(float fElapsedTime)
        {
            if (spawnRate < spawnTimer)
            {
                if( particles.Count > numSpawned  && particleCount != 0)
                    numSpawned++;
                spawnTimer = 0;
                Random r = new Random();
                float l = (float)r.Next((int)(spawnMin * 100), (int)(spawnMax * 100));
                spawnRate = l / 100.0f;
            }

            spawnTimer += fElapsedTime;

            for (int i = 0; i < numSpawned; i++)
            {
                particles[i].Update(fElapsedTime);

                if (particles[i].CurLife >= particles[i].MaxLife)
                {
                    particles.Remove(particles[i]);

                    if (particles.Count == 0)
                    {
                        if (looping == true)
                            InitParticle();
                        else
                            alive = false;
                    }

                    i--;
                    numSpawned--;
                    continue;
                }

                float time = particles[i].MaxLife;

                // Color changing over time
                Color oldColor = particles[i].Color;
                float dtA, dtR, dtG, dtB;

                dtA = (float)((colorEnd.A - colorStart.A) / time);
                dtR = (float)((colorEnd.R - colorStart.R) / time);
                dtG = (float)((colorEnd.G - colorStart.G) / time);
                dtB = (float)((colorEnd.B - colorStart.B) / time);

                dtA *= fElapsedTime;
                dtR *= fElapsedTime;
                dtG *= fElapsedTime;
                dtB *= fElapsedTime;

                particles[i].myColor.A += dtA / 255;
                particles[i].myColor.R += dtR / 255;
                particles[i].myColor.G += dtG / 255;
                particles[i].myColor.B += dtB / 255;

                float a = particles[i].myColor.A;
                float r = particles[i].myColor.R;
                float g = Math.Abs(particles[i].myColor.G);
                float b = Math.Abs(particles[i].myColor.B);

                Color newColor = Color.FromArgb((int)(a * 255), (int)(Math.Abs(r) * 255), (int)(g * 255), (int)(b* 255));
                particles[i].Color = newColor;

                // Rotation changing over time
                float oldRot = particles[i].Rotation;
                float dtRot = (rotationEnd - rotationStart) / time;
                dtRot *= fElapsedTime;
                float newRot = dtRot + oldRot;
                particles[i].Rotation = newRot;

                // Scale changing over time
                float oldScale = particles[i].Scale;
                float dtScale = (scaleEnd - scaleStart) / time;
                dtScale *= fElapsedTime;
                float newScale = dtScale + oldScale;
                particles[i].Scale = newScale;

                // Velocity changing over time
                Vector2D oldVel = particles[i].CurVelocity;
                float dtX = (particles[i].VelocityEnd.x - particles[i].VelocityStart.x) / time;
                float dtY = (particles[i].VelocityEnd.y - particles[i].VelocityStart.y) / time;

                dtX = dtX * fElapsedTime;
                dtY = dtY * fElapsedTime;

                Vector2D newVel = new Vector2D();
                newVel.Init(dtX + oldVel.x, dtY + oldVel.y);
                particles[i].CurVelocity = newVel;
            }
        }