/// <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(); } }
private void LoadThreaded() { // Ensure we're using the invariant culture. Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture; Thread.CurrentThread.CurrentUICulture = CultureInfo.InvariantCulture; LoadStatus = LoadingStatus.Loading; SetLoadingMessage("Initializing ..."); while (GraphicsDevice == null) { Thread.Sleep(100); } Thread.Sleep(1000); #if CREATE_CRASH_LOGS try #endif #if !DEBUG try { #endif bool fileExists = !string.IsNullOrEmpty(ExistingFile); SetLoadingMessage("Creating Sky..."); Sky = new SkyRenderer( TextureManager.GetTexture(ContentPaths.Sky.moon), TextureManager.GetTexture(ContentPaths.Sky.sun), Content.Load <TextureCube>(ContentPaths.Sky.day_sky), Content.Load <TextureCube>(ContentPaths.Sky.night_sky), TextureManager.GetTexture(ContentPaths.Gradients.skygradient), Content.Load <Model>(ContentPaths.Models.sphereLowPoly), Content.Load <Effect>(ContentPaths.Shaders.SkySphere), Content.Load <Effect>(ContentPaths.Shaders.Background)); #region Reading game file if (fileExists) { SetLoadingMessage("Loading " + ExistingFile); gameFile = SaveGame.CreateFromDirectory(ExistingFile); if (gameFile == null) { throw new InvalidOperationException("Game File does not exist."); } // Todo: REMOVE THIS WHEN THE NEW SAVE SYSTEM IS COMPLETE. if (gameFile.Metadata.Version != Program.Version && !Program.CompatibleVersions.Contains(gameFile.Metadata.Version)) { throw new InvalidOperationException(String.Format("Game file is from version {0}. Compatible versions are {1}.", gameFile.Metadata.Version, TextGenerator.GetListString(Program.CompatibleVersions))); } Sky.TimeOfDay = gameFile.Metadata.TimeOfDay; Time = gameFile.Metadata.Time; WorldOrigin = gameFile.Metadata.WorldOrigin; WorldScale = gameFile.Metadata.WorldScale; WorldSize = gameFile.Metadata.NumChunks; GameID = gameFile.Metadata.GameID; if (gameFile.Metadata.OverworldFile != null && gameFile.Metadata.OverworldFile != "flat") { SetLoadingMessage("Loading world " + gameFile.Metadata.OverworldFile); Overworld.Name = gameFile.Metadata.OverworldFile; DirectoryInfo worldDirectory = Directory.CreateDirectory(DwarfGame.GetWorldDirectory() + ProgramData.DirChar + Overworld.Name); var overWorldFile = new NewOverworldFile(worldDirectory.FullName); Overworld.Map = overWorldFile.Data.Data; Overworld.Name = overWorldFile.Data.Name; } else { SetLoadingMessage("Generating flat world.."); Overworld.CreateUniformLand(GraphicsDevice); } } #endregion #region Initialize static data { Vector3 origin = new Vector3(WorldOrigin.X, 0, WorldOrigin.Y); Vector3 extents = new Vector3(1500, 1500, 1500); CollisionManager = new CollisionManager(new BoundingBox(origin - extents, origin + extents)); CraftLibrary = new CraftLibrary(); new PrimitiveLibrary(GraphicsDevice, Content); NewInstanceManager = new NewInstanceManager(GraphicsDevice, new BoundingBox(origin - extents, origin + extents), Content); Color[] white = new Color[1]; white[0] = Color.White; pixel = new Texture2D(GraphicsDevice, 1, 1); pixel.SetData(white); Tilesheet = TextureManager.GetTexture(ContentPaths.Terrain.terrain_tiles); AspectRatio = GraphicsDevice.Viewport.AspectRatio; DefaultShader = new Shader(Content.Load <Effect>(ContentPaths.Shaders.TexturedShaders), true); DefaultShader.ScreenWidth = GraphicsDevice.Viewport.Width; DefaultShader.ScreenHeight = GraphicsDevice.Viewport.Height; VoxelLibrary.InitializeDefaultLibrary(GraphicsDevice, Tilesheet); GrassLibrary.InitializeDefaultLibrary(); DecalLibrary.InitializeDefaultLibrary(); bloom = new BloomComponent(Game) { Settings = BloomSettings.PresetSettings[5] }; bloom.Initialize(); fxaa = new FXAA(); fxaa.Initialize(); SoundManager.Content = Content; if (PlanService != null) { PlanService.Restart(); } JobLibrary.Initialize(); MonsterSpawner = new MonsterSpawner(this); EntityFactory.Initialize(this); } #endregion SetLoadingMessage("Creating Planner ..."); PlanService = new PlanService(); SetLoadingMessage("Creating Shadows..."); Shadows = new ShadowRenderer(GraphicsDevice, 1024, 1024); SetLoadingMessage("Creating Liquids ..."); #region liquids WaterRenderer = new WaterRenderer(GraphicsDevice); LiquidAsset waterAsset = new LiquidAsset { Type = LiquidType.Water, Opactiy = 0.8f, Reflection = 1.0f, WaveHeight = 0.1f, WaveLength = 0.05f, WindForce = 0.001f, BumpTexture = TextureManager.GetTexture(ContentPaths.Terrain.water_normal), BaseTexture = TextureManager.GetTexture(ContentPaths.Terrain.cartoon_water), MinOpacity = 0.4f, RippleColor = new Vector4(0.6f, 0.6f, 0.6f, 0.0f), FlatColor = new Vector4(0.3f, 0.3f, 0.9f, 1.0f) }; WaterRenderer.AddLiquidAsset(waterAsset); LiquidAsset lavaAsset = new LiquidAsset { Type = LiquidType.Lava, Opactiy = 0.95f, Reflection = 0.0f, WaveHeight = 0.1f, WaveLength = 0.05f, WindForce = 0.001f, MinOpacity = 0.8f, BumpTexture = TextureManager.GetTexture(ContentPaths.Terrain.water_normal), BaseTexture = TextureManager.GetTexture(ContentPaths.Terrain.lava), RippleColor = new Vector4(0.5f, 0.4f, 0.04f, 0.0f), FlatColor = new Vector4(0.9f, 0.7f, 0.2f, 1.0f) }; WaterRenderer.AddLiquidAsset(lavaAsset); #endregion SetLoadingMessage("Generating Initial Terrain Chunks ..."); if (!fileExists) { GameID = MathFunctions.Random.Next(0, 1024); } ChunkGenerator = new ChunkGenerator(VoxelLibrary, Seed, 0.02f, this.WorldScale) { SeaLevel = SeaLevel }; #region Load Components if (fileExists) { ChunkManager = new ChunkManager(Content, this, Camera, GraphicsDevice, ChunkGenerator, WorldSize.X, WorldSize.Y, WorldSize.Z); ChunkRenderer = new ChunkRenderer(this, Camera, GraphicsDevice, ChunkManager.ChunkData); SetLoadingMessage("Loading Terrain..."); gameFile.ReadChunks(ExistingFile); ChunkManager.ChunkData.LoadFromFile(gameFile, SetLoadingMessage); ChunkManager.ChunkData.SetMaxViewingLevel(gameFile.Metadata.Slice > 0 ? gameFile.Metadata.Slice : ChunkManager.ChunkData.MaxViewingLevel, ChunkManager.SliceMode.Y); SetLoadingMessage("Loading Entities..."); gameFile.LoadPlayData(ExistingFile, this); Camera = gameFile.PlayData.Camera; ChunkManager.camera = Camera; ChunkRenderer.camera = Camera; DesignationDrawer = gameFile.PlayData.Designations; Vector3 origin = new Vector3(WorldOrigin.X, 0, WorldOrigin.Y); Vector3 extents = new Vector3(1500, 1500, 1500); CollisionManager = new CollisionManager(new BoundingBox(origin - extents, origin + extents)); if (gameFile.PlayData.Resources != null) { foreach (var resource in gameFile.PlayData.Resources) { if (!ResourceLibrary.Resources.ContainsKey(resource.Key)) { ResourceLibrary.Add(resource.Value); } } } ComponentManager = new ComponentManager(gameFile.PlayData.Components, this); foreach (var component in gameFile.PlayData.Components.SaveableComponents) { if (!ComponentManager.HasComponent(component.GlobalID) && ComponentManager.HasComponent(component.Parent.GlobalID)) { // Logically impossible. throw new InvalidOperationException("Component exists in save data but not in manager."); } } Factions = gameFile.PlayData.Factions; ComponentManager.World = this; Sky.TimeOfDay = gameFile.Metadata.TimeOfDay; Time = gameFile.Metadata.Time; WorldOrigin = gameFile.Metadata.WorldOrigin; WorldScale = gameFile.Metadata.WorldScale; // Restore native factions from deserialized data. Natives = new List <Faction>(); foreach (Faction faction in Factions.Factions.Values) { if (faction.Race.IsNative && faction.Race.IsIntelligent && !faction.IsRaceFaction) { Natives.Add(faction); } } Diplomacy = gameFile.PlayData.Diplomacy; GoalManager = new Goals.GoalManager(); GoalManager.Initialize(gameFile.PlayData.Goals); TutorialManager = new Tutorial.TutorialManager(Program.CreatePath("Content", "tutorial.txt")); TutorialManager.SetFromSaveData(gameFile.PlayData.TutorialSaveData); } else { Time = new WorldTime(); // WorldOrigin is in "map" units. Convert to voxels var globalOffset = new Vector3(WorldOrigin.X, 0, WorldOrigin.Y) * WorldScale; Camera = new OrbitCamera(this, new Vector3(VoxelConstants.ChunkSizeX, VoxelConstants.ChunkSizeY - 1.0f, VoxelConstants.ChunkSizeZ) + new Vector3(WorldOrigin.X, 0, WorldOrigin.Y) * WorldScale, new Vector3(VoxelConstants.ChunkSizeY, VoxelConstants.ChunkSizeY - 1.0f, VoxelConstants.ChunkSizeZ) + new Vector3(WorldOrigin.X, 0, WorldOrigin.Y) * WorldScale + Vector3.Up * 10.0f + Vector3.Backward * 10, MathHelper.PiOver4, AspectRatio, 0.1f, GameSettings.Default.VertexCullDistance); ChunkManager = new ChunkManager(Content, this, Camera, GraphicsDevice, ChunkGenerator, WorldSize.X, WorldSize.Y, WorldSize.Z); ChunkRenderer = new ChunkRenderer(this, Camera, GraphicsDevice, ChunkManager.ChunkData); var chunkOffset = GlobalVoxelCoordinate.FromVector3(globalOffset).GetGlobalChunkCoordinate(); //var chunkOffset = ChunkManager.ChunkData.RoundToChunkCoords(globalOffset); //globalOffset.X = chunkOffset.X * VoxelConstants.ChunkSizeX; //globalOffset.Y = chunkOffset.Y * VoxelConstants.ChunkSizeY; //globalOffset.Z = chunkOffset.Z * VoxelConstants.ChunkSizeZ; WorldOrigin = new Vector2(globalOffset.X, globalOffset.Z); Camera.Position = new Vector3(0, 10, 0) + globalOffset; Camera.Target = new Vector3(0, 10, 1) + globalOffset; // If there's no file, we have to initialize the first chunk coordinate if (gameFile == null) // Todo: Always true? { ChunkManager.GenerateInitialChunks( GlobalVoxelCoordinate.FromVector3(globalOffset).GetGlobalChunkCoordinate(), SetLoadingMessage); } ComponentManager = new ComponentManager(this); ComponentManager.SetRootComponent(new Body(ComponentManager, "root", Matrix.Identity, Vector3.Zero, Vector3.Zero, false)); if (Natives == null) // Todo: Always true?? { FactionLibrary library = new FactionLibrary(); library.Initialize(this, CompanyMakerState.CompanyInformation); Natives = new List <Faction>(); for (int i = 0; i < 10; i++) { Natives.Add(library.GenerateFaction(this, i, 10)); } } #region Prepare Factions foreach (Faction faction in Natives) { faction.World = this; if (faction.RoomBuilder == null) { faction.RoomBuilder = new RoomBuilder(faction, this); } if (faction.CraftBuilder == null) { faction.CraftBuilder = new CraftBuilder(faction, this); } } Factions = new FactionLibrary(); if (Natives != null && Natives.Count > 0) { Factions.AddFactions(this, Natives); } Factions.Initialize(this, CompanyMakerState.CompanyInformation); Point playerOrigin = new Point((int)(WorldOrigin.X), (int)(WorldOrigin.Y)); Factions.Factions["Player"].Center = playerOrigin; Factions.Factions["The Motherland"].Center = new Point(playerOrigin.X + 50, playerOrigin.Y + 50); #endregion Diplomacy = new Diplomacy(this); Diplomacy.Initialize(Time.CurrentDate); // Initialize goal manager here. GoalManager = new Goals.GoalManager(); GoalManager.Initialize(new List <Goals.Goal>()); TutorialManager = new Tutorial.TutorialManager(Program.CreatePath("Content", "tutorial.txt")); TutorialManager.TutorialEnabled = !GameSettings.Default.TutorialDisabledGlobally; Tutorial("new game start"); } Camera.World = this; //Drawer3D.Camera = Camera; #endregion ChunkManager.camera = Camera; SetLoadingMessage("Creating Particles ..."); ParticleManager = new ParticleManager(GraphicsDevice, ComponentManager); SetLoadingMessage("Creating GameMaster ..."); Master = new GameMaster(Factions.Factions["Player"], Game, ComponentManager, ChunkManager, Camera, GraphicsDevice); if (gameFile != null) { if (gameFile.PlayData.Spells != null) { Master.Spells = gameFile.PlayData.Spells; } if (gameFile.PlayData.Tasks != null) { Master.TaskManager = gameFile.PlayData.Tasks; } } if (Master.Faction.Economy.Company.Information == null) { Master.Faction.Economy.Company.Information = new CompanyInformation(); } CreateInitialEmbarkment(); foreach (var chunk in ChunkManager.ChunkData.ChunkMap) { chunk.CalculateInitialSunlight(); } VoxelHelpers.InitialReveal(ChunkManager.ChunkData, new VoxelHandle( ChunkManager.ChunkData.GetChunkEnumerator().FirstOrDefault(), new LocalVoxelCoordinate(0, VoxelConstants.ChunkSizeY - 1, 0))); foreach (var chunk in ChunkManager.ChunkData.ChunkMap) { ChunkManager.InvalidateChunk(chunk); } ChunkManager.StartThreads(); SetLoadingMessage("Presimulating ..."); ShowingWorld = false; OnLoadedEvent(); Thread.Sleep(1000); ShowingWorld = true; SetLoadingMessage("Complete."); // GameFile is no longer needed. gameFile = null; LoadStatus = LoadingStatus.Success; #if !DEBUG } catch (Exception exception) { Game.CaptureException(exception); LoadingException = exception; LoadStatus = LoadingStatus.Failure; } #endif }