private void ReadChunkData(ref CubeChunk chunk, int dataOffset)
        {
            var stream = File.OpenRead(FilePath);

            stream.Seek((int)dataOffset, SeekOrigin.Begin);

            using (var reader = new BinaryReader(stream))
            {
                for (int i = 0; i < CubeChunk.TotalSize; i++)
                {
                    int r = reader.Read(),
                        g = reader.Read(),
                        b = reader.Read();

                    if (Convert.ToChar(r) == 'N')
                    {
                        chunk[i] = null;
                    }
                    else
                    {
                        chunk[i] = new CubeColor(r, g, b);
                    }
                }
            }
        }
Beispiel #2
0
 private void TrySetChunk(CubeChunk chunk)
 {
     if (!chunk.IsEmpty() ||          // optimized, if we try to set an empty chunk it will not really write it
         Handler.ChunkExistsAt(chunk.Coordinates))                    // if the chunk already exists, set it anyway because it would not be overwrote
     {
         Handler.SetChunk(chunk);
     }
 }
        private void AddChunk(CubeChunk chunk)
        {
            var stream = File.Open(FilePath, FileMode.Append);

            using (var writer = new BinaryWriter(stream))
            {
                WriteChunkCoordinates(chunk.Coordinates, writer);
                WriteChunkData(chunk, writer);
            }
        }
        private void ReplaceChunk(CubeChunk chunk, int offset)
        {
            var stream = File.Open(FilePath, FileMode.Open);

            stream.Seek(offset, SeekOrigin.Begin);

            using (var writer = new BinaryWriter(stream))
            {
                WriteChunkData(chunk, writer);
            }
        }
Beispiel #5
0
        private void DrawChunk(CubeChunk chunk)
        {
            Vector3 position = Nocubeless.CubeWorld.GetGraphicsCubePosition(chunk.Coordinates);
            float   gaps     = 1 / Nocubeless.CubeWorld.GetGraphicsCubeRatio(); // DESIGN: don't do 1 / x

            EffectMatrices effectMatrices =
                new EffectMatrices(Nocubeless.Camera.GetProjection(),
                                   Nocubeless.Camera.GetView(),
                                   Matrix.Identity);

            chunkDrawer.Draw(ref chunk, position, gaps, effectMatrices);
        }
        public void SetChunk(CubeChunk chunk)
        {
            for (int i = 0; i < chunks.Count; i++)
            {
                if (chunks[i].Coordinates.Equals(chunk.Coordinates))
                {
                    chunks.RemoveAt(i);
                }
            }

            chunks.Add(chunk);
        }
        public CubeChunk GetChunkAt(CubeCoordinates coordinates)
        {
            var dataOffset = GetChunkDataOffset(coordinates);

            if (dataOffset == -1)
            {
                return(null);
            }
            else
            {
                var readChunk = new CubeChunk(coordinates);
                ReadChunkData(ref readChunk, (int)dataOffset);
                return(readChunk);
            }
        }
 private static void WriteChunkData(CubeChunk chunk, BinaryWriter writer)
 {
     for (int i = 0; i < CubeChunk.TotalSize; i++)
     {
         if (chunk[i] != null)
         {
             writer.Write((byte)chunk[i].Red);
             writer.Write((byte)chunk[i].Green);
             writer.Write((byte)chunk[i].Blue);
         }
         else
         {
             writer.Write("NNN".ToCharArray());
         }
     }
 }
        public void SetChunk(CubeChunk chunk)
        {
            if (chunk == null)
            {
                throw new NullReferenceException();
            }

            var dataOffset = GetChunkDataOffset(chunk.Coordinates);

            if (dataOffset == -1) // if the file doesn't contain a chunk at these coordinates, add it
            {
                AddChunk(chunk);
            }
            else // else, replace at the offset in the file
            {
                ReplaceChunk(chunk, (int)dataOffset);
            }
        }
Beispiel #10
0
        public void Draw(ref CubeChunk chunk, Vector3 position, float gap, EffectMatrices effectMatrices) // TO-OPTIMIZE
        {
            cubeEffect.View       = effectMatrices.View;
            cubeEffect.Projection = effectMatrices.Projection;

            Game.GraphicsDevice.SetVertexBuffer(cubeMeshPart.VertexBuffer);
            Game.GraphicsDevice.Indices = cubeMeshPart.IndexBuffer;

            for (int x = 0; x < CubeChunk.Size; x++)
            {
                for (int y = 0; y < CubeChunk.Size; y++)
                {
                    for (int z = 0; z < CubeChunk.Size; z++)
                    {
                        if (chunk[x + (y * CubeChunk.Size) + (z * CubeChunk.Size * CubeChunk.Size)] == null)
                        {
                            continue;
                        }

                        Vector3 cubePosition = new Vector3(position.X + (x * gap), position.Y + (y * gap), position.Z + (z * gap));

                        Matrix translation = Matrix.CreateTranslation(cubePosition);
                        Matrix world       = cubeScale * translation * effectMatrices.World;

                        cubeEffect.World = world;
                        cubeEffect.Alpha = 1; // actually, it's always full alpha.

                        cubeEffect.Color = chunk[x + (y * CubeChunk.Size) + (z * CubeChunk.Size * CubeChunk.Size)].ToVector3();

                        foreach (EffectPass pass in cubeEffect.CurrentTechnique.Passes)
                        {
                            pass.Apply();
                            Game.GraphicsDevice.DrawIndexedPrimitives(
                                PrimitiveType.TriangleList,
                                0,
                                cubeMeshPart.StartIndex,
                                cubeMeshPart.PrimitiveCount);
                        }
                    }
                }
            }
        }
        public CubeChunk GetChunkAt(CubeCoordinates coordinates)
        {
            var gotChunk = (from chunk in chunks
                            where chunk.Coordinates.Equals(coordinates)
                            select chunk).FirstOrDefault();

            if (gotChunk != null) // shallow copy
            {
                var newChunk = new CubeChunk(gotChunk.Coordinates);
                for (int i = 0; i < CubeChunk.TotalSize; i++)
                {
                    newChunk[i] = gotChunk[i];
                }
                return(newChunk);
            }
            else
            {
                return(null);
            }
        }
Beispiel #12
0
 public void UnloadChunk(CubeChunk chunk)
 {
     TrySetChunk(chunk);
     LoadedChunks.Remove(chunk);
 }