public static void Load(ref VoxelSprite vs, GraphicsDevice gd)
        {
            OpenFileDialog sfd = new OpenFileDialog();
            sfd.AddExtension = true;
            sfd.DefaultExt = ".png";
            sfd.Filter = "Tilesheet|*.png";
            DialogResult dr = sfd.ShowDialog();

            if (string.IsNullOrEmpty(sfd.FileName) || dr != DialogResult.OK) return;

            Texture2D inTex;

            try
            {
                using (Stream fs = sfd.OpenFile())
                {
                    inTex = Texture2D.FromStream(gd, fs);
                }
            }
            catch (Exception ex)
            {
                return;
            }

            vs.AnimChunks.Clear();

            Color[] colData = new Color[inTex.Width * inTex.Height];
            inTex.GetData(colData);

            int xTiles = inTex.Width / vs.X_SIZE;
            int yTiles = inTex.Height / vs.Y_SIZE;

            int depthIntensity = 1;

            for (int yt = 0; yt < yTiles; yt++)
            {
                for (int xt = 0; xt < xTiles; xt++)
                {

                    vs.AddFrame(false);
                    for (int vx = 0; vx < vs.X_SIZE; vx++)
                    {
                        for (int vy = 0; vy < vs.Y_SIZE; vy++)
                        {
                            Color col = colData[((vy + (yt * vs.Y_SIZE)) * inTex.Width) + (vx + (xt*vs.X_SIZE))];
                            if (col.A > 0)
                            {
                                //int depth = depthIntensity - (int)(((float)depthIntensity / (255f + 255f + 255f)) * ((float)col.R + (float)col.G + (float)col.B));
                                int depth = 25;
                                for (int vz = vs.Z_SIZE-1; vz>(depth-3); vz--)
                                    vs.AnimChunks[vs.AnimChunks.Count-1].SetVoxel(vx, vz, vs.Y_SIZE-1-vy, true, new Color(col.R, col.G, col.B));
                            }
                        }
                    }
                    vs.AnimChunks[vs.AnimChunks.Count - 1].UpdateMesh();
                }
            }
        }
        public static void Save(VoxelSprite sprite, ref Color[] swatches)
        {
            //StringBuilder sb = new StringBuilder();

            //using (StringWriter str = new StringWriter(sb))
            //{

            SaveFileDialog sfd = new SaveFileDialog();

            sfd.AddExtension = true;
            sfd.DefaultExt   = ".vxl";
            sfd.Filter       = "Voxel Sprite|*.vxs";
            DialogResult dr = sfd.ShowDialog();

            if (string.IsNullOrEmpty(sfd.FileName) || dr != DialogResult.OK)
            {
                return;
            }


            using (FileStream str = new FileStream(sfd.FileName, FileMode.Create))
            {
                //str.Write(gameWorld.X_CHUNKS + "," + gameWorld.Y_CHUNKS + "," + gameWorld.Z_CHUNKS + "\n");
                using (GZipStream gzstr = new GZipStream(str, CompressionMode.Compress))
                {
                    gzstr.WriteByte(Convert.ToByte(sprite.X_SIZE));
                    gzstr.WriteByte(Convert.ToByte(sprite.Y_SIZE));
                    gzstr.WriteByte(Convert.ToByte(sprite.Z_SIZE));
                    gzstr.WriteByte(Convert.ToByte(sprite.AnimChunks.Count));

                    for (int i = 0; i < 10; i++)
                    {
                        gzstr.WriteByte(swatches[i].R);
                        gzstr.WriteByte(swatches[i].G);
                        gzstr.WriteByte(swatches[i].B);
                    }

                    foreach (AnimChunk c in sprite.AnimChunks)
                    {
                        //str.Write("C\n");

                        //Chunk c = gameWorld.Chunks[x, y, z];
                        for (int vx = 0; vx < sprite.X_SIZE; vx++)
                        {
                            for (int vy = 0; vy < sprite.Y_SIZE; vy++)
                            {
                                for (int vz = 0; vz < sprite.Z_SIZE; vz++)
                                {
                                    if (!c.Voxels[vx, vy, vz].Active)
                                    {
                                        continue;
                                    }

                                    //string vox = vx + "," + vy + "," + vz + ",";
                                    //vox += ((int)c.Voxels[vx, vy, vz].Type);
                                    //str.Write(vox + "\n");

                                    gzstr.WriteByte(Convert.ToByte(vx));
                                    gzstr.WriteByte(Convert.ToByte(vy));
                                    gzstr.WriteByte(Convert.ToByte(vz));
                                    gzstr.WriteByte(c.Voxels[vx, vy, vz].Color.R);
                                    gzstr.WriteByte(c.Voxels[vx, vy, vz].Color.G);
                                    gzstr.WriteByte(c.Voxels[vx, vy, vz].Color.B);
                                }
                            }
                        }
                        gzstr.WriteByte(Convert.ToByte('c'));
                    }
                    //str.Flush();
                }
            }
            //}

            //using (StreamWriter fs = new StreamWriter(fn))
            //{
            //    fs.Write(Compress(sb.ToString()));
            //    fs.Flush();
            //}

            //sb.Clear();
            //sb = null;

            GC.Collect();
        }
        public static void Load(ref VoxelSprite sprite, GraphicsDevice gd, ref Color[] swatches)
        {
            //string fileString = "";

            //using (StreamReader fs = new StreamReader(fn))
            //{
            //    fileString = fs.ReadToEnd();
            //}

            //fileString = Decompress(fileString);

            //string[] fileSplit = fileString.Split('\n');

            //int cl = 0;

            //string line;
            //string[] split;

            //line = fileSplit[0];
            //split = line.Split(',');

            //cl+=2;
            OpenFileDialog sfd = new OpenFileDialog();

            sfd.AddExtension = true;
            sfd.DefaultExt   = ".vxs";
            sfd.Filter       = "Voxel Sprite|*.vxs";
            DialogResult dr = sfd.ShowDialog();

            if (string.IsNullOrEmpty(sfd.FileName) || dr != DialogResult.OK)
            {
                return;
            }


            byte[] buffer;

            using (FileStream gstr = new FileStream(sfd.FileName, FileMode.Open))
            {
                byte[] lb = new byte[4];
                gstr.Position = gstr.Length - 4;
                gstr.Read(lb, 0, 4);
                int msgLength = BitConverter.ToInt32(lb, 0);

                buffer = new byte[msgLength];

                gstr.Position = 0;

                using (GZipStream str = new GZipStream(gstr, CompressionMode.Decompress))
                {
                    str.Read(buffer, 0, msgLength);
                }
            }

            int pos = 0;

            int xs     = buffer[0];
            int ys     = buffer[1];
            int zs     = buffer[2];
            int frames = buffer[3];

            sprite = new VoxelSprite(xs, ys, zs, gd);
            sprite.AnimChunks.Clear();
            sprite.ChunkRTs.Clear();

            pos = 4;

            for (int i = 0; i < 10; i++)
            {
                swatches[i] = new Color(buffer[pos], buffer[pos + 1], buffer[pos + 2]);
                pos        += 3;
            }


            for (int frame = 0; frame < frames; frame++)
            {
                sprite.AddFrame(false);

                AnimChunk c = sprite.AnimChunks[frame];

                while (pos < buffer.Length)
                {
                    if (Convert.ToChar(buffer[pos]) != 'c')
                    {
                        //str.Seek(-1, SeekOrigin.Current);
                        //str.Read(ba, 0, 10);
                        int   vx  = buffer[pos];
                        int   vy  = buffer[pos + 1];
                        int   vz  = buffer[pos + 2];
                        Color top = new Color(buffer[pos + 3], buffer[pos + 4], buffer[pos + 5]);

                        c.SetVoxel(vx, vy, vz, true, top);
                        pos += 6;
                    }
                    else
                    {
                        pos++;
                        break;
                    }
                }

                c.UpdateMesh();
            }

            GC.Collect();
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            AnimChunk currentChunk = sprite.AnimChunks[sprite.CurrentFrame];

            KeyboardState cks = Keyboard.GetState();
            MouseState cms = Mouse.GetState();
            Vector2 mousePos = new Vector2(cms.X, cms.Y);
            Vector2 mousedelta = mousePos - new Vector2(lms.X, lms.Y);
            int wheelDelta = cms.ScrollWheelValue - lms.ScrollWheelValue;

            Vector3 moveVector = new Vector3(0, 0, 0);
            if (cks.IsKeyDown(Keys.Up) && !lks.IsKeyDown(Keys.Up))
            {
                moveVector += new Vector3(0, -1, 0);

            }
            if (cks.IsKeyDown(Keys.Down) && !lks.IsKeyDown(Keys.Down))
            {
                moveVector += new Vector3(0, 1, 0);
            }
            if (cks.IsKeyDown(Keys.Right) && !lks.IsKeyDown(Keys.Right))
            {
                moveVector += new Vector3(1, 0, 0);
            }
            if (cks.IsKeyDown(Keys.Left) && !lks.IsKeyDown(Keys.Left))
            {
                moveVector += new Vector3(-1, 0, 0);
            }
            if (cks.IsKeyDown(Keys.PageDown) && !lks.IsKeyDown(Keys.PageDown))
            {
                moveVector += new Vector3(0, 0, -1);
            }
            if (cks.IsKeyDown(Keys.PageUp) && !lks.IsKeyDown(Keys.PageUp))
            {
                moveVector += new Vector3(0, 0, 1);
            }
            if (cks.IsKeyDown(Keys.LeftShift)) cursor.ChangeSize(moveVector, currentChunk.X_SIZE);
            if(!cks.IsKeyDown(Keys.LeftShift)) cursor.Position += moveVector;

            cursor.Position = Vector3.Clamp(cursor.Position, Vector3.Zero, (Vector3.One * (currentChunk.X_SIZE)) - (cursor.Size));

            if (cks.IsKeyDown(Keys.Space) && !lks.IsKeyDown(Keys.Space))
            {
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                            currentChunk.SetVoxel(x, y, z, true, selectedColor);
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.Tab) && !lks.IsKeyDown(Keys.Tab))
            {
                for(int x=(int)cursor.Position.X;x<(int)cursor.Position.X + (int)cursor.Size.X;x++)
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                            currentChunk.SetVoxel(x, y, z, false, currentChunk.Voxels[x, y, z].Color);
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.LeftControl) && (cks.IsKeyDown(Keys.C) && !lks.IsKeyDown(Keys.C)))
            {
                ClearClipboard();
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                            clipBoard.SetVoxel(x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z, currentChunk.Voxels[x,y,z].Active, currentChunk.Voxels[x,y,z].Color);
            }
            if (cks.IsKeyDown(Keys.LeftControl) && (cks.IsKeyDown(Keys.X) && !lks.IsKeyDown(Keys.X)))
            {
                ClearClipboard();
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                            clipBoard.SetVoxel(x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z, currentChunk.Voxels[x, y, z].Active, currentChunk.Voxels[x, y, z].Color);

                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                            currentChunk.SetVoxel(x, y, z, false, currentChunk.Voxels[x, y, z].Color);
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.LeftControl) && (cks.IsKeyDown(Keys.V) && !lks.IsKeyDown(Keys.V)))
            {
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                            currentChunk.SetVoxel(x,y,z, clipBoard.Voxels[x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z].Active, clipBoard.Voxels[x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z].Color);
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.NumPad4) && !lks.IsKeyDown(Keys.NumPad4))
            {
                sprite.CurrentFrame--;
                if (sprite.CurrentFrame < 0) sprite.CurrentFrame = sprite.AnimChunks.Count - 1;

            }
            if (cks.IsKeyDown(Keys.NumPad6) && !lks.IsKeyDown(Keys.NumPad6))
            {
                sprite.CurrentFrame++;
                if (sprite.CurrentFrame >= sprite.AnimChunks.Count) sprite.CurrentFrame = 0;
            }

            if (cks.IsKeyDown(Keys.NumPad5) && !lks.IsKeyDown(Keys.NumPad5))
            {
                playingAnim = !playingAnim;
                animTime = 0;

            }

            if (cks.IsKeyDown(Keys.Insert) && !lks.IsKeyDown(Keys.Insert)) sprite.InsertFrame();
            if (cks.IsKeyDown(Keys.Home) && !lks.IsKeyDown(Keys.Home)) sprite.CopyFrame();
            if (cks.IsKeyDown(Keys.End) && !lks.IsKeyDown(Keys.End)) sprite.AddFrame(true);
            if (cks.IsKeyDown(Keys.Delete) && !lks.IsKeyDown(Keys.Delete)) sprite.DeleteFrame();

            if (cks.IsKeyDown(Keys.F2) && !lks.IsKeyDown(Keys.F2)) LoadSave.Save(sprite, ref swatches);
            if (cks.IsKeyDown(Keys.F5) && !lks.IsKeyDown(Keys.F5)) LoadSave.Load(ref sprite, GraphicsDevice, ref swatches);

            if (cks.IsKeyDown(Keys.F8) && !lks.IsKeyDown(Keys.F8)) TileSheetConverter.Load(ref sprite, GraphicsDevice);

            if (cks.IsKeyDown(Keys.D1)) { selectedSwatch = 0; selectedColor = swatches[0]; }
            if (cks.IsKeyDown(Keys.D2)) { selectedSwatch = 1; selectedColor = swatches[1]; }
            if (cks.IsKeyDown(Keys.D3)) { selectedSwatch = 2; selectedColor = swatches[2]; }
            if (cks.IsKeyDown(Keys.D4)) { selectedSwatch = 3; selectedColor = swatches[3]; }
            if (cks.IsKeyDown(Keys.D5)) { selectedSwatch = 4; selectedColor = swatches[4]; }
            if (cks.IsKeyDown(Keys.D6)) { selectedSwatch = 5; selectedColor = swatches[5]; }
            if (cks.IsKeyDown(Keys.D7)) { selectedSwatch = 6; selectedColor = swatches[6]; }
            if (cks.IsKeyDown(Keys.D8)) { selectedSwatch = 7; selectedColor = swatches[7]; }
            if (cks.IsKeyDown(Keys.D9)) { selectedSwatch = 8; selectedColor = swatches[8]; }
            if (cks.IsKeyDown(Keys.D0)) { selectedSwatch = 9; selectedColor = swatches[9]; }

            if (cks.IsKeyDown(Keys.F12) && !lks.IsKeyDown(Keys.F12))
            {
                if(sprite.X_SIZE<32)
                    sprite = new VoxelSprite(sprite.X_SIZE + 1, sprite.Y_SIZE + 1, sprite.Z_SIZE + 1, GraphicsDevice);
            }
            if (cks.IsKeyDown(Keys.F11) && !lks.IsKeyDown(Keys.F11))
            {
                if (sprite.X_SIZE > 3)
                    sprite = new VoxelSprite(sprite.X_SIZE - 1, sprite.Y_SIZE - 1, sprite.Z_SIZE - 1, GraphicsDevice);
            }

            if (wheelDelta != 0)
            {
                if (wheelDelta > 0)
                {
                    viewZoom += 1f;
                }
                else
                {
                    viewZoom -= 1f;
                }
            }

            if (cms.LeftButton == ButtonState.Pressed)
            {
                Point mp = Helper.VtoP(mousePos);

                if (viewRect.Contains(new Point((int)mousePos.X, (int)mousePos.Y)))
                {
                    viewPitch -= mousedelta.Y * 0.01f;
                    viewYaw += mousedelta.X * 0.01f;
                }

                Color prevColor = selectedColor;
                if (redRect.Contains(mp))
                {
                    selectedColor.R = (byte)MathHelper.Clamp(((256f / 400f) * ((mp.X - (float)redRect.Left))), 0f, 255f);
                    swatches[selectedSwatch] = selectedColor;
                    if (cks.IsKeyDown(Keys.LeftShift)) sprite.ReplaceColor(prevColor, selectedColor);
                }
                if (greenRect.Contains(mp))
                {
                    selectedColor.G = (byte)MathHelper.Clamp(((256f / 400f) * ((mp.X - (float)greenRect.Left))), 0f, 255f);
                    swatches[selectedSwatch] = selectedColor;
                    if (cks.IsKeyDown(Keys.LeftShift)) sprite.ReplaceColor(prevColor, selectedColor);

                }
                if (blueRect.Contains(mp))
                {
                    selectedColor.B = (byte)MathHelper.Clamp(((256f / 400f) * ((mp.X - (float)blueRect.Left))), 0f, 255f);
                    swatches[selectedSwatch] = selectedColor;
                    if (cks.IsKeyDown(Keys.LeftShift)) sprite.ReplaceColor(prevColor, selectedColor);

                }

                if (lms.LeftButton != ButtonState.Pressed)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        if (swatchRects[i].Contains(mp))
                        {
                            selectedColor = swatches[i];
                            selectedSwatch = i;
                        }
                    }

                    if (prevFrameRect.Contains(mp))
                    {
                        sprite.CurrentFrame--;
                        if (sprite.CurrentFrame < 0) sprite.CurrentFrame = sprite.AnimChunks.Count-1;

                    }
                    if (nextFrameRect.Contains(mp))
                    {
                        sprite.CurrentFrame++;
                        if (sprite.CurrentFrame >= sprite.AnimChunks.Count) sprite.CurrentFrame = 0;
                    }

                }
            }

            cursor.Update(gameTime, selectedColor);

            lks = cks;
            lms = cms;

            if (playingAnim)
            {
                animTime += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (animTime > 100)
                {
                    animTime = 0;
                    sprite.CurrentFrame++;
                    if (sprite.CurrentFrame > sprite.AnimChunks.Count - 1) sprite.CurrentFrame = 0;
                }
            }

            drawEffect.World = worldMatrix * Matrix.CreateRotationY(viewYaw) * Matrix.CreateRotationX(viewPitch);
            drawEffect.View = Matrix.CreateLookAt(new Vector3(0, 0, viewZoom), new Vector3(0, 0, 0), Vector3.Up);
            cursorEffect.World = Matrix.CreateTranslation((-((Vector3.One * Voxel.SIZE) * (currentChunk.X_SIZE / 2f)) + (cursor.Position * Voxel.SIZE))) * drawEffect.World;
            cursorEffect.View = drawEffect.View;

            base.Update(gameTime);
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            font = Content.Load<SpriteFont>("hudfont");

            LoadTex("arrow");
            LoadTex("colors");
            LoadTex("triangles");
            LoadTex("square");

            paintPos = new Vector2(GraphicsDevice.Viewport.Width - 215, GraphicsDevice.Viewport.Height-100);

            viewRT = new RenderTarget2D(GraphicsDevice, 800,600, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
            viewRect = new Rectangle(0, GraphicsDevice.Viewport.Height - 600, 800, 600);

            worldMatrix = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Down);
            viewMatrix = Matrix.CreateLookAt(new Vector3(0, 0, -10), new Vector3(0, 0, 0), Vector3.Up);
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 800f/600f, 0.001f, 100f);

            sprite = new VoxelSprite(16, 16,16, GraphicsDevice);

            drawEffect = new BasicEffect(GraphicsDevice)
            {
                World = worldMatrix,
                View = viewMatrix,
                Projection = projectionMatrix,
                VertexColorEnabled = true,
                //LightingEnabled = true
            };
            drawEffect.EnableDefaultLighting();

            cursorEffect = new BasicEffect(GraphicsDevice)
            {
                World = worldMatrix,
                View = viewMatrix,
                Projection = projectionMatrix,
                VertexColorEnabled = true
            };

            redRect = new Rectangle((int)paintPos.X - (texList["colors"].Width / 2), (int)paintPos.Y - (texList["colors"].Height / 2), texList["colors"].Width, 40);
            greenRect = redRect;
            greenRect.Offset(0, 60);
            blueRect = greenRect;
            blueRect.Offset(0, 60);

            swatches[0]= new Color(255, 255, 255);
            swatches[1]= new Color(255, 0, 0);
            swatches[2]= new Color(0, 255, 0);
            swatches[3]= new Color(0, 0, 255);
            swatches[4]= new Color(255, 255, 0);
            swatches[5]= new Color(255, 0, 255);
            swatches[6]= new Color(0, 255, 255);
            swatches[7]= new Color(81, 81, 81);
            swatches[8]= new Color(183, 183, 183);
            swatches[9]= new Color(0, 0, 0);

            Rectangle swRect = new Rectangle(GraphicsDevice.Viewport.Width - 420, 170, 410, 30);
            for (int i = 0; i < 10; i++)
            {
                swatchRects[i] = swRect;
                swRect.Offset(0, 40);
            }

            nextFrameRect = new Rectangle(GraphicsDevice.Viewport.Width - 75, 25, 50, 100);
            prevFrameRect = new Rectangle(25, 25, 50, 100);

            selectedColor = swatches[0];
        }
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            font = Content.Load <SpriteFont>("hudfont");

            LoadTex("arrow");
            LoadTex("colors");
            LoadTex("triangles");
            LoadTex("square");

            paintPos = new Vector2(GraphicsDevice.Viewport.Width - 215, GraphicsDevice.Viewport.Height - 100);

            viewRT   = new RenderTarget2D(GraphicsDevice, 800, 600, false, SurfaceFormat.Color, DepthFormat.Depth24Stencil8);
            viewRect = new Rectangle(0, GraphicsDevice.Viewport.Height - 600, 800, 600);

            worldMatrix      = Matrix.CreateWorld(Vector3.Zero, Vector3.Forward, Vector3.Down);
            viewMatrix       = Matrix.CreateLookAt(new Vector3(0, 0, -10), new Vector3(0, 0, 0), Vector3.Up);
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4, 800f / 600f, 0.001f, 100f);

            sprite = new VoxelSprite(16, 16, 16, GraphicsDevice);

            drawEffect = new BasicEffect(GraphicsDevice)
            {
                World              = worldMatrix,
                View               = viewMatrix,
                Projection         = projectionMatrix,
                VertexColorEnabled = true,
                //LightingEnabled = true
            };
            drawEffect.EnableDefaultLighting();

            cursorEffect = new BasicEffect(GraphicsDevice)
            {
                World              = worldMatrix,
                View               = viewMatrix,
                Projection         = projectionMatrix,
                VertexColorEnabled = true
            };

            redRect   = new Rectangle((int)paintPos.X - (texList["colors"].Width / 2), (int)paintPos.Y - (texList["colors"].Height / 2), texList["colors"].Width, 40);
            greenRect = redRect;
            greenRect.Offset(0, 60);
            blueRect = greenRect;
            blueRect.Offset(0, 60);

            swatches[0] = new Color(255, 255, 255);
            swatches[1] = new Color(255, 0, 0);
            swatches[2] = new Color(0, 255, 0);
            swatches[3] = new Color(0, 0, 255);
            swatches[4] = new Color(255, 255, 0);
            swatches[5] = new Color(255, 0, 255);
            swatches[6] = new Color(0, 255, 255);
            swatches[7] = new Color(81, 81, 81);
            swatches[8] = new Color(183, 183, 183);
            swatches[9] = new Color(0, 0, 0);

            Rectangle swRect = new Rectangle(GraphicsDevice.Viewport.Width - 420, 170, 410, 30);

            for (int i = 0; i < 10; i++)
            {
                swatchRects[i] = swRect;
                swRect.Offset(0, 40);
            }

            nextFrameRect = new Rectangle(GraphicsDevice.Viewport.Width - 75, 25, 50, 100);
            prevFrameRect = new Rectangle(25, 25, 50, 100);

            selectedColor = swatches[0];
        }
        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            {
                this.Exit();
            }

            AnimChunk currentChunk = sprite.AnimChunks[sprite.CurrentFrame];

            KeyboardState cks        = Keyboard.GetState();
            MouseState    cms        = Mouse.GetState();
            Vector2       mousePos   = new Vector2(cms.X, cms.Y);
            Vector2       mousedelta = mousePos - new Vector2(lms.X, lms.Y);
            int           wheelDelta = cms.ScrollWheelValue - lms.ScrollWheelValue;

            Vector3 moveVector = new Vector3(0, 0, 0);

            if (cks.IsKeyDown(Keys.Up) && !lks.IsKeyDown(Keys.Up))
            {
                moveVector += new Vector3(0, -1, 0);
            }
            if (cks.IsKeyDown(Keys.Down) && !lks.IsKeyDown(Keys.Down))
            {
                moveVector += new Vector3(0, 1, 0);
            }
            if (cks.IsKeyDown(Keys.Right) && !lks.IsKeyDown(Keys.Right))
            {
                moveVector += new Vector3(1, 0, 0);
            }
            if (cks.IsKeyDown(Keys.Left) && !lks.IsKeyDown(Keys.Left))
            {
                moveVector += new Vector3(-1, 0, 0);
            }
            if (cks.IsKeyDown(Keys.PageDown) && !lks.IsKeyDown(Keys.PageDown))
            {
                moveVector += new Vector3(0, 0, -1);
            }
            if (cks.IsKeyDown(Keys.PageUp) && !lks.IsKeyDown(Keys.PageUp))
            {
                moveVector += new Vector3(0, 0, 1);
            }
            if (cks.IsKeyDown(Keys.LeftShift))
            {
                cursor.ChangeSize(moveVector, currentChunk.X_SIZE);
            }
            if (!cks.IsKeyDown(Keys.LeftShift))
            {
                cursor.Position += moveVector;
            }

            cursor.Position = Vector3.Clamp(cursor.Position, Vector3.Zero, (Vector3.One * (currentChunk.X_SIZE)) - (cursor.Size));

            if (cks.IsKeyDown(Keys.Space) && !lks.IsKeyDown(Keys.Space))
            {
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            currentChunk.SetVoxel(x, y, z, true, selectedColor);
                        }
                    }
                }
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.Tab) && !lks.IsKeyDown(Keys.Tab))
            {
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            currentChunk.SetVoxel(x, y, z, false, currentChunk.Voxels[x, y, z].Color);
                        }
                    }
                }
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.LeftControl) && (cks.IsKeyDown(Keys.C) && !lks.IsKeyDown(Keys.C)))
            {
                ClearClipboard();
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            clipBoard.SetVoxel(x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z, currentChunk.Voxels[x, y, z].Active, currentChunk.Voxels[x, y, z].Color);
                        }
                    }
                }
            }
            if (cks.IsKeyDown(Keys.LeftControl) && (cks.IsKeyDown(Keys.X) && !lks.IsKeyDown(Keys.X)))
            {
                ClearClipboard();
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            clipBoard.SetVoxel(x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z, currentChunk.Voxels[x, y, z].Active, currentChunk.Voxels[x, y, z].Color);
                        }
                    }
                }

                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            currentChunk.SetVoxel(x, y, z, false, currentChunk.Voxels[x, y, z].Color);
                        }
                    }
                }
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.LeftControl) && (cks.IsKeyDown(Keys.V) && !lks.IsKeyDown(Keys.V)))
            {
                for (int x = (int)cursor.Position.X; x < (int)cursor.Position.X + (int)cursor.Size.X; x++)
                {
                    for (int y = (int)cursor.Position.Y; y < (int)cursor.Position.Y + (int)cursor.Size.Y; y++)
                    {
                        for (int z = (int)cursor.Position.Z; z < (int)cursor.Position.Z + (int)cursor.Size.Z; z++)
                        {
                            currentChunk.SetVoxel(x, y, z, clipBoard.Voxels[x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z].Active, clipBoard.Voxels[x - (int)cursor.Position.X, y - (int)cursor.Position.Y, z - (int)cursor.Position.Z].Color);
                        }
                    }
                }
                currentChunk.UpdateMesh();
            }

            if (cks.IsKeyDown(Keys.NumPad4) && !lks.IsKeyDown(Keys.NumPad4))
            {
                sprite.CurrentFrame--;
                if (sprite.CurrentFrame < 0)
                {
                    sprite.CurrentFrame = sprite.AnimChunks.Count - 1;
                }
            }
            if (cks.IsKeyDown(Keys.NumPad6) && !lks.IsKeyDown(Keys.NumPad6))
            {
                sprite.CurrentFrame++;
                if (sprite.CurrentFrame >= sprite.AnimChunks.Count)
                {
                    sprite.CurrentFrame = 0;
                }
            }

            if (cks.IsKeyDown(Keys.NumPad5) && !lks.IsKeyDown(Keys.NumPad5))
            {
                playingAnim = !playingAnim;
                animTime    = 0;
            }

            if (cks.IsKeyDown(Keys.Insert) && !lks.IsKeyDown(Keys.Insert))
            {
                sprite.InsertFrame();
            }
            if (cks.IsKeyDown(Keys.Home) && !lks.IsKeyDown(Keys.Home))
            {
                sprite.CopyFrame();
            }
            if (cks.IsKeyDown(Keys.End) && !lks.IsKeyDown(Keys.End))
            {
                sprite.AddFrame(true);
            }
            if (cks.IsKeyDown(Keys.Delete) && !lks.IsKeyDown(Keys.Delete))
            {
                sprite.DeleteFrame();
            }

            if (cks.IsKeyDown(Keys.F2) && !lks.IsKeyDown(Keys.F2))
            {
                LoadSave.Save(sprite, ref swatches);
            }
            if (cks.IsKeyDown(Keys.F5) && !lks.IsKeyDown(Keys.F5))
            {
                LoadSave.Load(ref sprite, GraphicsDevice, ref swatches);
            }

            if (cks.IsKeyDown(Keys.F8) && !lks.IsKeyDown(Keys.F8))
            {
                TileSheetConverter.Load(ref sprite, GraphicsDevice);
            }


            if (cks.IsKeyDown(Keys.D1))
            {
                selectedSwatch = 0; selectedColor = swatches[0];
            }
            if (cks.IsKeyDown(Keys.D2))
            {
                selectedSwatch = 1; selectedColor = swatches[1];
            }
            if (cks.IsKeyDown(Keys.D3))
            {
                selectedSwatch = 2; selectedColor = swatches[2];
            }
            if (cks.IsKeyDown(Keys.D4))
            {
                selectedSwatch = 3; selectedColor = swatches[3];
            }
            if (cks.IsKeyDown(Keys.D5))
            {
                selectedSwatch = 4; selectedColor = swatches[4];
            }
            if (cks.IsKeyDown(Keys.D6))
            {
                selectedSwatch = 5; selectedColor = swatches[5];
            }
            if (cks.IsKeyDown(Keys.D7))
            {
                selectedSwatch = 6; selectedColor = swatches[6];
            }
            if (cks.IsKeyDown(Keys.D8))
            {
                selectedSwatch = 7; selectedColor = swatches[7];
            }
            if (cks.IsKeyDown(Keys.D9))
            {
                selectedSwatch = 8; selectedColor = swatches[8];
            }
            if (cks.IsKeyDown(Keys.D0))
            {
                selectedSwatch = 9; selectedColor = swatches[9];
            }

            if (cks.IsKeyDown(Keys.F12) && !lks.IsKeyDown(Keys.F12))
            {
                if (sprite.X_SIZE < 32)
                {
                    sprite = new VoxelSprite(sprite.X_SIZE + 1, sprite.Y_SIZE + 1, sprite.Z_SIZE + 1, GraphicsDevice);
                }
            }
            if (cks.IsKeyDown(Keys.F11) && !lks.IsKeyDown(Keys.F11))
            {
                if (sprite.X_SIZE > 3)
                {
                    sprite = new VoxelSprite(sprite.X_SIZE - 1, sprite.Y_SIZE - 1, sprite.Z_SIZE - 1, GraphicsDevice);
                }
            }

            if (wheelDelta != 0)
            {
                if (wheelDelta > 0)
                {
                    viewZoom += 1f;
                }
                else
                {
                    viewZoom -= 1f;
                }
            }


            if (cms.LeftButton == ButtonState.Pressed)
            {
                Point mp = Helper.VtoP(mousePos);

                if (viewRect.Contains(new Point((int)mousePos.X, (int)mousePos.Y)))
                {
                    viewPitch -= mousedelta.Y * 0.01f;
                    viewYaw   += mousedelta.X * 0.01f;
                }

                Color prevColor = selectedColor;
                if (redRect.Contains(mp))
                {
                    selectedColor.R          = (byte)MathHelper.Clamp(((256f / 400f) * ((mp.X - (float)redRect.Left))), 0f, 255f);
                    swatches[selectedSwatch] = selectedColor;
                    if (cks.IsKeyDown(Keys.LeftShift))
                    {
                        sprite.ReplaceColor(prevColor, selectedColor);
                    }
                }
                if (greenRect.Contains(mp))
                {
                    selectedColor.G          = (byte)MathHelper.Clamp(((256f / 400f) * ((mp.X - (float)greenRect.Left))), 0f, 255f);
                    swatches[selectedSwatch] = selectedColor;
                    if (cks.IsKeyDown(Keys.LeftShift))
                    {
                        sprite.ReplaceColor(prevColor, selectedColor);
                    }
                }
                if (blueRect.Contains(mp))
                {
                    selectedColor.B          = (byte)MathHelper.Clamp(((256f / 400f) * ((mp.X - (float)blueRect.Left))), 0f, 255f);
                    swatches[selectedSwatch] = selectedColor;
                    if (cks.IsKeyDown(Keys.LeftShift))
                    {
                        sprite.ReplaceColor(prevColor, selectedColor);
                    }
                }

                if (lms.LeftButton != ButtonState.Pressed)
                {
                    for (int i = 0; i < 10; i++)
                    {
                        if (swatchRects[i].Contains(mp))
                        {
                            selectedColor  = swatches[i];
                            selectedSwatch = i;
                        }
                    }

                    if (prevFrameRect.Contains(mp))
                    {
                        sprite.CurrentFrame--;
                        if (sprite.CurrentFrame < 0)
                        {
                            sprite.CurrentFrame = sprite.AnimChunks.Count - 1;
                        }
                    }
                    if (nextFrameRect.Contains(mp))
                    {
                        sprite.CurrentFrame++;
                        if (sprite.CurrentFrame >= sprite.AnimChunks.Count)
                        {
                            sprite.CurrentFrame = 0;
                        }
                    }
                }
            }

            cursor.Update(gameTime, selectedColor);

            lks = cks;
            lms = cms;

            if (playingAnim)
            {
                animTime += gameTime.ElapsedGameTime.TotalMilliseconds;
                if (animTime > 100)
                {
                    animTime = 0;
                    sprite.CurrentFrame++;
                    if (sprite.CurrentFrame > sprite.AnimChunks.Count - 1)
                    {
                        sprite.CurrentFrame = 0;
                    }
                }
            }

            drawEffect.World   = worldMatrix * Matrix.CreateRotationY(viewYaw) * Matrix.CreateRotationX(viewPitch);
            drawEffect.View    = Matrix.CreateLookAt(new Vector3(0, 0, viewZoom), new Vector3(0, 0, 0), Vector3.Up);
            cursorEffect.World = Matrix.CreateTranslation((-((Vector3.One * Voxel.SIZE) * (currentChunk.X_SIZE / 2f)) + (cursor.Position * Voxel.SIZE))) * drawEffect.World;
            cursorEffect.View  = drawEffect.View;

            base.Update(gameTime);
        }
