Skip to content
This repository was archived by the owner on Sep 21, 2023. It is now read-only.

Commit 9276c46

Browse files
author
Alexis Huvier
committed
feat(renderer): Create Instruction and DMRender
1 parent 7250f17 commit 9276c46

File tree

4 files changed

+186
-0
lines changed

4 files changed

+186
-0
lines changed

SharpEngine/Renderer/DMRender.cs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using Raylib_cs;
6+
using SharpEngine.Math;
7+
using Color = SharpEngine.Utils.Color;
8+
9+
namespace SharpEngine.Renderer;
10+
11+
public static class DMRender
12+
{
13+
public static List<Instruction> Instructions = new();
14+
15+
private static void DrawInstructions(ICollection instructions)
16+
{
17+
foreach (var instruction in Instructions)
18+
{
19+
switch (instruction.Type)
20+
{
21+
case InstructionType.ScissorMode:
22+
Raylib.BeginScissorMode((int)instruction.Parameters[0], (int)instruction.Parameters[1],
23+
(int)instruction.Parameters[2], (int)instruction.Parameters[3]);
24+
DrawInstructions(instruction.Parameters.GetRange(4, instructions.Count - 4)
25+
.Select(x => (Instruction)x).ToList());
26+
Raylib.EndScissorMode();
27+
break;
28+
case InstructionType.DrawRectangle:
29+
Raylib.DrawRectangle((int)instruction.Parameters[0], (int)instruction.Parameters[1],
30+
(int)instruction.Parameters[2], (int)instruction.Parameters[3],
31+
(Color)instruction.Parameters[4]);
32+
break;
33+
case InstructionType.DrawRectanglePro:
34+
Raylib.DrawRectanglePro((Rect)instruction.Parameters[0], (Vec2)instruction.Parameters[1],
35+
(float)instruction.Parameters[2], (Color)instruction.Parameters[3]);
36+
break;
37+
case InstructionType.DrawTexturePro:
38+
Raylib.DrawTexturePro((Texture2D)instruction.Parameters[0], (Rect)instruction.Parameters[1],
39+
(Rect)instruction.Parameters[2], (Vec2)instruction.Parameters[3],
40+
(float)instruction.Parameters[4], (Color)instruction.Parameters[5]);
41+
break;
42+
case InstructionType.DrawTextPro:
43+
Raylib.DrawTextPro((Font)instruction.Parameters[0], (string)instruction.Parameters[1],
44+
(Vec2)instruction.Parameters[2], (Vec2)instruction.Parameters[3],
45+
(float)instruction.Parameters[4], (float)instruction.Parameters[5],
46+
(float)instruction.Parameters[6], (Color)instruction.Parameters[7]);
47+
break;
48+
case InstructionType.DrawRectangleLinesEx:
49+
Raylib.DrawRectangleLinesEx((Rect)instruction.Parameters[0], (float)instruction.Parameters[1],
50+
(Color)instruction.Parameters[2]);
51+
break;
52+
case InstructionType.DrawTextEx:
53+
Raylib.DrawTextEx((Font)instruction.Parameters[0], (string)instruction.Parameters[1],
54+
(Vec2)instruction.Parameters[2], (float)instruction.Parameters[3],
55+
(float)instruction.Parameters[4], (Color)instruction.Parameters[5]);
56+
break;
57+
default:
58+
throw new ArgumentOutOfRangeException();
59+
}
60+
}
61+
}
62+
63+
public static void Draw()
64+
{
65+
Instructions.Sort((i1, i2) => i1.ZLayer.CompareTo(i2.ZLayer));
66+
DrawInstructions(Instructions);
67+
}
68+
69+
public static void ScissorMode(int posX, int posY, int width, int height, InstructionDestination destination, int zLayer, Action scissorAction)
70+
{
71+
var instructions = new List<Instruction>(Instructions);
72+
Instructions.Clear();
73+
scissorAction();
74+
var instruction = new Instruction
75+
{
76+
Type = InstructionType.ScissorMode,
77+
Destination = destination,
78+
ZLayer = zLayer,
79+
Parameters = new List<object> { posX, posY, width, height }
80+
};
81+
instruction.Parameters.AddRange(Instructions.Select(x => (object)x));
82+
Instructions = instructions;
83+
Instructions.Add(instruction);
84+
}
85+
86+
public static void DrawRectangle(Rect rectangle, Vec2 origin, float rotation, Color color, InstructionDestination destination, int zLayer)
87+
{
88+
Instructions.Add(new Instruction
89+
{
90+
Type = InstructionType.DrawRectanglePro,
91+
Destination = destination,
92+
ZLayer = zLayer,
93+
Parameters = new List<object> { rectangle, origin, rotation, color }
94+
});
95+
}
96+
97+
public static void DrawRectangle(int posX, int posY, int width, int height, Color color,
98+
InstructionDestination destination, int zLayer)
99+
{
100+
Instructions.Add(new Instruction
101+
{
102+
Type = InstructionType.DrawRectangle,
103+
Destination = destination,
104+
ZLayer = zLayer,
105+
Parameters = new List<object> { posX, posY, width, height, color }
106+
});
107+
}
108+
109+
public static void DrawRectanglesLines(Rectangle rect, int borderSize, Color borderColor,
110+
InstructionDestination destination, int zLayer)
111+
{
112+
Instructions.Add(new Instruction
113+
{
114+
Type = InstructionType.DrawRectangleLinesEx,
115+
Destination = destination,
116+
ZLayer = zLayer,
117+
Parameters = new List<object> { rect, borderSize, borderColor }
118+
});
119+
}
120+
121+
public static void DrawTexture(Texture2D texture, Rect source, Rect dest, Vec2 origin, float rotation, Color tint,
122+
InstructionDestination destination, int zLayer)
123+
{
124+
Instructions.Add(new Instruction
125+
{
126+
Type = InstructionType.DrawTexturePro,
127+
Destination = destination,
128+
ZLayer = zLayer,
129+
Parameters = new List<object> { texture, source, dest, origin, rotation, tint }
130+
});
131+
}
132+
133+
public static void DrawText(Font font, string text, Vec2 position, Vec2 origin, float rotation, int fontSize,
134+
int spacing, Color color, InstructionDestination destination, int zLayer)
135+
{
136+
Instructions.Add(new Instruction
137+
{
138+
Type = InstructionType.DrawTextPro,
139+
Destination = destination,
140+
ZLayer = zLayer,
141+
Parameters = new List<object> { font, text, position, origin, rotation, fontSize, spacing, color }
142+
});
143+
}
144+
145+
public static void DrawText(Font font, string text, Vec2 position, int fontSize, int spacing, Color color,
146+
InstructionDestination destination, int zLayer)
147+
{
148+
Instructions.Add(new Instruction
149+
{
150+
Type = InstructionType.DrawTextEx,
151+
Destination = destination,
152+
ZLayer = zLayer,
153+
Parameters = new List<object> { font, text, position, fontSize, spacing, color }
154+
});
155+
}
156+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Collections.Generic;
2+
3+
namespace SharpEngine.Renderer;
4+
5+
public struct Instruction
6+
{
7+
public InstructionType Type;
8+
public InstructionDestination Destination;
9+
public int ZLayer;
10+
public List<object> Parameters;
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace SharpEngine.Renderer;
2+
3+
public enum InstructionDestination
4+
{
5+
Entity,
6+
UI
7+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace SharpEngine.Renderer;
2+
3+
public enum InstructionType
4+
{
5+
DrawRectanglePro,
6+
DrawTexturePro,
7+
DrawTextPro,
8+
DrawRectangle,
9+
DrawRectangleLinesEx,
10+
DrawTextEx,
11+
ScissorMode
12+
}

0 commit comments

Comments
 (0)