Merge pull request #4852 from FernetMenta/aefixes
[vuplus_xbmc] / lib / snesapu / A2Misc.h
1 /***************************************************************************************************
2 * Program:    Miscellaneous Classes                                                                *
3 * Programmer: Anti Resonance                                                                       *
4 *                                                                                                  *
5 * This library is free software; you can redistribute it and/or modify it under the terms of the   *
6 * GNU Lesser General Public License as published by the Free Software Foundation; either version   *
7 * 2.1 of the License, or (at your option) any later version.                                       *
8 *                                                                                                  *
9 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;        *
10 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.        *
11 * See the GNU Lesser General Public License for more details.                                      *
12 *                                                                                                  *
13 * You should have received a copy of the GNU Lesser General Public License along with this         *
14 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,        *
15 * Boston, MA  02111-1307  USA                                                                      *
16 *                                                                                                  *
17 *                                                           Copyright (C)2006 Alpha-II Productions *
18 ***************************************************************************************************/
19
20 #include "Types.h"
21
22 namespace A2
23 {
24
25         ////////////////////////////////////////////////////////////////////////////////////////////////
26         // Set Class Template
27         //
28         // Used to maintain a set of integral values from 0 to 31.  For a larger range or for sets
29         // containing negative values, use SetL.  Behaviour is undefined for Sets of a non-integral
30         // type.  No range checking is performed.
31
32         template<typename T>
33         class Set
34         {
35                 u32             bits;
36
37         public:
38                 inline                  Set<T>()                                                {bits = 0;}
39                 inline                  Set<T>(const Set<T>& s)                 {bits = s.bits;}
40                 inline                  Set<T>(const T i)                               {bits = 1 << i;}
41                                                 Set<T>(u32 num, const T* items)
42                                                 {       bits = 0; while (num) bits |= 1 << items[--num]; }
43
44                 inline void             Clear()                                                 {bits = 0;}
45
46                 inline bool             HasItems() const                                {return bits != 0;}
47                 u32                             NumItems() const
48                                                 {       u32 i=0,j=bits; while (j) {i += (j & 1); j >>= 1;} return i; }
49
50                 inline Set<T>&  operator =  (const Set<T> s)    {bits = s; return *this;}
51                 inline Set<T>&  operator << (const Set<T> s)    {bits |= s.bits; return *this;}
52                 inline Set<T>&  operator >> (const Set<T> s)    {bits &= ~s.bits; return *this;}
53                 inline Set<T>   operator ~  () const                    {Set<T> t; t.bits = ~bits; return t;}
54
55                 inline Set<T>&  operator =  (const T i)                 {bits = 1 << i; return *this;}
56                 inline Set<T>&  operator << (const T i)                 {bits |= 1 << i; return *this;}
57                 inline Set<T>&  operator >> (const T i)                 {bits &= ~(1 << i); return *this;}
58                 inline bool             operator [] (const T i) const   {return (bits & (1 << i)) != 0;}
59
60                 inline bool             operator && (const Set<T> s) const      {return (bits & s.bits) == s.bits;}
61                 inline bool             operator || (const Set<T> s) const      {return (bits & s.bits) != 0;}
62         };
63
64
65
66         ////////////////////////////////////////////////////////////////////////////////////////////////
67         // Large Set Class Template
68         //
69         // Like a Set, but can contain much larger ranges (-2^31 to 2^31-1).  Range checking _is_
70         // performed on the input.
71
72 #ifndef _MSC_VER                                                                //MS VC++ 6 doesn't like this class
73         template<typename T, T start, T end>
74         class SetL
75         {
76         private:
77                 u8              bits[((end - start) + 8) >> 3];
78
79         public:
80                 inline                  SetL<T,start,end>()                             {Clear();}
81
82                 inline                  SetL<T,start,end>(const SetL<T,start,end>& s)
83                                                 {       operator = (s); }
84
85                                                 SetL<T,start,end>(u32 num, const s32* items)
86                                                 {       Clear(); while (num) operator << (items[--num]); }
87
88
89                 void                    Clear()
90                                                 {       for (u32 i=((end - start) + 7) >> 3; i;) bits[--i] = 0; }
91
92
93                 inline SetL<T,start,end>&       operator << (const s32 i)
94                                                 {       if (i >= start && i <= end)
95                                                         {       const s32       b = i - start; bits[b >> 3] |= 1 << (b & 7); }
96                                                         return *this; }
97
98                 inline SetL<T,start,end>&       operator >> (const s32 i)
99                                                 {       if (i >= start && i <= end)
100                                                         {       const s32       b = i - start; bits[b >> 3] &= ~(1 << (b & 7)); }
101                                                         return *this; }
102
103                 inline bool             operator [] (const s32 i) const
104                                                 {       if (i >= start && i <= end)
105                                                         {       const s32       b = i - start;
106                                                                 return (bits[b >> 3] & (1 << (b & 7))) != 0; }
107                                                         return false; }
108         };
109 #endif
110
111
112         ////////////////////////////////////////////////////////////////////////////////////////////////
113         // Color Class
114
115         class Color
116         {
117                 union
118                 {
119                         struct {u8 r,g,b,a;};
120                         u32     rgba;
121                 };
122
123         public:
124                 inline Color&   operator = (const Color& c)
125                                                 {this->rgba = c.rgba; return *this;}
126
127                 inline Color&   operator = (const u32 rgba)
128                                                 {this->rgba = rgba; return *this;}
129
130                 inline void             operator () (const u8 r, const u8 g, const u8 b, const u8 a = 255)
131                                                 {this->r = r; this->g = g; this->b = b; this->a = a;}
132
133 //              inline void             operator () (const f32 r, const f32 g, const f32 b, const f32 a = 1.0f)
134 //                                              {       this->r = (u8)(r * 255.0f); this->g = (u8)(g * 255.0f);
135 //                                                      this->b = (u8)(b * 255.0f); this->a = (u8)(a * 255.0f); }
136
137                 inline                  Color()                                 {}
138                 inline                  Color(const Color& c)   {operator = (c.rgba);}
139                 inline                  Color(const u32 rgba)   {operator = (rgba);}
140                 inline                  Color(const u8 r, const u8 g, const u8 b, const u8 a = 255)
141                                                                                                 {operator () (r, g, b, a);}
142 //              inline                  Color(const f32 r, const f32 g, const f32 b, const f32 a = 1.0f)
143 //                                                                                              {operator () (r, g, b, a);}
144
145                 inline void             Red(const u8 val)               {r = val;}
146                 inline void             Green(const u8 val)             {g = val;}
147                 inline void             Blue(const u8 val)              {b = val;}
148                 inline void             Alpha(const u8 val)             {a = val;}
149
150                 inline u8               Red() const                             {return r;}
151                 inline u8               Green() const                   {return g;}
152                 inline u8               Blue() const                    {return b;}
153                 inline u8               Alpha() const                   {return a;}
154
155                 inline                  operator u32 () const   {return rgba;}
156
157 #if defined __BORLANDC__
158                 inline                  operator TColor () const
159                                                 {return static_cast<TColor>(rgba & 0xFFFFFF);}
160 #endif
161
162                 inline Color    operator >> (const u8 sa) const
163                                                 {return Color((u8)(r >> sa), (u8)(g >> sa), (u8)(b >> sa), (u8)(a >> sa));}
164
165                 inline void             Avg(const Color c1, const Color c2)
166                                                 {       r = (u8)(((u32)c1.r + (u32)c2.r) >> 1);
167                                                         g = (u8)(((u32)c1.g + (u32)c2.g) >> 1);
168                                                         b = (u8)(((u32)c1.b + (u32)c2.b) >> 1);
169                                                         a = (u8)(((u32)c1.a + (u32)c2.a) >> 1);}
170
171         };
172
173 }       //namespace A2