Fixed IDisposable implementation and usage.

- Implement IDisposable interface correctly, with sealed classes where possible for simplicity.
- Add using statement around undisposed local variables.
This commit is contained in:
RoosterDragon
2014-05-21 06:19:26 +01:00
parent 334a210231
commit a598a01108
37 changed files with 248 additions and 260 deletions

View File

@@ -70,6 +70,7 @@ namespace OpenRA
{
Game.OnQuit -= Cancel;
wc.CancelAsync();
wc.Dispose();
cancelled = true;
}
}

View File

@@ -17,7 +17,7 @@ using OpenRA.FileFormats;
namespace OpenRA.FileSystem
{
public class MixFile : IFolder
public sealed class MixFile : IFolder, IDisposable
{
readonly Dictionary<uint, PackageEntry> index;
readonly long dataStart;
@@ -258,5 +258,11 @@ namespace OpenRA.FileSystem
s.Write(file.Value);
}
}
public void Dispose()
{
if (s != null)
s.Dispose();
}
}
}

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
@@ -16,7 +17,7 @@ using SZipFile = ICSharpCode.SharpZipLib.Zip.ZipFile;
namespace OpenRA.FileSystem
{
public class ZipFile : IFolder
public sealed class ZipFile : IFolder, IDisposable
{
string filename;
SZipFile pkg;
@@ -105,6 +106,12 @@ namespace OpenRA.FileSystem
pkg.Close();
pkg = new SZipFile(new MemoryStream(File.ReadAllBytes(filename)));
}
public void Dispose()
{
if (pkg != null)
pkg.Close();
}
}
class StaticMemoryDataSource : IStaticDataSource

View File

@@ -504,6 +504,8 @@ namespace OpenRA
// Ensure that the active replay is properly saved
if (orderManager != null)
orderManager.Dispose();
Renderer.Device.Dispose();
OnQuit();
}

View File

@@ -31,7 +31,8 @@ namespace OpenRA.GameRules
return;
Exists = true;
Length = (int)AudLoader.SoundLength(GlobalFileSystem.Open(Filename));
using (var s = GlobalFileSystem.Open(Filename))
Length = (int)AudLoader.SoundLength(s);
}
public void Reload()
@@ -40,7 +41,8 @@ namespace OpenRA.GameRules
return;
Exists = true;
Length = (int)AudLoader.SoundLength(GlobalFileSystem.Open(Filename));
using (var s = GlobalFileSystem.Open(Filename))
Length = (int)AudLoader.SoundLength(s);
}
}
}

View File

