diff --git a/Capy64/Core/Drawing.cs b/Capy64/Core/Drawing.cs index 378b5cf..9e97cd9 100644 --- a/Capy64/Core/Drawing.cs +++ b/Capy64/Core/Drawing.cs @@ -16,6 +16,7 @@ public class Drawing : IDisposable private Texture2D _whitePixel; private RenderTarget2D _canvas; private bool _isDrawing; + private HashSet _disposeTextures = new(); public RenderTarget2D Canvas { get => _canvas; @@ -34,7 +35,7 @@ public class Drawing : IDisposable Begin(); } } - + public Drawing() { _fontSystem = new FontSystem(); @@ -59,6 +60,11 @@ public class Drawing : IDisposable _spriteBatch.End(); _graphicsDevice.SetRenderTarget(null); + + foreach (var t in _disposeTextures) + t.Dispose(); + _disposeTextures.Clear(); + _isDrawing = false; } @@ -78,10 +84,10 @@ public class Drawing : IDisposable { if (point.X < 0 || point.Y < 0) return; if (point.X >= _canvas.Width || point.Y >= _canvas.Height) return; - + var grid = new Color[_canvas.Width * _canvas.Height]; _canvas.GetData(grid); - + grid[point.X + (point.Y * _canvas.Width)] = color; _canvas.SetData(grid); @@ -178,6 +184,20 @@ public class Drawing : IDisposable _spriteBatch.Draw(_whitePixel, position3, null, color, rotation, Vector2.Zero, scale, SpriteEffects.None, layerDepth); } + public void DrawTexture(Texture2D texture, Vector2 pos) + { + _spriteBatch.Draw(texture, pos, null, Color.White, 0f, Vector2.Zero, 1, SpriteEffects.None, 0f); + } + + public void DrawBuffer(uint[] buffer, Rectangle rect) + { + var texture = new Texture2D(_graphicsDevice, rect.Width, rect.Height, false, SurfaceFormat.Color); + texture.SetData(buffer); + + DrawTexture(texture, new(rect.X, rect.Y)); + _disposeTextures.Add(texture); + } + public void Dispose() { GC.SuppressFinalize(this); diff --git a/Capy64/Runtime/Libraries/GPU.cs b/Capy64/Runtime/Libraries/GPU.cs index a091af2..12b8bf7 100644 --- a/Capy64/Runtime/Libraries/GPU.cs +++ b/Capy64/Runtime/Libraries/GPU.cs @@ -435,8 +435,8 @@ public class GPU : IPlugin { var L = Lua.FromIntPtr(state); - var from = L.CheckObject(1, GPUBuffer.ObjectType, false); - if (from is null) + var buffer = L.CheckObject(1, GPUBuffer.ObjectType, false); + if (buffer is null) { L.ArgumentError(1, GPUBuffer.ObjectType + " expected, got " + L.TypeName(L.Type(1))); } @@ -445,32 +445,14 @@ public class GPU : IPlugin var y = (int)L.CheckInteger(3) - 1; var w = (int)L.CheckInteger(4); var h = (int)L.CheckInteger(5); - try - { - _game.Drawing.Canvas.SetData(0, new Rectangle - { - X = x, - Y = y, - Width = w, - Height = h, - }, from, 0, from.Length); - } - catch (Exception e) - { - if (w * h != from.Length) - { - L.Error("buffer size does not match width and height"); - return 0; - } - if(x < 0 || y < 0 || x + w >= _game.Drawing.Canvas.Width || y + h >= _game.Drawing.Canvas.Height) - { - L.Error("position out of bounds"); - return 0; - } - - L.Error(e.ToString()); - } + _game.Drawing.DrawBuffer(buffer, new() + { + X = x, + Y = y, + Width = w, + Height = h, + }); return 0; }