Add gpu.drawBuffer

This commit is contained in:
Alessandro Proto 2023-02-01 10:22:43 +01:00
parent 50461558e4
commit 8eddd69616

View file

@ -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<uint[]>(3, GPUBuffer.ObjectType, false);
var from = L.CheckObject<uint[]>(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;
}
}