Codechange: be consistent in naming the paint function Paint()
[openttd-github.git] / src / direction_func.h
blobe1bad998aa1c472701374a4a270e7ba987e6fc2f
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file direction_func.h Different functions related to conversions between directions. */
10 #ifndef DIRECTION_FUNC_H
11 #define DIRECTION_FUNC_H
13 #include "direction_type.h"
15 /**
16 * Checks if an integer value is a valid DiagDirection
18 * @param d The value to check
19 * @return True if the value belongs to a DiagDirection, else false
21 static inline bool IsValidDiagDirection(DiagDirection d)
23 return d < DIAGDIR_END;
26 /**
27 * Checks if an integer value is a valid Direction
29 * @param d The value to check
30 * @return True if the value belongs to a Direction, else false
32 static inline bool IsValidDirection(Direction d)
34 return d < DIR_END;
37 /**
38 * Checks if an integer value is a valid Axis
40 * @param d The value to check
41 * @return True if the value belongs to an Axis, else false
43 static inline bool IsValidAxis(Axis d)
45 return d < AXIS_END;
48 /**
49 * Return the reverse of a direction
51 * @param d The direction to get the reverse from
52 * @return The reverse Direction
54 static inline Direction ReverseDir(Direction d)
56 assert(IsValidDirection(d));
57 return (Direction)(4 ^ d);
61 /**
62 * Calculate the difference between two directions
64 * @param d0 The first direction as the base
65 * @param d1 The second direction as the offset from the base
66 * @return The difference how the second direction drifts of the first one.
68 static inline DirDiff DirDifference(Direction d0, Direction d1)
70 assert(IsValidDirection(d0));
71 assert(IsValidDirection(d1));
72 /* Cast to uint so compiler can use bitmask. If the difference is negative
73 * and we used int instead of uint, further "+ 8" would have to be added. */
74 return (DirDiff)((uint)(d0 - d1) % 8);
77 /**
78 * Applies two differences together
80 * This function adds two differences together and returns the resulting
81 * difference. So adding two DIRDIFF_REVERSE together results in the
82 * DIRDIFF_SAME difference.
84 * @param d The first difference
85 * @param delta The second difference to add on
86 * @return The resulting difference
88 static inline DirDiff ChangeDirDiff(DirDiff d, DirDiff delta)
90 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
91 return (DirDiff)((uint)(d + delta) % 8);
94 /**
95 * Change a direction by a given difference
97 * This functions returns a new direction of the given direction
98 * which is rotated by the given difference.
100 * @param d The direction to get a new direction from
101 * @param delta The offset/drift applied to the direction
102 * @return The new direction
104 static inline Direction ChangeDir(Direction d, DirDiff delta)
106 assert(IsValidDirection(d));
107 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
108 return (Direction)((uint)(d + delta) % 8);
113 * Returns the reverse direction of the given DiagDirection
115 * @param d The DiagDirection to get the reverse from
116 * @return The reverse direction
118 static inline DiagDirection ReverseDiagDir(DiagDirection d)
120 assert(IsValidDiagDirection(d));
121 return (DiagDirection)(2 ^ d);
125 * Calculate the difference between two DiagDirection values
127 * @param d0 The first direction as the base
128 * @param d1 The second direction as the offset from the base
129 * @return The difference how the second direction drifts of the first one.
131 static inline DiagDirDiff DiagDirDifference(DiagDirection d0, DiagDirection d1)
133 assert(IsValidDiagDirection(d0));
134 assert(IsValidDiagDirection(d1));
135 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
136 return (DiagDirDiff)((uint)(d0 - d1) % 4);
140 * Applies a difference on a DiagDirection
142 * This function applies a difference on a DiagDirection and returns
143 * the new DiagDirection.
145 * @param d The DiagDirection
146 * @param delta The difference to apply on
147 * @return The new direction which was calculated
149 static inline DiagDirection ChangeDiagDir(DiagDirection d, DiagDirDiff delta)
151 assert(IsValidDiagDirection(d));
152 /* Cast to uint so compiler can use bitmask. Result can never be negative. */
153 return (DiagDirection)((uint)(d + delta) % 4);
157 * Convert a Direction to a DiagDirection.
159 * This function can be used to convert the 8-way Direction to
160 * the 4-way DiagDirection. If the direction cannot be mapped its
161 * "rounded clockwise". So DIR_N becomes DIAGDIR_NE.
163 * @param dir The direction to convert
164 * @return The resulting DiagDirection, maybe "rounded clockwise".
166 static inline DiagDirection DirToDiagDir(Direction dir)
168 assert(IsValidDirection(dir));
169 return (DiagDirection)(dir >> 1);
173 * Convert a DiagDirection to a Direction.
175 * This function can be used to convert the 4-way DiagDirection
176 * to the 8-way Direction. As 4-way are less than 8-way not all
177 * possible directions can be calculated.
179 * @param dir The direction to convert
180 * @return The resulting Direction
182 static inline Direction DiagDirToDir(DiagDirection dir)
184 assert(IsValidDiagDirection(dir));
185 return (Direction)(dir * 2 + 1);
190 * Select the other axis as provided.
192 * This is basically the not-operator for the axis.
194 * @param a The given axis
195 * @return The other axis
197 static inline Axis OtherAxis(Axis a)
199 assert(IsValidAxis(a));
200 return (Axis)(a ^ 1);
205 * Convert a DiagDirection to the axis.
207 * This function returns the axis which belongs to the given
208 * DiagDirection. The axis X belongs to the DiagDirection
209 * north-east and south-west.
211 * @param d The DiagDirection
212 * @return The axis which belongs to the direction
214 static inline Axis DiagDirToAxis(DiagDirection d)
216 assert(IsValidDiagDirection(d));
217 return (Axis)(d & 1);
222 * Converts an Axis to a DiagDirection
224 * This function returns the DiagDirection which
225 * belongs to the axis. As 2 directions are mapped to an axis
226 * this function returns the one which points to south,
227 * either south-west (on X axis) or south-east (on Y axis)
229 * @param a The axis
230 * @return The direction pointed to south
232 static inline DiagDirection AxisToDiagDir(Axis a)
234 assert(IsValidAxis(a));
235 return (DiagDirection)(2 - a);
239 * Converts an Axis to a Direction
241 * This function returns the Direction which
242 * belongs to the axis. As 2 directions are mapped to an axis
243 * this function returns the one which points to south,
244 * either south-west (on X axis) or south-east (on Y axis)
246 * @param a The axis
247 * @return The direction pointed to south
249 static inline Direction AxisToDirection(Axis a)
251 assert(IsValidAxis(a));
252 return (Direction)(5 - 2 * a);
256 * Convert an axis and a flag for north/south into a DiagDirection
257 * @param xy axis to convert
258 * @param ns north -> 0, south -> 1
259 * @return the desired DiagDirection
261 static inline DiagDirection XYNSToDiagDir(Axis xy, uint ns)
263 assert(IsValidAxis(xy));
264 return (DiagDirection)(xy * 3 ^ ns * 2);
268 * Checks if a given Direction is diagonal.
270 * @param dir The given direction.
271 * @return True if the direction is diagonal.
273 static inline bool IsDiagonalDirection(Direction dir)
275 assert(IsValidDirection(dir));
276 return (dir & 1) != 0;
279 #endif /* DIRECTION_FUNC_H */