forked from naev/naev
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui_omsg.c
338 lines (276 loc) · 6.94 KB
/
gui_omsg.c
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
* See Licensing and Copyright notice in naev.h
*/
/** @cond */
#include <stdlib.h>
#include "naev.h"
/** @endcond */
#include "gui_omsg.h"
#include "array.h"
#include "font.h"
#include "log.h"
#include "ndata.h"
#include "nstring.h"
#include "opengl.h"
/**
* @brief Fonts.
*/
typedef struct omsg_font_s {
int size; /**< Font size. */
glFont font; /**< Font itself. */
} omsg_font_t;
static omsg_font_t *omsg_font_array = NULL; /**< Array of fonts. */
/**
* @brief Message struct.
*/
typedef struct omsg_s {
unsigned int id; /**< Unique ID. */
char **msg; /**< Message to display. */
int nlines; /**< Message lines. */
double duration; /**< Time left. */
int font; /**< Font to use. */
glColour col; /**< Colour to use. */
} omsg_t;
static omsg_t *omsg_array = NULL; /**< Array of messages. */
static unsigned int omsg_idgen = 0; /**< Unique ID generator. */
static double omsg_center_x = 0.; /**< X center of the overlay messages. */
static double omsg_center_y = 0.; /**< Y center of the overlay messages. */
static double omsg_center_w = 100.; /**< Width of the overlay messages. */
/*
* Prototypes.
*/
static omsg_t* omsg_get( unsigned int id );
static void omsg_free( omsg_t *omsg );
static void omsg_setMsg( omsg_t *omsg, const char *msg );
static int omsg_getFontID( int size );
static glFont* omsg_getFont( int font );
/**
* @brief Gets an overlay message from id.
*/
static omsg_t* omsg_get( unsigned int id )
{
int i;
for (i=0; i<array_size(omsg_array); i++)
if (omsg_array[i].id == id)
return &omsg_array[i];
return NULL;
}
/**
* @brief Frees all internal stuff of a msg.
*/
static void omsg_free( omsg_t *omsg )
{
int i;
if (omsg->msg != NULL) {
for (i=0; i<omsg->nlines; i++)
free( omsg->msg[i] );
free( omsg->msg );
omsg->msg = NULL;
omsg->nlines = 0;
}
}
/**
* @brief Sets the message for an omsg.
*/
static void omsg_setMsg( omsg_t *omsg, const char *msg )
{
int l, n, s, m;
glFont *font;
/* Clean up after old stuff. */
omsg_free( omsg );
/* Create data. */
l = strlen( msg );
if (l==0)
return;
font = omsg_getFont( omsg->font );
/* First pass size. */
n = 0;
m = 0;
while (n < l) {
s = gl_printWidthForText( font, &msg[n], omsg_center_w, NULL );
n += s+1;
m++;
}
/* Avoid zero-length malloc. */
if (m == 0)
return;
/* Second pass allocate. */
omsg->msg = malloc( m * sizeof(char*) );
omsg->nlines = m;
n = 0;
m = 0;
while (n < l) {
s = gl_printWidthForText( font, &msg[n], omsg_center_w, NULL );
omsg->msg[m] = strndup( &msg[n], s );
m++;
n += s+1;
}
}
/**
* @brief Gets a font by size.
*/
static int omsg_getFontID( int size )
{
int i;
omsg_font_t *font;
/* Create array if not done so yet. */
if (omsg_font_array == NULL)
omsg_font_array = array_create( omsg_font_t );
/* Try to match. */
for (i=0; i<array_size( omsg_font_array ); i++)
if (size == omsg_font_array[i].size)
return i;
/* Create font. */
font = &array_grow( &omsg_font_array );
gl_fontInit( &font->font, _(FONT_MONOSPACE_PATH), size, FONT_PATH_PREFIX, 0 );
font->size = size;
return array_size(omsg_font_array) - 1;
}
/**
* @brief Gets a font by id.
*/
static glFont* omsg_getFont( int font )
{
return &omsg_font_array[ font ].font;
}
/**
* @brief Positions the overlay messages.
*
* @param center_x X center.
* @param center_y Y center.
* @param width Width to target.
*/
void omsg_position( double center_x, double center_y, double width )
{
omsg_center_x = center_x;
omsg_center_y = center_y;
omsg_center_w = width;
}
/**
* @brief Cleans up after the overlay.
*/
void omsg_cleanup (void)
{
int i;
/* Free fonts. */
for (i=0; i<array_size( omsg_font_array ); i++)
gl_freeFont( &omsg_font_array[i].font );
array_free( omsg_font_array );
omsg_font_array = NULL;
/* Destroy messages. */
for (i=0; i<array_size(omsg_array); i++)
omsg_free( &omsg_array[i] );
array_free( omsg_array );
omsg_array = NULL;
}
/**
* @brief Renders the overlays.
*/
void omsg_render( double dt )
{
int i, j;
double x, y;
omsg_t *omsg;
glFont *font;
glColour col;
/* Case nothing to do. */
if (omsg_array == NULL)
return;
/* Center. */
x = omsg_center_x - omsg_center_w/2.;
y = omsg_center_y;
/* Render. */
for (i=0; i<array_size(omsg_array); i++) {
omsg = &omsg_array[i];
/* Render. */
font = omsg_getFont( omsg->font );
col = omsg->col;
if (omsg->duration < 1.)
col.a = omsg->duration;
gl_printRestoreClear();
for (j=0; j<omsg->nlines; j++) {
y -= font->h * 1.5;
gl_printRestoreLast();
gl_printMidRaw( font, omsg_center_w, x, y, &col, -1., omsg->msg[j] );
}
/* Check if time to erase. */
omsg->duration -= dt;
if (omsg->duration < 0.) {
omsg_free( omsg );
array_erase( &omsg_array, &omsg[0], &omsg[1] );
i--;
continue;
}
}
}
/**
* @brief Adds a message to the overlay.
*
* @param msg Message to add.
* @param duration Duration of message on screen (in seconds).
* @param fontsize Size of the font to use.
* @param col Colour to use.
* @return Unique ID to the message.
*/
unsigned int omsg_add( const char *msg, double duration, int fontsize, const glColour *col )
{
omsg_t *omsg;
int font;
/* Create if necessary. */
if (omsg_array == NULL)
omsg_array = array_create( omsg_t );
/* Get font size. */
font = omsg_getFontID( fontsize );
/* Create the message. */
omsg = &array_grow( &omsg_array );
memset( omsg, 0, sizeof(omsg_t) );
omsg->id = ++omsg_idgen;
omsg->duration = duration;
omsg->font = font;
omsg->col = *col;
omsg_setMsg( omsg, msg );
return omsg->id;
}
/**
* @brief Changes an overlay message.
*
* @param id ID of the message to change.
* @param msg New message for the overlay.
* @param duration New duration for the overlay.
* @return 0 on success.
*/
int omsg_change( unsigned int id, const char *msg, double duration )
{
omsg_t *omsg;
omsg = omsg_get(id);
if (omsg == NULL)
return -1;
omsg_setMsg( omsg, msg );
omsg->duration = duration;
return 0;
}
/**
* @brief Checks to see if an overlay message exists.
*
* @param id ID of the message to check if exists.
* @return 1 if it exists, 0 otherwise.
*/
int omsg_exists( unsigned int id )
{
return (omsg_get(id) != NULL);
}
/**
* @brief Removes an overlay message.
*
* @param id ID of the message to remove.
*/
void omsg_rm( unsigned int id )
{
omsg_t *omsg;
omsg = omsg_get(id);
if (omsg == NULL)
return;
/* Destroy. */
omsg_free( omsg );
array_erase( &omsg_array, &omsg[0], &omsg[1] );
}