Some cleanup (mostly removing redundancies from the GameMain class)

This commit is contained in:
Regalis
2017-05-03 00:27:13 +03:00
parent 39ddd63e0c
commit ba49f59204
12 changed files with 81 additions and 108 deletions

View File

@@ -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();
}
/// <summary>
@@ -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
/// </summary>
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<object> 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<object> loader, bool waitKeyHit = true)
public CoroutineHandle ShowLoading(IEnumerable<object> loader, bool waitKeyHit = true)
{
waitForKeyHit = waitKeyHit;
loadingScreenOpen = true;

View File

@@ -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;
}
}

View File

@@ -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;

View File

@@ -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)

View File

@@ -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;

View File

@@ -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);
}

View File

@@ -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);

View File

@@ -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;

View File

@@ -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<VertexPositionTexture>(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
);

View File

@@ -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:

View File

@@ -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;
}

View File

@@ -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;
}