@@ -15,16 +15,20 @@ public sealed class RenderBatch
1515 {
1616 public readonly List < float > SolidLineVertices = new ( ) ;
1717 public readonly List < float > SolidTriangleVertices = new ( ) ;
18- public readonly List < float > TexVertices = new ( ) ;
18+ public readonly Dictionary < int , List < float > > TexturedBatches = new ( ) ;
19+
1920 public readonly List < Action > HostActions = new ( ) ;
2021
21- public void AddColoredLine (
22- float x0 , float y0 ,
23- float x1 , float y1 ,
24- int r , int g , int b , int a )
22+ public void AddColoredLine ( float x , float y , int r , int g , int b , int a )
2523 {
26- AddColoredVertex ( x0 , y0 , r , g , b , a , SolidLineVertices ) ;
27- AddColoredVertex ( x1 , y1 , r , g , b , a , SolidLineVertices ) ;
24+ // Pushes a single vertex into the line batch.
25+ // When flushed, OpenGL will connect every 2 consecutive vertices into a line segment.
26+ AddColoredVertex ( x , y , r , g , b , a , SolidLineVertices ) ;
27+ }
28+
29+ public void AddSolidTriangleVertex ( float x , float y , int r , int g , int b , int a )
30+ {
31+ AddColoredVertex ( x , y , r , g , b , a , SolidTriangleVertices ) ;
2832 }
2933
3034 public void AddColoredTriangle (
@@ -54,25 +58,35 @@ public void AddSolidQuad(
5458 public void AddTexturedQuad (
5559 ( int x , int y ) p1 , ( int x , int y ) p2 ,
5660 ( int x , int y ) p3 , ( int x , int y ) p4 ,
57- int textureId ) // Note: Batching assumes the same textureId for the whole batch for now
61+ int textureId )
5862 {
59- AddTexturedTriangle ( p1 , p2 , p3 ) ;
60- AddTexturedTriangle ( p1 , p3 , p4 ) ;
63+ AddTexturedTriangle ( p1 , p2 , p3 , textureId ) ;
64+ AddTexturedTriangle ( p1 , p3 , p4 , textureId ) ;
6165 }
66+
6267 public void AddTexturedTriangle (
63- ( int x , int y ) p1 , ( int x , int y ) p2 , ( int x , int y ) p3 )
68+ ( int x , int y ) p1 , ( int x , int y ) p2 , ( int x , int y ) p3 ,
69+ int textureId )
6470 {
65- AddTexturedVertex ( p1 . x , p1 . y , 0f , 0f ) ;
66- AddTexturedVertex ( p2 . x , p2 . y , 1f , 0f ) ;
67- AddTexturedVertex ( p3 . x , p3 . y , 0f , 1f ) ;
71+ // Grab the list for this specific texture, or create it if it doesn't exist
72+ if ( ! TexturedBatches . TryGetValue ( textureId , out var list ) )
73+ {
74+ list = new List < float > ( ) ;
75+ TexturedBatches [ textureId ] = list ;
76+ }
77+
78+ // Notice we only push the 4 required floats, but into the correct texture's list
79+ AddTexturedVertex ( p1 . x , p1 . y , 0f , 0f , list ) ;
80+ AddTexturedVertex ( p2 . x , p2 . y , 1f , 0f , list ) ;
81+ AddTexturedVertex ( p3 . x , p3 . y , 0f , 1f , list ) ;
6882 }
6983
70- private void AddTexturedVertex ( float x , float y , float u , float v )
84+ private void AddTexturedVertex ( float x , float y , float u , float v , List < float > targetList )
7185 {
72- TexVertices . Add ( x ) ;
73- TexVertices . Add ( y ) ;
74- TexVertices . Add ( u ) ;
75- TexVertices . Add ( v ) ;
86+ targetList . Add ( x ) ;
87+ targetList . Add ( y ) ;
88+ targetList . Add ( u ) ;
89+ targetList . Add ( v ) ;
7690 }
7791
7892 private void AddColoredVertex (
@@ -85,11 +99,12 @@ private void AddColoredVertex(
8599 targetList . Add ( b / 255f ) ;
86100 targetList . Add ( a / 255f ) ;
87101 }
102+
88103 public void Clear ( )
89104 {
90- TexVertices . Clear ( ) ;
91105 SolidLineVertices . Clear ( ) ;
92106 SolidTriangleVertices . Clear ( ) ;
107+ TexturedBatches . Clear ( ) ; // Clear the dictionary here
93108 HostActions . Clear ( ) ;
94109 }
95110
0 commit comments