(b451554e8) Merge remote-tracking branch 'origin/dev' into keymapping-refactor-test
This commit is contained in:
@@ -12,6 +12,8 @@ namespace Barotrauma
|
||||
{
|
||||
private static List<DeformableSprite> list = new List<DeformableSprite>();
|
||||
|
||||
private bool initialized = false;
|
||||
|
||||
private int triangleCount;
|
||||
|
||||
private VertexBuffer vertexBuffer, flippedVertexBuffer;
|
||||
@@ -34,7 +36,7 @@ namespace Barotrauma
|
||||
private Point spritePos;
|
||||
private Point spriteSize;
|
||||
|
||||
partial void InitProjSpecific(XElement element, int? subdivisionsX, int? subdivisionsY)
|
||||
partial void InitProjSpecific(XElement element, int? subdivisionsX, int? subdivisionsY, bool lazyLoad)
|
||||
{
|
||||
if (effect == null)
|
||||
{
|
||||
@@ -56,11 +58,30 @@ namespace Barotrauma
|
||||
throw new ArgumentException("Deformable sprites must have one or more subdivisions on each axis.");
|
||||
}
|
||||
|
||||
if (!lazyLoad)
|
||||
{
|
||||
Init();
|
||||
}
|
||||
|
||||
list.Add(this);
|
||||
}
|
||||
|
||||
public void EnsureLazyLoaded()
|
||||
{
|
||||
if (!initialized) { Init(); }
|
||||
}
|
||||
|
||||
private void Init()
|
||||
{
|
||||
if (initialized) { return; }
|
||||
initialized = true;
|
||||
|
||||
foreach (DeformableSprite existing in list)
|
||||
{
|
||||
if (!existing.initialized || existing == this) { continue; }
|
||||
//share vertex and index buffers if there's already
|
||||
//an existing sprite with the same texture and subdivisions
|
||||
if (existing.sprite.Texture == sprite.Texture &&
|
||||
if (existing.Sprite.Texture == Sprite.Texture &&
|
||||
existing.subDivX == subDivX &&
|
||||
existing.subDivY == subDivY &&
|
||||
existing.Sprite.SourceRect == Sprite.SourceRect)
|
||||
@@ -83,19 +104,18 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
if (sprite.Texture != null)
|
||||
if (Sprite.Texture != null)
|
||||
{
|
||||
SetupVertexBuffers();
|
||||
SetupIndexBuffer();
|
||||
}
|
||||
list.Add(this);
|
||||
}
|
||||
|
||||
private void SetupVertexBuffers()
|
||||
{
|
||||
Vector2 textureSize = new Vector2(sprite.Texture.Width, sprite.Texture.Height);
|
||||
var pos = sprite.SourceRect.Location;
|
||||
var size = sprite.SourceRect.Size;
|
||||
Vector2 textureSize = new Vector2(Sprite.Texture.Width, Sprite.Texture.Height);
|
||||
var pos = Sprite.SourceRect.Location;
|
||||
var size = Sprite.SourceRect.Size;
|
||||
|
||||
uvTopLeft = Vector2.Divide(pos.ToVector2(), textureSize);
|
||||
uvBottomRight = Vector2.Divide((pos + size).ToVector2(), textureSize);
|
||||
@@ -119,7 +139,7 @@ namespace Barotrauma
|
||||
uvTopLeft + (uvBottomRight - uvTopLeft) * relativePos;
|
||||
|
||||
vertices[x + y * (subDivX + 1)] = new VertexPositionColorTexture(
|
||||
position: new Vector3(relativePos.X * sprite.SourceRect.Width, relativePos.Y * sprite.SourceRect.Height, 0.0f),
|
||||
position: new Vector3(relativePos.X * Sprite.SourceRect.Width, relativePos.Y * Sprite.SourceRect.Height, 0.0f),
|
||||
color: Color.White,
|
||||
textureCoordinate: uvCoord);
|
||||
}
|
||||
@@ -153,8 +173,8 @@ namespace Barotrauma
|
||||
}
|
||||
}
|
||||
|
||||
spritePos = sprite.SourceRect.Location;
|
||||
spriteSize = sprite.SourceRect.Size;
|
||||
spritePos = Sprite.SourceRect.Location;
|
||||
spriteSize = Sprite.SourceRect.Size;
|
||||
}
|
||||
|
||||
private void SetupIndexBuffer()
|
||||
@@ -195,6 +215,8 @@ namespace Barotrauma
|
||||
/// </summary>
|
||||
public void Deform(Func<Vector2, Vector2> deformFunction)
|
||||
{
|
||||
if (!initialized) { Init(); }
|
||||
|
||||
var deformAmount = new Vector2[subDivX + 1, subDivY + 1];
|
||||
for (int x = 0; x <= subDivX; x++)
|
||||
{
|
||||
@@ -208,6 +230,8 @@ namespace Barotrauma
|
||||
|
||||
public void Deform(Vector2[,] deform)
|
||||
{
|
||||
if (!initialized) { Init(); }
|
||||
|
||||
deformArrayWidth = deform.GetLength(0);
|
||||
deformArrayHeight = deform.GetLength(1);
|
||||
if (deformAmount == null || deformAmount.Length != deformArrayWidth * deformArrayHeight)
|
||||
@@ -234,6 +258,8 @@ namespace Barotrauma
|
||||
|
||||
public Matrix GetTransform(Vector3 pos, Vector2 origin, float rotate, Vector2 scale)
|
||||
{
|
||||
if (!initialized) { Init(); }
|
||||
|
||||
return
|
||||
Matrix.CreateTranslation(-origin.X, -origin.Y, 0) *
|
||||
Matrix.CreateScale(scale.X, -scale.Y, 1.0f) *
|
||||
@@ -243,18 +269,19 @@ namespace Barotrauma
|
||||
|
||||
public void Draw(Camera cam, Vector3 pos, Vector2 origin, float rotate, Vector2 scale, Color color, bool flip = false)
|
||||
{
|
||||
if (sprite.Texture == null) { return; }
|
||||
if (Sprite.Texture == null) { return; }
|
||||
if (!initialized) { Init(); }
|
||||
|
||||
// If the source rect is modified, we should recalculate the vertex buffer.
|
||||
if (sprite.SourceRect.Location != spritePos || sprite.SourceRect.Size != spriteSize)
|
||||
if (Sprite.SourceRect.Location != spritePos || Sprite.SourceRect.Size != spriteSize)
|
||||
{
|
||||
SetupVertexBuffers();
|
||||
}
|
||||
|
||||
#if (LINUX || OSX)
|
||||
effect.Parameters["TextureSampler+xTexture"].SetValue(sprite.Texture);
|
||||
effect.Parameters["TextureSampler+xTexture"].SetValue(Sprite.Texture);
|
||||
#else
|
||||
effect.Parameters["xTexture"].SetValue(sprite.Texture);
|
||||
effect.Parameters["xTexture"].SetValue(Sprite.Texture);
|
||||
#endif
|
||||
|
||||
Matrix matrix = GetTransform(pos, origin, rotate, scale);
|
||||
@@ -274,8 +301,8 @@ namespace Barotrauma
|
||||
|
||||
public void Remove()
|
||||
{
|
||||
sprite?.Remove();
|
||||
sprite = null;
|
||||
Sprite?.Remove();
|
||||
Sprite = null;
|
||||
|
||||
list.Remove(this);
|
||||
|
||||
|
||||
@@ -55,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();
|
||||
@@ -76,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))
|
||||
|
||||
Reference in New Issue
Block a user