mirror of
https://github.com/Ale32bit/Capy64.git
synced 2025-01-18 18:46:43 +00:00
Added gpu.getPixel, fixed timer.sleep ignoring interrupt events
This commit is contained in:
parent
107019b68f
commit
5398405f77
4 changed files with 62 additions and 14 deletions
|
@ -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
|
|
@ -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<Point> 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)
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
{
|
||||
b = (byte)(packed & 0xff);
|
||||
|
|
Loading…
Reference in a new issue