File: messages.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 (174 lines) | stat: -rw-r--r-- 4,198 bytes parent folder | download | duplicates (8)
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
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#include "../include/string.h"

#include "obj.h"
#include "messages.h"
#include "sar.h"
#include "config.h"


void SARMessageAdd(sar_scene_struct *scene, const char *message);
void SARMessageClearAll(sar_scene_struct *scene);
void SARBannerMessageAppend(sar_scene_struct *scene, const char *message);
void SARCameraRefTitleSet(sar_scene_struct *scene, const char *message);


#define ATOI(s)		(((s) != NULL) ? atoi(s) : 0)
#define ATOL(s)		(((s) != NULL) ? atol(s) : 0)
#define ATOF(s)		(((s) != NULL) ? (float)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 STRLEN(s)	(((s) != NULL) ? (int)strlen(s) : 0)
#define STRISEMPTY(s)	(((s) != NULL) ? (*(s) == '\0') : 1)

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


/*
 *	Adds the specified message to the list in the scene.
 *	The new message will be inserted at the last index position.
 *
 *	The message pointer array on the scene will not change in size.
 */
void SARMessageAdd(sar_scene_struct *scene, const char *message)
{
	int i, n;


	if(scene == NULL)
	    return;

	/* No message line pointers allocated? */
	if(scene->total_messages <= 0)
	    return;

	/* Free oldest message */
	free(scene->message[0]);

	/* Get new position to insert new message */
	n = scene->total_messages - 1;

	/* Shift message pointers */
	for(i = 0; i < n; i++)
	    scene->message[i] = scene->message[i + 1];

	/* Copy new message to pointer array */
	scene->message[n] = STRDUP(message);

	/* Update the time the message should be shown until */
	scene->message_display_until = cur_millitime +
	    SAR_MESSAGE_SHOW_INT;
}

/*
 *	Deletes all messages on the message pointer array on the scene
 *	structure and resets all pointers to NULL.
 *
 *	The size of the message pointer array will not be changed.
 *
 *	The message_display_until member on the scene will be reset
 *	to 0.
 */
void SARMessageClearAll(sar_scene_struct *scene)
{
	int i;

	if(scene == NULL)
	    return;

	/* Reset the time to keep displaying messages to 0 so messages
	 * shouldn't be displayed anymore if they were being displayed.
	 */
	scene->message_display_until = 0;

	/* Delete each message and set pointer to NULL */
	for(i = 0; i < scene->total_messages; i++)
	{
	    free(scene->message[i]);
	    scene->message[i] = NULL;
	}
}

/*
 *	Appends given message line to scene's sticky banner message, can
 *	be called multiple times to set several lines.
 *
 *	Passing NULL clears the entire list.
 */
void SARBannerMessageAppend(sar_scene_struct *scene, const char *message)
{
	if(scene == NULL)
	    return;

	if(message == NULL)
	{
	    strlistfree(
		scene->sticky_banner_message, 
		scene->total_sticky_banner_messages 
	    ); 
	    scene->sticky_banner_message = NULL;
	    scene->total_sticky_banner_messages = 0;
	}
	else
	{
	    int i;

	    if(scene->total_sticky_banner_messages < 0)
		scene->total_sticky_banner_messages = 0;

	    i = scene->total_sticky_banner_messages;
	    scene->total_sticky_banner_messages = i + 1;

	    scene->sticky_banner_message = (char **)realloc(
		scene->sticky_banner_message,
		scene->total_sticky_banner_messages * sizeof(char *)
	    );
	    if(scene->sticky_banner_message == NULL)
	    {
		scene->total_sticky_banner_messages = 0;
		return;
	    }

	    scene->sticky_banner_message[i] = STRDUP(message);
	}
}

/*
 *	Replaces the current camera reference title message on the scene
 *	structure.
 *
 *	Passing NULL clears it.
 */
void SARCameraRefTitleSet(sar_scene_struct *scene, const char *message)
{
	if(scene == NULL)
	    return;

	if(message == NULL)
	{
	    free(scene->camera_ref_title);
	    scene->camera_ref_title = NULL;
	    scene->camera_ref_title_display_until = 0;
	}
	else
	{
	    char *s;

	    free(scene->camera_ref_title);
	    scene->camera_ref_title = s = STRDUP(message);

	    /* Make first character upper case */
	    if(s[0] != '\0')
		s[0] = toupper(s[0]);

	    scene->camera_ref_title_display_until = cur_millitime +
		SAR_MESSAGE_SHOW_INT;
	}
}