Skip to content

Commit

Permalink
RGBXY
Browse files Browse the repository at this point in the history
add catches
  • Loading branch information
SupinePandora43 committed Dec 28, 2020
1 parent cfc0ee1 commit 83d06eb
Show file tree
Hide file tree
Showing 4 changed files with 126 additions and 3 deletions.
1 change: 1 addition & 0 deletions GmodUltralight/GmodUltralight.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,6 @@
<ItemGroup>
<PackageReference Include="GmodNET.API" Version="[0.7.0-beta.2.30293992.master]" />
<PackageReference Include="ImpromptuNinjas.UltralightSharp" Version="1.2.0-beta-r11" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>
5 changes: 5 additions & 0 deletions GmodUltralight/View.server.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GmodNET.API;
using ImpromptuNinjas.UltralightSharp.Safe;
using System;
using System.Runtime.InteropServices;

namespace GmodUltralight
Expand Down Expand Up @@ -71,6 +72,10 @@ int View_DrawSingle(ILua lua)
SendPixel(lua, a, r, g, b, x, y);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
bitmap.UnlockPixels();
Expand Down
57 changes: 54 additions & 3 deletions GmodUltralight/View.shared.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using GmodNET.API;
using ImpromptuNinjas.UltralightSharp.Enums;
using ImpromptuNinjas.UltralightSharp.Safe;
using Newtonsoft.Json.Linq;
using String = GmodUltralight.Safe.String;

namespace GmodUltralight
Expand Down Expand Up @@ -258,18 +259,23 @@ void finishcallback(IntPtr data, View caller, ulong frameId, bool isMainFrame, s
case "ToAscii":
lua.PushManagedFunction(View_ToAscii);
break;
case "ToJsonRGBXY":
lua.PushManagedFunction(View_ToJsonRGBXY);
break;
// CLIENT
case "DrawDirty":
lua.PushManagedFunction(View_DrawDirty);
break;
case "DrawSingle":
lua.PushManagedFunction(View_DrawSingle);
break;
case "DrawToSurface":
lua.PushManagedFunction(View_DrawToSurface);
break;
case "DrawToSurfaceByLines":
lua.PushManagedFunction(View_DrawToSurfaceByLines);
break;
// SERVER
case "DrawSingle":
lua.PushManagedFunction(View_DrawSingle);
break;
default:
/*lua.PushManagedFunction((lua) =>
{
Expand Down Expand Up @@ -438,12 +444,57 @@ int View_ToAscii(ILua lua)
}
}
}
catch(Exception e)
{
Console.WriteLine(e);
}
finally
{
bitmap.UnlockPixels();
}
lua.PushString(stringBuilder.ToString());
return 1;
}
int View_ToJsonRGBXY(ILua lua)
{
string viewID = (string)GCHandle.FromIntPtr(lua.GetUserType(1, View_TypeId)).Target;
View view = views[viewID];
Surface surface = view.GetSurface();
Bitmap bitmap = surface.GetBitmap();

JArray pixelArray = new();

try
{
unsafe
{
byte* pixels = (byte*)bitmap.LockPixels();
long index = 0;
uint height = view.GetHeight();
uint width = view.GetWidth();
for (uint y = 0; y < height; y++)
{
for (uint x = 0; x < width; x++)
{
pixelArray.Add(new JArray(new[] { pixels[index + 2], pixels[index + 1], pixels[index], x, y }));

index += 4;
}
index = y * bitmap.GetRowBytes();
}
pixels = null;
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
finally
{
bitmap.UnlockPixels();
}
lua.PushString(pixelArray.ToString());
return 1;
}
}
}
66 changes: 66 additions & 0 deletions GmodUltralight/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ local function run_test()

assert(dohavelines)

local ToJsonRGBXY_start = SysTime()
local RGBXY = view:ToJsonRGBXY()
local ToJsonRGBXY_end = SysTime()

print("RGBXY", ToJsonRGBXY_end-ToJsonRGBXY_start)

print(RGBXY)

print("Dispose")
--view:Dispose()
view = null
Expand All @@ -75,6 +83,64 @@ run_test()
print("tests are successful!")
file.Write("success.txt", "done")

if CLIENT then

Ultralight = Ultralight or {}
function Ultralight.DrawFromDataRGBXY(data)
local jsonString = util.Decompress(data)
local tblToDraw = util.JSONToTable(jsonString)

local i = 0
local length = #tblToDraw
while i < length do
i = i +1
local obj = tblToDraw[i]
surface.SetDrawColor(obj[1],obj[2],obj[3])
surface.DrawRect(obj[4],obj[5],1,1)
end
end
-- TODO:
function Ultralight.DrawFromDataRGBAXY(data)
local jsonString = util.Decompress(data)
local tblToDraw = util.JSONToTable(jsonString)

local i = 0
local length = #tblToDraw
while i < length do
i = i +1
local obj = tblToDraw[i]
surface.SetDrawColor(obj[1],obj[2],obj[3],obj[4])
surface.DrawRect(obj[5],obj[6],1,1)
end
end
function Ultralight.DrawFromDataRGBXYWH(data)
local jsonString = util.Decompress(data)
local tblToDraw = util.JSONToTable(jsonString)

local i = 0
local length = #tblToDraw
while i < length do
i = i +1
local obj = tblToDraw[i]
surface.SetDrawColor(obj[1],obj[2],obj[3])
surface.DrawRect(obj[4],obj[5],obj[6],obj[7])
end
end
function Ultralight.DrawFromDataRGBAXYWH(data)
local jsonString = util.Decompress(data)
local tblToDraw = util.JSONToTable(jsonString)

local i = 0
local length = #tblToDraw
while i < length do
i = i +1
local obj = tblToDraw[i]
surface.SetDrawColor(obj[1],obj[2],obj[3],obj[4])
surface.DrawRect(obj[5],obj[6],obj[7],obj[8])
end
end
end

--Ultralight.View_SV_DrawDirty(view)

-- CLIENT
Expand Down

0 comments on commit 83d06eb

Please sign in to comment.