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 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);
        }
Esempio n. 4
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. 5
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();
        }