naev 0.11.5
gui_omsg.c
1/*
2 * See Licensing and Copyright notice in naev.h
3 */
5#include <stdlib.h>
6
7#include "naev.h"
10#include "gui_omsg.h"
11
12#include "array.h"
13#include "font.h"
14#include "log.h"
15#include "ndata.h"
16#include "nstring.h"
17#include "opengl.h"
18
22typedef struct omsg_font_s {
23 int size;
26static omsg_font_t *omsg_font_array = NULL;
31typedef struct omsg_s {
32 unsigned int id;
33 char **msg;
34 double duration;
35 int font;
36 glColour col;
37} omsg_t;
38static omsg_t *omsg_array = NULL;
39static unsigned int omsg_idgen = 0;
41static double omsg_center_x = 0.;
42static double omsg_center_y = 0.;
43static double omsg_center_w = 100.;
45/*
46 * Prototypes.
47 */
48static omsg_t* omsg_get( unsigned int id );
49static void omsg_free( omsg_t *omsg );
50static void omsg_setMsg( omsg_t *omsg, const char *msg );
51static int omsg_getFontID( int size );
52static glFont* omsg_getFont( int font );
53
57static omsg_t* omsg_get( unsigned int id )
58{
59 for (int i=0; i<array_size(omsg_array); i++)
60 if (omsg_array[i].id == id)
61 return &omsg_array[i];
62 return NULL;
63}
64
68static void omsg_free( omsg_t *omsg )
69{
70 for (int i=0; i<array_size(omsg->msg); i++)
71 free( omsg->msg[i] );
72 array_free( omsg->msg );
73 omsg->msg = NULL;
74}
75
79static void omsg_setMsg( omsg_t *omsg, const char *msg )
80{
81 glFont *font;
83
84 /* Clean up after old stuff. */
85 omsg_free( omsg );
86
87 /* Create data. */
88 font = omsg_getFont( omsg->font );
89
90 omsg->msg = array_create( char* );
91 gl_printLineIteratorInit( &iter, font, msg, omsg_center_w );
92 while (gl_printLineIteratorNext( &iter ))
93 array_push_back( &omsg->msg, strndup( &iter.text[iter.l_begin], iter.l_end - iter.l_begin ) );
94}
95
99static int omsg_getFontID( int size )
100{
101 omsg_font_t *font;
102
103 /* Create array if not done so yet. */
104 if (omsg_font_array == NULL)
105 omsg_font_array = array_create( omsg_font_t );
106
107 /* Try to match. */
108 for (int i=0; i<array_size( omsg_font_array ); i++)
109 if (size == omsg_font_array[i].size)
110 return i;
111
112 /* Create font. */
113 font = &array_grow( &omsg_font_array );
114 gl_fontInit( &font->font, _(FONT_MONOSPACE_PATH), size, FONT_PATH_PREFIX, 0 );
115 font->size = size;
116 return array_size(omsg_font_array) - 1;
117}
118
122static glFont* omsg_getFont( int font )
123{
124 return &omsg_font_array[ font ].font;
125}
126
134void omsg_position( double center_x, double center_y, double width )
135{
136 omsg_center_x = center_x;
137 omsg_center_y = center_y;
138 omsg_center_w = width;
139}
140
144void omsg_cleanup (void)
145{
146 /* Free fonts. */
147 for (int i=0; i<array_size( omsg_font_array ); i++)
148 gl_freeFont( &omsg_font_array[i].font );
149 array_free( omsg_font_array );
150 omsg_font_array = NULL;
151
152 /* Destroy messages. */
153 for (int i=0; i<array_size(omsg_array); i++)
154 omsg_free( &omsg_array[i] );
155 array_free( omsg_array );
156 omsg_array = NULL;
157}
158
162void omsg_render( double dt )
163{
164 double x, y;
165
166 /* Case nothing to do. */
167 if (omsg_array == NULL)
168 return;
169
170 /* Center. */
171 x = omsg_center_x - omsg_center_w/2.;
172 y = omsg_center_y;
173
174 /* Render. */
175 for (int i=0; i<array_size(omsg_array); i++) {
176 omsg_t *omsg = &omsg_array[i];
177
178 /* Render. */
179 glFont *font = omsg_getFont( omsg->font );
180 glColour col = omsg->col;
181 if (omsg->duration < 1.)
182 col.a = omsg->duration;
184 for (int j=0; j<array_size(omsg->msg); j++) {
185 y -= font->h * 1.5;
187 gl_printMidRaw( font, omsg_center_w, x, y, &col, -1., omsg->msg[j] );
188 }
189
190 /* Check if time to erase. */
191 omsg->duration -= dt;
192 if (omsg->duration < 0.) {
193 omsg_free( omsg );
194 array_erase( &omsg_array, &omsg[0], &omsg[1] );
195 i--;
196 continue;
197 }
198 }
199}
200
210unsigned int omsg_add( const char *msg, double duration, int fontsize, const glColour *col )
211{
212 omsg_t *omsg;
213 int font;
214
215 /* Create if necessary. */
216 if (omsg_array == NULL)
217 omsg_array = array_create( omsg_t );
218
219 /* Get font size. */
220 font = omsg_getFontID( fontsize );
221
222 /* Create the message. */
223 omsg = &array_grow( &omsg_array );
224 memset( omsg, 0, sizeof(omsg_t) );
225 omsg->id = ++omsg_idgen;
226 omsg->duration = duration;
227 omsg->font = font;
228 omsg->col = *col;
229 omsg_setMsg( omsg, msg );
230
231 return omsg->id;
232}
233
242int omsg_change( unsigned int id, const char *msg, double duration )
243{
244 omsg_t *omsg = omsg_get(id);
245 if (omsg == NULL)
246 return -1;
247
248 omsg_setMsg( omsg, msg );
249 omsg->duration = duration;
250 return 0;
251}
252
259int omsg_exists( unsigned int id )
260{
261 return (omsg_get(id) != NULL);
262}
263
269void omsg_rm( unsigned int id )
270{
271 omsg_t *omsg = omsg_get(id);
272 if (omsg == NULL)
273 return;
274
275 /* Destroy. */
276 omsg_free( omsg );
277 array_erase( &omsg_array, &omsg[0], &omsg[1] );
278}
Provides macros to work with dynamic arrays.
#define array_free(ptr_array)
Frees memory allocated and sets array to NULL.
Definition array.h:158
#define array_erase(ptr_array, first, last)
Erases elements in interval [first, last).
Definition array.h:140
static ALWAYS_INLINE int array_size(const void *array)
Returns number of elements in the array.
Definition array.h:168
#define array_grow(ptr_array)
Increases the number of elements by one and returns the last element.
Definition array.h:119
#define array_push_back(ptr_array, element)
Adds a new element at the end of the array.
Definition array.h:129
#define array_create(basic_type)
Creates a new dynamic array of ‘basic_type’.
Definition array.h:93
void gl_printRestoreClear(void)
Clears the restoration.
Definition font.c:378
int gl_printLineIteratorNext(glPrintLineIterator *iter)
Updates iter with the next line's information.
Definition font.c:526
int gl_printMidRaw(const glFont *ft_font, int width, double x, double y, const glColour *c, double outlineR, const char *text)
Displays text centered in position and width.
Definition font.c:788
void gl_freeFont(glFont *font)
Frees a loaded font. Caution: its glFontStash still has a slot in avail_fonts. At the time of writing...
Definition font.c:1723
void gl_printLineIteratorInit(glPrintLineIterator *iter, const glFont *ft_font, const char *text, int width)
Initialize an iterator object for breaking text into lines.
Definition font.c:506
int gl_fontInit(glFont *font, const char *fname, const unsigned int h, const char *prefix, unsigned int flags)
Initializes a font.
Definition font.c:1517
void gl_printRestoreLast(void)
Restores last colour.
Definition font.c:386
Header file with generic functions and naev-specifics.
char * strndup(const char *s, size_t n)
Return a pointer to a new string, which is a duplicate of the string s (or, if necessary,...
Definition nstring.c:67
Represents a font in memory.
Definition font.h:16
int h
Definition font.h:18
The state of a line iteration. This matches the process of rendering text into an on-screen box: An e...
Definition font.h:40
const char * text
Definition font.h:41
Fonts.
Definition gui_omsg.c:22
glFont font
Definition gui_omsg.c:24
Message struct.
Definition gui_omsg.c:31
double duration
Definition gui_omsg.c:34
glColour col
Definition gui_omsg.c:36
int font
Definition gui_omsg.c:35
char ** msg
Definition gui_omsg.c:33
unsigned int id
Definition gui_omsg.c:32