File: sardrawselect.c

package info (click to toggle)
searchandrescue 0.8.2-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 4,656 kB
  • ctags: 6,111
  • sloc: ansic: 89,072; cpp: 7,691; sh: 90; makefile: 80
file content (318 lines) | stat: -rw-r--r-- 7,362 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#include <stdlib.h>
#include <string.h>
#ifdef __MSW__
# include <windows.h>
#endif
#include <GL/gl.h>
#include "obj.h"
#include "sar.h"
#include "sardraw.h"
#include "sardrawselect.h"
#include "config.h"


void SARDrawMapProcessHits(
	sar_core_struct *core_ptr, GLint hits, GLuint buffer[]
);

int SARDrawMapObjNameListAppend(
	sar_drawmap_objname_struct ***ptr, int *total,
	GLuint gl_name, const char *obj_name
);
void SARDrawMapObjNameListDelete(
	sar_drawmap_objname_struct ***ptr, int *total
);

int *SARGetGCCHitList(
	sar_core_struct *core_ptr, sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	int obj_num,
	int *hits
);
int SARGetGHCOverWater(
	sar_core_struct *core_ptr, sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	int obj_num, Boolean *got_hit, Boolean *over_water
);


#define ATOI(s)         (((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)         (((s) != NULL) ? atol(s) : 0)
#define ATOF(s)         (((s) != NULL) ? atof(s) : 0.0f)
#define STRDUP(s)       (((s) != NULL) ? strdup(s) : NULL)

#define MAX(a,b)	(((a) > (b)) ? (a) : (b))
#define MIN(a,b)	(((a) < (b)) ? (a) : (b))
#define CLIP(a,l,h)	(MIN(MAX((a),(l)),(h)))
#define STRISEMPTY(s)	(((s) != NULL) ? (*(s) == '\0') : TRUE)

#define RADTODEG(r)	((r) * 180 / PI)
#define DEGTORAD(d)	((d) * PI / 180)


/*
 *	Called by SARDrawMap() to process the hits after drawing the
 *	map in GL_SELECT render mode.
 *
 *	Hits will be recorded on specified core's draw map object names
 *	list.
 *
 *	Inputs assumed valid.
 */
void SARDrawMapProcessHits(
	sar_core_struct *core_ptr, GLint hits, GLuint buffer[]
)
{
	int i, j;
	GLuint gl_name, names, *ptr = buffer;

	/* Iterate through each hit */
	for(i = 0; i < hits; i++)
	{
	    names = *ptr;	/* Number of names on this hit */
	    ptr += 3;		/* Seek to start of names on this hit */

	    /* Iterate through each name on this hit */
	    for(j = 0; j < (int)names; j++)
	    {
		/* Get the GL name which is the object's index number */
		gl_name = *ptr++;

		/* Append this object to the names list */
		SARDrawMapObjNameListAppend(
		    &core_ptr->drawmap_objname,
		    &core_ptr->total_drawmap_objnames,
		    gl_name,		/* Object's index */
		    NULL		/* No object names for now */
		);
	    }
	}
}

/*
 *	Appends an Object Name to the Object Names List.
 *
 *	Returns the index of the new Object Name or negative on error.
 */
int SARDrawMapObjNameListAppend(
	sar_drawmap_objname_struct ***ptr, int *total,
	GLuint gl_name, const char *obj_name
)
{
	int n;
	sar_drawmap_objname_struct *name;

	if((ptr == NULL) || (total == NULL))
	    return(-2);

	/* Increase allocation of the Object Names List */
	n = MAX(*total, 0);
	*total = n + 1;
	*ptr = (sar_drawmap_objname_struct **)realloc(
	    *ptr,
	    (*total) * sizeof(sar_drawmap_objname_struct *)
	);
	if(*ptr == NULL)
	{
	    *total = 0;
	    return(-3);
	}
	/* Allocate a new Object Name */
	(*ptr)[n] = name = (sar_drawmap_objname_struct *)calloc(
	    1, sizeof(sar_drawmap_objname_struct)
	);
	if(name == NULL)
	    return(-3);

	name->gl_name = gl_name;		/* Object's index */
	name->obj_name = STRDUP(obj_name);

	return(n);
}

/*
 *	Deletes the Object Names List.
 */