Esempio n. 8
0
        public static void Load(ref VoxelSprite vs, GraphicsDevice gd)
        {
            OpenFileDialog sfd = new OpenFileDialog();

            sfd.AddExtension = true;
            sfd.DefaultExt   = ".png";
            sfd.Filter       = "Tilesheet|*.png";
            DialogResult dr = sfd.ShowDialog();

            if (string.IsNullOrEmpty(sfd.FileName) || dr != DialogResult.OK)
            {
                return;
            }

            Texture2D inTex;

            try
            {
                using (Stream fs = sfd.OpenFile())
                {
                    inTex = Texture2D.FromStream(gd, fs);
                }
            }
            catch (Exception ex)
            {
                return;
            }

            vs.AnimChunks.Clear();

            Color[] colData = new Color[inTex.Width * inTex.Height];
            inTex.GetData(colData);

            int xTiles = inTex.Width / vs.X_SIZE;
            int yTiles = inTex.Height / vs.Y_SIZE;

            int depthIntensity = 1;

            for (int yt = 0; yt < yTiles; yt++)
            {
                for (int xt = 0; xt < xTiles; xt++)
                {
                    vs.AddFrame(false);
                    for (int vx = 0; vx < vs.X_SIZE; vx++)
                    {
                        for (int vy = 0; vy < vs.Y_SIZE; vy++)
                        {
                            Color col = colData[((vy + (yt * vs.Y_SIZE)) * inTex.Width) + (vx + (xt * vs.X_SIZE))];
                            if (col.A > 0)
                            {
                                //int depth = depthIntensity - (int)(((float)depthIntensity / (255f + 255f + 255f)) * ((float)col.R + (float)col.G + (float)col.B));
                                int depth = 25;
                                for (int vz = vs.Z_SIZE - 1; vz > (depth - 3); vz--)
                                {
                                    vs.AnimChunks[vs.AnimChunks.Count - 1].SetVoxel(vx, vz, vs.Y_SIZE - 1 - vy, true, new Color(col.R, col.G, col.B));
                                }
                            }
                        }
                    }
                    vs.AnimChunks[vs.AnimChunks.Count - 1].UpdateMesh();
                }
            }
        }
