]> git.tdb.fi Git - libs/gl.git/blob - source/core/blend.cpp
Check the flat qualifier from the correct member
[libs/gl.git] / source / core / blend.cpp
1 #include <msp/strings/format.h>
2 #include <msp/strings/utils.h>
3 #include "blend.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Blend::Blend(BlendFactor sf, BlendFactor df):
11         enabled(true),
12         src_factor(sf),
13         dst_factor(df)
14 { }
15
16 Blend::Blend(BlendEquation e, BlendFactor sf, BlendFactor df):
17         enabled(true),
18         equation(e),
19         src_factor(sf),
20         dst_factor(df)
21 { }
22
23
24 Blend::Loader::Loader(Blend &b):
25         ObjectLoader<Blend>(b)
26 {
27         add("equation", &Loader::equation);
28         add("factors", &Loader::factors);
29         add("constant", &Loader::constant);
30 }
31
32 void Blend::Loader::constant(float r, float g, float b, float a)
33 {
34         obj.constant = Color(r, g, b, a);
35 }
36
37 void Blend::Loader::equation(BlendEquation eq)
38 {
39         obj.enabled = true;
40         obj.equation = eq;
41 }
42
43 void Blend::Loader::factors(BlendFactor sf, BlendFactor df)
44 {
45         obj.enabled = true;
46         obj.src_factor = sf;
47         obj.dst_factor = df;
48 }
49
50
51 void operator>>(const LexicalConverter &conv, BlendEquation &eq)
52 {
53         const string &str = conv.get();
54         if(str=="ADD")
55                 eq = ADD;
56         else if(str=="SUBTRACT")
57                 eq = SUBTRACT;
58         else if(str=="REVERSE_SUBTRACT")
59                 eq = REVERSE_SUBTRACT;
60         else if(str=="MIN")
61                 eq = MIN;
62         else if(str=="MAX")
63                 eq = MAX;
64         else
65                 throw lexical_error(format("conversion of '%s' to BlendEquation", str));
66 }
67
68 void operator<<(LexicalConverter &conv, BlendEquation eq)
69 {
70         switch(eq)
71         {
72         case ADD: conv.result("ADD"); break;
73         case SUBTRACT: conv.result("SUBTRACT"); break;
74         case REVERSE_SUBTRACT: conv.result("REVERSE_SUBTRACT"); break;
75         case MIN: conv.result("MIN"); break;
76         case MAX: conv.result("MAX"); break;
77         default: conv.result(format("BlendEquation(%#x)", static_cast<int>(eq)));
78         }
79 }
80
81 void operator>>(const LexicalConverter &conv, BlendFactor &factor)
82 {
83         const string &str = conv.get();
84         if(str=="ZERO")
85                 factor = ZERO;
86         else if(str=="ONE")
87                 factor = ONE;
88         else if(str=="SRC_COLOR")
89                 factor = SRC_COLOR;
90         else if(str=="ONE_MINUS_SRC_COLOR")
91                 factor = ONE_MINUS_SRC_COLOR;
92         else if(str=="SRC_ALPHA")
93                 factor = SRC_ALPHA;
94         else if(str=="ONE_MINUS_SRC_ALPHA")
95                 factor = ONE_MINUS_SRC_ALPHA;
96         else if(str=="DST_COLOR")
97                 factor = DST_COLOR;
98         else if(str=="ONE_MINUS_DST_COLOR")
99                 factor = ONE_MINUS_DST_COLOR;
100         else if(str=="DST_ALPHA")
101                 factor = DST_ALPHA;
102         else if(str=="ONE_MINUS_DST_ALPHA")
103                 factor = ONE_MINUS_DST_ALPHA;
104         else if(str=="CONSTANT_COLOR")
105                 factor = CONSTANT_COLOR;
106         else if(str=="ONE_MINUS_CONSTANT_COLOR")
107                 factor = ONE_MINUS_CONSTANT_COLOR;
108         else if(str=="CONSTANT_ALPHA")
109                 factor = CONSTANT_ALPHA;
110         else if(str=="ONE_MINUS_CONSTANT_ALPHA")
111                 factor = ONE_MINUS_CONSTANT_ALPHA;
112         else
113                 throw lexical_error(format("conversion of '%s' to BlendFactor", str));
114 }
115
116 void operator<<(LexicalConverter &conv, BlendFactor factor)
117 {
118         switch(factor)
119         {
120         case ZERO: conv.result("ZERO"); break;
121         case ONE: conv.result("ONE"); break;
122         case SRC_COLOR: conv.result("SRC_COLOR"); break;
123         case ONE_MINUS_SRC_COLOR: conv.result("ONE_MINUS_SRC_COLOR"); break;
124         case SRC_ALPHA: conv.result("SRC_ALPHA"); break;
125         case ONE_MINUS_SRC_ALPHA: conv.result("ONE_MINUS_SRC_ALPHA"); break;
126         case DST_COLOR: conv.result("DST_COLOR"); break;
127         case ONE_MINUS_DST_COLOR: conv.result("ONE_MINUS_DST_COLOR"); break;
128         case DST_ALPHA: conv.result("DST_ALPHA"); break;
129         case ONE_MINUS_DST_ALPHA: conv.result("ONE_MINUS_DST_ALPHA"); break;
130         case CONSTANT_COLOR: conv.result("CONSTANT_COLOR"); break;
131         case ONE_MINUS_CONSTANT_COLOR: conv.result("ONE_MINUS_CONSTANT_COLOR"); break;
132         case CONSTANT_ALPHA: conv.result("CONSTANT_ALPHA"); break;
133         case ONE_MINUS_CONSTANT_ALPHA: conv.result("ONE_MINUS_CONSTANT_ALPHA"); break;
134         default: conv.result(format("BlendFactor(%#x)", static_cast<int>(factor)));
135         }
136 }
137
138 void operator>>(const LexicalConverter &conv, ColorWriteMask &mask)
139 {
140         ColorWriteMask result = WRITE_NONE;
141         for(const string &p: split(conv.get(), '_'))
142         {
143                 if(p=="ALL")
144                         result = result|WRITE_ALL;
145                 else if(p=="RED")
146                         result = result|WRITE_RED;
147                 else if(p=="GREEN")
148                         result = result|WRITE_GREEN;
149                 else if(p=="BLUE")
150                         result = result|WRITE_BLUE;
151                 else if(p=="ALPHA")
152                         result = result|WRITE_ALPHA;
153                 else
154                         throw lexical_error(format("conversion of '%s' to ColorWriteMask", conv.get()));
155         }
156         mask = result;
157 }
158
159 void operator<<(LexicalConverter &conv, ColorWriteMask mask)
160 {
161         if(mask==WRITE_ALL)
162                 conv.result("ALL");
163         else if(mask&~WRITE_ALL)
164                 conv.result(format("ColorWriteMask(%#x)", static_cast<int>(mask)));
165         else
166         {
167                 string result;
168                 if(mask&WRITE_RED)
169                         result = "RED";
170                 if(mask&WRITE_GREEN)
171                         append(result, "_", "GREEN");
172                 if(mask&WRITE_BLUE)
173                         append(result, "_", "BLUE");
174                 if(mask&WRITE_ALPHA)
175                         append(result, "_", "ALPHA");
176                 conv.result(result);
177         }
178 }
179
180 } // namespace GL
181 } // namespace Msp