From 006850935792001975f47491e056fc4def954a05 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Wed, 1 Feb 2023 18:59:26 +0100 Subject: [PATCH] Add gpu.loadImage --- Capy64/Runtime/Libraries/GPU.cs | 45 ++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/Capy64/Runtime/Libraries/GPU.cs b/Capy64/Runtime/Libraries/GPU.cs index deea9bb..a091af2 100644 --- a/Capy64/Runtime/Libraries/GPU.cs +++ b/Capy64/Runtime/Libraries/GPU.cs @@ -2,8 +2,10 @@ using Capy64.Runtime.Objects; using KeraLua; using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; using System; using System.Collections.Generic; +using System.IO; namespace Capy64.Runtime.Libraries; @@ -112,6 +114,11 @@ public class GPU : IPlugin name = "drawBuffer", function = L_DrawBuffer, }, + new() + { + name = "loadImage", + function = L_LoadImage, + }, new(), // NULL }; @@ -424,7 +431,6 @@ public class GPU : IPlugin return 1; } - // WIP private static int L_DrawBuffer(IntPtr state) { var L = Lua.FromIntPtr(state); @@ -468,4 +474,41 @@ public class GPU : IPlugin return 0; } + + private static int L_LoadImage(IntPtr state) + { + var L = Lua.FromIntPtr(state); + + var path = L.CheckString(1); + + path = FileSystem.Resolve(path); + + if (!File.Exists(path)) + { + L.Error("file not found"); + return 0; + } + + Texture2D texture; + try + { + texture = Texture2D.FromFile(Capy64.Instance.Drawing.Canvas.GraphicsDevice, path); + } + catch (Exception e) + { + L.Error(e.Message); + return 0; + } + + var data = new uint[texture.Width * texture.Height]; + texture.GetData(data); + + GPUBuffer.Push(L, data); + L.PushInteger(texture.Width); + L.PushInteger(texture.Height); + + texture.Dispose(); + + return 3; + } }