Esempio n. 9
0
        public static void Load(ref VoxelSprite sprite, GraphicsDevice gd, ref Color[] swatches)
        {
            //string fileString = "";

            //using (StreamReader fs = new StreamReader(fn))
            //{
            //    fileString = fs.ReadToEnd();
            //}

            //fileString = Decompress(fileString);

            //string[] fileSplit = fileString.Split('\n');

            //int cl = 0;

            //string line;
            //string[] split;

            //line = fileSplit[0];
            //split = line.Split(',');

            //cl+=2;
            OpenFileDialog sfd = new OpenFileDialog();
            sfd.AddExtension = true;
            sfd.DefaultExt = ".vxs";
            sfd.Filter = "Voxel Sprite|*.vxs";
            DialogResult dr = sfd.ShowDialog();

            if (string.IsNullOrEmpty(sfd.FileName) || dr != DialogResult.OK) return;

            byte[] buffer;

            using (FileStream gstr = new FileStream(sfd.FileName, FileMode.Open))
            {
                byte[] lb = new byte[4];
                gstr.Position = gstr.Length - 4;
                gstr.Read(lb, 0, 4);
                int msgLength = BitConverter.ToInt32(lb, 0);

                buffer = new byte[msgLength];

                gstr.Position = 0;

                using (GZipStream str = new GZipStream(gstr, CompressionMode.Decompress))
                {

                    str.Read(buffer, 0, msgLength);
                }
            }

            int pos = 0;

            int xs = buffer[0];
            int ys = buffer[1];
            int zs = buffer[2];
            int frames = buffer[3];
            sprite = new VoxelSprite(xs, ys, zs, gd);
            sprite.AnimChunks.Clear();
            sprite.ChunkRTs.Clear();

            pos = 4;

            for (int i = 0; i < 10; i++)
            {
                swatches[i] = new Color(buffer[pos], buffer[pos+1],buffer[pos+2]);
                pos += 3;
            }

            for(int frame = 0;frame<frames;frame++)
            {
                sprite.AddFrame(false);

                AnimChunk c = sprite.AnimChunks[frame];

                while (pos < buffer.Length)
                {
                    if (Convert.ToChar(buffer[pos]) != 'c')
                    {
                        //str.Seek(-1, SeekOrigin.Current);
                        //str.Read(ba, 0, 10);
                        int vx = buffer[pos];
                        int vy = buffer[pos + 1];
                        int vz = buffer[pos + 2];
                        Color top = new Color(buffer[pos + 3], buffer[pos + 4], buffer[pos + 5]);

                        c.SetVoxel(vx, vy, vz, true, top);
                        pos += 6;

                    }
                    else
                    {
                        pos++;
                        break;
                    }

                }

                c.UpdateMesh();

            }

            GC.Collect();
        }
