public override void Update(DwarfTime gameTime) { TimeSinceOutput += (float)gameTime.ElapsedRealTime.TotalSeconds; SoundManager.Update(gameTime, null, null); SkipNextLine = false; foreach (var @event in DwarfGame.GumInputMapper.GetInputQueue()) { GuiRoot.HandleInput(@event.Message, @event.Args); if ([email protected] && (@event.Message == InputEvents.KeyUp || @event.Message == InputEvents.MouseClick)) { SkipNextLine = true; } } if (AutoHideBubble) { _Output.Hidden = TimeSinceOutput > 2.0f; SpeakerBorder.Hidden = TimeSinceOutput > 2.0f; GuiRoot.RootItem.Invalidate(); } else { _Output.Hidden = false; SpeakerBorder.Hidden = false; GuiRoot.RootItem.Invalidate(); } GuiRoot.Update(gameTime.ToRealTime()); YarnEngine.Update(gameTime); }
public override void Update(DwarfTime gameTime) { DwarfGame.GumInput.FireActions(GuiRoot, (@event, args) => { if (@event == InputEvents.KeyUp && args.KeyValue == (int)Microsoft.Xna.Framework.Input.Keys.Escape) { GameStateManager.PopState(); } }); GuiRoot.Update(gameTime.ToRealTime()); SoundManager.Update(gameTime, null, null); base.Update(gameTime); }
public override void Update(DwarfTime gameTime) { foreach (var @event in DwarfGame.GumInputMapper.GetInputQueue()) { GuiRoot.HandleInput(@event.Message, @event.Args); if ([email protected]) { // Pass event to game... if (@event.Message == InputEvents.KeyUp && @event.Args.KeyValue == (int)Microsoft.Xna.Framework.Input.Keys.Escape) { StateManager.PopState(); } } } GuiRoot.Update(gameTime.ToRealTime()); SoundManager.Update(gameTime, null, null); base.Update(gameTime); }
private void Draw(GraphicsDevice graphics, DwarfTime time) { if (DwarfGame.SpriteBatch.IsDisposed) { return; } if (Tiles.IsDisposed) { CreateAssets(); } Bloom.BeginDraw(); try { graphics.Clear(Color.SkyBlue); DwarfGame.SafeSpriteBatchBegin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, Drawer2D.PointMagLinearMin, DepthStencilState.Default, RasterizerState.CullNone, null, Matrix.Identity); Rectangle screenRect = graphics.Viewport.Bounds; int maxX = screenRect.Width / TileSize + 2; int maxY = screenRect.Width / TileSize; float t = (float)time.TotalRealTime.TotalSeconds; float offsetX = t * 2.0f; float offsetY = 0.0f; float st = (float)Math.Abs(Math.Sin(t)); float lava = LavaHeight; int backSize = 2; for (int ix = 0; ix < maxX * backSize; ix++) { float x = ix + (int)(offsetX * 0.6f); float height = Noise.Noise(x * HeightScale * 3, 0, 100) * 0.5f + 0.6f; for (int iy = 0; iy < maxY * backSize; iy++) { float y = iy + (int)offsetY; float normalizedY = (1.0f) - (float)y / (float)(maxY * backSize); if (normalizedY < height) { float tileX = ix * (TileSize / backSize) - ((offsetX * 0.6f) * (TileSize / backSize)) % (TileSize / backSize); float tileY = iy * (TileSize / backSize); Drawer2D.FillRect(DwarfGame.SpriteBatch, new Rectangle((int)tileX, (int)tileY, TileSize / backSize, TileSize / backSize), new Color((int)(Color.SkyBlue.R * normalizedY * 0.8f), (int)(Color.SkyBlue.G * normalizedY * 0.8f), (int)(Color.SkyBlue.B * normalizedY))); } } } for (int ix = 0; ix < maxX; ix++) { float x = ix + (int)offsetX; float height = Noise.Noise(x * HeightScale, 0, 0) * 0.8f + MinHeight; for (int iy = 0; iy < maxY; iy++) { float y = iy + (int)offsetY; float normalizedY = (1.0f) - (float)y / (float)maxY; if (Math.Abs(normalizedY - height) < 0.01f) { Color tint = new Color(normalizedY, normalizedY, normalizedY); RenderTile(Grass, DwarfGame.SpriteBatch, ix, iy, offsetX, t, tint); } else if (normalizedY > height - 0.1f && normalizedY < height) { Color tint = new Color((float)Math.Pow(normalizedY, 1.5f), (float)Math.Pow(normalizedY, 1.6f), normalizedY); RenderTile(Soil, DwarfGame.SpriteBatch, ix, iy, offsetX, t, tint); } else if (normalizedY < height) { float caviness = Noise.Noise(x * CaveScale, y * CaveScale, 0); if (caviness < CaveThreshold) { TerrainElement?oreFound = null; int i = 0; foreach (TerrainElement ore in Ores) { i++; float oreNess = Noise.Noise(x * ore.SpawnScale, y * ore.SpawnScale, i); if (oreNess > ore.SpawnThreshold) { oreFound = ore; } } Color tint = new Color((float)Math.Pow(normalizedY, 1.5f) * 0.5f, (float)Math.Pow(normalizedY, 1.6f) * 0.5f, normalizedY * 0.5f); if (oreFound == null) { RenderTile(Substrate, DwarfGame.SpriteBatch, ix, iy, offsetX, t, tint); } else { RenderTile(oreFound.Value, DwarfGame.SpriteBatch, ix, iy, offsetX, t, tint); } } else { if (normalizedY < lava) { float glowiness = Noise.Noise(x * CaveScale * 2, y * CaveScale * 2, t); RenderTile(Lava, DwarfGame.SpriteBatch, ix, iy, offsetX, t, new Color(0.5f * glowiness + 0.5f, 0.7f * glowiness + 0.3f * st, glowiness)); } else { RenderTile(Cave, DwarfGame.SpriteBatch, ix, iy, offsetX, t, new Color((float)Math.Pow(normalizedY, 1.5f) * (1.0f - caviness) * 0.8f, (float)Math.Pow(normalizedY, 1.6f) * (1.0f - caviness) * 0.8f, normalizedY * (1.0f - caviness))); } } } } } } finally { DwarfGame.SpriteBatch.End(); } Bloom.Draw(time.ToRealTime()); }
/// <summary> /// Called when a frame is to be drawn to the screen /// </summary> /// <param name="gameTime">The current time</param> public void Render(DwarfTime gameTime) { if (!World.ShowingWorld) { return; } ValidateShader(); var frustum = Camera.GetDrawFrustum(); var renderables = World.EnumerateIntersectingObjects(frustum, r => r.IsVisible && !World.ChunkManager.IsAboveCullPlane(r.GetBoundingBox())); // Controls the sky fog float x = (1.0f - Sky.TimeOfDay); x = x * x; DefaultShader.FogColor = new Color(0.32f * x, 0.58f * x, 0.9f * x); DefaultShader.LightPositions = LightPositions; CompositeLibrary.Render(GraphicsDevice); CompositeLibrary.Update(); GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; if (lastWaterHeight < 0) // Todo: Seriously, every single frame?? { lastWaterHeight = 0; foreach (var chunk in World.ChunkManager.ChunkMap) { for (int y = 0; y < VoxelConstants.ChunkSizeY; y++) { if (chunk.Data.LiquidPresent[y] > 0) { lastWaterHeight = Math.Max(y + chunk.Origin.Y, lastWaterHeight); } } } } // Computes the water height. float wHeight = WaterRenderer.GetVisibleWaterHeight(World.ChunkManager, Camera, GraphicsDevice.Viewport, lastWaterHeight); lastWaterHeight = wHeight; // Draw reflection/refraction images WaterRenderer.DrawReflectionMap(renderables, gameTime, World, wHeight - 0.1f, GetReflectedCameraMatrix(wHeight), DefaultShader, GraphicsDevice); #region Draw Selection Buffer. if (SelectionBuffer == null) { SelectionBuffer = new SelectionBuffer(8, GraphicsDevice); } GraphicsDevice.RasterizerState = RasterizerState.CullNone; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; // Defines the current slice for the GPU var level = PersistentSettings.MaxViewingLevel >= World.WorldSizeInVoxels.Y ? 1000.0f : PersistentSettings.MaxViewingLevel + 0.25f; Plane slicePlane = WaterRenderer.CreatePlane(level, new Vector3(0, -1, 0), Camera.ViewMatrix, false); if (SelectionBuffer.Begin(GraphicsDevice)) { // Draw the whole world, and make sure to handle slicing DefaultShader.ClipPlane = new Vector4(slicePlane.Normal, slicePlane.D); DefaultShader.ClippingEnabled = true; DefaultShader.View = Camera.ViewMatrix; DefaultShader.Projection = Camera.ProjectionMatrix; DefaultShader.World = Matrix.Identity; //GamePerformance.Instance.StartTrackPerformance("Render - Selection Buffer - Chunks"); ChunkRenderer.RenderSelectionBuffer(DefaultShader, GraphicsDevice, Camera.ViewMatrix); //GamePerformance.Instance.StopTrackPerformance("Render - Selection Buffer - Chunks"); //GamePerformance.Instance.StartTrackPerformance("Render - Selection Buffer - Components"); ComponentRenderer.RenderSelectionBuffer(renderables, gameTime, World.ChunkManager, Camera, DwarfGame.SpriteBatch, GraphicsDevice, DefaultShader); //GamePerformance.Instance.StopTrackPerformance("Render - Selection Buffer - Components"); //GamePerformance.Instance.StartTrackPerformance("Render - Selection Buffer - Instances"); InstanceRenderer.Flush(GraphicsDevice, DefaultShader, Camera, InstanceRenderMode.SelectionBuffer); //GamePerformance.Instance.StopTrackPerformance("Render - Selection Buffer - Instances"); SelectionBuffer.End(GraphicsDevice); } #endregion // Start drawing the bloom effect if (GameSettings.Default.EnableGlow) { bloom.BeginDraw(); } // Draw the sky GraphicsDevice.Clear(DefaultShader.FogColor); DrawSky(gameTime, Camera.ViewMatrix, 1.0f, DefaultShader.FogColor); DefaultShader.FogEnd = GameSettings.Default.ChunkDrawDistance; DefaultShader.FogStart = GameSettings.Default.ChunkDrawDistance * 0.8f; CaveView = CaveView * 0.9f + TargetCaveView * 0.1f; DefaultShader.WindDirection = World.Weather.CurrentWind; DefaultShader.WindForce = 0.0005f * (1.0f + (float)Math.Sin(World.Time.GetTotalSeconds() * 0.001f)); // Draw the whole world, and make sure to handle slicing DefaultShader.ClipPlane = new Vector4(slicePlane.Normal, slicePlane.D); DefaultShader.ClippingEnabled = true; //Blue ghost effect above the current slice. DefaultShader.GhostClippingEnabled = true; Draw3DThings(gameTime, DefaultShader, Camera.ViewMatrix); // Now we want to draw the water on top of everything else DefaultShader.ClippingEnabled = true; DefaultShader.GhostClippingEnabled = false; //ComponentManager.CollisionManager.DebugDraw(); DefaultShader.View = Camera.ViewMatrix; DefaultShader.Projection = Camera.ProjectionMatrix; DefaultShader.GhostClippingEnabled = true; // Now draw all of the entities in the game DefaultShader.ClipPlane = new Vector4(slicePlane.Normal, slicePlane.D); DefaultShader.ClippingEnabled = true; // Render simple geometry (boxes, etc.) Drawer3D.Render(GraphicsDevice, DefaultShader, Camera, World.PersistentData.Designations, World); DefaultShader.EnableShadows = false; DefaultShader.View = Camera.ViewMatrix; ComponentRenderer.Render(renderables, gameTime, World.ChunkManager, Camera, DwarfGame.SpriteBatch, GraphicsDevice, DefaultShader, ComponentRenderer.WaterRenderType.None, lastWaterHeight); InstanceRenderer.Flush(GraphicsDevice, DefaultShader, Camera, InstanceRenderMode.Normal); if (World.UserInterface.CurrentToolMode == "BuildZone" || // Todo: ?? World.UserInterface.CurrentToolMode == "BuildWall" || World.UserInterface.CurrentToolMode == "BuildObject") { DefaultShader.View = Camera.ViewMatrix; DefaultShader.Projection = Camera.ProjectionMatrix; DefaultShader.SetTexturedTechnique(); GraphicsDevice.BlendState = BlendState.NonPremultiplied; } WaterRenderer.DrawWater( GraphicsDevice, (float)gameTime.TotalGameTime.TotalSeconds, DefaultShader, Camera.ViewMatrix, GetReflectedCameraMatrix(wHeight), Camera.ProjectionMatrix, new Vector3(0.1f, 0.0f, 0.1f), Camera, World.ChunkManager); World.ParticleManager.Render(World, GraphicsDevice); DefaultShader.ClippingEnabled = false; if (UseFXAA && fxaa == null) { fxaa = new FXAA(); fxaa.Initialize(); } if (GameSettings.Default.EnableGlow) { if (UseFXAA) { fxaa.Begin(DwarfTime.LastTime); } bloom.DrawTarget = UseFXAA ? fxaa.RenderTarget : null; bloom.Draw(gameTime.ToRealTime()); if (UseFXAA) { fxaa.End(DwarfTime.LastTime); } } else if (UseFXAA) { fxaa.End(DwarfTime.LastTime); } RasterizerState rasterizerState = new RasterizerState() { ScissorTestEnable = true }; if (Debugger.Switches.DrawSelectionBuffer) { SelectionBuffer.DebugDraw(GraphicsDevice.Viewport.Bounds); } try { DwarfGame.SafeSpriteBatchBegin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, Drawer2D.PointMagLinearMin, null, rasterizerState, null, Matrix.Identity); //DwarfGame.SpriteBatch.Draw(Shadows.ShadowTexture, Vector2.Zero, Color.White); if (IsCameraUnderwater()) { Drawer2D.FillRect(DwarfGame.SpriteBatch, GraphicsDevice.Viewport.Bounds, new Color(10, 40, 60, 200)); } Drawer2D.Render(DwarfGame.SpriteBatch, Camera, GraphicsDevice.Viewport); IndicatorManager.Render(gameTime); } finally { try { DwarfGame.SpriteBatch.End(); } catch (Exception exception) { DwarfGame.SpriteBatch = new SpriteBatch(GraphicsDevice); } } if (Debugger.Switches.DrawComposites) { Vector2 offset = Vector2.Zero; foreach (var composite in CompositeLibrary.Composites) { offset = composite.Value.DebugDraw(DwarfGame.SpriteBatch, (int)offset.X, (int)offset.Y); } } DwarfGame.SpriteBatch.GraphicsDevice.ScissorRectangle = DwarfGame.SpriteBatch.GraphicsDevice.Viewport.Bounds; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; foreach (var module in World.UpdateSystems) { module.Render(gameTime, World.ChunkManager, Camera, DwarfGame.SpriteBatch, GraphicsDevice, DefaultShader); } lock (ScreenshotLock) { foreach (Screenshot shot in Screenshots) { TakeScreenshot(shot.FileName, shot.Resolution); } Screenshots.Clear(); } }
/// <summary> /// Called when a frame is to be drawn to the screen /// </summary> /// <param name="gameTime">The current time</param> public void Render(DwarfTime gameTime) { if (!ShowingWorld) { return; } var frustum = Camera.GetDrawFrustum(); var renderables = EnumerateIntersectingObjects(frustum) .Where(r => r.IsVisible && !ChunkManager.IsAboveCullPlane(r.GetBoundingBox())); // Controls the sky fog float x = (1.0f - Sky.TimeOfDay); x = x * x; DefaultShader.FogColor = new Color(0.32f * x, 0.58f * x, 0.9f * x); DefaultShader.LightPositions = LightPositions; CompositeLibrary.Render(GraphicsDevice); CompositeLibrary.Update(); GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; // Computes the water height. float wHeight = WaterRenderer.GetVisibleWaterHeight(ChunkManager, Camera, GraphicsDevice.Viewport, lastWaterHeight); lastWaterHeight = wHeight; // Draw reflection/refraction images WaterRenderer.DrawReflectionMap(renderables, gameTime, this, wHeight - 0.1f, GetReflectedCameraMatrix(wHeight), DefaultShader, GraphicsDevice); #region Draw Selection Buffer. if (SelectionBuffer == null) { SelectionBuffer = new SelectionBuffer(8, GraphicsDevice); } GraphicsDevice.RasterizerState = RasterizerState.CullNone; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; Plane slicePlane = WaterRenderer.CreatePlane(SlicePlane, new Vector3(0, -1, 0), Camera.ViewMatrix, false); if (SelectionBuffer.Begin(GraphicsDevice)) { // Draw the whole world, and make sure to handle slicing DefaultShader.ClipPlane = new Vector4(slicePlane.Normal, slicePlane.D); DefaultShader.ClippingEnabled = true; DefaultShader.View = Camera.ViewMatrix; DefaultShader.Projection = Camera.ProjectionMatrix; DefaultShader.World = Matrix.Identity; //GamePerformance.Instance.StartTrackPerformance("Render - Selection Buffer - Chunks"); ChunkRenderer.RenderSelectionBuffer(DefaultShader, GraphicsDevice, Camera.ViewMatrix); //GamePerformance.Instance.StopTrackPerformance("Render - Selection Buffer - Chunks"); //GamePerformance.Instance.StartTrackPerformance("Render - Selection Buffer - Components"); ComponentRenderer.RenderSelectionBuffer(renderables, gameTime, ChunkManager, Camera, DwarfGame.SpriteBatch, GraphicsDevice, DefaultShader); //GamePerformance.Instance.StopTrackPerformance("Render - Selection Buffer - Components"); //GamePerformance.Instance.StartTrackPerformance("Render - Selection Buffer - Instances"); InstanceRenderer.Flush(GraphicsDevice, DefaultShader, Camera, InstanceRenderMode.SelectionBuffer); //GamePerformance.Instance.StopTrackPerformance("Render - Selection Buffer - Instances"); SelectionBuffer.End(GraphicsDevice); } #endregion // Start drawing the bloom effect if (GameSettings.Default.EnableGlow) { bloom.BeginDraw(); } // Draw the sky GraphicsDevice.Clear(DefaultShader.FogColor); DrawSky(gameTime, Camera.ViewMatrix, 1.0f, DefaultShader.FogColor); // Defines the current slice for the GPU float level = ChunkManager.World.Master.MaxViewingLevel + 0.25f; if (level > VoxelConstants.ChunkSizeY) { level = 1000; } SlicePlane = level; DefaultShader.WindDirection = Weather.CurrentWind; DefaultShader.WindForce = 0.0005f * (1.0f + (float)Math.Sin(Time.GetTotalSeconds() * 0.001f)); // Draw the whole world, and make sure to handle slicing DefaultShader.ClipPlane = new Vector4(slicePlane.Normal, slicePlane.D); DefaultShader.ClippingEnabled = true; //Blue ghost effect above the current slice. DefaultShader.GhostClippingEnabled = true; Draw3DThings(gameTime, DefaultShader, Camera.ViewMatrix); // Now we want to draw the water on top of everything else DefaultShader.ClippingEnabled = true; DefaultShader.GhostClippingEnabled = false; //ComponentManager.CollisionManager.DebugDraw(); DefaultShader.View = Camera.ViewMatrix; DefaultShader.Projection = Camera.ProjectionMatrix; DefaultShader.GhostClippingEnabled = true; // Now draw all of the entities in the game DefaultShader.ClipPlane = new Vector4(slicePlane.Normal, slicePlane.D); DefaultShader.ClippingEnabled = true; if (Debugger.Switches.DrawOcttree) { foreach (var box in OctTree.EnumerateBounds(frustum)) { Drawer3D.DrawBox(box.Item2, Color.Yellow, 1.0f / (float)(box.Item1 + 1), false); } } // Render simple geometry (boxes, etc.) Drawer3D.Render(GraphicsDevice, DefaultShader, Camera, DesignationDrawer, PlayerFaction.Designations, this); DefaultShader.EnableShadows = false; DefaultShader.View = Camera.ViewMatrix; ComponentRenderer.Render(renderables, gameTime, ChunkManager, Camera, DwarfGame.SpriteBatch, GraphicsDevice, DefaultShader, ComponentRenderer.WaterRenderType.None, lastWaterHeight); InstanceRenderer.Flush(GraphicsDevice, DefaultShader, Camera, InstanceRenderMode.Normal); if (Master.CurrentToolMode == GameMaster.ToolMode.BuildZone || Master.CurrentToolMode == GameMaster.ToolMode.BuildWall || Master.CurrentToolMode == GameMaster.ToolMode.BuildObject) { DefaultShader.View = Camera.ViewMatrix; DefaultShader.Projection = Camera.ProjectionMatrix; DefaultShader.SetTexturedTechnique(); GraphicsDevice.BlendState = BlendState.NonPremultiplied; } WaterRenderer.DrawWater( GraphicsDevice, (float)gameTime.TotalGameTime.TotalSeconds, DefaultShader, Camera.ViewMatrix, GetReflectedCameraMatrix(wHeight), Camera.ProjectionMatrix, new Vector3(0.1f, 0.0f, 0.1f), Camera, ChunkManager); ParticleManager.Render(this, GraphicsDevice); DefaultShader.ClippingEnabled = false; if (GameSettings.Default.EnableGlow) { bloom.DrawTarget = UseFXAA ? fxaa.RenderTarget : null; if (UseFXAA) { fxaa.Begin(DwarfTime.LastTime); } bloom.Draw(gameTime.ToRealTime()); if (UseFXAA) { fxaa.End(DwarfTime.LastTime); } } else if (UseFXAA) { fxaa.End(DwarfTime.LastTime); } RasterizerState rasterizerState = new RasterizerState() { ScissorTestEnable = true }; if (Debugger.Switches.DrawSelectionBuffer) { SelectionBuffer.DebugDraw(GraphicsDevice.Viewport.Bounds); } try { DwarfGame.SafeSpriteBatchBegin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, Drawer2D.PointMagLinearMin, null, rasterizerState, null, Matrix.Identity); //DwarfGame.SpriteBatch.Draw(Shadows.ShadowTexture, Vector2.Zero, Color.White); if (IsCameraUnderwater()) { Drawer2D.FillRect(DwarfGame.SpriteBatch, GraphicsDevice.Viewport.Bounds, new Color(10, 40, 60, 200)); } Drawer2D.Render(DwarfGame.SpriteBatch, Camera, GraphicsDevice.Viewport); IndicatorManager.Render(gameTime); } finally { DwarfGame.SpriteBatch.End(); } if (Debugger.Switches.DrawComposites) { Vector2 offset = Vector2.Zero; foreach (var composite in CompositeLibrary.Composites) { offset = composite.Value.DebugDraw(DwarfGame.SpriteBatch, (int)offset.X, (int)offset.Y); } } Master.Render(Game, gameTime, GraphicsDevice); DwarfGame.SpriteBatch.GraphicsDevice.ScissorRectangle = DwarfGame.SpriteBatch.GraphicsDevice.Viewport.Bounds; GraphicsDevice.DepthStencilState = DepthStencilState.Default; GraphicsDevice.BlendState = BlendState.Opaque; lock (ScreenshotLock) { foreach (Screenshot shot in Screenshots) { TakeScreenshot(shot.FileName, shot.Resolution); } Screenshots.Clear(); } }