SpriteBatch is now Volatile and can deal with display resolution changes.

This commit is contained in:
rude
2009-08-18 18:43:59 +02:00
parent c52c218ba1
commit d58182c4c2
2 changed files with 35 additions and 6 deletions
+29 -5
View File
@@ -52,6 +52,22 @@ namespace opengl
indices[i*6+5] = 3+(i*4); indices[i*6+5] = 3+(i*4);
} }
loadVolatile();
}
SpriteBatch::~SpriteBatch()
{
image->release();
if(vbo[0] != 0 && vbo[1] != 0)
glDeleteBuffers(2, vbo);
delete [] vertices;
delete [] indices;
}
bool SpriteBatch::loadVolatile()
{
// Find out which OpenGL VBO usage hint to use. // Find out which OpenGL VBO usage hint to use.
gl_usage = GL_STREAM_DRAW; gl_usage = GL_STREAM_DRAW;
gl_usage = (usage == USAGE_DYNAMIC) ? GL_DYNAMIC_DRAW : gl_usage; gl_usage = (usage == USAGE_DYNAMIC) ? GL_DYNAMIC_DRAW : gl_usage;
@@ -68,17 +84,21 @@ namespace opengl
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*size*6, indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*size*6, indices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return true;
} }
SpriteBatch::~SpriteBatch() void SpriteBatch::unloadVolatile()
{ {
image->release(); vertex * v = (vertex *)lock();
// Copy to system memory.
memcpy(vertices, v, sizeof(vertex)*size*4);
unlock();
// Delete the buffers.
if(vbo[0] != 0 && vbo[1] != 0) if(vbo[0] != 0 && vbo[1] != 0)
glDeleteBuffers(2, vbo); glDeleteBuffers(2, vbo);
delete [] vertices;
delete [] indices;
} }
void SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy) void SpriteBatch::add(float x, float y, float a, float sx, float sy, float ox, float oy)
@@ -135,6 +155,10 @@ namespace opengl
void * SpriteBatch::lock() void * SpriteBatch::lock()
{ {
// If already locked, prevent from locking again.
if(lockp != 0)
return lockp;
glBindBuffer(GL_ARRAY_BUFFER, vbo[0]); glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
lockp = (vertex *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE); lockp = (vertex *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
return lockp; return lockp;
+6 -1
View File
@@ -30,6 +30,7 @@
#include <common/Vector.h> #include <common/Vector.h>
#include <common/Matrix.h> #include <common/Matrix.h>
#include <graphics/Drawable.h> #include <graphics/Drawable.h>
#include <graphics/Volatile.h>
// OpenGL // OpenGL
#include "GLee.h" #include "GLee.h"
@@ -45,7 +46,7 @@ namespace opengl
class Image; class Image;
class Quad; class Quad;
class SpriteBatch : public Drawable class SpriteBatch : public Drawable, public Volatile
{ {
private: private:
@@ -84,6 +85,10 @@ namespace opengl
SpriteBatch(Image * image, int size, int usage); SpriteBatch(Image * image, int size, int usage);
virtual ~SpriteBatch(); virtual ~SpriteBatch();
// Implements Volatile.
bool loadVolatile();
void unloadVolatile();
void add(float x, float y, float a, float sx, float sy, float ox, float oy); void add(float x, float y, float a, float sx, float sy, float ox, float oy);
void addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy); void addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy);
void clear(); void clear();