Fixed timestep for some GUI & input code

Fixed timestep only applied to the physics simulation, but input and GUI updates were not being handled properly, so here's some progress into fixing them.
This commit is contained in:
juanjp600
2016-09-18 11:03:52 -03:00
parent 179c0424d7
commit 33641ead3e
14 changed files with 153 additions and 50 deletions
+11 -2
View File
@@ -450,13 +450,11 @@ namespace Barotrauma
if (pauseMenuOpen) if (pauseMenuOpen)
{ {
pauseMenu.Update(0.016f);
pauseMenu.Draw(spriteBatch); pauseMenu.Draw(spriteBatch);
} }
if (settingsMenuOpen) if (settingsMenuOpen)
{ {
GameMain.Config.SettingsFrame.Update(0.016f);
GameMain.Config.SettingsFrame.Draw(spriteBatch); GameMain.Config.SettingsFrame.Draw(spriteBatch);
} }
@@ -469,6 +467,17 @@ namespace Barotrauma
public static void Update(float deltaTime) public static void Update(float deltaTime)
{ {
if (pauseMenuOpen)
{
pauseMenu.Update(0.016f);
}
if (settingsMenuOpen)
{
GameMain.Config.SettingsFrame.Update(0.016f);
}
if (GUIMessageBox.MessageBoxes.Count > 0) if (GUIMessageBox.MessageBoxes.Count > 0)
{ {
var messageBox = GUIMessageBox.MessageBoxes.Peek(); var messageBox = GUIMessageBox.MessageBoxes.Peek();
+22 -15
View File
@@ -162,6 +162,27 @@ namespace Barotrauma
{ {
if (!Visible) return; if (!Visible) return;
//Color currColor = color;
//if (state == ComponentState.Hover) currColor = hoverColor;
//if (state == ComponentState.Selected) currColor = selectedColor;
//GUI.DrawRectangle(spriteBatch, rect, currColor * alpha, true);
////spriteBatch.DrawString(HUD.font, text, new Vector2(rect.X+rect.Width/2, rect.Y+rect.Height/2), Color.Black, 0.0f, new Vector2(0.5f,0.5f), 1.0f, SpriteEffects.None, 0.0f);
//GUI.DrawRectangle(spriteBatch, rect, Color.Black * alpha, false);
DrawChildren(spriteBatch);
//if (!Enabled) GUI.DrawRectangle(spriteBatch, rect, Color.Gray*0.5f, true);
}
public override void Update(float deltaTime)
{
if (!Visible) return;
base.Update(deltaTime);
if (rect.Contains(PlayerInput.MousePosition) && CanBeSelected && Enabled && (MouseOn == null || MouseOn == this || IsParentOf(MouseOn))) if (rect.Contains(PlayerInput.MousePosition) && CanBeSelected && Enabled && (MouseOn == null || MouseOn == this || IsParentOf(MouseOn)))
{ {
state = ComponentState.Hover; state = ComponentState.Hover;
@@ -183,7 +204,7 @@ namespace Barotrauma
else else
{ {
Selected = !Selected; Selected = !Selected;
// state = state == ComponentState.Selected ? ComponentState.None : ComponentState.Selected; // state = state == ComponentState.Selected ? ComponentState.None : ComponentState.Selected;
} }
} }
} }
@@ -193,20 +214,6 @@ namespace Barotrauma
} }
frame.State = state; frame.State = state;
//Color currColor = color;
//if (state == ComponentState.Hover) currColor = hoverColor;
//if (state == ComponentState.Selected) currColor = selectedColor;
//GUI.DrawRectangle(spriteBatch, rect, currColor * alpha, true);
////spriteBatch.DrawString(HUD.font, text, new Vector2(rect.X+rect.Width/2, rect.Y+rect.Height/2), Color.Black, 0.0f, new Vector2(0.5f,0.5f), 1.0f, SpriteEffects.None, 0.0f);
//GUI.DrawRectangle(spriteBatch, rect, Color.Black * alpha, false);
DrawChildren(spriteBatch);
//if (!Enabled) GUI.DrawRectangle(spriteBatch, rect, Color.Gray*0.5f, true);
} }
} }
} }
+41 -23
View File
@@ -71,6 +71,9 @@ namespace Barotrauma
private bool hasLoaded; private bool hasLoaded;
private GameTime fixedTime;
private double updatesToMake;
//public static Random localRandom; //public static Random localRandom;
//public static Random random; //public static Random random;
@@ -141,6 +144,9 @@ namespace Barotrauma
IsFixedTimeStep = false; IsFixedTimeStep = false;
//TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 55); //TargetElapsedTime = new TimeSpan(0, 0, 0, 0, 55);
updatesToMake = 0.0;
fixedTime = new GameTime();
World = new World(new Vector2(0, -9.82f)); World = new World(new Vector2(0, -9.82f));
FarseerPhysics.Settings.AllowSleep = true; FarseerPhysics.Settings.AllowSleep = true;
FarseerPhysics.Settings.ContinuousPhysics = false; FarseerPhysics.Settings.ContinuousPhysics = false;
@@ -286,40 +292,52 @@ namespace Barotrauma
/// <param name="gameTime">Provides a snapshot of timing values.</param> /// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime) protected override void Update(GameTime gameTime)
{ {
base.Update(gameTime); double realDeltaTime = gameTime.ElapsedGameTime.TotalSeconds;
double deltaTime = 0.016;
updatesToMake += realDeltaTime;
double deltaTime = gameTime.ElapsedGameTime.TotalSeconds; while (updatesToMake > 0.0)
PlayerInput.Update(deltaTime);
bool paused = false;
if (hasLoaded && !titleScreenOpen)
{ {
SoundPlayer.Update(); fixedTime.IsRunningSlowly = gameTime.IsRunningSlowly;
TimeSpan addTime = new TimeSpan(0,0,0,0,16);
fixedTime.ElapsedGameTime = addTime;
fixedTime.TotalGameTime.Add(addTime);
base.Update(fixedTime);
if (PlayerInput.KeyHit(Keys.Escape)) GUI.TogglePauseMenu(); PlayerInput.Update(deltaTime);
DebugConsole.Update(this, (float)deltaTime); bool paused = false;
paused = (DebugConsole.IsOpen || GUI.PauseMenuOpen || GUI.SettingsMenuOpen) && if (hasLoaded && !titleScreenOpen)
(NetworkMember == null || !NetworkMember.GameStarted);
if (!paused) Screen.Selected.Update(deltaTime);
if (NetworkMember != null)
{
NetworkMember.Update((float)deltaTime);
}
else
{ {
SoundPlayer.Update();
if (PlayerInput.KeyHit(Keys.Escape)) GUI.TogglePauseMenu();
DebugConsole.Update(this, (float)deltaTime);
paused = (DebugConsole.IsOpen || GUI.PauseMenuOpen || GUI.SettingsMenuOpen) &&
(NetworkMember == null || !NetworkMember.GameStarted);
if (!paused || Screen.Selected is MainMenuScreen) Screen.Selected.Update(deltaTime);
if (NetworkMember != null)
{
NetworkMember.Update((float)deltaTime);
}
else
{
}
GUI.Update((float)deltaTime);
} }
GUI.Update((float)deltaTime); CoroutineManager.Update((float)deltaTime, paused ? 0.0f : (float)deltaTime);
updatesToMake -= deltaTime;
} }
CoroutineManager.Update((float)deltaTime, paused ? 0.0f : (float)deltaTime);
} }
@@ -470,6 +470,8 @@ namespace Barotrauma.Items.Components
public virtual void DrawHUD(SpriteBatch spriteBatch, Character character) { } public virtual void DrawHUD(SpriteBatch spriteBatch, Character character) { }
public virtual void UpdateHUD(Character character) { }
/// <returns>true if the operation was completed</returns> /// <returns>true if the operation was completed</returns>
public virtual bool AIOperate(float deltaTime, Character character, AIObjectiveOperateItem objective) public virtual bool AIOperate(float deltaTime, Character character, AIObjectiveOperateItem objective)
{ {
@@ -82,10 +82,14 @@ namespace Barotrauma.Items.Components
public override void DrawHUD(SpriteBatch spriteBatch, Character character) public override void DrawHUD(SpriteBatch spriteBatch, Character character)
{ {
GuiFrame.Update((float)Physics.step);
GuiFrame.Draw(spriteBatch); GuiFrame.Draw(spriteBatch);
} }
public override void UpdateHUD(Character character)
{
GuiFrame.Update((float)Physics.step);
}
private bool ToggleActive(GUIButton button, object obj) private bool ToggleActive(GUIButton button, object obj)
{ {
SetActive(!IsActive); SetActive(!IsActive);
@@ -338,6 +338,11 @@ namespace Barotrauma.Items.Components
} }
public override void DrawHUD(SpriteBatch spriteBatch, Character character) public override void DrawHUD(SpriteBatch spriteBatch, Character character)
{
GuiFrame.Draw(spriteBatch);
}
public override void UpdateHUD(Character character)
{ {
FabricableItem targetItem = itemList.SelectedData as FabricableItem; FabricableItem targetItem = itemList.SelectedData as FabricableItem;
if (targetItem != null) if (targetItem != null)
@@ -346,7 +351,6 @@ namespace Barotrauma.Items.Components
} }
GuiFrame.Update((float)Physics.step); GuiFrame.Update((float)Physics.step);
GuiFrame.Draw(spriteBatch);
} }
private bool CanBeFabricated(FabricableItem fabricableItem, Character user) private bool CanBeFabricated(FabricableItem fabricableItem, Character user)
@@ -156,13 +156,17 @@ namespace Barotrauma.Items.Components
int x = GuiFrame.Rect.X; int x = GuiFrame.Rect.X;
int y = GuiFrame.Rect.Y; int y = GuiFrame.Rect.Y;
GuiFrame.Update(1.0f / 60.0f);
GuiFrame.Draw(spriteBatch); GuiFrame.Draw(spriteBatch);
spriteBatch.DrawString(GUI.Font, "Pumping speed: " + (int)flowPercentage + " %", new Vector2(x + 40, y + 85), Color.White); spriteBatch.DrawString(GUI.Font, "Pumping speed: " + (int)flowPercentage + " %", new Vector2(x + 40, y + 85), Color.White);
} }
public override void UpdateHUD(Character character)
{
GuiFrame.Update(1.0f / 60.0f);
}
public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f) public override void ReceiveSignal(int stepsTaken, string signal, Connection connection, Item sender, float power=0.0f)
{ {
base.ReceiveSignal(stepsTaken, signal, connection, sender, power); base.ReceiveSignal(stepsTaken, signal, connection, sender, power);
@@ -102,8 +102,6 @@ namespace Barotrauma.Items.Components
public override void DrawHUD(SpriteBatch spriteBatch, Character character) public override void DrawHUD(SpriteBatch spriteBatch, Character character)
{ {
GuiFrame.Update(1.0f / 60.0f);
GuiFrame.Draw(spriteBatch); GuiFrame.Draw(spriteBatch);
if (voltage < minVoltage && powerConsumption > 0.0f) return; if (voltage < minVoltage && powerConsumption > 0.0f) return;
@@ -112,6 +110,11 @@ namespace Barotrauma.Items.Components
DrawRadar(spriteBatch, new Rectangle((int)GuiFrame.Center.X - radius, (int)GuiFrame.Center.Y - radius, radius * 2, radius * 2)); DrawRadar(spriteBatch, new Rectangle((int)GuiFrame.Center.X - radius, (int)GuiFrame.Center.Y - radius, radius * 2, radius * 2));
} }
public override void UpdateHUD(Character character)
{
GuiFrame.Update(1.0f / 60.0f);
}
private void DrawRadar(SpriteBatch spriteBatch, Rectangle rect) private void DrawRadar(SpriteBatch spriteBatch, Rectangle rect)
{ {
Vector2 center = new Vector2(rect.X + rect.Width*0.5f, rect.Center.Y); Vector2 center = new Vector2(rect.X + rect.Width*0.5f, rect.Center.Y);
@@ -438,7 +438,6 @@ namespace Barotrauma.Items.Components
int x = GuiFrame.Rect.X; int x = GuiFrame.Rect.X;
int y = GuiFrame.Rect.Y; int y = GuiFrame.Rect.Y;
GuiFrame.Update(1.0f / 60.0f);
GuiFrame.Draw(spriteBatch); GuiFrame.Draw(spriteBatch);
float xOffset = (graphTimer / (float)updateGraphInterval); float xOffset = (graphTimer / (float)updateGraphInterval);
@@ -481,6 +480,11 @@ namespace Barotrauma.Items.Components
//y = y - 260; //y = y - 260;
} }
public override void UpdateHUD(Character character)
{
GuiFrame.Update(1.0f / 60.0f);
}
private bool ToggleAutoTemp(GUITickBox tickBox) private bool ToggleAutoTemp(GUITickBox tickBox)
{ {
unsentChanges = true; unsentChanges = true;
@@ -161,7 +161,6 @@ namespace Barotrauma.Items.Components
int x = GuiFrame.Rect.X; int x = GuiFrame.Rect.X;
int y = GuiFrame.Rect.Y; int y = GuiFrame.Rect.Y;
GuiFrame.Update(1.0f / 60.0f);
GuiFrame.Draw(spriteBatch); GuiFrame.Draw(spriteBatch);
if (voltage < minVoltage && powerConsumption > 0.0f) return; if (voltage < minVoltage && powerConsumption > 0.0f) return;
@@ -210,6 +209,11 @@ namespace Barotrauma.Items.Components
} }
} }
public override void UpdateHUD(Character character)
{
GuiFrame.Update(1.0f / 60.0f);
}
private void UpdateAutoPilot(float deltaTime) private void UpdateAutoPilot(float deltaTime)
{ {
if (posToMaintain != null) if (posToMaintain != null)
+7 -2
View File
@@ -184,18 +184,23 @@ namespace Barotrauma
} }
public static void DrawHud(SpriteBatch spriteBatch, Item item, Character character) public static void DrawHud(SpriteBatch spriteBatch, Item item, Character character)
{
if (frame == null) return;
frame.Draw(spriteBatch);
}
public static void UpdateHud(Item item,Character character)
{ {
if (frame == null || frame.UserData != item) if (frame == null || frame.UserData != item)
{ {
CreateGUIFrame(item); CreateGUIFrame(item);
} }
UpdateGUIFrame(item, character); UpdateGUIFrame(item, character);
if (frame == null) return; if (frame == null) return;
frame.Update((float)Physics.step); frame.Update((float)Physics.step);
frame.Draw(spriteBatch);
} }
} }
} }
+14
View File
@@ -1057,6 +1057,20 @@ namespace Barotrauma
} }
} }
public virtual void UpdateHUD(Character character)
{
if (condition <= 0.0f)
{
FixRequirement.UpdateHud(this, character);
return;
}
foreach (ItemComponent ic in components)
{
ic.UpdateHUD(character);
}
}
public List<T> GetConnectedComponents<T>(bool recursive = false) public List<T> GetConnectedComponents<T>(bool recursive = false)
{ {
List<T> connectedComponents = new List<T>(); List<T> connectedComponents = new List<T>();
+8
View File
@@ -108,6 +108,14 @@ namespace Barotrauma
Character.UpdateAll(cam, (float)deltaTime); Character.UpdateAll(cam, (float)deltaTime);
if (Character.Controlled != null && Character.Controlled.SelectedConstruction != null)
{
if (Character.Controlled.SelectedConstruction == Character.Controlled.ClosestItem)
{
Character.Controlled.SelectedConstruction.UpdateHUD(Character.Controlled);
}
}
BackgroundCreatureManager.Update(cam, (float)deltaTime); BackgroundCreatureManager.Update(cam, (float)deltaTime);
GameMain.ParticleManager.Update((float)deltaTime); GameMain.ParticleManager.Update((float)deltaTime);
@@ -12,6 +12,8 @@ namespace Barotrauma
{ {
public enum Tab { NewGame = 1, LoadGame = 2, HostServer = 3, Settings = 4 } public enum Tab { NewGame = 1, LoadGame = 2, HostServer = 3, Settings = 4 }
private GUIButton[] menuButtons;
GUIFrame buttonsTab; GUIFrame buttonsTab;
private GUIFrame[] menuTabs; private GUIFrame[] menuTabs;
@@ -43,42 +45,52 @@ namespace Barotrauma
290, y, 290, y,
500, 360); 500, 360);
menuButtons = new GUIButton[8];
GUIButton button = new GUIButton(new Rectangle(50, y, 200, 30), "Tutorial", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab); GUIButton button = new GUIButton(new Rectangle(50, y, 200, 30), "Tutorial", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f; button.Color = button.Color * 0.8f;
button.OnClicked = TutorialButtonClicked; button.OnClicked = TutorialButtonClicked;
menuButtons[0] = button;
button = new GUIButton(new Rectangle(50, y + 60, 200, 30), "New Game", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab); button = new GUIButton(new Rectangle(50, y + 60, 200, 30), "New Game", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f; button.Color = button.Color * 0.8f;
button.UserData = Tab.NewGame; button.UserData = Tab.NewGame;
button.OnClicked = SelectTab; button.OnClicked = SelectTab;
menuButtons[1] = button;
button = new GUIButton(new Rectangle(50, y + 100, 200, 30), "Load Game", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab); button = new GUIButton(new Rectangle(50, y + 100, 200, 30), "Load Game", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f; button.Color = button.Color * 0.8f;
button.UserData = Tab.LoadGame; button.UserData = Tab.LoadGame;
button.OnClicked = SelectTab; button.OnClicked = SelectTab;
menuButtons[2] = button;
button = new GUIButton(new Rectangle(50, y + 160, 200, 30), "Join Server", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab); button = new GUIButton(new Rectangle(50, y + 160, 200, 30), "Join Server", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f; button.Color = button.Color * 0.8f;
//button.UserData = (int)Tabs.JoinServer; //button.UserData = (int)Tabs.JoinServer;
button.OnClicked = JoinServerClicked; button.OnClicked = JoinServerClicked;
menuButtons[3] = button;
button = new GUIButton(new Rectangle(50, y + 200, 200, 30), "Host Server", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab); button = new GUIButton(new Rectangle(50, y + 200, 200, 30), "Host Server", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f; button.Color = button.Color * 0.8f;
button.UserData = Tab.HostServer; button.UserData = Tab.HostServer;
button.OnClicked = SelectTab; button.OnClicked = SelectTab;
menuButtons[4] = button;
button = new GUIButton(new Rectangle(50, y + 260, 200, 30), "Submarine Editor", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab); button = new GUIButton(new Rectangle(50, y + 260, 200, 30), "Submarine Editor", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f; button.Color = button.Color * 0.8f;
button.OnClicked = (GUIButton btn, object userdata) => { GameMain.EditMapScreen.Select(); return true; }; button.OnClicked = (GUIButton btn, object userdata) => { GameMain.EditMapScreen.Select(); return true; };
menuButtons[5] = button;
button = new GUIButton(new Rectangle(50, y + 320, 200, 30), "Settings", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab); button = new GUIButton(new Rectangle(50, y + 320, 200, 30), "Settings", null, Alignment.TopLeft, Alignment.Left, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f; button.Color = button.Color * 0.8f;
button.UserData = Tab.Settings; button.UserData = Tab.Settings;
button.OnClicked = SelectTab; button.OnClicked = SelectTab;
menuButtons[6] = button;
button = new GUIButton(new Rectangle(0, 0, 150, 30), "Quit", Alignment.BottomRight, GUI.Style, buttonsTab); button = new GUIButton(new Rectangle(0, 0, 150, 30), "Quit", Alignment.BottomRight, GUI.Style, buttonsTab);
button.Color = button.Color * 0.8f; button.Color = button.Color * 0.8f;
button.OnClicked = QuitClicked; button.OnClicked = QuitClicked;
menuButtons[7] = button;
panelRect.Y += 10; panelRect.Y += 10;
@@ -478,6 +490,11 @@ namespace Barotrauma
public override void Update(double deltaTime) public override void Update(double deltaTime)
{ {
foreach (GUIButton button in menuButtons)
{
button.Update((float)deltaTime);
}
buttonsTab.Update((float)deltaTime); buttonsTab.Update((float)deltaTime);
if (selectedTab>0) menuTabs[selectedTab].Update((float)deltaTime); if (selectedTab>0) menuTabs[selectedTab].Update((float)deltaTime);