SoftwareRasterizer/RenderDevice.cs

57 lines
1.6 KiB
C#

using System.Numerics;
using System.Runtime.InteropServices;
using Raylib_cs;
public class RenderDevice
{
public int width { get; private set; }
public int height { get; private set; }
private int screenWidth, screenHeight;
private Texture2D renderTarget;
private IntPtr pixelPtr;
private byte[] pixelData;
public RenderDevice(int width, int height, int screenWidth, int screenHeight)
{
this.width = width;
this.height = height;
this.screenWidth = screenWidth;
this.screenHeight = screenHeight;
Image image = Raylib.GenImageColor(width, height, Color.Black);
renderTarget = Raylib.LoadTextureFromImage(image);
Raylib.UnloadImage(image);
pixelData = new byte[width * height * 4];
pixelPtr = Marshal.AllocHGlobal(pixelData.Length);
}
public void render()
{
Marshal.Copy(pixelData, 0, pixelPtr, pixelData.Length);
unsafe
{
Raylib.UpdateTexture(renderTarget, (void*)pixelPtr);
}
Raylib.DrawTexturePro(renderTarget, new Rectangle(0,0, width, height), new Rectangle(0, 0, screenWidth, screenHeight), Vector2.Zero, 0f, Color.White);
}
public void SetPixel(int x, int y, float3 color)
{
int idx = (y * width + x) * 4;
pixelData[idx] = (byte)color.r;
pixelData[idx + 1] = (byte)color.g;
pixelData[idx + 2] = (byte)color.b;
pixelData[idx + 3] = 0xFF;
}
public void cleanup()
{
Raylib.UnloadTexture(renderTarget);
Marshal.FreeHGlobal(pixelPtr); // always free after upload
}
}