diff engine/core/video/opengl/glimage.cpp @ 620:853d25234671

* Moved the GLEnable and GLDisable structures from opengl_gui_graphics.cpp to fife_opengl.h as they may prove to be useful elsewhere. * Fixed the color mask definitions in fife_stdint.h * Added the nextPow2() function to calculate the nearest (greater) power of 2 * Removed a bunch of re-definitions of RGB masks * Modified GLImage to only generate one "texture chunk". I hope this makes better use of memory and speeds things up a hair * Made use of the GLEnable structure when clearing the screen
author prock@33b003aa-7bff-0310-803a-e67f0ece8222
date Thu, 30 Sep 2010 21:24:55 +0000
parents 739d8a43d771
children 92290efadab7
line wrap: on
line diff
--- a/engine/core/video/opengl/glimage.cpp	Wed Sep 29 15:15:45 2010 +0000
+++ b/engine/core/video/opengl/glimage.cpp	Thu Sep 30 21:24:55 2010 +0000
@@ -21,7 +21,7 @@
 
 // Standard C++ library includes
 #include <cassert>
-
+#include <iostream>
 // 3rd party library includes
 
 // FIFE includes
@@ -38,11 +38,17 @@
 	GLImage::GLImage(SDL_Surface* surface):
 		Image(surface) {
 		m_sdlimage = new SDLImage(surface);
+
+		m_textureids = NULL;
+
 		resetGlimage();
 	}
 
 	GLImage::GLImage(const uint8_t* data, unsigned int width, unsigned int height):
 		Image(data, width, height) {
+
+		m_textureids = NULL;
+
 		resetGlimage();
 	}
 
@@ -50,235 +56,157 @@
 		// remove surface so that deletion happens correctly (by base class destructor)
 		m_sdlimage->detachSurface();
 		delete m_sdlimage;
