changeset 425:ad7969d9460b

A client can now specify a global color key to be used with all images. The default color key is (255,0,255) in RGB format. Also the client can enable/disable the color key feature by using the setColorKeyEnabled function in the EngineSettings class. By default the color key feature is disabled. fixes[t:451]
author vtchill@33b003aa-7bff-0310-803a-e67f0ece8222
date Sat, 20 Feb 2010 19:11:01 +0000
parents e29fbf84cb41
children a79084d8167c
files demos/pychan_demo/settings.py engine/core/controller/engine.cpp engine/core/controller/engine.i engine/core/controller/enginesettings.cpp engine/core/controller/enginesettings.h engine/core/video/fonts/subimagefont.cpp engine/core/video/fonts/subimagefont.h engine/core/video/opengl/glimage.cpp engine/core/video/opengl/glimage.h engine/core/video/opengl/renderbackendopengl.cpp engine/core/video/opengl/renderbackendopengl.h engine/core/video/renderbackend.cpp engine/core/video/renderbackend.h engine/core/video/sdl/renderbackendsdl.cpp engine/core/video/sdl/renderbackendsdl.h engine/core/video/sdl/sdlimage.cpp engine/core/video/sdl/sdlimage.h engine/core/video/video.i engine/python/fife/extensions/basicapplication.py
diffstat 19 files changed, 178 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/demos/pychan_demo/settings.py	Fri Feb 12 22:00:58 2010 +0000
+++ b/demos/pychan_demo/settings.py	Sat Feb 20 19:11:01 2010 +0000
@@ -37,5 +37,9 @@
 FontGlyphs          = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,!?-+/():;%&`'*#=[]\""
 Font                = 'fonts/freefont/FreeMono.ttf'
 
+# optional color key settings
+ColorKeyEnabled		= 0
+ColorKey			= 255,0,255
+
 WindowTitle			= 'Pychan demo [FIFE Client]'
 WindowIcon			= 'gui/icons/pychan_logo.png'
--- a/engine/core/controller/engine.cpp	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/controller/engine.cpp	Sat Feb 20 19:11:01 2010 +0000
@@ -175,14 +175,14 @@
 		FL_LOG(_log, "Creating render backend");
 		std::string rbackend(m_settings.getRenderBackend());
 		if (rbackend == "SDL") {
-			m_renderbackend = new RenderBackendSDL();
+			m_renderbackend = new RenderBackendSDL(m_settings.getColorKey());
 			FL_LOG(_log, "SDL Render backend created");
 		} else {
 #ifdef HAVE_OPENGL
-			m_renderbackend = new RenderBackendOpenGL();
+			m_renderbackend = new RenderBackendOpenGL(m_settings.getColorKey());
 			FL_LOG(_log, "OpenGL Render backend created");
 #else
-			m_renderbackend = new RenderBackendSDL();
+			m_renderbackend = new RenderBackendSDL(m_settings.getColorKey());
 			// Remember  the choice so we pick the right graphics class.
 			rbackend = "SDL";
 			FL_WARN(_log, "Tried to select OpenGL, even though it is not compiled into the engine. Falling back to SDL Render backend");
@@ -190,6 +190,7 @@
 		}
 		FL_LOG(_log, "Initializing render backend");
 		m_renderbackend->setChunkingSize(m_settings.getImageChunkingSize());
+		m_renderbackend->setColorKeyEnabled(m_settings.isColorKeyEnabled());
 		m_renderbackend->init();
 
 		FL_LOG(_log, "Creating main screen");
--- a/engine/core/controller/engine.i	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/controller/engine.i	Sat Feb 20 19:11:01 2010 +0000
@@ -74,6 +74,10 @@
 		std::string getWindowTitle() const;
 		void setWindowIcon(const std::string& icon);
 		std::string getWindowIcon() const;
+		void setColorKeyEnabled(bool colorkeyenable);
+		bool isColorKeyEnabled() const;
+		void setColorKey(Uint8 r, Uint8 g, Uint8 b);
+		const SDL_Color& getColorKey() const;
 		
 	private:
 		EngineSettings();
--- a/engine/core/controller/enginesettings.cpp	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/controller/enginesettings.cpp	Sat Feb 20 19:11:01 2010 +0000
@@ -50,7 +50,11 @@
 		m_defaultfontpath(""),
 		m_defaultfontsize(8),
 		m_defaultfontglyphs(""),
-		m_image_chunking_size(256) {
+		m_image_chunking_size(256),
+		m_iscolorkeyenabled(false) {
+			m_colorkey.r = 255;
+			m_colorkey.g = 0;
+			m_colorkey.b = 255;
 	}
 	
 	EngineSettings::~EngineSettings() {
@@ -159,6 +163,23 @@
 	void EngineSettings::setWindowIcon(const std::string& icon) {
 		m_windowicon = icon;
 	}
-	
+
+	void EngineSettings::setColorKeyEnabled(bool colorkeyenable) {
+		m_iscolorkeyenabled = colorkeyenable;
+	}
+
+	bool EngineSettings::isColorKeyEnabled() const {
+		return m_iscolorkeyenabled;
+	}
+
+	void EngineSettings::setColorKey(Uint8 r, Uint8 g, Uint8 b) {
+		m_colorkey.r = r;
+		m_colorkey.g = g;
+		m_colorkey.b = b;
+	}
+
+	const SDL_Color& EngineSettings::getColorKey() const {
+		return m_colorkey;
+	}
 }
 
--- a/engine/core/controller/enginesettings.h	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/controller/enginesettings.h	Sat Feb 20 19:11:01 2010 +0000
@@ -186,17 +186,41 @@
 			return m_image_chunking_size;
 		}
 
+		/** Sets the title of the window
+		 */
 		void setWindowTitle(const std::string& title);
 
+		/** Gets the current window title
+		 */
 		std::string getWindowTitle() const {
 			return m_windowtitle;
 		}
 	
+		/** Sets the icon that appears in the window title bar
+		 */
 		void setWindowIcon(const std::string& icon);
 
+		/** Gets the icon in the window title bar
+		 */
 		std::string getWindowIcon() const {
 			return m_windowicon;
 		}
+
+		/** Sets whether to use the colorkey feature
+		 */
+		void setColorKeyEnabled(bool colorkeyenable);
+
+		/** Gets whether the colorkey feature is in use
+		 */
+		bool isColorKeyEnabled() const;
+
+		/** Sets the global colorkey to use for images
+		 */
+		void setColorKey(Uint8 r, Uint8 g, Uint8 b);
+
+		/** Gets the global colorkey setting
+		 */
+		const SDL_Color& getColorKey() const;
 		
 	private:
 		unsigned int m_bitsperpixel;
@@ -214,6 +238,8 @@
 		unsigned int m_defaultfontsize;
 		std::string m_defaultfontglyphs;
 		unsigned int m_image_chunking_size;
+		bool m_iscolorkeyenabled;
+		SDL_Color m_colorkey;
 	};
 
 }//FIFE
--- a/engine/core/video/fonts/subimagefont.cpp	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/fonts/subimagefont.cpp	Sat Feb 20 19:11:01 2010 +0000
@@ -53,6 +53,7 @@
 		int image_id = m_pool.addResourceFromFile(filename);
 		Image& img = dynamic_cast<Image&>(m_pool.get(image_id));
 		SDL_Surface* surface = img.getSurface();
+		m_colorkey = RenderBackend::instance()->getColorKey();
 
 		if( !surface ) {
 			throw CannotOpenFile(filename);
@@ -77,11 +78,22 @@
 
 		src.h = surface->h;
 		src.y = 0;
-
+		
 		uint32_t separator = pixels[0];
-		while(x < surface->w && pixels[x] == separator)
-			++x;
-		uint32_t colorkey = pixels[x];
+		uint32_t colorkey = SDL_MapRGB(surface->format, m_colorkey.r, m_colorkey.g, m_colorkey.b);
+
+		// if colorkey feature is not enabled then manually find the color key in the font
+		if (!RenderBackend::instance()->isColorKeyEnabled()) {	
+			while(x < surface->w && pixels[x] == separator) {
+				++x;
+			}
+			
+			colorkey = pixels[x];
+		}
+		
+		// Disable alpha blending, so that we use color keying
+		SDL_SetAlpha(surface,0,255);
+		SDL_SetColorKey(surface,SDL_SRCCOLORKEY,colorkey);
 
 		FL_DBG(_log, LMsg("image_font")
 			<< " glyph separator is " 
@@ -89,10 +101,6 @@
 			<< " transparent color is " 
 			<< pprint(reinterpret_cast<void*>(colorkey)));
 
-		// Disable alpha blending, so that we use colorkeying
-		SDL_SetAlpha(surface,0,255);
-		SDL_SetColorKey(surface,SDL_SRCCOLORKEY,colorkey);
-
 		// Finally extract all glyphs
 		std::string::const_iterator text_it = glyphs.begin();
 		while(text_it != glyphs.end()) {
--- a/engine/core/video/fonts/subimagefont.h	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/fonts/subimagefont.h	Sat Feb 20 19:11:01 2010 +0000
@@ -54,6 +54,7 @@
 			~SubImageFont() {}
 		private:
 			ImagePool& m_pool;
+			SDL_Color m_colorkey;
 	};
 
 }
--- a/engine/core/video/opengl/glimage.cpp	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/opengl/glimage.cpp	Sat Feb 20 19:11:01 2010 +0000
@@ -62,6 +62,7 @@
 		m_last_col_width = 0;
 		m_last_row_height = 0;
 		m_chunk_size = RenderBackend::instance()->getChunkingSize();
+		m_colorkey = RenderBackend::instance()->getColorKey();
 	}
 	
 	void GLImage::cleanup() {
@@ -231,15 +232,19 @@
 					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;
 
-						// FIXME
-						// The following code might not be endianness correct
+						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 + 0];
-						uint8_t g = data[pos + 1];
-						uint8_t b = data[pos + 2];
-						uint8_t a = data[pos + 3];
+						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 << 24) | (g << 16) | (b << 8) | a;
+						oglbuffer[(y*chunk_width) + x] = r | (g << 8) | (b << 16) | (a<<24);
 					}
 				}
 
--- a/engine/core/video/opengl/glimage.h	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/opengl/glimage.h	Sat Feb 20 19:11:01 2010 +0000
@@ -29,6 +29,7 @@
 #include "util/base/fife_stdint.h"
 
 // 3rd party library includes
+#include <SDL_video.h>
 
 // FIFE includes
 // These includes are split up in two parts, separated by one empty line
@@ -117,6 +118,7 @@
 		SDLImage* m_sdlimage;
 		
 		unsigned int m_chunk_size;
+		SDL_Color m_colorkey;
 	};
 }
 
--- a/engine/core/video/opengl/renderbackendopengl.cpp	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/opengl/renderbackendopengl.cpp	Sat Feb 20 19:11:01 2010 +0000
@@ -36,7 +36,7 @@
 
 namespace FIFE {
 
-	RenderBackendOpenGL::RenderBackendOpenGL() : RenderBackend() {
+	RenderBackendOpenGL::RenderBackendOpenGL(const SDL_Color& colorkey) : RenderBackend(colorkey) {
 		// Get the pixelformat we want.
 		SDL_Surface* testsurface = SDL_CreateRGBSurface(SDL_SWSURFACE | SDL_SRCALPHA, 1, 1, 32,
 				RMASK, GMASK, BMASK ,AMASK);
--- a/engine/core/video/opengl/renderbackendopengl.h	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/opengl/renderbackendopengl.h	Sat Feb 20 19:11:01 2010 +0000
@@ -39,7 +39,7 @@
 	 */
 	class RenderBackendOpenGL : public RenderBackend {
 	public:
-		RenderBackendOpenGL();
+		RenderBackendOpenGL(const SDL_Color& colorkey);
 		virtual ~RenderBackendOpenGL();
 		const std::string& getName() const;
 		void startFrame();
--- a/engine/core/video/renderbackend.cpp	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/renderbackend.cpp	Sat Feb 20 19:11:01 2010 +0000
@@ -34,8 +34,12 @@
 	const unsigned int DEFAULT_CHUNKING_SIZE = 256;
 	const unsigned int MAX_CHUNKING_SIZE = 262144;  // pixels!
 	
-	RenderBackend::RenderBackend(): 
-		m_screen(NULL), m_isalphaoptimized(false), m_chunkingsize(DEFAULT_CHUNKING_SIZE) {
+	RenderBackend::RenderBackend(const SDL_Color& colorkey): 
+		m_screen(NULL), 
+		m_isalphaoptimized(false), 
+		m_chunkingsize(DEFAULT_CHUNKING_SIZE),
+		m_iscolorkeyenabled(false),
+		m_colorkey(colorkey) {
 	}
 
 
@@ -122,4 +126,20 @@
 	unsigned int RenderBackend::getChunkingSize() {
 		return m_chunkingsize;
 	}
+
+	void RenderBackend::setColorKeyEnabled(bool colorkeyenable) {
+		m_iscolorkeyenabled = colorkeyenable;
+	}
+
+	bool RenderBackend::isColorKeyEnabled() const {
+		return m_iscolorkeyenabled;
+	}
+
+	void RenderBackend::setColorKey(const SDL_Color& colorkey) {
+		m_colorkey = colorkey;
+	}
+
+	const SDL_Color& RenderBackend::getColorKey() const {
+		return m_colorkey;
+	}
 }
--- a/engine/core/video/renderbackend.h	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/renderbackend.h	Sat Feb 20 19:11:01 2010 +0000
@@ -52,7 +52,7 @@
 		/** Constructor.
 		 * @param name The name of the new renderbackend.
 		 */
-		RenderBackend();
+		RenderBackend(const SDL_Color& colorkey);
 		
 		/** Destructor.
 		 */
@@ -134,10 +134,28 @@
 		void setChunkingSize(unsigned int size);
 		unsigned int getChunkingSize();
 
+		/** Sets whether to use the colorkey feature
+		*/
+		void setColorKeyEnabled(bool colorkeyenable);
+
+		/** Gets whether the colorkey feature is in use
+		*/
+		bool isColorKeyEnabled() const;
+
+		/** Sets the global colorkey to use for images
+		 */ 
+		void setColorKey(const SDL_Color& colorkey);
+
+		/** Gets the global colorkey setting
+		 */ 
+		const SDL_Color& getColorKey() const;
+
 	protected:
 		Image* m_screen;
 		bool m_isalphaoptimized;
 		unsigned int m_chunkingsize;
+		bool m_iscolorkeyenabled;
+		SDL_Color m_colorkey;
 	};
 }
 
--- a/engine/core/video/sdl/renderbackendsdl.cpp	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/sdl/renderbackendsdl.cpp	Sat Feb 20 19:11:01 2010 +0000
@@ -39,7 +39,7 @@
 namespace FIFE {
 	static Logger _log(LM_VIDEO);
 
-	RenderBackendSDL::RenderBackendSDL() : RenderBackend() {
+	RenderBackendSDL::RenderBackendSDL(const SDL_Color& colorkey) : RenderBackend(colorkey) {
 	}
 
 
--- a/engine/core/video/sdl/renderbackendsdl.h	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/sdl/renderbackendsdl.h	Sat Feb 20 19:11:01 2010 +0000
@@ -40,7 +40,7 @@
 	 */
 	class RenderBackendSDL : public RenderBackend {
 	public:
-		RenderBackendSDL();
+		RenderBackendSDL(const SDL_Color& colorkey);
 		virtual ~RenderBackendSDL();
 		const std::string& getName() const;
 
--- a/engine/core/video/sdl/sdlimage.cpp	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/sdl/sdlimage.cpp	Sat Feb 20 19:11:01 2010 +0000
@@ -31,6 +31,7 @@
 // Second block: files included from the same folder
 #include "util/log/logger.h"
 #include "util/structures/rect.h"
+#include "video/renderbackend.h"
 
 #include "renderbackendsdl.h"
 #include "sdlblendingfunctions.h"
@@ -53,6 +54,7 @@
 		m_last_alpha = 255;
 		m_finalized = false;
 		m_isalphaoptimized = false;
+		m_colorkey = RenderBackend::instance()->getColorKey();
 	}
 	
 	SDLImage::~SDLImage() { }
@@ -231,9 +233,16 @@
 		}
 		m_finalized = true;
 		SDL_Surface *old_surface = m_surface;
+		Uint32 key = SDL_MapRGB(m_surface->format, m_colorkey.r, m_colorkey.g, m_colorkey.b);
 
 		if (m_surface->format->Amask == 0) {
 			SDL_SetAlpha(m_surface, SDL_SRCALPHA | SDL_RLEACCEL, 255);
+
+			// only use color key if feature is enabled
+			if (RenderBackend::instance()->isColorKeyEnabled()) {
+				SDL_SetColorKey(m_surface, SDL_SRCCOLORKEY, key);
+			}
+
 			m_surface = SDL_DisplayFormat(m_surface);
 		} else {
 			RenderBackendSDL* be = static_cast<RenderBackendSDL*>(RenderBackend::instance());
@@ -242,6 +251,12 @@
 				m_surface = optimize(m_surface);
 			} else  {
 				SDL_SetAlpha(m_surface, SDL_SRCALPHA, 255);
+
+				// only use color key if feature is enabled
+				if (RenderBackend::instance()->isColorKeyEnabled()) {
+					SDL_SetColorKey(m_surface, SDL_SRCCOLORKEY, key);
+				}
+
 				m_surface = SDL_DisplayFormatAlpha(m_surface);
 			}
 		}
@@ -359,10 +374,17 @@
 		                                        src->format->Rmask,  src->format->Gmask,
 		                                        src->format->Bmask, 0);
 		bpp = dst->format->BytesPerPixel;
-		Uint32 key = SDL_MapRGB(dst->format,
-		                        (((keycolor & 0xf00) >> 4) | 0xf),
-		                        ((keycolor & 0xf0) | 0xf),
-		                        (((keycolor & 0xf) << 4) | 0xf));
+		
+		Uint32 key = SDL_MapRGB(dst->format, m_colorkey.r, m_colorkey.g, m_colorkey.b);
+
+		// if the global color key feature is disabled, then use the manually found color key
+		if (!RenderBackend::instance()->isColorKeyEnabled()) {
+			key = SDL_MapRGB(dst->format,
+							(((keycolor & 0xf00) >> 4) | 0xf),
+							((keycolor & 0xf0) | 0xf),
+							(((keycolor & 0xf) << 4) | 0xf));
+		}
+		
 		if(SDL_MUSTLOCK(src)) {
 			SDL_LockSurface(src);
 		}
--- a/engine/core/video/sdl/sdlimage.h	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/sdl/sdlimage.h	Sat Feb 20 19:11:01 2010 +0000
@@ -68,6 +68,7 @@
 		Uint8 m_last_alpha;
 		// Is the surface already optimized for rendering
 		bool m_finalized;
+		SDL_Color m_colorkey;
 	};
 
 }
--- a/engine/core/video/video.i	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/core/video/video.i	Sat Feb 20 19:11:01 2010 +0000
@@ -161,6 +161,10 @@
 		void setAlphaOptimizerEnabled(bool enabled);
 		bool isAlphaOptimizerEnabled();
 		void saveImage(const std::string& filename);
+		void setColorKeyEnabled(bool colorkeyenable);
+		bool isColorKeyEnabled() const;
+		void setColorKey(const SDL_Color& colorkey);
+		const SDL_Color& getColorKey() const;
 	};
 	
 	enum MouseCursorType {
--- a/engine/python/fife/extensions/basicapplication.py	Fri Feb 12 22:00:58 2010 +0000
+++ b/engine/python/fife/extensions/basicapplication.py	Sat Feb 20 19:11:01 2010 +0000
@@ -103,7 +103,16 @@
 		engineSetting.setScreenWidth(settings.ScreenWidth)
 		engineSetting.setScreenHeight(settings.ScreenHeight)
 
-
+		try:
+			engineSetting.setColorKeyEnabled(settings.ColorKeyEnabled)
+		except:
+			pass
+			
+		try:
+			engineSetting.setColorKey(*settings.ColorKey)
+		except:
+			pass
+			
 		try:
 			engineSetting.setWindowTitle(settings.WindowTitle)
 			engineSetting.setWindowIcon(settings.WindowIcon)