From ba49f592044b6175931d32eb625308ea65825865 Mon Sep 17 00:00:00 2001 From: Regalis Date: Wed, 3 May 2017 00:27:13 +0300 Subject: [PATCH] Some cleanup (mostly removing redundancies from the GameMain class) --- Subsurface/Source/GameMain.cs | 117 ++++++++---------- Subsurface/Source/GameSession/ShiftSummary.cs | 17 +-- Subsurface/Source/GameSettings.cs | 4 +- Subsurface/Source/Map/Levels/LevelRenderer.cs | 10 +- Subsurface/Source/Map/Levels/WaterRenderer.cs | 2 +- Subsurface/Source/Map/Levels/WrappingWall.cs | 4 +- Subsurface/Source/Map/Lights/ConvexHull.cs | 6 +- Subsurface/Source/Map/Lights/LightManager.cs | 2 +- Subsurface/Source/Map/Lights/LightSource.cs | 16 +-- Subsurface/Source/Networking/GameClient.cs | 2 +- Subsurface/Source/Networking/GameServer.cs | 2 +- Subsurface/Source/Screens/LobbyScreen.cs | 7 +- 12 files changed, 81 insertions(+), 108 deletions(-) diff --git a/Subsurface/Source/GameMain.cs b/Subsurface/Source/GameMain.cs index 4cb9f2492..7ca41b8b0 100644 --- a/Subsurface/Source/GameMain.cs +++ b/Subsurface/Source/GameMain.cs @@ -7,30 +7,15 @@ using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; using Barotrauma.Networking; using Barotrauma.Particles; -using System.Collections; using System.Collections.Generic; using Microsoft.Xna.Framework.Input; -using System.Xml; namespace Barotrauma { class GameMain : Game { - public static GraphicsDeviceManager Graphics; - static int graphicsWidth, graphicsHeight; - static SpriteBatch spriteBatch; - - public static GameMain Instance; - - public static bool WindowActive - { - get { return Instance == null || Instance.IsActive; } - } - public static bool DebugDraw; - - public static GraphicsDevice CurrGraphicsDevice; - + public static FrameCounter FrameCounter; public static readonly Version Version = Assembly.GetEntryAssembly().GetName().Version; @@ -51,21 +36,17 @@ namespace Barotrauma { get { return Config.SelectedContentPackage; } } - - public static Level Level; - + public static GameSession GameSession; public static NetworkMember NetworkMember; public static ParticleManager ParticleManager; - - //public static TextureLoader TextureLoader; public static World World; public static LoadingScreen TitleScreen; - private static bool loadingScreenOpen; + private bool loadingScreenOpen; public static GameSettings Config; @@ -74,25 +55,35 @@ namespace Barotrauma private GameTime fixedTime; - //public static Random localRandom; - //public static Random random; + private static SpriteBatch spriteBatch; - //private Stopwatch renderTimer; - //public static int renderTimeElapsed; - - public Camera Cam + public static GameMain Instance { - get { return GameScreen.Cam; } + get; + private set; } + public static GraphicsDeviceManager GraphicsDeviceManager + { + get; + private set; + } + public static int GraphicsWidth { - get { return graphicsWidth; } + get; + private set; } public static int GraphicsHeight { - get { return graphicsHeight; } + get; + private set; + } + + public static bool WindowActive + { + get { return Instance == null || Instance.IsActive; } } public static GameServer Server @@ -122,7 +113,7 @@ namespace Barotrauma public GameMain() { - Graphics = new GraphicsDeviceManager(this); + GraphicsDeviceManager = new GraphicsDeviceManager(this); Window.Title = "Barotrauma"; @@ -141,11 +132,8 @@ namespace Barotrauma Content.RootDirectory = "Content"; FrameCounter = new FrameCounter(); - - //IsMouseVisible = true; - + IsFixedTimeStep = false; - //TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 55); Timing.Accumulator = 0.0f; fixedTime = new GameTime(); @@ -155,22 +143,21 @@ namespace Barotrauma FarseerPhysics.Settings.ContinuousPhysics = false; FarseerPhysics.Settings.VelocityIterations = 1; FarseerPhysics.Settings.PositionIterations = 1; - } public void ApplyGraphicsSettings() { - graphicsWidth = Config.GraphicsWidth; - graphicsHeight = Config.GraphicsHeight; - Graphics.SynchronizeWithVerticalRetrace = Config.VSyncEnabled; - - Graphics.HardwareModeSwitch = Config.WindowMode != WindowMode.BorderlessWindowed; + GraphicsWidth = Config.GraphicsWidth; + GraphicsHeight = Config.GraphicsHeight; + GraphicsDeviceManager.SynchronizeWithVerticalRetrace = Config.VSyncEnabled; - Graphics.IsFullScreen = Config.WindowMode == WindowMode.Fullscreen || Config.WindowMode == WindowMode.BorderlessWindowed; - Graphics.PreferredBackBufferWidth = graphicsWidth; - Graphics.PreferredBackBufferHeight = graphicsHeight; - - Graphics.ApplyChanges(); + GraphicsDeviceManager.HardwareModeSwitch = Config.WindowMode != WindowMode.BorderlessWindowed; + + GraphicsDeviceManager.IsFullScreen = Config.WindowMode == WindowMode.Fullscreen || Config.WindowMode == WindowMode.BorderlessWindowed; + GraphicsDeviceManager.PreferredBackBufferWidth = GraphicsWidth; + GraphicsDeviceManager.PreferredBackBufferHeight = GraphicsHeight; + + GraphicsDeviceManager.ApplyChanges(); } /// @@ -182,9 +169,7 @@ namespace Barotrauma protected override void Initialize() { base.Initialize(); - - CurrGraphicsDevice = GraphicsDevice; - + ScissorTestEnable = new RasterizerState() { ScissorTestEnable = true }; Hyper.ComponentModel.HyperTypeDescriptionProvider.Add(typeof(Character)); @@ -199,25 +184,25 @@ namespace Barotrauma /// protected override void LoadContent() { - graphicsWidth = GraphicsDevice.Viewport.Width; - graphicsHeight = GraphicsDevice.Viewport.Height; - + GraphicsWidth = GraphicsDevice.Viewport.Width; + GraphicsHeight = GraphicsDevice.Viewport.Height; + Sound.Init(); ConvertUnits.SetDisplayUnitToSimUnitRatio(Physics.DisplayToSimRation); - - spriteBatch = new SpriteBatch(GraphicsDevice); - TextureLoader.Init(GraphicsDevice); + + spriteBatch = new SpriteBatch(base.GraphicsDevice); + TextureLoader.Init(base.GraphicsDevice); loadingScreenOpen = true; - TitleScreen = new LoadingScreen(GraphicsDevice); + TitleScreen = new LoadingScreen(base.GraphicsDevice); loadingCoroutine = CoroutineManager.StartCoroutine(Load()); } public IEnumerable Load() { - GUI.GraphicsDevice = GraphicsDevice; + GUI.GraphicsDevice = base.GraphicsDevice; GUI.Init(Content); GUIComponent.Init(Window); @@ -225,9 +210,9 @@ namespace Barotrauma DebugConsole.Log(SelectedPackage == null ? "No content package selected" : "Content package \"" + SelectedPackage.Name + "\" selected"); yield return CoroutineStatus.Running; - LightManager = new Lights.LightManager(GraphicsDevice); + LightManager = new Lights.LightManager(base.GraphicsDevice); - Hull.renderer = new WaterRenderer(GraphicsDevice, Content); + Hull.renderer = new WaterRenderer(base.GraphicsDevice, Content); TitleScreen.LoadState = 1.0f; yield return CoroutineStatus.Running; @@ -272,7 +257,7 @@ namespace Barotrauma TitleScreen.LoadState = 80.0f; yield return CoroutineStatus.Running; - GameScreen = new GameScreen(Graphics.GraphicsDevice, Content); + GameScreen = new GameScreen(GraphicsDeviceManager.GraphicsDevice, Content); TitleScreen.LoadState = 90.0f; yield return CoroutineStatus.Running; @@ -286,7 +271,7 @@ namespace Barotrauma yield return CoroutineStatus.Running; - ParticleManager = new ParticleManager("Content/Particles/ParticlePrefabs.xml", Cam); + ParticleManager = new ParticleManager("Content/Particles/ParticlePrefabs.xml", GameScreen.Cam); yield return CoroutineStatus.Running; LocationType.Init(); @@ -320,8 +305,6 @@ namespace Barotrauma while (Timing.Accumulator >= Timing.Step) { - TotalElapsedTime = gameTime.TotalGameTime.TotalSeconds; - fixedTime.IsRunningSlowly = gameTime.IsRunningSlowly; TimeSpan addTime = new TimeSpan(0, 0, 0, 0, 16); fixedTime.ElapsedGameTime = addTime; @@ -409,11 +392,11 @@ namespace Barotrauma if (loadingScreenOpen) { - TitleScreen.Draw(spriteBatch, GraphicsDevice, (float)deltaTime); + TitleScreen.Draw(spriteBatch, base.GraphicsDevice, (float)deltaTime); } else if (hasLoaded) { - Screen.Selected.Draw(deltaTime, GraphicsDevice, spriteBatch); + Screen.Selected.Draw(deltaTime, base.GraphicsDevice, spriteBatch); } if (!DebugDraw) return; @@ -426,7 +409,7 @@ namespace Barotrauma } static bool waitForKeyHit = true; - public static CoroutineHandle ShowLoading(IEnumerable loader, bool waitKeyHit = true) + public CoroutineHandle ShowLoading(IEnumerable loader, bool waitKeyHit = true) { waitForKeyHit = waitKeyHit; loadingScreenOpen = true; diff --git a/Subsurface/Source/GameSession/ShiftSummary.cs b/Subsurface/Source/GameSession/ShiftSummary.cs index d4227a667..232a16cc8 100644 --- a/Subsurface/Source/GameSession/ShiftSummary.cs +++ b/Subsurface/Source/GameSession/ShiftSummary.cs @@ -20,12 +20,7 @@ namespace Barotrauma selectedMission = gameSession.Mission; } - - - //public void AddCasualty(Character character, CauseOfDeath causeOfDeath) - //{ - // casualties.Add(new Casualty(character.Info, causeOfDeath, "")); - //} + public GUIFrame CreateSummaryFrame(string endMessage) { @@ -90,7 +85,6 @@ namespace Barotrauma } else { - if (character.IsUnconscious) { statusText = "Unconscious"; @@ -101,11 +95,12 @@ namespace Barotrauma statusText = "Injured"; statusColor = Color.DarkOrange; } - } - new GUITextBlock(new Rectangle(0, 0, 0, 20), statusText, - "", Alignment.BottomLeft, Alignment.TopCenter, characterFrame, true, GUI.SmallFont).Color = statusColor * 0.7f; + new GUITextBlock( + new Rectangle(0, 0, 0, 20), statusText, statusColor * 0.8f, Color.White, + Alignment.BottomLeft, Alignment.Center, + null, characterFrame, true, GUI.SmallFont); x += characterFrame.Rect.Width + 10; } @@ -120,12 +115,10 @@ namespace Barotrauma new GUITextBlock(new Rectangle(0, y, innerFrame.Rect.Width - 170, 0), (GameMain.GameSession.Mission.Completed) ? GameMain.GameSession.Mission.SuccessMessage : GameMain.GameSession.Mission.FailureMessage, "", innerFrame, true); - //y += 50; if (GameMain.GameSession.Mission.Completed && singleplayer) { new GUITextBlock(new Rectangle(0, 0, 0, 30), "Reward: " + GameMain.GameSession.Mission.Reward, "", Alignment.BottomLeft, Alignment.BottomLeft, innerFrame); - //y += 30; } } diff --git a/Subsurface/Source/GameSettings.cs b/Subsurface/Source/GameSettings.cs index 5116d4a99..faf32b758 100644 --- a/Subsurface/Source/GameSettings.cs +++ b/Subsurface/Source/GameSettings.cs @@ -365,8 +365,8 @@ namespace Barotrauma vsyncTickBox.OnSelected = (GUITickBox box) => { VSyncEnabled = !VSyncEnabled; - GameMain.Graphics.SynchronizeWithVerticalRetrace = VSyncEnabled; - GameMain.Graphics.ApplyChanges(); + GameMain.GraphicsDeviceManager.SynchronizeWithVerticalRetrace = VSyncEnabled; + GameMain.GraphicsDeviceManager.ApplyChanges(); UnsavedSettings = true; return true; diff --git a/Subsurface/Source/Map/Levels/LevelRenderer.cs b/Subsurface/Source/Map/Levels/LevelRenderer.cs index 3c1cb824a..fb05b856d 100644 --- a/Subsurface/Source/Map/Levels/LevelRenderer.cs +++ b/Subsurface/Source/Map/Levels/LevelRenderer.cs @@ -41,7 +41,7 @@ namespace Barotrauma if (wallEdgeEffect == null) { - wallEdgeEffect = new BasicEffect(GameMain.CurrGraphicsDevice) + wallEdgeEffect = new BasicEffect(GameMain.Instance.GraphicsDevice) { DiffuseColor = new Vector3(0.8f, 0.8f, 0.8f), VertexColorEnabled = false, @@ -53,7 +53,7 @@ namespace Barotrauma if (wallCenterEffect == null) { - wallCenterEffect = new BasicEffect(GameMain.CurrGraphicsDevice) + wallCenterEffect = new BasicEffect(GameMain.Instance.GraphicsDevice) { VertexColorEnabled = false, TextureEnabled = true, @@ -90,13 +90,13 @@ namespace Barotrauma public void SetWallVertices(VertexPositionTexture[] vertices) { - wallVertices = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionTexture.VertexDeclaration, vertices.Length,BufferUsage.WriteOnly); + wallVertices = new VertexBuffer(GameMain.Instance.GraphicsDevice, VertexPositionTexture.VertexDeclaration, vertices.Length,BufferUsage.WriteOnly); wallVertices.SetData(vertices); } public void SetBodyVertices(VertexPositionTexture[] vertices) { - bodyVertices = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly); + bodyVertices = new VertexBuffer(GameMain.Instance.GraphicsDevice, VertexPositionTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly); bodyVertices.SetData(vertices); } @@ -164,7 +164,7 @@ namespace Barotrauma spriteBatch.End(); - RenderWalls(GameMain.CurrGraphicsDevice, cam); + RenderWalls(GameMain.Instance.GraphicsDevice, cam); } public void Draw(SpriteBatch spriteBatch) diff --git a/Subsurface/Source/Map/Levels/WaterRenderer.cs b/Subsurface/Source/Map/Levels/WaterRenderer.cs index e897f8b45..87e0b26a1 100644 --- a/Subsurface/Source/Map/Levels/WaterRenderer.cs +++ b/Subsurface/Source/Map/Levels/WaterRenderer.cs @@ -48,7 +48,7 @@ namespace Barotrauma if (basicEffect == null) { - basicEffect = new BasicEffect(GameMain.CurrGraphicsDevice); + basicEffect = new BasicEffect(GameMain.Instance.GraphicsDevice); basicEffect.VertexColorEnabled = false; basicEffect.TextureEnabled = true; diff --git a/Subsurface/Source/Map/Levels/WrappingWall.cs b/Subsurface/Source/Map/Levels/WrappingWall.cs index 1c200fa6a..1433c97ea 100644 --- a/Subsurface/Source/Map/Levels/WrappingWall.cs +++ b/Subsurface/Source/Map/Levels/WrappingWall.cs @@ -113,13 +113,13 @@ namespace Barotrauma public void SetWallVertices(VertexPositionTexture[] vertices) { - wallVertices = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly); + wallVertices = new VertexBuffer(GameMain.Instance.GraphicsDevice, VertexPositionTexture.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly); wallVertices.SetData(vertices); } public void SetBodyVertices(VertexPositionColor[] vertices) { - bodyVertices = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly); + bodyVertices = new VertexBuffer(GameMain.Instance.GraphicsDevice, VertexPositionColor.VertexDeclaration, vertices.Length, BufferUsage.WriteOnly); bodyVertices.SetData(vertices); } diff --git a/Subsurface/Source/Map/Lights/ConvexHull.cs b/Subsurface/Source/Map/Lights/ConvexHull.cs index 941cc827e..bbea0b1da 100644 --- a/Subsurface/Source/Map/Lights/ConvexHull.cs +++ b/Subsurface/Source/Map/Lights/ConvexHull.cs @@ -173,12 +173,12 @@ namespace Barotrauma.Lights { if (shadowEffect == null) { - shadowEffect = new BasicEffect(GameMain.CurrGraphicsDevice); + shadowEffect = new BasicEffect(GameMain.Instance.GraphicsDevice); shadowEffect.VertexColorEnabled = true; } if (penumbraEffect == null) { - penumbraEffect = new BasicEffect(GameMain.CurrGraphicsDevice); + penumbraEffect = new BasicEffect(GameMain.Instance.GraphicsDevice); penumbraEffect.TextureEnabled = true; //shadowEffect.VertexColorEnabled = true; penumbraEffect.LightingEnabled = false; @@ -601,7 +601,7 @@ namespace Barotrauma.Lights } CalculateShadowVertices(lightSourcePos, los); - ShadowBuffer = new VertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionColor.VertexDeclaration, 6 * 2, BufferUsage.None); + ShadowBuffer = new VertexBuffer(GameMain.Instance.GraphicsDevice, VertexPositionColor.VertexDeclaration, 6 * 2, BufferUsage.None); ShadowBuffer.SetData(shadowVertices, 0, shadowVertices.Length); graphicsDevice.SetVertexBuffer(ShadowBuffer); diff --git a/Subsurface/Source/Map/Lights/LightManager.cs b/Subsurface/Source/Map/Lights/LightManager.cs index b1ae2f9f9..ba8212110 100644 --- a/Subsurface/Source/Map/Lights/LightManager.cs +++ b/Subsurface/Source/Map/Lights/LightManager.cs @@ -64,7 +64,7 @@ namespace Barotrauma.Lights if (lightEffect == null) { - lightEffect = new BasicEffect(GameMain.CurrGraphicsDevice); + lightEffect = new BasicEffect(GameMain.Instance.GraphicsDevice); lightEffect.VertexColorEnabled = false; lightEffect.TextureEnabled = true; diff --git a/Subsurface/Source/Map/Lights/LightSource.cs b/Subsurface/Source/Map/Lights/LightSource.cs index 0aeeada4e..e735fabe3 100644 --- a/Subsurface/Source/Map/Lights/LightSource.cs +++ b/Subsurface/Source/Map/Lights/LightSource.cs @@ -490,16 +490,16 @@ namespace Barotrauma.Lights //now we just create a buffer for 64 verts and make it larger if needed if (lightVolumeBuffer == null) { - lightVolumeBuffer = new DynamicVertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionTexture.VertexDeclaration, Math.Max(64, (int)(vertexCount*1.5)), BufferUsage.None); - lightVolumeIndexBuffer = new DynamicIndexBuffer(GameMain.CurrGraphicsDevice, typeof(short), Math.Max(64*3, (int)(indexCount * 1.5)), BufferUsage.None); + lightVolumeBuffer = new DynamicVertexBuffer(GameMain.Instance.GraphicsDevice, VertexPositionTexture.VertexDeclaration, Math.Max(64, (int)(vertexCount*1.5)), BufferUsage.None); + lightVolumeIndexBuffer = new DynamicIndexBuffer(GameMain.Instance.GraphicsDevice, typeof(short), Math.Max(64*3, (int)(indexCount * 1.5)), BufferUsage.None); } else if (vertexCount > lightVolumeBuffer.VertexCount) { lightVolumeBuffer.Dispose(); lightVolumeIndexBuffer.Dispose(); - lightVolumeBuffer = new DynamicVertexBuffer(GameMain.CurrGraphicsDevice, VertexPositionTexture.VertexDeclaration, (int)(vertexCount*1.5), BufferUsage.None); - lightVolumeIndexBuffer = new DynamicIndexBuffer(GameMain.CurrGraphicsDevice, typeof(short), (int)(indexCount * 1.5), BufferUsage.None); + lightVolumeBuffer = new DynamicVertexBuffer(GameMain.Instance.GraphicsDevice, VertexPositionTexture.VertexDeclaration, (int)(vertexCount*1.5), BufferUsage.None); + lightVolumeIndexBuffer = new DynamicIndexBuffer(GameMain.Instance.GraphicsDevice, typeof(short), (int)(indexCount * 1.5), BufferUsage.None); } lightVolumeBuffer.SetData(vertices.ToArray()); @@ -566,10 +566,10 @@ namespace Barotrauma.Lights } lightEffect.CurrentTechnique.Passes[0].Apply(); - GameMain.CurrGraphicsDevice.SetVertexBuffer(lightVolumeBuffer); - GameMain.CurrGraphicsDevice.Indices = lightVolumeIndexBuffer; - - GameMain.CurrGraphicsDevice.DrawIndexedPrimitives + GameMain.Instance.GraphicsDevice.SetVertexBuffer(lightVolumeBuffer); + GameMain.Instance.GraphicsDevice.Indices = lightVolumeIndexBuffer; + + GameMain.Instance.GraphicsDevice.DrawIndexedPrimitives ( PrimitiveType.TriangleList, 0, 0, indexCount / 3 ); diff --git a/Subsurface/Source/Networking/GameClient.cs b/Subsurface/Source/Networking/GameClient.cs index 3d6c0dcd7..fbb5e7fdf 100644 --- a/Subsurface/Source/Networking/GameClient.cs +++ b/Subsurface/Source/Networking/GameClient.cs @@ -561,7 +561,7 @@ namespace Barotrauma.Networking case (byte)PacketTypes.StartGame: if (Screen.Selected == GameMain.GameScreen) continue; - startGameCoroutine = GameMain.ShowLoading(StartGame(inc), false); + startGameCoroutine = GameMain.Instance.ShowLoading(StartGame(inc), false); break; case (byte)PacketTypes.EndGame: diff --git a/Subsurface/Source/Networking/GameServer.cs b/Subsurface/Source/Networking/GameServer.cs index 8fc732759..30cb6be2f 100644 --- a/Subsurface/Source/Networking/GameServer.cs +++ b/Subsurface/Source/Networking/GameServer.cs @@ -932,7 +932,7 @@ namespace Barotrauma.Networking } } - startGameCoroutine = GameMain.ShowLoading(StartGame(selectedSub, selectedShuttle, selectedMode), false); + startGameCoroutine = GameMain.Instance.ShowLoading(StartGame(selectedSub, selectedShuttle, selectedMode), false); yield return CoroutineStatus.Success; } diff --git a/Subsurface/Source/Screens/LobbyScreen.cs b/Subsurface/Source/Screens/LobbyScreen.cs index f94dbdeb1..26305c993 100644 --- a/Subsurface/Source/Screens/LobbyScreen.cs +++ b/Subsurface/Source/Screens/LobbyScreen.cs @@ -524,11 +524,8 @@ namespace Barotrauma { if (GameMain.GameSession.Map.SelectedConnection == null) return false; - GameMain.ShowLoading(ShiftLoading()); - - //GameMain.GameSession.StartShift(selectedLevel, false); - //GameMain.GameScreen.Select(); - + GameMain.Instance.ShowLoading(ShiftLoading()); + return true; }