From 8eddd696162d2f3ca35848ec851cdf850e790af6 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Wed, 1 Feb 2023 10:22:43 +0100 Subject: [PATCH] Add gpu.drawBuffer --- Capy64/Runtime/Libraries/GPU.cs | 47 ++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/Capy64/Runtime/Libraries/GPU.cs b/Capy64/Runtime/Libraries/GPU.cs index 278d4b4..4a9c589 100644 --- a/Capy64/Runtime/Libraries/GPU.cs +++ b/Capy64/Runtime/Libraries/GPU.cs @@ -107,6 +107,11 @@ public class GPU : IPlugin name = "newBuffer", function = L_NewBuffer, }, + new() + { + name = "drawBuffer", + function = L_DrawBuffer, + }, new(), // NULL }; @@ -420,23 +425,47 @@ public class GPU : IPlugin } // WIP - private static int L_CopyBuffer(IntPtr state) + private static int L_DrawBuffer(IntPtr state) { var L = Lua.FromIntPtr(state); - var x = L.CheckInteger(1); - var y = L.CheckInteger(2); - - var from = L.CheckObject(3, GPUBuffer.ObjectType, false); + var from = L.CheckObject(1, GPUBuffer.ObjectType, false); if (from is null) { - L.ArgumentError(3, GPUBuffer.ObjectType + " expected, got " + L.TypeName(L.Type(3))); + L.ArgumentError(1, GPUBuffer.ObjectType + " expected, got " + L.TypeName(L.Type(1))); } - var origin = new uint[_game.Width * _game.Height]; - _game.Drawing.Canvas.GetData(origin); + var x = (int)L.CheckInteger(2) - 1; + 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; + } - return 0; + L.Error(e.ToString()); + } + + return -1; } }