From 92713cf587b8ce6ad856529d829fe320b26c0208 Mon Sep 17 00:00:00 2001 From: Joonas Rikkonen Date: Wed, 3 Apr 2019 16:25:02 +0300 Subject: [PATCH] (d041dfb1b) Implemented lazy sprite loading (= the texture isn't loaded until it's accessed for the first time). Atm only used in LevelObjectPrefabs and location portraits. --- .../BarotraumaClient/Source/GameSettings.cs | 19 ++++++++++ .../Map/Levels/LevelObjects/LevelObject.cs | 3 ++ .../Source/Map/Lights/LightManager.cs | 5 --- .../BarotraumaClient/Source/Sprite/Sprite.cs | 36 ++++++++++++++++--- .../Items/Components/Signal/LightComponent.cs | 4 --- .../Levels/LevelObjects/LevelObjectPrefab.cs | 4 +-- .../Source/Map/Map/LocationType.cs | 2 +- .../BarotraumaShared/Source/Sprite/Sprite.cs | 13 +++++-- 8 files changed, 68 insertions(+), 18 deletions(-) diff --git a/Barotrauma/BarotraumaClient/Source/GameSettings.cs b/Barotrauma/BarotraumaClient/Source/GameSettings.cs index 91e698e09..724e6a60c 100644 --- a/Barotrauma/BarotraumaClient/Source/GameSettings.cs +++ b/Barotrauma/BarotraumaClient/Source/GameSettings.cs @@ -720,6 +720,25 @@ namespace Barotrauma } }; + //spacing + new GUIFrame(new RectTransform(new Vector2(1.0f, 0.02f), generalLayoutGroup.RectTransform), style: null); + + new GUIButton(new RectTransform(new Vector2(0.4f, 1.0f), buttonArea.RectTransform, Anchor.BottomLeft), + TextManager.Get("Cancel"), style: "GUIButtonLarge") + { + IgnoreLayoutGroups = true, + OnClicked = (x, y) => + { + if (UnsavedSettings) + { + LoadPlayerConfig(); + } + if (Screen.Selected == GameMain.MainMenuScreen) GameMain.MainMenuScreen.ReturnToMainMenu(null, null); + GUI.SettingsMenuOpen = false; + return true; + } + }; + applyButton = new GUIButton(new RectTransform(new Vector2(0.4f, 1.0f), buttonArea.RectTransform, Anchor.BottomRight), TextManager.Get("ApplySettingsButton"), style: "GUIButtonLarge") { diff --git a/Barotrauma/BarotraumaClient/Source/Map/Levels/LevelObjects/LevelObject.cs b/Barotrauma/BarotraumaClient/Source/Map/Levels/LevelObjects/LevelObject.cs index aca975434..6ce5f1fb8 100644 --- a/Barotrauma/BarotraumaClient/Source/Map/Levels/LevelObjects/LevelObject.cs +++ b/Barotrauma/BarotraumaClient/Source/Map/Levels/LevelObjects/LevelObject.cs @@ -76,6 +76,9 @@ namespace Barotrauma partial void InitProjSpecific() { + Prefab.Sprite?.EnsureLazyLoaded(); + Prefab.SpecularSprite?.EnsureLazyLoaded(); + CurrentSwingAmount = Prefab.SwingAmountRad; CurrentScaleOscillation = Prefab.ScaleOscillation; diff --git a/Barotrauma/BarotraumaClient/Source/Map/Lights/LightManager.cs b/Barotrauma/BarotraumaClient/Source/Map/Lights/LightManager.cs index a1fb2e137..49fa1d587 100644 --- a/Barotrauma/BarotraumaClient/Source/Map/Lights/LightManager.cs +++ b/Barotrauma/BarotraumaClient/Source/Map/Lights/LightManager.cs @@ -291,11 +291,6 @@ namespace Barotrauma.Lights spriteBatch.Draw(HighlightMap, Vector2.Zero, Color.White); spriteBatch.End(); } - GameMain.ParticleManager.Draw(spriteBatch, true, null, Particles.ParticleBlendState.Additive); - spriteBatch.End(); - - //draw a black rectangle on hulls to hide background lights behind subs - //--------------------------------------------------------------------------------------------------- //draw characters to obstruct the highlighted items/characters and light sprites //--------------------------------------------------------------------------------------------------- diff --git a/Barotrauma/BarotraumaClient/Source/Sprite/Sprite.cs b/Barotrauma/BarotraumaClient/Source/Sprite/Sprite.cs index 0fc1be586..50640ffbf 100644 --- a/Barotrauma/BarotraumaClient/Source/Sprite/Sprite.cs +++ b/Barotrauma/BarotraumaClient/Source/Sprite/Sprite.cs @@ -14,7 +14,11 @@ namespace Barotrauma public Texture2D Texture { - get { return texture; } + get + { + EnsureLazyLoaded(); + return texture; + } } public Sprite(Texture2D texture, Rectangle? sourceRectangle, Vector2? newOffset, float newRotation = 0.0f) @@ -51,6 +55,28 @@ namespace Barotrauma if (sourceVector.W == 0.0f) sourceVector.W = texture.Height; } + public void EnsureLazyLoaded() + { + if (!lazyLoad || texture != null) { return; } + + Vector4 sourceVector = Vector4.Zero; + bool temp2 = false; + LoadTexture(ref sourceVector, ref temp2, preMultipliedAlpha); + if (sourceRect.Width == 0 && sourceRect.Height == 0) + { + sourceRect = new Rectangle((int)sourceVector.X, (int)sourceVector.Y, (int)sourceVector.Z, (int)sourceVector.W); + size = SourceElement.GetAttributeVector2("size", Vector2.One); + size.X *= sourceRect.Width; + size.Y *= sourceRect.Height; + RelativeOrigin = SourceElement.GetAttributeVector2("origin", new Vector2(0.5f, 0.5f)); + } + foreach (Sprite s in list) + { + if (s == this) { continue; } + if (s.FullPath == FullPath && s.texture != null) { s.texture = texture; } + } + } + public void ReloadTexture() { var sprites = LoadedSprites.Where(s => s.Texture == texture).ToList(); @@ -72,11 +98,12 @@ namespace Barotrauma public static Texture2D LoadTexture(string file, bool preMultiplyAlpha = true) { + if (string.IsNullOrWhiteSpace(file)) { return new Texture2D(GameMain.GraphicsDeviceManager.GraphicsDevice, 1, 1); } file = Path.GetFullPath(file); foreach (Sprite s in list) { - if (s.FullPath == file) return s.texture; + if (s.FullPath == file && s.texture != null) { return s.texture; } } if (File.Exists(file)) @@ -109,7 +136,7 @@ namespace Barotrauma public virtual void Draw(SpriteBatch spriteBatch, Vector2 pos, Color color, Vector2 origin, float rotate, Vector2 scale, SpriteEffects spriteEffect = SpriteEffects.None, float? depth = null) { - if (texture == null) return; + if (Texture == null) { return; } //DrawSilhouette(spriteBatch, pos, origin, rotate, scale, spriteEffect, depth); spriteBatch.Draw(texture, pos + offset, sourceRect, color, rotation + rotate, origin, scale, spriteEffect, depth ?? this.depth); } @@ -119,7 +146,7 @@ namespace Barotrauma /// public void DrawSilhouette(SpriteBatch spriteBatch, Vector2 pos, Vector2 origin, float rotate, Vector2 scale, SpriteEffects spriteEffect = SpriteEffects.None, float? depth = null) { - if (texture == null) return; + if (Texture == null) { return; } for (int x = -1; x <= 1; x += 2) { for (int y = -1; y <= 1; y += 2) @@ -132,6 +159,7 @@ namespace Barotrauma public void DrawTiled(SpriteBatch spriteBatch, Vector2 position, Vector2 targetSize, Rectangle? rect = null, Color? color = null, Point? startOffset = null, Vector2? textureScale = null, float? depth = null) { + if (Texture == null) { return; } //Init optional values Vector2 drawOffset = startOffset.HasValue ? new Vector2(startOffset.Value.X, startOffset.Value.Y) : Vector2.Zero; Vector2 scale = textureScale ?? Vector2.One; diff --git a/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs b/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs index eebf1146a..7845f609c 100644 --- a/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs +++ b/Barotrauma/BarotraumaShared/Source/Items/Components/Signal/LightComponent.cs @@ -26,10 +26,6 @@ namespace Barotrauma.Items.Components private float blinkTimer; - private bool itemLoaded; - - private float blinkTimer; - public PhysicsBody ParentBody; [Editable(MinValueFloat = 0.0f, MaxValueFloat = 2048.0f), Serialize(100.0f, true)] diff --git a/Barotrauma/BarotraumaShared/Source/Map/Levels/LevelObjects/LevelObjectPrefab.cs b/Barotrauma/BarotraumaShared/Source/Map/Levels/LevelObjects/LevelObjectPrefab.cs index 79c61f55e..7246fd680 100644 --- a/Barotrauma/BarotraumaShared/Source/Map/Levels/LevelObjects/LevelObjectPrefab.cs +++ b/Barotrauma/BarotraumaShared/Source/Map/Levels/LevelObjects/LevelObjectPrefab.cs @@ -331,10 +331,10 @@ namespace Barotrauma switch (subElement.Name.ToString().ToLowerInvariant()) { case "sprite": - Sprite = new Sprite(subElement); + Sprite = new Sprite(subElement, lazyLoad: true); break; case "specularsprite": - SpecularSprite = new Sprite(subElement); + SpecularSprite = new Sprite(subElement, lazyLoad: true); break; case "deformablesprite": DeformableSprite = new DeformableSprite(subElement); diff --git a/Barotrauma/BarotraumaShared/Source/Map/Map/LocationType.cs b/Barotrauma/BarotraumaShared/Source/Map/Map/LocationType.cs index aecfd8149..7e22a1f71 100644 --- a/Barotrauma/BarotraumaShared/Source/Map/Map/LocationType.cs +++ b/Barotrauma/BarotraumaShared/Source/Map/Map/LocationType.cs @@ -126,7 +126,7 @@ namespace Barotrauma CanChangeTo.Add(new LocationTypeChange(Identifier, subElement)); break; case "portrait": - var portrait = new Sprite(subElement); + var portrait = new Sprite(subElement, lazyLoad: true); if (portrait != null) { portraits.Add(portrait); diff --git a/Barotrauma/BarotraumaShared/Source/Sprite/Sprite.cs b/Barotrauma/BarotraumaShared/Source/Sprite/Sprite.cs index d6141f6ad..7862e4e82 100644 --- a/Barotrauma/BarotraumaShared/Source/Sprite/Sprite.cs +++ b/Barotrauma/BarotraumaShared/Source/Sprite/Sprite.cs @@ -28,6 +28,10 @@ namespace Barotrauma //the offset used when drawing the sprite protected Vector2 offset; + private bool preMultipliedAlpha; + + private bool lazyLoad; + protected Vector2 origin; //the size of the drawn sprite, if larger than the source, @@ -99,8 +103,9 @@ namespace Barotrauma partial void LoadTexture(ref Vector4 sourceVector, ref bool shouldReturn, bool premultiplyAlpha = true); partial void CalculateSourceRect(); - public Sprite(XElement element, string path = "", string file = "", bool? preMultiplyAlpha = null) + public Sprite(XElement element, string path = "", string file = "", bool? preMultiplyAlpha = null, bool lazyLoad = false) { + this.lazyLoad = lazyLoad; SourceElement = element; if (file == "") { @@ -123,8 +128,12 @@ namespace Barotrauma Name = SourceElement.GetAttributeString("name", null); Vector4 sourceVector = SourceElement.GetAttributeVector4("sourcerect", Vector4.Zero); + preMultipliedAlpha = preMultiplyAlpha ?? SourceElement.GetAttributeBool("premultiplyalpha", true); bool shouldReturn = false; - LoadTexture(ref sourceVector, ref shouldReturn, preMultiplyAlpha ?? SourceElement.GetAttributeBool("premultiplyalpha", true)); + if (!lazyLoad) + { + LoadTexture(ref sourceVector, ref shouldReturn, preMultipliedAlpha); + } if (shouldReturn) return; sourceRect = new Rectangle((int)sourceVector.X, (int)sourceVector.Y, (int)sourceVector.Z, (int)sourceVector.W); size = SourceElement.GetAttributeVector2("size", Vector2.One);