(ded4a3e0a) v0.9.0.7

This commit is contained in:
Joonas Rikkonen
2019-06-25 16:00:44 +03:00
parent e5ae622c77
commit 4a51db77b5
1777 changed files with 421528 additions and 917 deletions
@@ -0,0 +1,63 @@
using System.Collections.Generic;
using System.Threading;
namespace Barotrauma
{
public static class CrossThread
{
public delegate void TaskDelegate();
private class Task
{
public TaskDelegate Deleg;
public ManualResetEvent Mre;
public bool Done;
public Task(TaskDelegate d)
{
Deleg = d;
Mre = new ManualResetEvent(false);
Done = false;
}
public void PerformWait()
{
if (!Done) { Mre.WaitOne(); }
}
}
private static List<Task> enqueuedTasks;
static CrossThread() { enqueuedTasks = new List<Task>(); }
public static void ProcessTasks()
{
lock (enqueuedTasks)
{
foreach (var task in enqueuedTasks)
{
task.Deleg();
task.Mre.Set();
task.Done = true;
}
enqueuedTasks.Clear();
}
}
public static void RequestExecutionOnMainThread(TaskDelegate deleg)
{
if (Thread.CurrentThread == GameMain.MainThread)
{
deleg();
}
else
{
Task newTask = new Task(deleg);
lock (enqueuedTasks)
{
enqueuedTasks.Add(newTask);
}
newTask.PerformWait();
}
}
}
}
@@ -18,34 +18,26 @@ namespace Barotrauma
public string FileName { get; private set; }
public string[] FileNames { get; private set; }
public OpenFileDialog()
{
ofd = new System.Windows.Forms.OpenFileDialog();
}
public OpenFileDialog() { }
public System.Windows.Forms.DialogResult ShowDialog()
{
ofd = new System.Windows.Forms.OpenFileDialog();
ofd.Multiselect = Multiselect;
ofd.InitialDirectory = InitialDirectory;
ofd.Filter = Filter;
ofd.Title = Title;
#if LINUX
System.Windows.Forms.DialogResult result;
var wrapperForm = new WrapperForm(ofd);
System.Windows.Forms.Application.Run(wrapperForm);
System.Windows.Forms.Application.Exit();
FileName = wrapperForm.FileName;
FileNames = wrapperForm.FileNames;
return wrapperForm.Result;
#else
var result = ofd.ShowDialog();
FileName = ofd.FileName;
FileNames = ofd.FileNames;
result = wrapperForm.Result;
ofd = null;
return result;
#endif
}
#if LINUX
private class WrapperForm : System.Windows.Forms.Form
{
private System.Windows.Forms.OpenFileDialog ofd;
@@ -66,9 +58,8 @@ namespace Barotrauma
FileName = ofd.FileName;
FileNames = ofd.FileNames;
System.Threading.Thread.Sleep(100);
this.Close();
System.Windows.Forms.Application.Exit();
}
}
#endif
}
}
@@ -1,7 +1,7 @@
using Microsoft.Xna.Framework.Graphics;
using System;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using Color = Microsoft.Xna.Framework.Color;
namespace Barotrauma
@@ -43,15 +43,18 @@ namespace Barotrauma
_graphicsDevice = graphicsDevice;
_needsBmp = needsBmp;
_spriteBatch = new SpriteBatch(_graphicsDevice);
PlaceHolderTexture = new Texture2D(graphicsDevice, 32, 32);
Color[] data = new Color[32 * 32];
for (int i = 0; i < 32 * 32; i++)
{
data[i] = Color.Magenta;
}
PlaceHolderTexture.SetData(data);
CrossThread.RequestExecutionOnMainThread(() =>
{
PlaceHolderTexture = new Texture2D(graphicsDevice, 32, 32);
PlaceHolderTexture.SetData(data);
});
}
public static Texture2D FromFile(string path, bool preMultiplyAlpha = true)
@@ -60,12 +63,7 @@ namespace Barotrauma
{
using (Stream fileStream = File.OpenRead(path))
{
var texture = Texture2D.FromStream(_graphicsDevice, fileStream);
if (preMultiplyAlpha)
{
PreMultiplyAlpha(ref texture);
}
return texture;
return FromStream(fileStream, preMultiplyAlpha, path);
}
}
@@ -76,16 +74,33 @@ namespace Barotrauma
}
}
public static Texture2D FromStream(Stream fileStream, bool preMultiplyAlpha = true)
public static Texture2D FromStream(Stream fileStream, bool preMultiplyAlpha = true, string path=null)
{
try
{
var texture = Texture2D.FromStream(_graphicsDevice, fileStream);
int width = 0; int height = 0; int channels = 0;
byte[] textureData = null;
Task loadTask = Task.Run(() =>
{
textureData = Texture2D.TextureDataFromStream(fileStream, out width, out height, out channels);
});
bool success = loadTask.Wait(10000);
if (!success)
{
DebugConsole.ThrowError("Failed to load texture data from " + (path ?? "stream") + ": timed out");
return null;
}
if (preMultiplyAlpha)
{
PreMultiplyAlpha(ref texture);
PreMultiplyAlpha(ref textureData);
}
return texture;
Texture2D tex = null;
CrossThread.RequestExecutionOnMainThread(() =>
{
tex = new Texture2D(_graphicsDevice, width, height);
tex.SetData(textureData);
});
return tex;
}
catch (Exception e)
{
@@ -94,40 +109,35 @@ namespace Barotrauma
}
}
private static void PreMultiplyAlpha(ref Texture2D texture)
private static void PreMultiplyAlpha(ref byte[] data)
{
UInt32[] data = new UInt32[texture.Width * texture.Height];
texture.GetData(data);
for (int i = 0; i < data.Length; i++)
for (int i = 0; i < data.Length; i+=4)
{
uint a = (data[i] & 0xff000000) >> 24;
uint a = data[i+3];
if (a == 0)
{
data[i] = 0;
data[i + 0] = 0;
data[i + 1] = 0;
data[i + 2] = 0;
continue;
}
else if (a == uint.MaxValue)
{
continue;
}
uint r = (data[i] & 0x00ff0000) >> 16;
uint g = (data[i] & 0x0000ff00) >> 8;
uint b = (data[i] & 0x000000ff);
uint r = data[i+0];
uint g = data[i+1];
uint b = data[i+2];
// Monogame 3.7 needs the line below.
a *= a; a /= 255;
b *= a; b /= 255;
g *= a; g /= 255;
r *= a; r /= 255;
data[i] = (a << 24) | (r << 16) | (g << 8) | b;
data[i + 0] = (byte)r;
data[i + 1] = (byte)g;
data[i + 2] = (byte)b;
data[i + 3] = (byte)a;
}
//not sure why this is needed, but it seems to cut the memory usage of the game almost in half
//GetData/SetData might be leaking memory?
int width = texture.Width; int height = texture.Height;
texture.Dispose();
texture = new Texture2D(_graphicsDevice, width, height);
texture.SetData(data);
}
@@ -138,4 +148,4 @@ namespace Barotrauma
private static SpriteBatch _spriteBatch;
private static bool _needsBmp;
}
}
}