Sfoglia il codice sorgente

Fix Issue #278: Set default filter mode for new images.

Add the somewhat lengthy named love.graphics.getDefaultImageFilter() and
love.graphics.getDefaultImageFilter(). The verbose name should point to the
fact that changing the default filter midway through the game will not affect
the images already loaded using love.graphics.newImage().
vrld 14 anni fa
parent
commit
48137cd2e1

+ 12 - 2
src/modules/graphics/opengl/Graphics.cpp

@@ -37,7 +37,7 @@ namespace opengl
 {
 
 	Graphics::Graphics()
-		: currentFont(0), lineWidth(1), matrixLimit(0), userMatrices(0)
+		: currentFont(0), currentImageFilter(), lineWidth(1), matrixLimit(0), userMatrices(0)
 	{
 		// Indicates that there is no screen
 		// created yet.
@@ -476,7 +476,7 @@ namespace opengl
 	Image * Graphics::newImage(love::image::ImageData * data)
 	{
 		// Create the image.
-		Image * image = new Image(data);
+		Image * image = new Image(data, currentImageFilter);
 		bool success;
 		try {
 			success = image->load();
@@ -673,6 +673,11 @@ namespace opengl
 			glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
 	}
 
+	void Graphics::setDefaultImageFilter(const Image::Filter& f)
+	{
+		currentImageFilter = f;
+	}
+
 	Graphics::BlendMode Graphics::getBlendMode ()
 	{
 		GLint dst, src, equation;
@@ -705,6 +710,11 @@ namespace opengl
 			return COLOR_REPLACE;
 	}
 
+	const Image::Filter& Graphics::getDefaultImageFilter() const
+	{
+		return currentImageFilter;
+	}
+
 	void Graphics::setLineWidth( float width )
 	{
 		lineWidth = width;

+ 11 - 0
src/modules/graphics/opengl/Graphics.h

@@ -113,6 +113,7 @@ namespace opengl
 	private:
 
 		Font * currentFont;
+		Image::Filter currentImageFilter;
 		DisplayMode currentMode;
 
 		float lineWidth;
@@ -332,6 +333,11 @@ namespace opengl
 		**/
 		void setColorMode (ColorMode mode);
 
+		/**
+		 * Sets the current image filter.
+		 **/
+		void setDefaultImageFilter(const Image::Filter& f);
+
 		/**
 		* Gets the current blend mode.
 		**/
@@ -342,6 +348,11 @@ namespace opengl
 		**/
 		ColorMode getColorMode();
 
+		/**
+		 * Gets the current image filter.
+		 **/
+		const Image::Filter& getDefaultImageFilter() const;
+
 		/**
 		* Sets the line width.
 		* @param width The new width of the line.

+ 3 - 2
src/modules/graphics/opengl/Image.cpp

@@ -29,7 +29,7 @@ namespace graphics
 {
 namespace opengl
 {
-	Image::Image(love::image::ImageData * data)
+	Image::Image(love::image::ImageData * data, const Image::Filter& filter)
 		: width((float)(data->getWidth())), height((float)(data->getHeight())), texture(0)
 	{
 		data->retain();
@@ -47,6 +47,7 @@ namespace opengl
 		vertices[2].s = 1; vertices[2].t = 1;
 		vertices[3].s = 1; vertices[3].t = 0;
 
+		setFilter(filter);
 	}
 
 	Image::~Image()
@@ -119,7 +120,7 @@ namespace opengl
 		drawv(t, v);
 	}
 
-	void Image::setFilter(Image::Filter f)
+	void Image::setFilter(const Image::Filter& f)
 	{
 		GLint gmin, gmag;
 		gmin = gmag = 0; // so that they're not used uninitialized

+ 2 - 2
src/modules/graphics/opengl/Image.h

@@ -78,7 +78,7 @@ namespace opengl
 		*
 		* @param file The file from which to load the image.
 		**/
-		Image(love::image::ImageData * data);
+		Image(love::image::ImageData * data, const Image::Filter& f = Image::Filter());
 
 		/**
 		* Destructor. Deletes the hardware texture and other resources.
@@ -121,7 +121,7 @@ namespace opengl
 		*
 		* @param mode The filter mode.
 		**/
-		void setFilter(Image::Filter f);
+		void setFilter(const Image::Filter& f);
 
 		Image::Filter getFilter() const;
 

+ 33 - 0
src/modules/graphics/opengl/wrap_Graphics.cpp

@@ -568,6 +568,25 @@ namespace opengl
 		return 0;
 	}
 
+	int w_setDefaultImageFilter(lua_State * L)
+	{
+		Image::FilterMode min;
+		Image::FilterMode mag;
+		const char * minstr = luaL_checkstring(L, 1);
+		const char * magstr = luaL_checkstring(L, 2);
+		if (!Image::getConstant(minstr, min))
+			return luaL_error(L, "Invalid filter mode: %s", minstr);
+		if (!Image::getConstant(magstr, mag))
+			return luaL_error(L, "Invalid filter mode: %s", magstr);
+
+		Image::Filter f;
+		f.min = min;
+		f.mag = mag;
+		instance->setDefaultImageFilter(f);
+
+		return 0;
+	}
+
 	int w_getBlendMode(lua_State * L)
 	{
 		Graphics::BlendMode mode = instance->getBlendMode();
@@ -590,6 +609,18 @@ namespace opengl
 		return 1;
 	}
 
+	int w_getDefaultImageFilter(lua_State * L)
+	{
+		const Image::Filter& f = instance->getDefaultImageFilter();
+		const char * minstr;
+		const char * magstr;
+		Image::getConstant(f.min, minstr);
+		Image::getConstant(f.mag, magstr);
+		lua_pushstring(L, minstr);
+		lua_pushstring(L, magstr);
+		return 2;
+	}
+
 	int w_setLineWidth(lua_State * L)
 	{
 		float width = (float)luaL_checknumber(L, 1);
@@ -1159,8 +1190,10 @@ namespace opengl
 
 		{ "setBlendMode", w_setBlendMode },
 		{ "setColorMode", w_setColorMode },
+		{ "setDefaultImageFilter", w_setDefaultImageFilter },
 		{ "getBlendMode", w_getBlendMode },
 		{ "getColorMode", w_getColorMode },
+		{ "getDefaultImageFilter", w_getDefaultImageFilter },
 		{ "setLineWidth", w_setLineWidth },
 		{ "setLineStyle", w_setLineStyle },
 		{ "setLine", w_setLine },

+ 2 - 0
src/modules/graphics/opengl/wrap_Graphics.h

@@ -71,8 +71,10 @@ namespace opengl
 	int w_getFont(lua_State * L);
 	int w_setBlendMode(lua_State * L);
 	int w_setColorMode(lua_State * L);
+	int w_setDefaultImageFilter(lua_State * L);
 	int w_getBlendMode(lua_State * L);
 	int w_getColorMode(lua_State * L);
+	int w_getDefaultImageFilter(lua_State * L);
 	int w_setLineWidth(lua_State * L);
 	int w_setLineStyle(lua_State * L);
 	int w_setLine(lua_State * L);

+ 1 - 1
src/modules/graphics/opengl/wrap_Image.cpp

@@ -67,7 +67,7 @@ namespace opengl
 
 	int w_Image_getFilter(lua_State * L)
 	{
-		Image * t = luax_checkimage(L, 1); 
+		Image * t = luax_checkimage(L, 1);
 		Image::Filter f = t->getFilter();
 		Image::FilterMode min = f.min;
 		Image::FilterMode mag = f.mag;