Browse Source

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

rude 16 years ago
parent
commit
d58182c4c2

+ 29 - 5
src/modules/graphics/opengl/SpriteBatch.cpp

@@ -52,6 +52,22 @@ namespace opengl
 			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.
 		gl_usage = GL_STREAM_DRAW;
 		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);
 		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)
 			glDeleteBuffers(2, vbo);
-
-		delete [] vertices;
-		delete [] indices;
 	}
 
 	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()
 	{
+		// If already locked, prevent from locking again.
+		if(lockp != 0)
+			return lockp;
+
 		glBindBuffer(GL_ARRAY_BUFFER, vbo[0]);
 		lockp = (vertex *)glMapBuffer(GL_ARRAY_BUFFER, GL_READ_WRITE);
 		return lockp;

+ 6 - 1
src/modules/graphics/opengl/SpriteBatch.h

@@ -30,6 +30,7 @@
 #include <common/Vector.h>
 #include <common/Matrix.h>
 #include <graphics/Drawable.h>
+#include <graphics/Volatile.h>
 
 // OpenGL
 #include "GLee.h"
@@ -45,7 +46,7 @@ namespace opengl
 	class Image;
 	class Quad;
 
-	class SpriteBatch : public Drawable
+	class SpriteBatch : public Drawable, public Volatile
 	{
 	private:
 
@@ -84,6 +85,10 @@ namespace opengl
 		SpriteBatch(Image * image, int size, int usage);
 		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 addq(Quad * quad, float x, float y, float a, float sx, float sy, float ox, float oy);
 		void clear();