/// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); GraphicsDevice.DepthStencilState = DepthStencilState.Default; //GraphicsDevice.RasterizerState = RasterizerState.CullNone; //GraphicsDevice.BlendState = BlendState.Opaque; GraphicsDevice.SamplerStates[0] = SamplerState.PointClamp; GraphicsDevice.BlendState = BlendState.AlphaBlend; foreach (EffectPass pass in drawEffect.CurrentTechnique.Passes) { pass.Apply(); for (int y = 0; y < gameWorld.Y_CHUNKS; y++) { for (int x = 0; x < gameWorld.X_CHUNKS; x++) { Chunk c = gameWorld.Chunks[x, y, 0]; if (!c.Visible) { continue; } if (c == null || c.VertexArray == null || c.VertexArray.Length == 0) { continue; } if (!gameCamera.boundingFrustum.Intersects(c.boundingSphere.Transform(Matrix.CreateTranslation(gameCamera.Position)))) { continue; } GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, c.VertexArray, 0, c.VertexArray.Length, c.IndexArray, 0, c.VertexArray.Length / 2); } } } foreach (Spawn s in gameWorld.Spawns) { drawEffect.World = Matrix.CreateRotationZ(-(MathHelper.PiOver2 * s.Rotation)) * Matrix.CreateTranslation(s.Position * Voxel.SIZE) * gameCamera.worldMatrix; foreach (EffectPass pass in drawEffect.CurrentTechnique.Passes) { pass.Apply(); AnimChunk c = spawnSprites.AnimChunks[(int)s.Type]; if (c.VertexArray != null) { GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, c.VertexArray, 0, c.VertexArray.Length, c.IndexArray, 0, c.VertexArray.Length / 2); } } } if (cursor.Mode == CursorMode.Prefab) { if (Prefabs.Count > 0) { cursorEffect.Alpha = 0.5f; foreach (EffectPass pass in cursorEffect.CurrentTechnique.Passes) { pass.Apply(); PrefabChunk c = Prefabs[selectedPrefab]; if (c.VertexArray != null) { GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, c.VertexArray, 0, c.VertexArray.Length, c.IndexArray, 0, c.VertexArray.Length / 2); } } } } else if (cursor.Mode == CursorMode.Spawn) { cursorEffect.Alpha = 0.5f; cursorEffect.World = Matrix.CreateRotationZ(-(MathHelper.PiOver2 * spawnRot)) * Matrix.CreateTranslation(cursor.Position * Voxel.SIZE) * gameCamera.worldMatrix; foreach (EffectPass pass in cursorEffect.CurrentTechnique.Passes) { pass.Apply(); AnimChunk c = spawnSprites.AnimChunks[selectedSpawn]; if (c.VertexArray != null) { GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, c.VertexArray, 0, c.VertexArray.Length, c.IndexArray, 0, c.VertexArray.Length / 2); } } } else { cursorEffect.Alpha = 1f; foreach (EffectPass pass in cursorEffect.CurrentTechnique.Passes) { pass.Apply(); if (cursor.VertexArray != null) { GraphicsDevice.DrawUserIndexedPrimitives <VertexPositionNormalColor>(PrimitiveType.TriangleList, cursor.VertexArray, 0, cursor.VertexArray.Length, cursor.IndexArray, 0, cursor.VertexArray.Length / 2); } } } spriteBatch.Begin(); spriteBatch.DrawString(font, cursor.Mode.ToString(), Vector2.One * 20, Color.White); if (cursor.Mode == CursorMode.LandScape || cursor.Mode == CursorMode.Water) { spriteBatch.DrawString(font, "Brush height: " + cursor.Height, new Vector2(20, 40), Color.White); spriteBatch.DrawString(font, "Theme: " + cursor.Theme.ToString(), new Vector2(20, 60), Color.White); } if (cursor.Mode == CursorMode.Prefab) { spriteBatch.DrawString(font, "Brush height: " + cursor.Height, new Vector2(20, 40), Color.White); spriteBatch.DrawString(font, "Destructable level: " + cursor.destructable, new Vector2(20, 60), Color.White); } if (cursor.Mode == CursorMode.Spawn) { spriteBatch.DrawString(font, "Spawn height: " + cursor.Height, new Vector2(20, 40), Color.White); } if (cursor.MirrorMode) { spriteBatch.DrawString(font, "Mirror mode", new Vector2(300, 40), Color.White); } spriteBatch.End(); base.Draw(gameTime); }
public static void LoadAnim(ref VoxelSprite sprite, string fn) { byte[] buffer; using (FileStream gstr = new FileStream(fn, 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); sprite.AnimChunks.Clear(); sprite.ChunkRTs.Clear(); pos = 4; for (int i = 0; i < 10; i++) { 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, (zs - 1) - vz, vy, true, top); pos += 6; } else { pos++; break; } } c.UpdateMesh(); } GC.Collect(); }