Esempio n. 10
0
        public static void Save(VoxelSprite sprite, ref Color[] swatches)
        {
            //StringBuilder sb = new StringBuilder();

            //using (StringWriter str = new StringWriter(sb))
            //{

            SaveFileDialog sfd = new SaveFileDialog();
            sfd.AddExtension = true;
            sfd.DefaultExt = ".vxl";
            sfd.Filter = "Voxel Sprite|*.vxs";
            DialogResult dr = sfd.ShowDialog();

            if (string.IsNullOrEmpty(sfd.FileName) || dr != DialogResult.OK) return;

            using (FileStream str = new FileStream(sfd.FileName, FileMode.Create))
            {
                //str.Write(gameWorld.X_CHUNKS + "," + gameWorld.Y_CHUNKS + "," + gameWorld.Z_CHUNKS + "\n");
                using (GZipStream gzstr = new GZipStream(str, CompressionMode.Compress))
                {

                    gzstr.WriteByte(Convert.ToByte(sprite.X_SIZE));
                    gzstr.WriteByte(Convert.ToByte(sprite.Y_SIZE));
                    gzstr.WriteByte(Convert.ToByte(sprite.Z_SIZE));
                    gzstr.WriteByte(Convert.ToByte(sprite.AnimChunks.Count));

                    for (int i = 0; i < 10; i++)
                    {
                        gzstr.WriteByte(swatches[i].R);
                        gzstr.WriteByte(swatches[i].G);
                        gzstr.WriteByte(swatches[i].B);
                    }

                    foreach (AnimChunk c in sprite.AnimChunks)
                    {
                        //str.Write("C\n");

                        //Chunk c = gameWorld.Chunks[x, y, z];
                        for (int vx = 0; vx < sprite.X_SIZE; vx++)
                            for (int vy = 0; vy < sprite.Y_SIZE; vy++)
                                for (int vz = 0; vz < sprite.Z_SIZE; vz++)
                                {
                                    if (!c.Voxels[vx, vy, vz].Active) continue;

                                    //string vox = vx + "," + vy + "," + vz + ",";
                                    //vox += ((int)c.Voxels[vx, vy, vz].Type);
                                    //str.Write(vox + "\n");

                                    gzstr.WriteByte(Convert.ToByte(vx));
                                    gzstr.WriteByte(Convert.ToByte(vy));
                                    gzstr.WriteByte(Convert.ToByte(vz));
                                    gzstr.WriteByte(c.Voxels[vx, vy, vz].Color.R);
                                    gzstr.WriteByte(c.Voxels[vx, vy, vz].Color.G);
                                    gzstr.WriteByte(c.Voxels[vx, vy, vz].Color.B);
                                }
                        gzstr.WriteByte(Convert.ToByte('c'));

                    }
                    //str.Flush();

                }
            }
            //}

            //using (StreamWriter fs = new StreamWriter(fn))
            //{
            //    fs.Write(Compress(sb.ToString()));
            //    fs.Flush();
            //}

            //sb.Clear();
            //sb = null;

            GC.Collect();
        }