@@ -39,31 +39,34 @@ namespace OpenRA.Graphics
public Sheet(string filename)
{
var bitmap = (Bitmap)Image.FromStream(GlobalFileSystem.Open(filename));
Size = bitmap.Size;
data = new byte[4*Size.Width*Size.Height];
var b = bitmap.LockBits(bitmap.Bounds(),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
using (var stream = GlobalFileSystem.Open(filename))
using (var bitmap = (Bitmap)Image.FromStream(stream))
{
int* c = (int*)b.Scan0;
Size = bitmap.Size;
for (var x = 0; x < Size.Width; x++)
for (var y = 0; y < Size.Height; y++)
data = new byte[4 * Size.Width * Size.Height];
var b = bitmap.LockBits(bitmap.Bounds(),
ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
var i = 4*Size.Width*y + 4*x;
int* c = (int*)b.Scan0;
// Convert argb to bgra
var argb = *(c + (y * b.Stride >> 2) + x);
data[i++] = (byte)(argb >> 0);
data[i++] = (byte)(argb >> 8);
data[i++] = (byte)(argb >> 16);
data[i++] = (byte)(argb >> 24);
for (var x = 0; x < Size.Width; x++)
for (var y = 0; y < Size.Height; y++)
{
var i = 4 * Size.Width * y + 4 * x;
// Convert argb to bgra
var argb = *(c + (y * b.Stride >> 2) + x);
data[i++] = (byte)(argb >> 0);
data[i++] = (byte)(argb >> 8);
data[i++] = (byte)(argb >> 16);
data[i++] = (byte)(argb >> 24);
}
}
bitmap.UnlockBits(b);
}
bitmap.UnlockBits(b);
}
public ITexture Texture

View File

@@ -108,27 +108,29 @@ namespace OpenRA.Graphics
Offset = { X = face.Glyph.BitmapLeft, Y = -face.Glyph.BitmapTop }
};
unsafe
{
var p = (byte*)face.Glyph.Bitmap.Buffer;
var dest = s.sheet.Data;
var destStride = s.sheet.Size.Width * 4;
for (var j = 0; j < s.size.Y; j++)
// A new bitmap is generated each time this property is accessed, so we do need to dispose it.
using (var bitmap = face.Glyph.Bitmap)
unsafe
{
for (var i = 0; i < s.size.X; i++)
if (p[i] != 0)
{
var q = destStride * (j + s.bounds.Top) + 4 * (i + s.bounds.Left);
dest[q] = c.Second.B;
dest[q + 1] = c.Second.G;
dest[q + 2] = c.Second.R;
dest[q + 3] = p[i];
}
var p = (byte*)bitmap.Buffer;
var dest = s.sheet.Data;
var destStride = s.sheet.Size.Width * 4;
p += face.Glyph.Bitmap.Pitch;
for (var j = 0; j < s.size.Y; j++)
{
for (var i = 0; i < s.size.X; i++)
if (p[i] != 0)
{
var q = destStride * (j + s.bounds.Top) + 4 * (i + s.bounds.Left);
dest[q] = c.Second.B;
dest[q + 1] = c.Second.G;
dest[q + 2] = c.Second.R;
dest[q + 3] = p[i];
}
p += bitmap.Pitch;
}
}
}
s.sheet.CommitData();
return g;

View File

@@ -33,10 +33,10 @@ namespace OpenRA.Graphics
Sprite[] CacheSpriteFrames(string filename)
{
var stream = GlobalFileSystem.OpenWithExts(filename, exts);
return SpriteSource.LoadSpriteSource(stream, filename).Frames
.Select(a => SheetBuilder.Add(a))
.ToArray();
using (var stream = GlobalFileSystem.OpenWithExts(filename, exts))
return SpriteSource.LoadSpriteSource(stream, filename).Frames
.Select(a => SheetBuilder.Add(a))
.ToArray();
}
public Sprite[] LoadAllSprites(string filename) { return sprites[filename]; }

View File

@@ -210,8 +210,12 @@ namespace OpenRA.Graphics
Voxel LoadFile(Pair<string,string> files)
{
var vxl = new VxlReader(GlobalFileSystem.OpenWithExts(files.First, ".vxl"));
var hva = new HvaReader(GlobalFileSystem.OpenWithExts(files.Second, ".hva"));
VxlReader vxl;
HvaReader hva;
using (var s = GlobalFileSystem.OpenWithExts(files.First, ".vxl"))
vxl = new VxlReader(s);
using (var s = GlobalFileSystem.OpenWithExts(files.Second, ".hva"))
hva = new HvaReader(s);
return new Voxel(this, vxl, hva);
}

View File

@@ -34,7 +34,7 @@ namespace OpenRA
public enum BlendMode { None, Alpha, Additive, Subtractive, Multiply }
public interface IGraphicsDevice
public interface IGraphicsDevice : IDisposable
{
IVertexBuffer<Vertex> CreateVertexBuffer(int length);
ITexture CreateTexture(Bitmap bitmap);
@@ -58,8 +58,6 @@ namespace OpenRA
void DisableDepthBuffer();
void SetBlendMode(BlendMode mode);
void Quit();
}
public interface IVertexBuffer<T>

View File

