From 5398405f77051ca5156146b69677302c493f1c4e Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 20 Jan 2023 21:29:59 +0100 Subject: [PATCH] Added gpu.getPixel, fixed timer.sleep ignoring interrupt events --- Capy64/Assets/Lua/boot/03_timer.lua | 3 ++- Capy64/Core/Drawing.cs | 29 +++++++++++++++++++++-------- Capy64/LuaRuntime/Libraries/GPU.cs | 27 +++++++++++++++++++++++---- Capy64/Utils.cs | 17 ++++++++++++++++- 4 files changed, 62 insertions(+), 14 deletions(-) diff --git a/Capy64/Assets/Lua/boot/03_timer.lua b/Capy64/Assets/Lua/boot/03_timer.lua index fe06efa..be532f4 100644 --- a/Capy64/Assets/Lua/boot/03_timer.lua +++ b/Capy64/Assets/Lua/boot/03_timer.lua @@ -1,4 +1,5 @@ local timer = require("timer") +local event = require("event") local expect = require("expect").expect local range = require("expect").range @@ -8,6 +9,6 @@ function timer.sleep(n) local timerId = timer.start(n) repeat - local _, par = coroutine.yield("timer") + local _, par = event.pull("timer") until par == timerId end \ No newline at end of file diff --git a/Capy64/Core/Drawing.cs b/Capy64/Core/Drawing.cs index b588bce..e053a6f 100644 --- a/Capy64/Core/Drawing.cs +++ b/Capy64/Core/Drawing.cs @@ -28,12 +28,16 @@ public class Drawing : IDisposable _spriteBatch = new SpriteBatch(_canvas.GraphicsDevice); _graphicsDevice = _canvas.GraphicsDevice; + Grid = new Color[_canvas.Width * _canvas.Height]; + _canvas.GetData(Grid); + _whitePixel = new Texture2D(_spriteBatch.GraphicsDevice, 1, 1, mipmap: false, SurfaceFormat.Color); _whitePixel.SetData(new Color[1] { Color.White }); if (isDrawing) Begin(); } } + public Color[] Grid; public Drawing() { @@ -60,6 +64,7 @@ public class Drawing : IDisposable _spriteBatch.End(); _graphicsDevice.SetRenderTarget(null); _isDrawing = false; + _canvas.GetData(Grid); } public void DrawString(Vector2 pos, string text, Color color, int size = 13) @@ -76,27 +81,35 @@ public class Drawing : IDisposable public void Plot(Point point, Color color) { - var grid = new Color[_canvas.Width * _canvas.Height]; - _canvas.GetData(grid); + //var grid = new Color[_canvas.Width * _canvas.Height]; + _canvas.GetData(Grid); if (point.X < 0 || point.Y < 0) return; if (point.X >= _canvas.Width || point.Y >= _canvas.Height) return; - grid[point.X + (point.Y * _canvas.Width)] = color; + Grid[point.X + (point.Y * _canvas.Width)] = color; - _canvas.SetData(grid); + _canvas.SetData(Grid); } public void Plot(IEnumerable points, Color color) { - var grid = new Color[_canvas.Width * _canvas.Height]; - _canvas.GetData(grid); + //var grid = new Color[_canvas.Width * _canvas.Height]; + _canvas.GetData(Grid); foreach (var point in points) { if (point.X < 0 || point.Y < 0) continue; if (point.X >= _canvas.Width || point.Y >= _canvas.Height) continue; - grid[point.X + (point.Y * _canvas.Width)] = color; + Grid[point.X + (point.Y * _canvas.Width)] = color; } - _canvas.SetData(grid); + _canvas.SetData(Grid); + } + + public Color GetPixel(Point point) + { + if (point.X < 0 || point.Y < 0) return Color.Black; + if (point.X >= _canvas.Width || point.Y >= _canvas.Height) return Color.Black; + + return Grid[point.X + (point.Y * _canvas.Width)]; } public void UnsafePlot(Point point, Color color) diff --git a/Capy64/LuaRuntime/Libraries/GPU.cs b/Capy64/LuaRuntime/Libraries/GPU.cs index 7c848e2..27e348c 100644 --- a/Capy64/LuaRuntime/Libraries/GPU.cs +++ b/Capy64/LuaRuntime/Libraries/GPU.cs @@ -37,6 +37,11 @@ public class GPU : IPlugin function = L_SetScale, }, new() + { + name = "getPixel", + function = L_GetPixel, + }, + new() { name = "plot", function = L_Plot, @@ -147,6 +152,20 @@ public class GPU : IPlugin return 0; } + private static int L_GetPixel(IntPtr state) + { + var L = Lua.FromIntPtr(state); + + var x = (int)L.CheckNumber(1) - 1; + var y = (int)L.CheckNumber(2) - 1; + + var c = _game.Drawing.GetPixel(new(x, y)); + + L.PushInteger(Utils.PackRGB(c)); + + return 1; + } + private static int L_Plot(IntPtr state) { var L = Lua.FromIntPtr(state); @@ -231,10 +250,10 @@ public class GPU : IPlugin { var L = Lua.FromIntPtr(state); - var x1 = (int)L.CheckNumber(1) - 1; - var y1 = (int)L.CheckNumber(2) - 1; - var x2 = (int)L.CheckNumber(3) - 1; - var y2 = (int)L.CheckNumber(4) - 1; + var x1 = (int)L.CheckNumber(1); + var y1 = (int)L.CheckNumber(2); + var x2 = (int)L.CheckNumber(3); + var y2 = (int)L.CheckNumber(4); var c = L.CheckInteger(5); var s = (int)L.OptNumber(6, 1); diff --git a/Capy64/Utils.cs b/Capy64/Utils.cs index 36f4b36..fe2d8e5 100644 --- a/Capy64/Utils.cs +++ b/Capy64/Utils.cs @@ -1,4 +1,6 @@ -namespace Capy64; +using Microsoft.Xna.Framework; + +namespace Capy64; public static class Utils { @@ -6,6 +8,19 @@ public static class Utils { public int Top, Bottom, Left, Right; } + + /// + /// Return the sane 0xRRGGBB format + /// + /// + public static int PackRGB(Color color) + { + return + (color.R << 16) + + (color.G << 8) + + (color.B); + } + public static void UnpackRGB(uint packed, out byte r, out byte g, out byte b) { b = (byte)(packed & 0xff);