changeset 186:f37b8e95aaaa

core: add blend routines in texture
author David Demelier <markand@malikania.fr>
date Fri, 06 Nov 2020 11:30:29 +0100
parents 7103d6574062
children 65a07c7d8ff4
files libcore/core/texture.c libcore/core/texture.h
diffstat 2 files changed, 44 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/libcore/core/texture.c	Tue Nov 03 18:13:40 2020 +0100
+++ b/libcore/core/texture.c	Fri Nov 06 11:30:29 2020 +0100
@@ -52,6 +52,25 @@
 }
 
 bool
+texture_set_blend_mode(struct texture *tex, enum texture_blend blend)
+{
+	assert(tex);
+	assert(blend >= TEXTURE_BLEND_NONE && blend <= TEXTURE_BLEND_MODULATE);
+
+	static const SDL_BlendMode table[] = {
+		[TEXTURE_BLEND_NONE] = SDL_BLENDMODE_NONE,
+		[TEXTURE_BLEND_BLEND] = SDL_BLENDMODE_BLEND,
+		[TEXTURE_BLEND_ADD] = SDL_BLENDMODE_ADD,
+		[TEXTURE_BLEND_MODULATE] = SDL_BLENDMODE_MOD
+	};
+
+	if (SDL_SetTextureBlendMode(tex->handle, table[blend]) < 0)
+		return errorf("%s", SDL_GetError());
+
+	return true;
+}
+
+bool
 texture_set_alpha_mod(struct texture *tex, unsigned int alpha)
 {
 	assert(texture_ok(tex));
--- a/libcore/core/texture.h	Tue Nov 03 18:13:40 2020 +0100
+++ b/libcore/core/texture.h	Fri Nov 06 11:30:29 2020 +0100
@@ -41,6 +41,16 @@
 };
 
 /**
+ * \brief Blend type when rendering.
+ */
+enum texture_blend {
+	TEXTURE_BLEND_NONE,     /*!< No pixel modulation. */
+	TEXTURE_BLEND_BLEND,    /*!< Blend transparency. */
+	TEXTURE_BLEND_ADD,      /*!< Additive blending. */
+	TEXTURE_BLEND_MODULATE  /*!< Color modulation. */
+};
+
+/**
  * Create a new texture.
  *
  * \pre tex != NULL
@@ -65,11 +75,25 @@
 texture_ok(const struct texture *tex);
 
 /**
+ * Set blend mode.
+ *
+ * \pre texture_ok(tex)
+ * \param tex the texture
+ * \param blend the blend mode
+ * \return False on errors.
+ */
+bool
+texture_set_blend_mode(struct texture *tex, enum texture_blend blend);
+
+/**
  * Apply an alpha modulation (aka transparency).
  *
+ * You may need to use texture_set_blend_mode before this function to work.
+ *
  * \pre texture_ok(tex)
  * \param tex the texture
  * \param alpha the alpha (0-255)
+ * \return False on errors.
  */
 bool
 texture_set_alpha_mod(struct texture *tex, unsigned int alpha);
@@ -80,6 +104,7 @@
  * \pre texture_ok(tex)
  * \param tex the texture to modify
  * \param color the color
+ * \return False on errors.
  */
 bool
 texture_set_color_mod(struct texture *tex, unsigned long color);