1 /***************************************************************************************************
2 * Program: Miscellaneous Classes *
3 * Programmer: Anti Resonance *
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. *
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. *
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 *
17 * Copyright (C)2006 Alpha-II Productions *
18 ***************************************************************************************************/
25 ////////////////////////////////////////////////////////////////////////////////////////////////
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.
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]; }
44 inline void Clear() {bits = 0;}
46 inline bool HasItems() const {return bits != 0;}
48 { u32 i=0,j=bits; while (j) {i += (j & 1); j >>= 1;} return i; }
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;}
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;}
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;}
66 ////////////////////////////////////////////////////////////////////////////////////////////////
67 // Large Set Class Template
69 // Like a Set, but can contain much larger ranges (-2^31 to 2^31-1). Range checking _is_
70 // performed on the input.
72 #ifndef _MSC_VER //MS VC++ 6 doesn't like this class
73 template<typename T, T start, T end>
77 u8 bits[((end - start) + 8) >> 3];
80 inline SetL<T,start,end>() {Clear();}
82 inline SetL<T,start,end>(const SetL<T,start,end>& s)
85 SetL<T,start,end>(u32 num, const s32* items)
86 { Clear(); while (num) operator << (items[--num]); }
90 { for (u32 i=((end - start) + 7) >> 3; i;) bits[--i] = 0; }
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); }
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)); }
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; }
112 ////////////////////////////////////////////////////////////////////////////////////////////////
119 struct {u8 r,g,b,a;};
124 inline Color& operator = (const Color& c)
125 {this->rgba = c.rgba; return *this;}
127 inline Color& operator = (const u32 rgba)
128 {this->rgba = rgba; return *this;}
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;}
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); }
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);}
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;}
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;}
155 inline operator u32 () const {return rgba;}
157 #if defined __BORLANDC__
158 inline operator TColor () const
159 {return static_cast<TColor>(rgba & 0xFFFFFF);}
162 inline Color operator >> (const u8 sa) const
163 {return Color((u8)(r >> sa), (u8)(g >> sa), (u8)(b >> sa), (u8)(a >> sa));}
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);}