SoftwareRasterizer/SoftwareRasterizer.cs
2025-06-02 22:16:49 -04:00

101 lines
2.3 KiB
C#

using System.Runtime.CompilerServices;
using Raylib_cs;
class SoftwareRasterizer
{
const int screenWidth = 1280 * 2;
const int screenHeight = 800 * 2;
const int scale = 8;
private RenderDevice renderer;
static void Main()
{
SoftwareRasterizer sr = new SoftwareRasterizer();
sr.start();
}
struct BouncingPoint()
{
float posX, posY;
float velX, velY;
public float2 pos => new(posX, posY);
public BouncingPoint(float posX, float posY, float velX, float velY) : this()
{
this.posX = posX;
this.posY = posY;
this.velX = velX;
this.velY = velY;
}
public void Update()
{
posX += velX;
posY += velY;
if (posX >= 1.0 || posX <= -1.0f)
{
velX = -velX;
}
if (posY >= 1.0 || posY <= -1.0f)
{
velY = -velY;
}
}
}
public void start()
{
Raylib.InitWindow(screenWidth, screenHeight, "Software Rasterizer");
Raylib.SetTargetFPS(60);
renderer = new RenderDevice(screenWidth / scale, screenHeight / scale, screenWidth, screenHeight);
VertexColorShader shader = new VertexColorShader();
int frameCounter = 0;
Random rand = new Random();
BouncingPoint v0 = new BouncingPoint( 0.0f, 0.8f, 0.01f, 0.01f); // Top middle
BouncingPoint v1 = new BouncingPoint( 0.8f, -0.8f, 0.01f, -0.01f); // Bottom right
BouncingPoint v2 = new BouncingPoint(-0.8f, -0.8f, -0.01f, 0.01f); // Bottom left
while (!Raylib.WindowShouldClose())
{
renderer.Clear();
v0.Update();
v1.Update();
v2.Update();
renderer.DrawTriangle(v0.pos, v1.pos, v2.pos, shader);
// Render
Raylib.BeginDrawing();
Raylib.ClearBackground(Color.Black);
//
renderer.render();
//
Raylib.DrawText($"FPS: {Raylib.GetFPS()}", 10, 10, 20, Color.White);
Raylib.EndDrawing();
frameCounter += 2;
}
// Cleanup
renderer.cleanup();
Raylib.CloseWindow();
}
}