Created
July 27, 2015 14:19
-
-
Save fkaa/6f2c8cea13e5f20d6cd5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SpriteBatch { | |
SpriteBatch(size) { | |
self.size = size; | |
self.idx = 0; | |
self.buffer = glCreateBuffers(); | |
glBufferData(..., size); | |
} | |
void begin(Shader shader) { | |
if (self.begun) error!("must end first!") | |
if (self.shader != shader) { | |
flush(); | |
self.shader = shader; | |
} | |
self.begun = true; | |
} | |
void draw(Sprite sprite) { | |
if (self.texture != sprite.texture) { | |
flush(); | |
} | |
self.data[self.idx++] = sprite.vertex; | |
self.data[self.idx++] = sprite.vertex; | |
self.data[self.idx++] = sprite.vertex; | |
self.data[self.idx++] = sprite.vertex; | |
self.data[self.idx++] = sprite.vertex; | |
self.data[self.idx++] = sprite.vertex; | |
} | |
void flush() { | |
// setup vertex array and other material | |
glUseProgram(self.shader); | |
glBindBuffer(self.buffer); | |
glBindTexture(self.texture); | |
glBufferData(..., self.data); | |
glDrawArrays(GL_TRIANGLES, 0, self.idx); | |
reset(); | |
} | |
void reset() { | |
glBufferData(..., nullptr); | |
self.idx = 0; | |
} | |
void end() { | |
if (!self.begun) error!("must start first!") | |
self.begun = false; | |
self.flush(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment