1 /*
2 * XPilot NG, a multiplayer space war game.
3 *
4 * Copyright (C) 2000-2004 by
5 *
6 * Uoti Urpala <uau@users.sourceforge.net>
7 * Kristian S�derblom <kps@users.sourceforge.net>
8 *
9 * Copyright (C) 1991-2001 by
10 *
11 * Bj�rn Stabell <bjoern@xpilot.org>
12 * Ken Ronny Schouten <ken@xpilot.org>
13 * Bert Gijsbers <bert@xpilot.org>
14 * Dick Balaska <dick@xpilot.org>
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 */
30
31 #ifndef CLICK_H
32 #define CLICK_H
33
34 #ifndef CONST_H
35 # include "const.h"
36 #endif
37
38 /*
39 * The wall collision detection routines depend on repeatability
40 * (getting the same result even after some "neutral" calculations)
41 * and an exact determination whether a point is in space,
42 * inside the wall (crash!) or on the edge.
43 * This will be hard to achieve if only floating point would be used.
44 * However, a resolution of a pixel is a bit rough and ugly.
45 * Therefore a fixed point sub-pixel resolution is used called clicks.
46 */
47
48 typedef int click_t;
49
50 typedef struct {
51 click_t cx, cy;
52 } clpos_t;
53
54 typedef struct {
55 click_t cx, cy;
56 } clvec_t;
57
58 #define CLICK_SHIFT 6
59 #define CLICK (1 << CLICK_SHIFT)
60 #define PIXEL_CLICKS CLICK
61 #define BLOCK_CLICKS (BLOCK_SZ << CLICK_SHIFT)
62 #define CLICK_TO_PIXEL(C) ((int)((C) >> CLICK_SHIFT))
63 #define CLICK_TO_BLOCK(C) ((int)((C) / (BLOCK_SZ << CLICK_SHIFT)))
64 #define CLICK_TO_FLOAT(C) ((double)(C) * (1.0 / CLICK))
65 #define PIXEL_TO_CLICK(I) ((click_t)(I) << CLICK_SHIFT)
66 #define FLOAT_TO_CLICK(F) ((int)((F) * CLICK))
67
68 /*
69 * Return the block position this click position is in.
70 */
Clpos_to_blkpos(clpos_t pos)71 static inline blkpos_t Clpos_to_blkpos(clpos_t pos)
72 {
73 blkpos_t bpos;
74
75 bpos.bx = CLICK_TO_BLOCK(pos.cx);
76 bpos.by = CLICK_TO_BLOCK(pos.cy);
77
78 return bpos;
79 }
80
81 #define BLOCK_CENTER(B) ((int)((B) * BLOCK_CLICKS) + BLOCK_CLICKS / 2)
82
83 /* calculate the clpos of the center of a block */
Block_get_center_clpos(blkpos_t bpos)84 static inline clpos_t Block_get_center_clpos(blkpos_t bpos)
85 {
86 clpos_t pos;
87
88 pos.cx = (bpos.bx * BLOCK_CLICKS) + BLOCK_CLICKS / 2;
89 pos.cy = (bpos.by * BLOCK_CLICKS) + BLOCK_CLICKS / 2;
90
91 return pos;
92 }
93
94 #endif
95