@@ -93,8 +93,9 @@ namespace OpenRA
List<string> extracted = new List<string>();
try
{
var z = new ZipInputStream(File.OpenRead(zipFile));
z.ExtractZip(dest, extracted, s => onProgress("Extracting " + s));
using (var stream = File.OpenRead(zipFile))
using (var z = new ZipInputStream(stream))
z.ExtractZip(dest, extracted, s => onProgress("Extracting " + s));
}
catch (SharpZipBaseException)
{

View File

@@ -172,13 +172,11 @@ namespace OpenRA
public static List<MiniYamlNode> FromFileInPackage(string path)
{
StreamReader reader = new StreamReader(GlobalFileSystem.Open(path));
List<string> lines = new List<string>();
while (!reader.EndOfStream)
lines.Add(reader.ReadLine());
reader.Close();
using (var stream = GlobalFileSystem.Open(path))
using (var reader = new StreamReader(stream))
while (!reader.EndOfStream)
lines.Add(reader.ReadLine());
return FromLines(lines.ToArray(), path);
}

View File

@@ -86,7 +86,7 @@ namespace OpenRA.Network
if (packet.Length == 0)
throw new NotImplementedException();
lock (this)
receivedPackets.Add(new ReceivedPacket { FromClient = LocalClientId, Data = packet } );
receivedPackets.Add(new ReceivedPacket { FromClient = LocalClientId, Data = packet });
}
public virtual void Receive(Action<int, byte[]> packetFn)
@@ -102,10 +102,16 @@ namespace OpenRA.Network
packetFn(p.FromClient, p.Data);
}
public virtual void Dispose() { }
protected virtual void Dispose(bool disposing) { }
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
class NetworkConnection : EchoConnection
sealed class NetworkConnection : EchoConnection
{
TcpClient socket;
int clientId;
@@ -193,22 +199,27 @@ namespace OpenRA.Network
bool disposed = false;
public override void Dispose()
protected override void Dispose(bool disposing)
{
if (disposed) return;
if (disposed)
return;
disposed = true;
GC.SuppressFinalize(this);
t.Abort();
if (socket != null)
socket.Client.Close();
if (disposing)
if (socket != null)
socket.Client.Close();
using (new PerfSample("Thread.Join"))
{
if (!t.Join(1000))
return;
}
base.Dispose(disposing);
}
~NetworkConnection() { Dispose(); }
~NetworkConnection()
{
Dispose(false);
}
}
}

View File

@@ -15,7 +15,7 @@ using OpenRA.Primitives;
namespace OpenRA.Network
{
public class OrderManager : IDisposable
public sealed class OrderManager : IDisposable
{
readonly SyncReport syncReport;
readonly FrameData frameData = new FrameData();
@@ -197,22 +197,10 @@ namespace OpenRA.Network
++NetFrameNumber;
}
bool disposed;
protected void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
Connection.Dispose();
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
if (Connection != null)
Connection.Dispose();
}
}
}

View File

@@ -16,7 +16,7 @@ using OpenRA.Primitives;
namespace OpenRA.Network
{
public class ReplayConnection : IConnection
public sealed class ReplayConnection : IConnection
{
class Chunk
{

View File

@@ -17,7 +17,7 @@ using OpenRA.Widgets;
namespace OpenRA.Network
{
class ReplayRecorderConnection : IConnection, IDisposable
sealed class ReplayRecorderConnection : IConnection
{
public ReplayMetadata Metadata;
@@ -98,30 +98,24 @@ namespace OpenRA.Network
}
bool disposed;
protected void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
if (Metadata != null)
{
if (Metadata.GameInfo != null)
Metadata.GameInfo.EndTimeUtc = DateTime.UtcNow;
Metadata.Write(writer);
}
writer.Close();
inner.Dispose();
}
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
if (disposed)
return;
disposed = true;
if (Metadata != null)
{
if (Metadata.GameInfo != null)
Metadata.GameInfo.EndTimeUtc = DateTime.UtcNow;
Metadata.Write(writer);
}
if (preStartBuffer != null)
preStartBuffer.Dispose();
writer.Close();
inner.Dispose();
}
}
}

View File