void SARDrawMapObjNameListDelete(
	sar_drawmap_objname_struct ***ptr, int *total
)
{
	int i;
	sar_drawmap_objname_struct *name;

	if((ptr == NULL) || (total == NULL))
	    return;

	for(i = 0; i < *total; i++)
	{
	    name = (*ptr)[i];
	    if(name == NULL)
		continue;

	    free(name->obj_name);
	    free(name);
	}
	free(*ptr);
	*ptr = NULL;
	*total = 0;
}

/*
 *	Draws the map in GL_SELECT render mode and processes the hits,
 *	storing the hits on the specified core's Object Names List.
 *
 *	Note: Special numbers have the base offset of (*total) added
 *	to them. So any index that is a special number is:
 *
 *		special_index = i - (*total).
 *
 *	The specified obj_num (or -1 for the player object) will be
 *	excluded from the returned index list.
 *
 *      Returns a list of object indices that were hit, the calling
 *	function must delete the returned list.
 */
int *SARGetGCCHitList(
	sar_core_struct *core_ptr, sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	int obj_num,
	int *hits
)
{
	int i, n, hit_obj_num, *list = NULL;
	sar_drawmap_objname_struct *name;

	if(hits != NULL)
	    *hits = 0;

	if((core_ptr == NULL) || (scene == NULL) || (ptr == NULL) ||
	   (total == NULL)
	)
	    return(list);

	/* Draw the map with draw_for_gcc set to True, this will draw
	 * the map with an isolated camera angle looking directly
	 * downward with respect to the specified object (or -1 for the
	 * player object)
	 *
	 * The core's Object Names List will be cleared and updated
	 * with the hits list
	 */
	SARDrawMap(core_ptr, True, False, obj_num);

	/* Get hits list */
	for(i = 0; i < core_ptr->total_drawmap_objnames; i++)
	{
	    name = core_ptr->drawmap_objname[i];
	    if(name == NULL)
		continue;

	    /* Get the hit object number as the gl name
	     *
	     * Note that the hit object number may be >= to
	     * total_objects in which case it has special meaning
	     */
	    hit_obj_num = (int)name->gl_name;

	    /* Skip if the hit object number is the same as given
	     * object number
	     */
	    if(hit_obj_num == obj_num)
		continue;

	    /* Allocate more hits and append this hit object number to
	     * the list
	     */
	    if(hits != NULL)
	    {
		n = MAX(*hits, 0);
		*hits = n + 1;

		list = (int *)realloc(
		    list,
		    (*hits) * sizeof(int)
		);
		if(list == NULL)
		{
		    *hits = 0;
		    break;
		}
		else
		{
		    list[n] = hit_obj_num;
		}
	    }
	}

	return(list);
}

/*
 *      Checks for ground hit contact, relative to the given obj_num.
 *
 *      Checks if there is any object directly under obj_num that is
 *      drawn. If nothing is drawn directly under the object then
 *      got_hit will be set to False, otherwise True.
 *
 *      If the scene's base type is water, then over_water will be set
 *      True if (and only if) got_hit is False.
 *
 *      Returns 0 on success or non-zero on error.
 */
int SARGetGHCOverWater(
	sar_core_struct *core_ptr, sar_scene_struct *scene,
	sar_object_struct ***ptr, int *total,
	int obj_num, Boolean *got_hit, Boolean *over_water
)
{
	if(got_hit != NULL)
	    *got_hit = False;
	if(over_water != NULL)
	    *over_water = False;

	if((core_ptr == NULL) || (scene == NULL) || (ptr == NULL) ||
	   (total == NULL)
	)
	    return(-1);

	/* Draw the map with draw_for_ghc set to True, this will draw
	 * the map with an isolated camera angle looking directly
	 * downward with respect to the specified object (or -1 for the
	 * player object)
	 *
	 * The core's drawmap_ghc_result will be updated
	 */
	SARDrawMap(core_ptr, False, True, obj_num);

	/* Got hit? */
	if(core_ptr->drawmap_ghc_result >= 0.5f)
	{
	    /* Mark that we got a hit */
	    if(got_hit != NULL)
		*got_hit = True;

	    /* Mark that the specified object is not over water */
	    if(over_water != NULL)
		*over_water = False;
	}
	else
	{
	    /* Mark that we did not get a hit */
	    if(got_hit != NULL)
		*got_hit = False;

	    /* Scene's base is water? */
	    if(scene->base_flags & SAR_SCENE_BASE_FLAG_IS_WATER)
	    {
		if(over_water != NULL)
		    *over_water = True;
	    }
	    else
	    {
		if(over_water != NULL)
		    *over_water = False;
	    }
	}

	return(0);
}