]> git.tdb.fi Git - libs/gl.git/blobdiff - source/core/blend.cpp
Use default member initializers for simple types
[libs/gl.git] / source / core / blend.cpp
index 3327885c74c1ef34b7a5c1d111f22935a8639de0..99ffcd075c933a4441a8535dfed14b735a09acb4 100644 (file)
@@ -1,6 +1,5 @@
-#include <msp/gl/extensions/ext_blend_minmax.h>
-#include <msp/gl/extensions/ext_blend_subtract.h>
 #include <msp/strings/format.h>
+#include <msp/strings/utils.h>
 #include "blend.h"
 
 using namespace std;
@@ -8,62 +7,75 @@ using namespace std;
 namespace Msp {
 namespace GL {
 
-Blend::Blend():
-       eq(ADD),
-       src_factor(ONE),
-       dst_factor(ZERO)
-{ }
-
 Blend::Blend(BlendFactor sf, BlendFactor df):
-       eq(ADD),
+       enabled(true),
        src_factor(sf),
        dst_factor(df)
 { }
 
 Blend::Blend(BlendEquation e, BlendFactor sf, BlendFactor df):
-       eq(e),
+       enabled(true),
+       equation(e),
        src_factor(sf),
        dst_factor(df)
+{ }
+
+
+Blend::Loader::Loader(Blend &b):
+       ObjectLoader<Blend>(b)
 {
-       if(eq==MIN || eq==MAX)
-               static Require _req(EXT_blend_minmax);
-       else if(eq==SUBTRACT || eq==REVERSE_SUBTRACT)
-               static Require _req(EXT_blend_subtract);
+       add("equation", &Loader::equation);
+       add("factors", &Loader::factors);
+       add("constant", &Loader::constant);
 }
 
-void Blend::bind() const
+void Blend::Loader::constant(float r, float g, float b, float a)
 {
-       if(set_current(this))
-       {
-               glEnable(GL_BLEND);
-               if(EXT_blend_minmax)
-                       glBlendEquation(eq);
-               glBlendFunc(src_factor, dst_factor);
-       }
+       obj.constant = Color(r, g, b, a);
 }
 
-void Blend::unbind()
+void Blend::Loader::equation(BlendEquation eq)
 {
-       if(set_current(0))
-               glDisable(GL_BLEND);
+       obj.enabled = true;
+       obj.equation = eq;
 }
 
-const Blend &Blend::alpha()
+void Blend::Loader::factors(BlendFactor sf, BlendFactor df)
 {
-       static Blend blend(SRC_ALPHA, ONE_MINUS_SRC_ALPHA);
-       return blend;
+       obj.enabled = true;
+       obj.src_factor = sf;
+       obj.dst_factor = df;
 }
 
-const Blend &Blend::additive()
+
+void operator>>(const LexicalConverter &conv, BlendEquation &eq)
 {
-       static Blend blend(ONE, ONE);
-       return blend;
+       const string &str = conv.get();
+       if(str=="ADD")
+               eq = ADD;
+       else if(str=="SUBTRACT")
+               eq = SUBTRACT;
+       else if(str=="REVERSE_SUBTRACT")
+               eq = REVERSE_SUBTRACT;
+       else if(str=="MIN")
+               eq = MIN;
+       else if(str=="MAX")
+               eq = MAX;
+       else
+               throw lexical_error(format("conversion of '%s' to BlendEquation", str));
 }
 
-const Blend &Blend::additive_alpha()
+void operator<<(LexicalConverter &conv, BlendEquation eq)
 {
-       static Blend blend(SRC_ALPHA, ONE);
-       return blend;
+       switch(eq)
+       {
+       case ADD: conv.result("ADD"); break;
+       case SUBTRACT: conv.result("SUBTRACT"); break;
+       case REVERSE_SUBTRACT: conv.result("REVERSE_SUBTRACT"); break;
+       case MIN: conv.result("MIN"); break;
+       case MAX: conv.result("MAX"); break;
+       default: conv.result(format("BlendEquation(%#x)", static_cast<int>(eq)));
+       }
 }
 
 void operator>>(const LexicalConverter &conv, BlendFactor &factor)
@@ -123,5 +135,47 @@ void operator<<(LexicalConverter &conv, BlendFactor factor)
        }
 }
 
+void operator>>(const LexicalConverter &conv, ColorWriteMask &mask)
+{
+       ColorWriteMask result = WRITE_NONE;
+       for(const string &p: split(conv.get(), '_'))
+       {
+               if(p=="ALL")
+                       result = result|WRITE_ALL;
+               else if(p=="RED")
+                       result = result|WRITE_RED;
+               else if(p=="GREEN")
+                       result = result|WRITE_GREEN;
+               else if(p=="BLUE")
+                       result = result|WRITE_BLUE;
+               else if(p=="ALPHA")
+                       result = result|WRITE_ALPHA;
+               else
+                       throw lexical_error(format("conversion of '%s' to ColorWriteMask", conv.get()));
+       }
+       mask = result;
+}
+
+void operator<<(LexicalConverter &conv, ColorWriteMask mask)
+{
+       if(mask==WRITE_ALL)
+               conv.result("ALL");
+       else if(mask&~WRITE_ALL)
+               conv.result(format("ColorWriteMask(%#x)", static_cast<int>(mask)));
+       else
+       {
+               string result;
+               if(mask&WRITE_RED)
+                       result = "RED";
+               if(mask&WRITE_GREEN)
+                       append(result, "_", "GREEN");
+               if(mask&WRITE_BLUE)
+                       append(result, "_", "BLUE");
+               if(mask&WRITE_ALPHA)
+                       append(result, "_", "ALPHA");
+               conv.result(result);
+       }
+}
+
 } // namespace GL
 } // namespace Msp