3030public class BatchRenderer {
3131 private Texture currentTexture = null ;
3232 private boolean drawing = false ;
33- private boolean currentBlend = false ;
33+ private boolean currentBlend ;
3434
3535 private static final int MAX_QUADS = 10000 ;
3636 private static final int VERTICES_PER_QUAD = 6 ;
@@ -89,8 +89,8 @@ public void begin() {
8989 buffer .clear ();
9090 vertexCount = 0 ;
9191 currentTexture = null ;
92- currentBlend = false ;
9392 drawing = true ;
93+ currentBlend = false ;
9494 }
9595
9696 // =========================
@@ -114,23 +114,27 @@ public void submitQuad(
114114 return ;
115115 }
116116
117- if (currentBlend != blend ) {
117+
118+ if (currentTexture != texture || currentBlend != blend ) {
118119 flush ();
120+
119121 currentBlend = blend ;
120122
121123 if (blend ) {
122124 glBlendFunc (GL_SRC_ALPHA , GL_ONE );
123125 } else {
124126 glBlendFunc (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
125127 }
126- }
127128
128- if (currentTexture != texture ) {
129- flush ();
130129 currentTexture = texture ;
131130 currentTexture .bind ();
132131 }
133132
133+ if (vertexCount + 6 >= MAX_QUADS * VERTICES_PER_QUAD ) {
134+ flush ();
135+ }
136+
137+
134138 push (x , y , u0 , v1 , r , g , b , a );
135139 push (x + w , y , u1 , v1 , r , g , b , a );
136140 push (x + w , y + h , u1 , v0 , r , g , b , a );
@@ -144,7 +148,15 @@ private void flush() {
144148 if (vertexCount == 0 ) return ;
145149
146150 buffer .flip ();
151+
147152 glBindBuffer (GL_ARRAY_BUFFER , vbo );
153+
154+ glBufferData (
155+ GL_ARRAY_BUFFER ,
156+ buffer .capacity () * Float .BYTES ,
157+ GL_DYNAMIC_DRAW
158+ );
159+
148160 glBufferSubData (GL_ARRAY_BUFFER , 0 , buffer );
149161
150162 glBindVertexArray (vao );
@@ -176,7 +188,6 @@ public void end() {
176188 flush ();
177189 drawing = false ;
178190 currentTexture = null ;
179- glBlendFunc (GL_SRC_ALPHA , GL_ONE_MINUS_SRC_ALPHA );
180191 }
181192
182193 public void render () {
0 commit comments