@@ -12,7 +12,7 @@ using System;
namespace OpenRA.Primitives
{
public class DisposableAction : IDisposable
public sealed class DisposableAction : IDisposable
{
public DisposableAction(Action onDispose, Action onFinalize)
{
@@ -26,7 +26,8 @@ namespace OpenRA.Primitives
public void Dispose()
{
if (disposed) return;
if (disposed)
return;
disposed = true;
onDispose();
GC.SuppressFinalize(this);

View File

@@ -81,12 +81,11 @@ namespace OpenRA.Scripting
public ScriptGlobalAttribute(string name) { Name = name; }
}
public class ScriptContext : IDisposable
public sealed class ScriptContext : IDisposable
{
public World World { get; private set; }
public WorldRenderer WorldRenderer { get; private set; }
bool disposed;
readonly MemoryConstrainedLuaRuntime runtime;
readonly LuaFunction tick;
@@ -100,6 +99,8 @@ namespace OpenRA.Scripting
public readonly Cache<ActorInfo, Type[]> ActorCommands;
public readonly Type[] PlayerCommands;
bool disposed;
public ScriptContext(World world, WorldRenderer worldRenderer,
IEnumerable<string> scripts)
{
@@ -196,27 +197,13 @@ namespace OpenRA.Scripting
tick.Call().Dispose();
}
protected void Dispose(bool disposing)
public void Dispose()
{
if (disposed)
return;
if (disposing)
runtime.Dispose();
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~ScriptContext()
{
// Dispose unmanaged resources only
Dispose(false);
if (runtime != null)
runtime.Dispose();
}
static Type[] ExtractRequiredTypes(Type t)

View File

@@ -39,9 +39,11 @@ namespace OpenRA
}
if (filename.ToLowerInvariant().EndsWith("wav"))
return LoadWave(new WavLoader(GlobalFileSystem.Open(filename)));
using (var s = GlobalFileSystem.Open(filename))
return LoadWave(new WavLoader(s));
return LoadSoundRaw(AudLoader.LoadSound(GlobalFileSystem.Open(filename)));
using (var s = GlobalFileSystem.Open(filename))
return LoadSoundRaw(AudLoader.LoadSound(s));
}
static ISoundSource LoadWave(WavLoader wave)

View File

@@ -105,7 +105,7 @@ namespace OpenRA.Support
}
}
public class PerfSample : IDisposable
public sealed class PerfSample : IDisposable
{
readonly Stopwatch sw = Stopwatch.StartNew();
readonly string Item;

View File

@@ -16,7 +16,7 @@ using System.Threading;
namespace OpenRA.Support
{
public class PerfTimer : IDisposable
public sealed class PerfTimer : IDisposable
{
readonly string name;
readonly float thresholdMs;

View File

@@ -57,7 +57,7 @@ namespace OpenRA
if (Game.Settings.Debug.ShowFatalErrorDialog && !Game.Settings.Server.Dedicated)
{
Game.Renderer.Device.Quit();
Game.Renderer.Device.Dispose();
Platform.ShowFatalErrorDialog();
}
}

View File

@@ -17,7 +17,7 @@ using OpenRA.Primitives;
namespace OpenRA.Irc
{
public class IrcClient : IDisposable
public sealed class IrcClient : IDisposable
{
public static readonly IrcClient Instance = new IrcClient();
@@ -252,14 +252,15 @@ namespace OpenRA.Irc
ConnectionState = IrcConnectionState.Disconnecting;
OnDisconnecting();
connection.Close();
if (connection != null)
connection.Close();
ConnectionState = IrcConnectionState.Disconnected;
OnDisconnect();
LocalUser = null;
connection = null;
}
void IDisposable.Dispose()
public void Dispose()
{
Disconnect();
}

View File

@@ -14,7 +14,7 @@ using System.Net.Sockets;
namespace OpenRA.Irc
{
public class IrcConnection : IDisposable
public sealed class IrcConnection : IDisposable
{
TcpClient socket;
Stream stream;
@@ -49,14 +49,8 @@ namespace OpenRA.Irc
public void Close()
{
CloseImpl();
GC.SuppressFinalize(this);
}
void CloseImpl()
{
if (disposed) return;
if (disposed)
return;
disposed = true;
if (socket != null) socket.Close();
if (stream != null) stream.Close();
@@ -64,16 +58,11 @@ namespace OpenRA.Irc
if (reader != null) reader.Close();
}
void IDisposable.Dispose()
public void Dispose()
{
Close();
}
~IrcConnection()
{
CloseImpl();
}
void CheckDisposed()
{
if (disposed)

View File

@@ -15,7 +15,7 @@ using OpenRA.Primitives;
namespace OpenRA.Mods.RA.Move
{
public class PathSearch : IDisposable
public sealed class PathSearch : IDisposable
{
World world;
public CellInfo[,] cellInfo;
@@ -291,11 +291,12 @@ namespace OpenRA.Mods.RA.Move
{
if (disposed)
return;
disposed = true;
GC.SuppressFinalize(this);
PutBackIntoPool(cellInfo);
cellInfo = null;
GC.SuppressFinalize(this);
}
~PathSearch() { Dispose(); }

View File

@@ -15,7 +15,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA.Activities
{
public class CallLuaFunc : Activity, IDisposable
public sealed class CallLuaFunc : Activity, IDisposable
{
LuaFunction function;
@@ -39,28 +39,11 @@ namespace OpenRA.Mods.RA.Activities
base.Cancel(self);
}
protected void Dispose(bool disposing)
{
if (function == null)
return;
if (disposing)
{
function.Dispose();
function = null;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~CallLuaFunc()
{
// Dispose unmanaged resources only
Dispose(false);
if (function == null) return;
function.Dispose();
function = null;
}
}
}

View File

@@ -8,6 +8,7 @@
*/
#endregion
using System;
using OpenRA.Graphics;
using OpenRA.Scripting;
using OpenRA.Traits;
@@ -21,7 +22,7 @@ namespace OpenRA.Mods.RA.Scripting
public object Create(ActorInitializer init) { return new LuaScript(this); }
}
public class LuaScript : ITick, IWorldLoaded
public sealed class LuaScript : ITick, IWorldLoaded, IDisposable
{
readonly LuaScriptInfo info;
ScriptContext context;
@@ -42,5 +43,11 @@ namespace OpenRA.Mods.RA.Scripting
{
context.Tick(self);
}
public void Dispose()
{
if (context != null)
context.Dispose();
}
}
}

View File

@@ -18,7 +18,7 @@ using OpenRA.Primitives;
namespace OpenRA.Mods.RA.Scripting
{
public class LuaScriptContext : IDisposable
public sealed class LuaScriptContext : IDisposable
{
public Lua Lua { get; private set; }
readonly Cache<string, LuaFunction> functionCache;
@@ -133,19 +133,9 @@ namespace OpenRA.Mods.RA.Scripting
}
public void Dispose()
{
if (Lua == null)
return;
GC.SuppressFinalize(this);
Lua.Dispose();
Lua = null;
}
~LuaScriptContext()
{
if (Lua != null)
Game.RunAfterTick(Dispose);
Lua.Dispose();
}
}
}

View File

@@ -32,7 +32,7 @@ namespace OpenRA.Mods.RA.Scripting
public object Create(ActorInitializer init) { return new LuaScriptInterface(this); }
}
public class LuaScriptInterface : IWorldLoaded, ITick
public sealed class LuaScriptInterface : IWorldLoaded, ITick, IDisposable
{
World world;
SpawnMapActors sma;
@@ -90,6 +90,11 @@ namespace OpenRA.Mods.RA.Scripting
context.InvokeLuaFunction("Tick");
}
public void Dispose()
{
context.Dispose();
}
[LuaGlobal]
public object New(string typeName, LuaTable args)
{

View File

@@ -21,7 +21,7 @@ namespace OpenRA.Mods.RA.Scripting
[Desc("Allows map scripts to attach triggers to this actor via the Triggers global.")]
public class ScriptTriggersInfo : TraitInfo<ScriptTriggers> { }
public class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, IDisposable
public sealed class ScriptTriggers : INotifyIdle, INotifyDamage, INotifyKilled, INotifyProduction, IDisposable
{
public event Action<Actor> OnKilledInternal = _ => {};
@@ -100,37 +100,11 @@ namespace OpenRA.Mods.RA.Scripting
}
}
bool disposed;
protected void Dispose(bool disposing)
{
if (disposed)
return;
if (disposing)
{
var toDispose = new [] { onIdle, onDamaged, onKilled, onProduction };
foreach (var f in toDispose.SelectMany(f => f))
f.First.Dispose();
foreach (var l in toDispose)
l.Clear();
}
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
~ScriptTriggers()
{
// Dispose unmanaged resources only
Dispose(false);
var pairs = new[] { onIdle, onDamaged, onKilled, onProduction };
pairs.SelectMany(l => l).Select(p => p.First).Do(f => f.Dispose());
pairs.Do(l => l.Clear());
}
}
}

View File

@@ -18,7 +18,6 @@ namespace OpenRA.Mods.RA.Widgets
{
public class HueSliderWidget : SliderWidget
{
Bitmap hueBitmap;
Sprite hueSprite;
public HueSliderWidget() {}
@@ -28,20 +27,22 @@ namespace OpenRA.Mods.RA.Widgets
{
base.Initialize(args);
hueBitmap = new Bitmap(256, 256);
hueSprite = new Sprite(new Sheet(new Size(256, 256)), new Rectangle(0, 0, 256, 1), TextureChannel.Alpha);
var bitmapData = hueBitmap.LockBits(hueBitmap.Bounds(),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
using (var hueBitmap = new Bitmap(256, 256))
{
int* c = (int*)bitmapData.Scan0;
for (var h = 0; h < 256; h++)
*(c + h) = HSLColor.FromHSV(h/255f, 1, 1).RGB.ToArgb();
}
hueBitmap.UnlockBits(bitmapData);
hueSprite = new Sprite(new Sheet(new Size(256, 256)), new Rectangle(0, 0, 256, 1), TextureChannel.Alpha);
hueSprite.sheet.Texture.SetData(hueBitmap);
var bitmapData = hueBitmap.LockBits(hueBitmap.Bounds(),
ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
unsafe
{
int* c = (int*)bitmapData.Scan0;
for (var h = 0; h < 256; h++)
*(c + h) = HSLColor.FromHSV(h / 255f, 1, 1).RGB.ToArgb();
}
hueBitmap.UnlockBits(bitmapData);
hueSprite.sheet.Texture.SetData(hueBitmap);
}
}
public override void Draw()

View File

@@ -68,16 +68,18 @@ namespace OpenRA.Mods.RA.Widgets
mapRect = new Rectangle(previewOrigin.X, previewOrigin.Y, (int)(previewScale * width), (int)(previewScale * height));
// Only needs to be done once
var terrainBitmap = Minimap.TerrainBitmap(world.Map.Rules.TileSets[world.Map.Tileset], world.Map);
var r = new Rectangle(0, 0, width, height);
var s = new Size(terrainBitmap.Width, terrainBitmap.Height);
terrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
terrainSprite.sheet.Texture.SetData(terrainBitmap);
using (var terrainBitmap = Minimap.TerrainBitmap(world.Map.Rules.TileSets[world.Map.Tileset], world.Map))
{
var r = new Rectangle(0, 0, width, height);
var s = new Size(terrainBitmap.Width, terrainBitmap.Height);
terrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
terrainSprite.sheet.Texture.SetData(terrainBitmap);
// Data is set in Tick()
customTerrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
actorSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
shroudSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
// Data is set in Tick()
customTerrainSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
actorSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
shroudSprite = new Sprite(new Sheet(s), r, TextureChannel.Alpha);
}
}
public override string GetCursor(int2 pos)
@@ -197,14 +199,17 @@ namespace OpenRA.Mods.RA.Widgets
if (updateTicks <= 0)
{
updateTicks = 12;
customTerrainSprite.sheet.Texture.SetData(Minimap.CustomTerrainBitmap(world));
using (var bitmap = Minimap.CustomTerrainBitmap(world))
customTerrainSprite.sheet.Texture.SetData(bitmap);
}
if (updateTicks == 8)
actorSprite.sheet.Texture.SetData(Minimap.ActorsBitmap(world));
using (var bitmap = Minimap.ActorsBitmap(world))
actorSprite.sheet.Texture.SetData(bitmap);
if (updateTicks == 4)
shroudSprite.sheet.Texture.SetData(Minimap.ShroudBitmap(world));
using (var bitmap = Minimap.ShroudBitmap(world))
shroudSprite.sheet.Texture.SetData(bitmap);
// Enable/Disable the radar
var enabled = IsEnabled();

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Renderer.Null
}
}
public class NullGraphicsDevice : IGraphicsDevice
public sealed class NullGraphicsDevice : IGraphicsDevice
{
public Size WindowSize { get; internal set; }
@@ -35,7 +35,7 @@ namespace OpenRA.Renderer.Null
WindowSize = size;
}
public void Quit() { }
public void Dispose() { }
public void EnableScissor(int left, int top, int width, int height) { }
public void DisableScissor() { }

View File

@@ -29,11 +29,12 @@ namespace OpenRA.Renderer.Sdl2
}
}
public class Sdl2GraphicsDevice : IGraphicsDevice
public sealed class Sdl2GraphicsDevice : IGraphicsDevice
{
Size size;
Sdl2Input input;
IntPtr context, window;
bool disposed;
public Size WindowSize { get { return size; } }
@@ -98,10 +99,21 @@ namespace OpenRA.Renderer.Sdl2
input = new Sdl2Input();
}
public virtual void Quit()
public void Dispose()
{
SDL.SDL_GL_DeleteContext(context);
SDL.SDL_DestroyWindow(window);
if (disposed)
return;
disposed = true;
if (context != IntPtr.Zero)
{
SDL.SDL_GL_DeleteContext(context);
context = IntPtr.Zero;
}
if (window != IntPtr.Zero)
{
SDL.SDL_DestroyWindow(window);
window = IntPtr.Zero;
}
SDL.SDL_Quit();
}

View File

@@ -106,18 +106,29 @@ namespace OpenRA.Renderer.Sdl2
public void SetData(Bitmap bitmap)
{
bool allocatedBitmap = false;
if (!Exts.IsPowerOf2(bitmap.Width) || !Exts.IsPowerOf2(bitmap.Height))
{
bitmap = new Bitmap(bitmap, bitmap.Size.NextPowerOf2());
allocatedBitmap = true;
}
try
{
size = new Size(bitmap.Width, bitmap.Height);
var bits = bitmap.LockBits(bitmap.Bounds(),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
size = new Size(bitmap.Width, bitmap.Height);
var bits = bitmap.LockBits(bitmap.Bounds(),
ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
PrepareTexture();
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, bits.Width, bits.Height,
0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bits.Scan0); // TODO: weird strides
ErrorHandler.CheckGlError();
bitmap.UnlockBits(bits);
PrepareTexture();
GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, bits.Width, bits.Height,
0, OpenTK.Graphics.OpenGL.PixelFormat.Bgra, PixelType.UnsignedByte, bits.Scan0); // TODO: weird strides
ErrorHandler.CheckGlError();
bitmap.UnlockBits(bits);
}
finally
{
if (allocatedBitmap)
bitmap.Dispose();
}
}
public byte[] GetData()

View File

@@ -51,8 +51,9 @@ namespace OpenRA.TilesetBuilder
this.size = TileSize;
surface1.TileSize = TileSize;
Bitmap fbitmap = new Bitmap(ImageFile);
Bitmap rbitmap = fbitmap.Clone(new Rectangle(0, 0, fbitmap.Width, fbitmap.Height),
Bitmap rbitmap;
using (var fbitmap = new Bitmap(ImageFile))
rbitmap = fbitmap.Clone(new Rectangle(0, 0, fbitmap.Width, fbitmap.Height),
fbitmap.PixelFormat);
int[] shadowIndex = { };

View File

@@ -162,7 +162,8 @@ namespace OpenRA.Utility
else
{
// CnC
UnpackCncTileData(GlobalFileSystem.Open(iniFile.Substring(0, iniFile.Length - 4) + ".bin"));
using (var s = GlobalFileSystem.Open(iniFile.Substring(0, iniFile.Length - 4) + ".bin"))
UnpackCncTileData(s);
ReadCncOverlay(file);
ReadCncTrees(file);
}