Commits

Anonymous committed 6a42866

rewrote blend modes, added commented skeletons for remaining ones

Comments (0)

Files changed (1)

src/main/java/net/fishbulb/jcod/util/BlendFunction.java

 
 import com.badlogic.gdx.graphics.Color;
 
-
-// Notes on alpha:
+// A note on alpha:
 //
-// From everything I've read, it would seem any proper blending needs premultiplied alpha, but we aren't using that.
-// Reason is, JCOD needs to play nice with the rest of libGDX, and GDX does not use premultiplied alpha or support it
-// in any image loaders.  This is never going to be a serious issue in any roguelike (or likely even noticeable) but
-// antialiased tiles and tile fringes will bring the the premultiplication problem back to the fore.
-//
-// We could have tilesets "fix" images when loaded, but this would go against the
-// "play nice" philosophy by treating tile images differently than the rest of libGFX.  So we'll punt and come back
-// to the problem if and when we support drawing fringes.
+// Every one of these blend modes ignores alpha completely, except for the ones that have "Alpha" in the name, which
+// use only source alpha .  Destination alpha is never used and always preserved, except for the default Set mode,
+// which replaces the destination alpha with the source.  The reasoning behind this is partly to keep compatibility with
+// TCOD, and also because there isn't really a consistent treatment of destination alpha that would also play nice with
+// blending for the second time in OpenGL.  So we do color manipulation here and "real" blending with OpenGL later.
 
 public interface BlendFunction {
+
     /**
      * Inputs that are not clamped with their rgba components between 0 and 1 will produce undefined output.
      * <p/>
-     * When implementing a blend mode, the blend method MUST NOT be destructive of either bottom or top,
-     * unless either color is also used as the output (i.e. "aliased"), in which case the blend function MUST still
-     * produce the same color as if output was not aliased to either input.<p/>
+     * When implementing a blend mode, the blend method MUST NOT be destructive of either source or dest, unless either
+     * color is also used as the output (i.e. "aliased"), in which case the blend function MUST still produce the same
+     * color as if output was not aliased to either input.<p/>
      *
-     * @param bottom
-     * @param top
+     * @param source
+     * @param dest
      * @param output
      */
-    public void blend(Color bottom, Color top, Color output);
+    public void blend(Color source, Color dest, Color output);
 }
 
 enum BlendMode implements BlendFunction {
-    /**
-     * Ignores the bottom color and returns the top color.
-     */
+    /** Ignores the source color and returns the dest color. */
     Set {
         // TCOD_BKGND_SET
-        @Override public void blend(Color bottom, Color top, Color output) {
-            output.set(top);
+        @Override
+        public void blend(Color source, Color dest, Color output) {
+            output.set(source);
         }
     },
 
-    /**
-     * Ignores the top color and returns the bottom color
-     */
+    /** Ignores the dest color and returns the source color */
     Ignore {
         // JCOD Extension
-        @Override public void blend(Color bottom, Color top, Color output) {
-            output.set(bottom);
+        @Override
+        public void blend(Color source, Color dest, Color output) {
+            output.set(dest);
         }
     },
 
-    /**
-     * Multiplies the bottom color by the top color
-     */
+    /** Multiplies the source color by the dest color */
     Multiply {
         // TCOD_BKGND_MULTIPLY
-        @Override public void blend(Color bottom, Color top, Color output) {
-            output.set(bottom);
-            output.mul(top);
+        @Override
+        public void blend(Color source, Color dest, Color output) {
+            output.set(dest);
+            output.mul(source);
+            output.a = dest.a;
         }
+
     },
 
-
+//    Lighten {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    },
+//
+//    Darken {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    },
+//
+//    Screen {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    },
+//
+//    ColorDodge {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    },
+//
+//    ColorBurn {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    },
+//
+//    Add {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    },
+//
+//    AddAlpha {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    },
+//
+//    Burn {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    },
+//
+//    Overlay {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    },
+//
+//    // This one is very dubious, I don't know what to call it or if I should even implement it
+//    Alpha {
+//        @Override
+//        public void blend(Color source, Color dest, Color output) {
+//            //To change body of implemented methods use File | Settings | File Templates.
+//        }
+//    }
+//
     ;
 
-    @Override abstract public void blend(Color bottom, Color top, Color output);
+
+    @Override
+    abstract public void blend(Color source, Color dest, Color output);
 }
 
 //    void TCOD_console_set_char_background(TCOD_console_t con,int x, int y, TCOD_color_t col, TCOD_bkgnd_flag_t flag) {
 
 
 // http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/AlphaComposite.html
-// http://download.java.net/javadesktop/scenario/releases/0.5/javadoc/com/sun/scenario/effect/Blend.Mode.html
+// http://download.java.net/javadeskdest/scenario/releases/0.5/javadoc/com/sun/scenario/effect/Blend.Mode.html
 
 // Note: Porter-Duff requires S and D are premultiplied.
 
 //PorterDuff.Mode  	CLEAR  	[0, 0]
 //PorterDuff.Mode  	DARKEN  	[Sa + Da - Sa*Da, Sc*(1 - Da) + Dc*(1 - Sa) + min(Sc, Dc)]
 //PorterDuff.Mode  	DST  	[Da, Dc]
-//PorterDuff.Mode  	DST_ATOP  	[Sa, Sa * Dc + Sc * (1 - Da)]
+//PorterDuff.Mode  	DST_Adest  	[Sa, Sa * Dc + Sc * (1 - Da)]
 //PorterDuff.Mode  	DST_IN  	[Sa * Da, Sa * Dc]
 //PorterDuff.Mode  	DST_OUT  	[Da * (1 - Sa), Dc * (1 - Sa)]
 //PorterDuff.Mode  	DST_OVER  	[Sa + (1 - Sa)*Da, Rc = Dc + (1 - Da)*Sc]
 //PorterDuff.Mode  	OVERLAY
 //PorterDuff.Mode  	SCREEN  	[Sa + Da - Sa * Da, Sc + Dc - Sc * Dc]
 //PorterDuff.Mode  	SRC  	[Sa, Sc]
-//PorterDuff.Mode  	SRC_ATOP  	[Da, Sc * Da + (1 - Sa) * Dc]
+//PorterDuff.Mode  	SRC_Adest  	[Da, Sc * Da + (1 - Sa) * Dc]
 //PorterDuff.Mode  	SRC_IN  	[Sa * Da, Sc * Da]
 //PorterDuff.Mode  	SRC_OUT  	[Sa * (1 - Da), Sc * (1 - Da)]
 //PorterDuff.Mode  	SRC_OVER  	[Sa + (1 - Sa)*Da, Rc = Sc + (1 - Sa)*Dc]