Skip to content

Commit

Permalink
Introduce BlendInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Reco1I committed Dec 26, 2024
1 parent 964949e commit 12b2d4a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
59 changes: 59 additions & 0 deletions src/com/reco1l/andengine/BlendInfo.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.reco1l.andengine

import javax.microedition.khronos.opengles.GL10

data class BlendInfo(

/**
* The blending function to use.
*/
var function: BlendingFunction,

/**
* Whether to mask the red channel.
*/
var redMask: Boolean = true,

/**
* Whether to mask the green channel.
*/
var greenMask: Boolean = true,

/**
* Whether to mask the blue channel.
*/
var blueMask: Boolean = true,

/**
* Whether to mask the alpha channel.
*/
var alphaMask: Boolean = true,

/**
* Whether to clear the color buffer.
*/
var clear: Boolean = false

) {

fun apply(gl: GL10) {

gl.glColorMask(redMask, greenMask, blueMask, alphaMask)

if (function != BlendingFunction.Inherit) {
gl.glBlendFunc(function.source, function.destination)
}

if (clear) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT)
}
}


companion object {

val Inherit = BlendInfo(BlendingFunction.Inherit)

}

}
37 changes: 7 additions & 30 deletions src/com/reco1l/andengine/ExtendedEntity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -201,22 +201,6 @@ abstract class ExtendedEntity(
mAlpha = value.alpha
}


/**
* The color blending function.
*/
open var blendingFunction: BlendingFunction? = null
set(value) {
if (field != value) {
field = value

if (value != null) {
mSourceBlendFunction = value.source
mDestinationBlendFunction = value.destination
}
}
}

/**
* The width of the content inside the entity.
*/
Expand Down Expand Up @@ -429,22 +413,13 @@ abstract class ExtendedEntity(

protected open fun applyBlending(pGL: GL10) {

// If there's a blending function set, apply it instead of the engine's method.
val blendingFunction = blendingFunction

if (blendingFunction != null) {
blendInfo?.apply(pGL) ?: GLHelper.blendFunction(pGL, mSourceBlendFunction, mDestinationBlendFunction)

val parent = parent as? ExtendedEntity

// If the blending function is set to inherit, apply the parent's blending function.
if (blendingFunction == BlendingFunction.Inherit && parent != null) {
if (blendInfo?.function == BlendingFunction.Inherit) {
val parent = parent
if (parent is ExtendedEntity) {
GLHelper.blendFunction(pGL, parent.mSourceBlendFunction, parent.mDestinationBlendFunction)
} else {
GLHelper.blendFunction(pGL, blendingFunction.source, blendingFunction.destination)
}

} else {
GLHelper.blendFunction(pGL, mSourceBlendFunction, mDestinationBlendFunction)
}
}

Expand Down Expand Up @@ -778,7 +753,9 @@ abstract class ExtendedEntity(
// Transformation

override fun setBlendFunction(pSourceBlendFunction: Int, pDestinationBlendFunction: Int) {
blendingFunction = null
if (blendInfo != null) {
Log.w("ExtendedEntity", "BlendInfo is set, use blendInfo property to change the blending function.")
}
super.setBlendFunction(pSourceBlendFunction, pDestinationBlendFunction)
}

Expand Down
7 changes: 4 additions & 3 deletions src/com/reco1l/osu/playfield/HealthBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,12 @@ class HealthBar(private val statistics: StatisticV2) : Container() {

marker = ExtendedSprite()
marker.origin = Anchor.Center
marker.blendInfo = BlendInfo(BlendingFunction.Additive)
attachChild(marker)

explode = ExtendedSprite()
explode.origin = Anchor.Center
explode.blendingFunction = BlendingFunction.Additive
explode.blendInfo = BlendInfo(BlendingFunction.Additive)
explode.alpha = 0f
attachChild(explode)

Expand Down Expand Up @@ -116,7 +117,7 @@ class HealthBar(private val statistics: StatisticV2) : Container() {

fill.color = color
marker.color = color
marker.blendingFunction = if (statistics.hp < EPIC_CUTOFF) BlendingFunction.Inherit else BlendingFunction.Additive
marker.blendInfo?.function = if (statistics.hp < EPIC_CUTOFF) BlendingFunction.Inherit else BlendingFunction.Additive

} else {

Expand All @@ -139,7 +140,7 @@ class HealthBar(private val statistics: StatisticV2) : Container() {
bulge()

explode.clearEntityModifiers()
explode.blendingFunction = if (isEpic) BlendingFunction.Additive else BlendingFunction.Inherit
explode.blendInfo?.function = if (isEpic) BlendingFunction.Additive else BlendingFunction.Inherit
explode.alpha = 1f
explode.setScale(1f)

Expand Down

0 comments on commit 12b2d4a

Please sign in to comment.