Added gpu.getPixel, fixed timer.sleep ignoring interrupt events

This commit is contained in:
Alessandro Proto 2023-01-20 21:29:59 +01:00
parent 107019b68f
commit 5398405f77
4 changed files with 62 additions and 14 deletions

View file

@ -1,4 +1,5 @@
local timer = require("timer") local timer = require("timer")
local event = require("event")
local expect = require("expect").expect local expect = require("expect").expect
local range = require("expect").range local range = require("expect").range
@ -8,6 +9,6 @@ function timer.sleep(n)
local timerId = timer.start(n) local timerId = timer.start(n)
repeat repeat
local _, par = coroutine.yield("timer") local _, par = event.pull("timer")
until par == timerId until par == timerId
end end

View file

@ -28,12 +28,16 @@ public class Drawing : IDisposable
_spriteBatch = new SpriteBatch(_canvas.GraphicsDevice); _spriteBatch = new SpriteBatch(_canvas.GraphicsDevice);
_graphicsDevice = _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 = new Texture2D(_spriteBatch.GraphicsDevice, 1, 1, mipmap: false, SurfaceFormat.Color);
_whitePixel.SetData(new Color[1] { Color.White }); _whitePixel.SetData(new Color[1] { Color.White });
if (isDrawing) if (isDrawing)
Begin(); Begin();
} }
} }
public Color[] Grid;
public Drawing() public Drawing()
{ {
@ -60,6 +64,7 @@ public class Drawing : IDisposable
_spriteBatch.End(); _spriteBatch.End();
_graphicsDevice.SetRenderTarget(null); _graphicsDevice.SetRenderTarget(null);
_isDrawing = false; _isDrawing = false;
_canvas.GetData(Grid);
} }
public void DrawString(Vector2 pos, string text, Color color, int size = 13) 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) public void Plot(Point point, Color color)
{ {
var grid = new Color[_canvas.Width * _canvas.Height]; //var grid = new Color[_canvas.Width * _canvas.Height];
_canvas.GetData(grid); _canvas.GetData(Grid);
if (point.X < 0 || point.Y < 0) return; if (point.X < 0 || point.Y < 0) return;
if (point.X >= _canvas.Width || point.Y >= _canvas.Height) 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<Point> points, Color color) public void Plot(IEnumerable<Point> points, Color color)
{ {
var grid = new Color[_canvas.Width * _canvas.Height]; //var grid = new Color[_canvas.Width * _canvas.Height];
_canvas.GetData(grid); _canvas.GetData(Grid);
foreach (var point in points) foreach (var point in points)
{ {
if (point.X < 0 || point.Y < 0) continue; if (point.X < 0 || point.Y < 0) continue;
if (point.X >= _canvas.Width || point.Y >= _canvas.Height) 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) public void UnsafePlot(Point point, Color color)

View file

@ -37,6 +37,11 @@ public class GPU : IPlugin
function = L_SetScale, function = L_SetScale,
}, },
new() new()
{
name = "getPixel",
function = L_GetPixel,
},
new()
{ {
name = "plot", name = "plot",
function = L_Plot, function = L_Plot,
@ -147,6 +152,20 @@ public class GPU : IPlugin
return 0; 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) private static int L_Plot(IntPtr state)
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
@ -231,10 +250,10 @@ public class GPU : IPlugin
{ {
var L = Lua.FromIntPtr(state); var L = Lua.FromIntPtr(state);
var x1 = (int)L.CheckNumber(1) - 1; var x1 = (int)L.CheckNumber(1);
var y1 = (int)L.CheckNumber(2) - 1; var y1 = (int)L.CheckNumber(2);
var x2 = (int)L.CheckNumber(3) - 1; var x2 = (int)L.CheckNumber(3);
var y2 = (int)L.CheckNumber(4) - 1; var y2 = (int)L.CheckNumber(4);
var c = L.CheckInteger(5); var c = L.CheckInteger(5);
var s = (int)L.OptNumber(6, 1); var s = (int)L.OptNumber(6, 1);

View file

@ -1,4 +1,6 @@
namespace Capy64; using Microsoft.Xna.Framework;
namespace Capy64;
public static class Utils public static class Utils
{ {
@ -6,6 +8,19 @@ public static class Utils
{ {
public int Top, Bottom, Left, Right; public int Top, Bottom, Left, Right;
} }
/// <summary>
/// Return the sane 0xRRGGBB format
/// </summary>
/// <param name="color"></param>
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) public static void UnpackRGB(uint packed, out byte r, out byte g, out byte b)
{ {
b = (byte)(packed & 0xff); b = (byte)(packed & 0xff);