Commits

vrld  committed 2f9c8a1

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().

  • Participants
  • Parent commits de2f7c6

Comments (0)

Files changed (7)

File src/modules/graphics/opengl/Graphics.cpp

 {
 
 	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.
 	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();
 			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;
 			return COLOR_REPLACE;
 	}
 
+	const Image::Filter& Graphics::getDefaultImageFilter() const
+	{
+		return currentImageFilter;
+	}
+
 	void Graphics::setLineWidth( float width )
 	{
 		lineWidth = width;

File src/modules/graphics/opengl/Graphics.h

 	private:
 
 		Font * currentFont;
+		Image::Filter currentImageFilter;
 		DisplayMode currentMode;
 
 		float lineWidth;
 		void setColorMode (ColorMode mode);
 
 		/**
+		 * Sets the current image filter.
+		 **/
+		void setDefaultImageFilter(const Image::Filter& f);
+
+		/**
 		* Gets the current blend mode.
 		**/
 		BlendMode getBlendMode();
 		ColorMode getColorMode();
 
 		/**
+		 * Gets the current image filter.
+		 **/
+		const Image::Filter& getDefaultImageFilter() const;
+
+		/**
 		* Sets the line width.
 		* @param width The new width of the line.
 		**/

File src/modules/graphics/opengl/Image.cpp

 {
 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();
 		vertices[2].s = 1; vertices[2].t = 1;
 		vertices[3].s = 1; vertices[3].t = 0;
 
+		setFilter(filter);
 	}
 
 	Image::~Image()
 		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

File src/modules/graphics/opengl/Image.h

 		*
 		* @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.
 		*
 		* @param mode The filter mode.
 		**/
-		void setFilter(Image::Filter f);
+		void setFilter(const Image::Filter& f);
 
 		Image::Filter getFilter() const;
 

File src/modules/graphics/opengl/wrap_Graphics.cpp

 		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();
 		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);
 
 		{ "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 },

File src/modules/graphics/opengl/wrap_Graphics.h

 	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);

File src/modules/graphics/opengl/wrap_Image.cpp

 
 	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;