+
 		cleanup();
 	}
 
 	void GLImage::resetGlimage() {
-		m_last_col_fill_ratio = 0;
-		m_last_row_fill_ratio = 0;
-		m_textureids = NULL;
-		m_rows = 0;
-		m_cols = 0;
-		m_last_col_width = 0;
-		m_last_row_height = 0;
-		m_chunk_size = RenderBackend::instance()->getChunkingSize();
+		cleanup();
+
+		m_chunk_size = 0;
 		m_colorkey = RenderBackend::instance()->getColorKey();
 	}
 
 	void GLImage::cleanup() {
-		for (unsigned int i = 0; i < m_rows*m_cols; ++i) {
-			glDeleteTextures(1, &m_textureids[i]);
+		if (m_textureids) {
+			glDeleteTextures(1, &m_textureids[0]);
+
+			delete[] m_textureids;
+			m_textureids = NULL;
 		}
-		delete[] m_textureids;
-		m_textureids = NULL;
-		resetGlimage();
+
+		m_col_tex_coord = 0;
+		m_row_tex_coord = 0;
 	}
 
 	void GLImage::render(const Rect& rect, SDL_Surface* screen, unsigned char alpha) {
 		if (!m_textureids) {
-			generateTextureChunks();
+			generateGLTexture();
 		}
 
+		//not on the screen.  dont render
 		if (rect.right() < 0 || rect.x > static_cast<int>(screen->w) || rect.bottom() < 0 || rect.y > static_cast<int>(screen->h)) {
 			return;
 		}
 
+		//completely transparent so dont bother rendering
 		if (0 == alpha) {
 			return;
 		}
 
-		// used to calculate the fill ratio for given chunk
-		float col_fill_ratio;
-		float row_fill_ratio;
-
 		// the amount of "zooming" for the image
 		float scale_x = static_cast<float>(rect.w) / static_cast<float>(m_surface->w);
 		float scale_y = static_cast<float>(rect.h) / static_cast<float>(m_surface->h);
 
-		// rectangle used for drawing
-		Rect target;
-		// zooming causes scaling sometimes to round pixels incorrectly. Instead of
-		//  recalculating it all, store the values from previous round and calculate
-		//  new x & y
-		Rect prev;
+		// apply the scale to the width and height of the image
+		uint16_t w = static_cast<int>(round(scale_x*m_surface->w));
+		uint16_t h = static_cast<int>(round(scale_y*m_surface->h));
 
 		/// setting transparency for the whole primitive:
 		glColor4ub( 255, 255, 255, alpha );
 
-		glEnable(GL_TEXTURE_2D);
-		for (unsigned int i = 0; i < m_cols; ++i) {
-			if (i == m_cols-1) {
-				col_fill_ratio = m_last_col_fill_ratio;
-				target.w = static_cast<int>(round(scale_x*m_last_col_width*m_last_col_fill_ratio));
-			} else {
-				col_fill_ratio = 1.0;
-				target.w = static_cast<int>(round(scale_x*m_chunk_size));
-			}
-			if (i > 0) {
-				target.x = prev.x + prev.w;
-			} else {
-				target.x = rect.x;
-			}
+		GLEnable flag(GL_TEXTURE_2D);
+		glBindTexture(GL_TEXTURE_2D, m_textureids[0]);
+
+		glBegin(GL_QUADS);
+			glTexCoord2f(0.0f, 0.0f);
+			glVertex2i(rect.x, rect.y);
 
-			for (unsigned int j = 0; j < m_rows; ++j) {
-				if (j == m_rows-1) {
-					row_fill_ratio = m_last_row_fill_ratio;
-					target.h = static_cast<int>(round(scale_y*m_last_row_height*m_last_row_fill_ratio));
-				} else {
-					row_fill_ratio = 1.0;
-					target.h = static_cast<int>(round(scale_y*m_chunk_size));
-				}
-				if (j > 0) {
-					target.y = prev.y + prev.h;
-				} else {
-					target.y = rect.y;
-				}
-				prev = target;
+			glTexCoord2f(0.0f, m_row_tex_coord);
+			glVertex2i(rect.x, rect.y + h);
 
-				glBindTexture(GL_TEXTURE_2D, m_textureids[j*m_cols + i]);
-				glBegin(GL_QUADS);
-					glTexCoord2f(0.0f, 0.0f);
-					glVertex2i(target.x, target.y);
-
-					glTexCoord2f(0.0f, row_fill_ratio);
-					glVertex2i(target.x, target.y + target.h);
+			glTexCoord2f(m_col_tex_coord, m_row_tex_coord);
+			glVertex2i(rect.x + w, rect.y + h);
 
-					glTexCoord2f(col_fill_ratio, row_fill_ratio);
-					glVertex2i(target.x + target.w, target.y + target.h);
+			glTexCoord2f(m_col_tex_coord, 0.0f);
+			glVertex2i(rect.x + w, rect.y);
+		glEnd();
 
-					glTexCoord2f(col_fill_ratio, 0.0f);
-					glVertex2i(target.x + target.w, target.y);
-				glEnd();
-			}
-		}
-		glDisable(GL_TEXTURE_2D);
 	}
 
-	void GLImage::generateTextureChunks() {
+	void GLImage::generateGLTexture() {
 		const unsigned int width = m_surface->w;
 		const unsigned int height = m_surface->h;
+
+		uint32_t size;
+
+		if (width > height){
+			size = width;
+		}
+		else {
+			size = height;
+		}
+
+		//calculate the nearest larger power of 2
+		m_chunk_size = nextPow2(size);
+
+		// used to calculate the fill ratio for given chunk
+		m_col_tex_coord = static_cast<float>(m_surface->w%m_chunk_size) / static_cast<float>(m_chunk_size);
+		m_row_tex_coord = static_cast<float>(m_surface->h%m_chunk_size) / static_cast<float>(m_chunk_size);
+
+		if (m_col_tex_coord == 0.0f){
+			m_col_tex_coord = 1.0f;
+		}
+
+		if (m_row_tex_coord == 0.0f){
+			m_row_tex_coord = 1.0f;
+		}
+
 		uint8_t* data = static_cast<uint8_t*>(m_surface->pixels);
 		int pitch = m_surface->pitch;
 
-		m_last_col_width = 1;
-		m_cols = static_cast<int>(width/m_chunk_size);
-		if (width%m_chunk_size) {
-			++m_cols;
-			while(m_last_col_width < width%m_chunk_size) {
-				m_last_col_width <<= 1;
-			}
-		} else {
-			m_last_col_width = m_chunk_size;
-		}
+
+		assert(!m_textureids);
 
-		m_last_row_height = 1;
-		m_rows = static_cast<int>(height/m_chunk_size);
-		if (height%m_chunk_size) {
-			++m_rows;
-			while(m_last_row_height < height%m_chunk_size) {
-				m_last_row_height <<= 1;
-			}
-		} else {
-			m_last_row_height = m_chunk_size;
-		}
-
-		m_textureids = new GLuint[m_rows*m_cols];
-		memset(m_textureids, 0x00, m_rows*m_cols*sizeof(GLuint));
+		m_textureids = new GLuint[1];
+		memset(m_textureids, 0x00, 1*sizeof(GLuint));
 
-		if(width%m_chunk_size) {
-			m_last_col_fill_ratio = static_cast<float>(width%m_chunk_size) / static_cast<float>(m_last_col_width);
-		} else {  // (width%m_chunk_size) / m_last_col_width == 0 == m_chunk_size (mod m_chunk_size)
-			m_last_col_fill_ratio = 1.0f;
-		}
 
-		if (height%m_chunk_size) {
-			m_last_row_fill_ratio = static_cast<float>(height%m_chunk_size) / static_cast<float>(m_last_row_height);
-		} else {
-			m_last_row_fill_ratio = 1.0f;
-		}
-
-		unsigned int chunk_width;
-		unsigned int chunk_height;
-		unsigned int data_chunk_height;
-		unsigned int data_chunk_width;
+		uint32_t* oglbuffer = new uint32_t[m_chunk_size * m_chunk_size];
+		memset(oglbuffer, 0x00, m_chunk_size*m_chunk_size*sizeof(uint32_t));
 
-		for (unsigned int i = 0; i < m_cols; ++i) {
-			for (unsigned int j = 0; j < m_rows; ++j) {
-				if (i==m_cols-1) {
-					chunk_width = m_last_col_width;
-					data_chunk_width = width%m_chunk_size;
-					if(data_chunk_width == 0) {  // 0 == m_chunk_size (mod m_chunk_size)
-						data_chunk_width = m_chunk_size;
-					}
-				} else {
-					chunk_width = m_chunk_size;
-					data_chunk_width = m_chunk_size;
-				}
-				if (j==m_rows-1) {
-					chunk_height = m_last_row_height;
-					data_chunk_height = height%m_chunk_size;
-					if(data_chunk_height == 0) {  // 0 = m_chunk_size (mod m_chunk_size)
-						data_chunk_height = m_chunk_size;
-					}
-				} else {
-					chunk_height = m_chunk_size;
-					data_chunk_height = m_chunk_size;
-				}
+		for (unsigned int y = 0;  y < height; ++y) {
+			for (unsigned int x = 0; x < width; ++x) {
+				unsigned int pos = (y * pitch) + (x * 4);
 
-				uint32_t* oglbuffer = new uint32_t[chunk_width * chunk_height];
-				memset(oglbuffer, 0x00, chunk_width*chunk_height*4);
-
-				for (unsigned int y = 0;  y < data_chunk_height; ++y) {
-					for (unsigned int x = 0; x < data_chunk_width; ++x) {
-						unsigned int pos = (y + j*m_chunk_size)*pitch + (x + i*m_chunk_size) * 4;
+				uint8_t r = data[pos + 3];
+				uint8_t g = data[pos + 2];
+				uint8_t b = data[pos + 1];
+				uint8_t a = data[pos + 0];
 
-						uint8_t r = data[pos + 3];
-						uint8_t g = data[pos + 2];
-						uint8_t b = data[pos + 1];
-						uint8_t a = data[pos + 0];
-
-						if (RenderBackend::instance()->isColorKeyEnabled()) {
-							// only set alpha to zero if colorkey feature is enabled
-							if (r == m_colorkey.r && g == m_colorkey.g && b == m_colorkey.b) {
-								a = 0;
-							}
-						}
-
-						oglbuffer[(y*chunk_width) + x] = r | (g << 8) | (b << 16) | (a<<24);
+				if (RenderBackend::instance()->isColorKeyEnabled()) {
+					// only set alpha to zero if colorkey feature is enabled
+					if (r == m_colorkey.r && g == m_colorkey.g && b == m_colorkey.b) {
+						a = 0;
 					}
 				}
 
-				// get texture id from opengl
-				glGenTextures(1, &m_textureids[j*m_cols + i]);
-				// set focus on that texture
-				glBindTexture(GL_TEXTURE_2D, m_textureids[j*m_cols + i]);
-				// set filters for texture
-				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
-				glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
-				// transfer data from sdl buffer
-				glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, chunk_width, chunk_height, 0, GL_RGBA, GL_UNSIGNED_BYTE, static_cast<GLvoid*>(oglbuffer));
-
-				delete[] oglbuffer;
+				oglbuffer[(y*m_chunk_size) + x] = r | (g << 8) | (b << 16) | (a<<24);
 			}
 		}
+
+		// get texture id from opengl
+		glGenTextures(1, &m_textureids[0]);
+		// set focus on that texture
+		glBindTexture(GL_TEXTURE_2D, m_textureids[0]);
+		// set filters for texture
+		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
+		// transfer data from sdl buffer
+		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, m_chunk_size, m_chunk_size, 0, GL_RGBA, GL_UNSIGNED_BYTE, static_cast<GLvoid*>(oglbuffer));
+
+		delete[] oglbuffer;
 	}
 
 	void GLImage::saveImage(const std::string& filename) {
 		const unsigned int swidth = getWidth();
 		const unsigned int sheight = getHeight();
-		Uint32 rmask, gmask, bmask, amask;
 		SDL_Surface *surface = NULL;
 		uint8_t *pixels;
 
-		#if SDL_BYTEORDER == SDL_BIG_ENDIAN
-		rmask = 0xff000000; gmask = 0x00ff0000; bmask = 0x0000ff00; amask = 0x000000ff;
-		#else
-		rmask = 0x000000ff; gmask = 0x0000ff00; bmask = 0x00ff0000; amask = 0xff000000;
-		#endif
-
 		surface = SDL_CreateRGBSurface(SDL_SWSURFACE, swidth,
 			sheight, 24,
-			rmask, gmask, bmask, 0);
+			RMASK,GMASK,BMASK, NULLMASK);
 
 		if(surface == NULL) {
 			return;
@@ -304,12 +232,11 @@
 		saveAsPng(filename, *surface);
 		SDL_FreeSurface(surface);
 		delete [] pixels;
-
-
 	}
 
 	void GLImage::setClipArea(const Rect& cliparea, bool clear) {
-	        glScissor(cliparea.x, getHeight() - cliparea.y - cliparea.h, cliparea.w, cliparea.h);
+		glScissor(cliparea.x, getHeight() - cliparea.y - cliparea.h, cliparea.w, cliparea.h);
+
 		if (clear) {
 	        	glClear(GL_COLOR_BUFFER_BIT);
 		}