@@ -36,6 +36,7 @@ static inline int TCODFOV_map2d_get_width(const TCODFOV_Map2D* __restrict map) {
3636 switch (map -> type ) {
3737 case TCODFOV_MAP2D_CALLBACK :
3838 case TCODFOV_MAP2D_BITPACKED :
39+ case TCODFOV_MAP2D_CONTIGIOUS :
3940 // Multiple structs share the same shape format
4041 return map -> bool_callback .shape [1 ];
4142 case TCODFOV_MAP2D_DEPRECATED :
@@ -52,6 +53,7 @@ static inline int TCODFOV_map2d_get_height(const TCODFOV_Map2D* __restrict map)
5253 switch (map -> type ) {
5354 case TCODFOV_MAP2D_CALLBACK :
5455 case TCODFOV_MAP2D_BITPACKED :
56+ case TCODFOV_MAP2D_CONTIGIOUS :
5557 return map -> bool_callback .shape [0 ];
5658 case TCODFOV_MAP2D_DEPRECATED :
5759 return map -> deprecated_map .map .height ;
@@ -76,7 +78,6 @@ static inline bool TCODFOV_map2d_in_bounds(const TCODFOV_Map2D* __restrict map,
7678/// @param y Y coordinate.
7779/// @return Boolean result, false if out-of-bounds or `map` is NULL.
7880static inline bool TCODFOV_map2d_get_bool (const TCODFOV_Map2D * __restrict map , int x , int y ) {
79- if (!map ) return 0 ;
8081 if (!TCODFOV_map2d_in_bounds (map , x , y )) return 0 ;
8182 switch (map -> type ) {
8283 case TCODFOV_MAP2D_CALLBACK :
@@ -98,6 +99,21 @@ static inline bool TCODFOV_map2d_get_bool(const TCODFOV_Map2D* __restrict map, i
9899 const uint8_t active_bit = 1 << (x % 8 );
99100 return (map -> bitpacked .data [map -> bitpacked .y_stride * y + (x / 8 )] & active_bit ) != 0 ;
100101 }
102+ case TCODFOV_MAP2D_CONTIGIOUS : {
103+ const ptrdiff_t index = map -> contigious .shape [0 ] * y + x ;
104+ switch (map -> contigious .item_type ) {
105+ case TCODFOV_DATATYPE_BOOL :
106+ return ((bool * )map -> contigious .data )[index ];
107+ case TCODFOV_DATATYPE_UINT8 :
108+ return ((uint8_t * )map -> contigious .data )[index ] != 0 ;
109+ case TCODFOV_DATATYPE_FLOAT :
110+ return ((float * )map -> contigious .data )[index ] != 0 ;
111+ case TCODFOV_DATATYPE_DOUBLE :
112+ return ((double * )map -> contigious .data )[index ] != 0 ;
113+ default :
114+ return 0 ;
115+ }
116+ };
101117 default :
102118 return 0 ;
103119 }
@@ -109,7 +125,6 @@ static inline bool TCODFOV_map2d_get_bool(const TCODFOV_Map2D* __restrict map, i
109125/// @param y Y coordinate.
110126/// @param value Assigned value.
111127static inline void TCODFOV_map2d_set_bool (TCODFOV_Map2D * __restrict map , int x , int y , bool value ) {
112- if (!map ) return ;
113128 if (!TCODFOV_map2d_in_bounds (map , x , y )) return ;
114129 switch (map -> type ) {
115130 case TCODFOV_MAP2D_CALLBACK :
@@ -137,25 +152,134 @@ static inline void TCODFOV_map2d_set_bool(TCODFOV_Map2D* __restrict map, int x,
137152 map -> bitpacked .data [index ] = (map -> bitpacked .data [index ] & ~active_bit ) | (value ? active_bit : 0 );
138153 return ;
139154 }
155+ case TCODFOV_MAP2D_CONTIGIOUS : {
156+ const ptrdiff_t index = map -> contigious .shape [0 ] * y + x ;
157+ switch (map -> contigious .item_type ) {
158+ case TCODFOV_DATATYPE_BOOL :
159+ ((bool * )map -> contigious .data )[index ] = value ;
160+ return ;
161+ case TCODFOV_DATATYPE_UINT8 :
162+ ((uint8_t * )map -> contigious .data )[index ] = value ;
163+ return ;
164+ case TCODFOV_DATATYPE_FLOAT :
165+ ((float * )map -> contigious .data )[index ] = value ;
166+ return ;
167+ case TCODFOV_DATATYPE_DOUBLE :
168+ ((double * )map -> contigious .data )[index ] = value ;
169+ return ;
170+ default :
171+ return ;
172+ }
173+ };
140174 default :
141175 return ;
142176 }
143177}
144178
145- /// @brief Assign `value` to `{x, y}` on `map`. Out-of-bounds writes are ignored.
179+ static inline uint8_t TCODFOV_map2d_get_u8 (TCODFOV_Map2D * __restrict map , int x , int y ) {
180+ if (!TCODFOV_map2d_in_bounds (map , x , y )) return 0 ;
181+ switch (map -> type ) {
182+ case TCODFOV_MAP2D_CONTIGIOUS : {
183+ const ptrdiff_t index = map -> contigious .shape [0 ] * y + x ;
184+ switch (map -> contigious .item_type ) {
185+ case TCODFOV_DATATYPE_BOOL :
186+ return ((bool * )map -> contigious .data )[index ] ? 255 : 0 ;
187+ case TCODFOV_DATATYPE_UINT8 :
188+ return ((uint8_t * )map -> contigious .data )[index ];
189+ case TCODFOV_DATATYPE_FLOAT :
190+ return (uint8_t )(((float * )map -> contigious .data )[index ] * 255.0f );
191+ case TCODFOV_DATATYPE_DOUBLE :
192+ return (uint8_t )(((double * )map -> contigious .data )[index ] * 255.0 );
193+ default :
194+ return 0 ;
195+ }
196+ };
197+ default :
198+ return TCODFOV_map2d_get_bool (map , x , y ) ? 255 : 0 ;
199+ }
200+ }
201+
202+ /// @brief Assign a normalized `value` to `{x, y}` on `map`. Out-of-bounds writes are ignored.
146203/// @param map Map union pointer, can be NULL.
147204/// @param x X coordinate.
148205/// @param y Y coordinate.
149206/// @param value Assigned value.
150- static inline void TCODFOV_map2d_set_int (TCODFOV_Map2D * __restrict map , int x , int y , int value ) {
151- TCODFOV_map2d_set_bool (map , x , y , value != 0 ); // Fallback until maps can hold integers
207+ static inline void TCODFOV_map2d_set_u8 (TCODFOV_Map2D * __restrict map , int x , int y , uint8_t value ) {
208+ if (!TCODFOV_map2d_in_bounds (map , x , y )) return ;
209+ switch (map -> type ) {
210+ case TCODFOV_MAP2D_CONTIGIOUS : {
211+ const ptrdiff_t index = map -> contigious .shape [0 ] * y + x ;
212+ switch (map -> contigious .item_type ) {
213+ case TCODFOV_DATATYPE_BOOL :
214+ ((bool * )map -> contigious .data )[index ] = value > 0 ;
215+ return ;
216+ case TCODFOV_DATATYPE_UINT8 :
217+ ((uint8_t * )map -> contigious .data )[index ] = value ;
218+ return ;
219+ case TCODFOV_DATATYPE_FLOAT :
220+ ((float * )map -> contigious .data )[index ] = (float )value * (1.0f / 255.0f );
221+ return ;
222+ case TCODFOV_DATATYPE_DOUBLE :
223+ ((double * )map -> contigious .data )[index ] = (double )value * (1.0 / 255.0 );
224+ return ;
225+ default :
226+ return ;
227+ }
228+ };
229+ default :
230+ TCODFOV_map2d_set_bool (map , x , y , value > 0 );
231+ return ;
232+ }
152233}
153234
154235static inline double TCODFOV_map2d_get_d (const TCODFOV_Map2D * __restrict map , int x , int y ) {
155- return TCODFOV_map2d_get_bool (map , x , y ) ? 1.0 : 0.0 ; // Fallback until maps can hold floats
236+ if (!TCODFOV_map2d_in_bounds (map , x , y )) return 0 ;
237+ switch (map -> type ) {
238+ case TCODFOV_MAP2D_CONTIGIOUS : {
239+ const ptrdiff_t index = map -> contigious .shape [0 ] * y + x ;
240+ switch (map -> contigious .item_type ) {
241+ case TCODFOV_DATATYPE_BOOL :
242+ return ((bool * )map -> contigious .data )[index ] ? 1.0 : 0.0 ;
243+ case TCODFOV_DATATYPE_UINT8 :
244+ return (double )((uint8_t * )map -> contigious .data )[index ] * (1.0 / 255.0 );
245+ case TCODFOV_DATATYPE_FLOAT :
246+ return (double )((float * )map -> contigious .data )[index ];
247+ case TCODFOV_DATATYPE_DOUBLE :
248+ return ((double * )map -> contigious .data )[index ];
249+ default :
250+ return 0 ;
251+ }
252+ };
253+ default :
254+ return TCODFOV_map2d_get_bool (map , x , y ) ? 1.0 : 0.0 ;
255+ }
156256}
157257
158258static inline void TCODFOV_map2d_set_d (TCODFOV_Map2D * __restrict map , int x , int y , double value ) {
159- TCODFOV_map2d_set_bool (map , x , y , value >= 0.5 ); // Fallback until maps can hold floats
259+ if (!TCODFOV_map2d_in_bounds (map , x , y )) return ;
260+ switch (map -> type ) {
261+ case TCODFOV_MAP2D_CONTIGIOUS : {
262+ const ptrdiff_t index = map -> contigious .shape [0 ] * y + x ;
263+ switch (map -> contigious .item_type ) {
264+ case TCODFOV_DATATYPE_BOOL :
265+ ((bool * )map -> contigious .data )[index ] = value >= 0.5 ;
266+ return ;
267+ case TCODFOV_DATATYPE_UINT8 :
268+ ((uint8_t * )map -> contigious .data )[index ] = (uint8_t )(value * 255.0 );
269+ return ;
270+ case TCODFOV_DATATYPE_FLOAT :
271+ ((float * )map -> contigious .data )[index ] = (float )value ;
272+ return ;
273+ case TCODFOV_DATATYPE_DOUBLE :
274+ ((double * )map -> contigious .data )[index ] = value ;
275+ return ;
276+ default :
277+ return ;
278+ }
279+ };
280+ default :
281+ TCODFOV_map2d_set_bool (map , x , y , value >= 0.5 );
282+ return ;
283+ }
160284}
161285#endif // TCODFOV_MAP_INLINE_H_
0 commit comments