1 /*
2  *
3  *  Iter Vehemens ad Necem (IVAN)
4  *  Copyright (C) Timo Kiviluoto
5  *  Released under the GNU General
6  *  Public License
7  *
8  *  See LICENSING which should be included
9  *  along with this file for more details
10  *
11  */
12 
13 #ifndef __FELIBDEF_H__
14 #define __FELIBDEF_H__
15 
16 /*
17  * Global defines for the project FeLib.
18  * This file is created to decrease the need of including headers in
19  * other headers just for the sake of some silly macros, because it
20  * decreases compilation efficiency and may cause cross-including
21  *
22  * List of macros that should be gathered here:
23  * 1. all numeric defines used in multiple .cpp or .h files
24  * 2. all inline functions used in multiple .cpp or .h files
25  *    and independent enough (do not require other headers)
26  * 3. class construction macros used in multiple .h files
27  */
28 
29 #include "typedef.h"
30 
31 cint MapMoveX[9] = { -1, 0, 1, -1, 1, -1, 0, 1, 0 };
32 cint MapMoveY[9] = { -1, -1, -1, 0, 0, 1, 1, 1, 0 };
33 
34 culong SquarePartTickMask[4] = { 0xFF, 0xFF00, 0xFF0000, 0xFF000000 };
35 
36 #define FPI 3.1415926535897932384626433832795
37 
38 /* Btw, both __attribute__ ((regparm(3))) and __fastcall SUCK! */
39 
40 #ifdef GCC
41 #define NO_ALIGNMENT __attribute__ ((packed))
42 #define LIKE_PRINTF(p1, p2) __attribute__ ((format(printf, p1, p2)))
43 #else
44 #define NO_ALIGNMENT
45 #define LIKE_PRINTF(p1, p2)
46 #endif
47 
48 template <class type>
Sign(type X)49 inline type Sign(type X) { return X > 0 ? 1 : X < 0 ? -1 : 0; }
50 
51 template <class type>
Max(type X,type Y)52 inline type Max(type X, type Y) { return X >= Y ? X : Y; }
53 
54 template <class type>
Max(type X,type Y,type Z)55 inline type Max(type X, type Y, type Z)
56 { return X >= Y ? (X >= Z ? X : Z) : (Y >= Z ? Y : Z); }
57 
58 template <class type>
Min(type X,type Y)59 inline type Min(type X, type Y) { return X <= Y ? X : Y; }
60 
61 template <class type>
Min(type X,type Y,type Z)62 inline type Min(type X, type Y, type Z)
63 { return X <= Y ? (X <= Z ? X : Z) : (Y <= Z ? Y : Z); }
64 
65 template <class type>
HypotSquare(type X,type Y)66 inline type HypotSquare(type X, type Y) { return X * X + Y * Y; }
67 
68 template <class type>
Limit(type Value,type Minimum,type Maximum)69 inline type Limit(type Value, type Minimum, type Maximum)
70 { return Value >= Minimum ? Value <= Maximum ? Value : Maximum : Minimum; }
71 
72 template <class type>
LimitRef(type & Value,type Minimum,type Maximum)73 inline void LimitRef(type& Value, type Minimum, type Maximum)
74 {
75   if(Value <= Minimum)
76     Value = Minimum;
77   else if(Value >= Maximum)
78     Value = Maximum;
79 }
80 
81 template <class type>
Wrap(type Value,type Minimum,type Maximum)82 inline type Wrap(type Value, type Minimum, type Maximum)
83 {
84   Value = (Value - Minimum) % (Maximum - Minimum);
85   return (Value >= 0 ? Minimum : Maximum) + Value;
86 }
87 
88 template <class type>
WrapRef(type & Value,type Minimum,type Maximum)89 inline void WrapRef(type& Value, type Minimum, type Maximum)
90 {
91   Value = (Value - Minimum) % (Maximum - Minimum);
92   Value += (Value >= 0 ? Minimum : Maximum);
93 }
94 
95 template <class type>
WrapF(type Value,type Minimum,type Maximum)96 inline type WrapF(type Value, type Minimum, type Maximum)
97 {
98   Value = fmod(Value - Minimum, Maximum - Minimum);
99   return Value >= 0. ? Value + Minimum : Value + Maximum;
100 }
101 
102 template <class type>
WrapFRef(type & Value,type Minimum,type Maximum)103 inline void WrapFRef(type& Value, type Minimum, type Maximum)
104 { Value = WrapF(Value, Minimum, Maximum); }
105 
106 template <class type>
WrapAverage(type X,type Y,type WrapLimit)107 inline double WrapAverage(type X, type Y, type WrapLimit)
108 {
109   type Minimum = Min(X, Y);
110   type Maximum = Max(X, Y);
111 
112   if(Maximum - Minimum > WrapLimit / 2)
113   {
114     double Avg = (Minimum + Maximum + WrapLimit) / 2.;
115     return Avg >= WrapLimit ? Avg - WrapLimit : Avg;
116   }
117   else
118     return (X + Y) / 2.;
119 }
120 
121 template <class type>
Swap(type & X,type & Y)122 inline void Swap(type& X, type& Y)
123 {
124   const type T = X;
125   X = Y;
126   Y = T;
127 }
128 
GetRed16(col16 Color)129 inline col16 GetRed16(col16 Color) { return Color >> 8 & 0xF8; }
GetGreen16(col16 Color)130 inline col16 GetGreen16(col16 Color) { return Color >> 3 & 0xFC; }
GetBlue16(col16 Color)131 inline col16 GetBlue16(col16 Color) { return Color << 3 & 0xF8; }
132 
MakeRGB16(int Red,int Green,int Blue)133 inline col16 MakeRGB16(int Red, int Green, int Blue)
134 {
135   return (Red << 8 & 0xF800) | (Green << 3 & 0x7E0) | (Blue >> 3 & 0x1F);
136 }
137 
MakeShadeColor(col16 Color)138 inline col16 MakeShadeColor(col16 Color)
139 {
140   return MakeRGB16(GetRed16(Color) / 3,
141                    GetGreen16(Color) / 3,
142                    GetBlue16(Color) / 3);
143 }
144 
GetRed24(col24 Color)145 inline col24 GetRed24(col24 Color) { return Color >> 16 & 0xFF; }
GetGreen24(col24 Color)146 inline col24 GetGreen24(col24 Color) { return Color >> 8 & 0xFF; }
GetBlue24(col24 Color)147 inline col24 GetBlue24(col24 Color) { return Color & 0xFF; }
148 
MakeRGB24(int Red,int Green,int Blue)149 inline col24 MakeRGB24(int Red, int Green, int Blue)
150 {
151   return (Red << 16 & 0xFF0000) | (Green << 8 & 0xFF00) | (Blue & 0xFF);
152 }
153 
GetMaxColor24(col24 Color)154 inline int GetMaxColor24(col24 Color)
155 {
156   return Max(GetRed24(Color), GetGreen24(Color), GetBlue24(Color));
157 }
158 
GetMinColor24(col24 Color)159 inline int GetMinColor24(col24 Color)
160 {
161   return Min(GetRed24(Color), GetGreen24(Color), GetBlue24(Color));
162 }
163 
164 #define NONE 0
165 #define MIRROR 1
166 #define FLIP 2
167 #define ROTATE 4
168 
169 #define TRANSPARENT_COLOR 0xF81F  // pink
170 
171 #define RED 0xF800
172 #define GREEN 0x07E0
173 #define BLUE 0x2ABF
174 
175 #define YELLOW 0xFFE0
176 #define PINK 0xF01E
177 
178 #define WHITE 0xFFFF
179 #define LIGHT_GRAY 0x94B2
180 #define DARK_GRAY 0x528A
181 #define BLACK 0x0000
182 
183 #define NORMAL_LUMINANCE 0x808080
184 
185 #ifdef USE_SDL
186 #include "SDL_version.h"
187 #endif
188 #if SDL_MAJOR_VERSION == 2 || !defined(__APPLE__)
189 #define KEY_BACK_SPACE 0x08
190 #else
191 #define KEY_BACK_SPACE 0x7F
192 #endif
193 #define KEY_ESC 0x1B
194 #define KEY_ENTER 0x0D
195 #define KEY_HOME      0x147
196 #define KEY_UP        0x148
197 #define KEY_PAGE_UP   0x149
198 #define KEY_LEFT      0x14B
199 #define KEY_RIGHT     0x14D
200 #define KEY_END       0x14F
201 #define KEY_DOWN      0x150
202 #define KEY_PAGE_DOWN 0x151
203 #define KEY_DELETE    0x152
204 #define KEY_INSERT    0x153
205 #define KEY_SPACE ' '
206 #define KEY_NUMPAD_5 2
207 
208 #define NO_FLAME 0xFFFF
209 
210 #define SELECTABLE 1
211 #define INVERSE_MODE 2
212 #define BLIT_AFTERWARDS 4
213 #define DRAW_BACKGROUND_AFTERWARDS 8
214 #define FADE 16
215 
216 /* felist errors */
217 
218 #define FELIST_ERROR_BIT 0x8000
219 #define LIST_WAS_EMPTY 0xFFFF
220 #define ESCAPED 0xFFFE
221 #define NOTHING_SELECTED 0xFFFD
222 
223 #define NO_LIMIT 0xFFFF
224 
225 #define MAX_CONTROLS 0x10
226 
227 #define HIGHEST 0xFF
228 
229 #define NORMAL_EXIT 0
230 #define ABORTED 1
231 
232 #define MAX_CONFIG_OPTIONS 0x100
233 
234 #define FLY_PRIORITY ((10 << 4) + 10)
235 #define SPARKLE_PRIORITY ((12 << 4) + 12)
236 #define LIGHTNING_PRIORITY ((14 << 4) + 14)
237 #define AVERAGE_PRIORITY ((8 << 4) + 8)
238 
239 #define NO_IMAGE 0xFFFF
240 
241 #define ZERO_POOLS 1
242 #define RAND_ALLOC 2
243 
244 #define REFS(ptr) reinterpret_cast<ulong*>(ptr)[-1]
245 
246 #define SKIP_FIRST 1
247 #define ALLOW_END_FAILURE 2
248 
249 #define MAX_RAND 0x7FFFFFFF
250 
251 #define TRANSPARENT_PALETTE_INDEX 191
252 
253 #define MAX_HIGHSCORES 100
254 
255 /* sparkling flags */
256 
257 #define SPARKLING_A 1
258 #define SPARKLING_B 2
259 #define SPARKLING_C 4
260 #define SPARKLING_D 8
261 
262 /* For control schemes: */
263 
264 #define DIR_NORM 0
265 #define DIR_ALT  1
266 #define DIR_HACK 2
267 
268 #endif
269