1 /************************************************************************
3 * voxelands - 3d voxel world sandbox game
4 * Copyright (C) Lisa 'darkrose' Milne 2016 <lisa@ltmnet.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>
18 ************************************************************************/
21 #define _WM_EXPOSE_ALL
27 static rendertext_t
*textbuffer_get_rendertext(textbuffer_t
*t
, uint32_t page
)
31 ft
= malloc(sizeof(rendertext_t
));
40 array_init(&ft
->v
,ARRAY_TYPE_FLOAT
);
41 array_init(&ft
->t
,ARRAY_TYPE_FLOAT
);
43 array_push_ptr(&t
->data
,ft
);
48 /* initialise a textbuffer */
49 void textbuffer_init(textbuffer_t
*t
, font_t
*f
, int size
, float x
, float y
, int max_x
, int max_y
, int max_c
, uint32_t options
)
71 array_init(&t
->data
,ARRAY_TYPE_PTR
);
76 t
->str
= malloc(sizeof(uint32_t)*t
->mc
);
84 /* create and initialise a textbuffer */
85 textbuffer_t
*textbuffer_create(font_t
*f
, int size
, float x
, float y
, int max_x
, int max_y
, int max_c
, uint32_t options
)
87 textbuffer_t
*t
= malloc(sizeof(textbuffer_t
));
91 textbuffer_init(t
,f
,size
,x
,y
,max_x
,max_y
, max_c
, options
);
96 /* clear the current string from a textbuffer */
97 void textbuffer_clear(textbuffer_t
*t
)
103 if (!t
->mc
&& t
->str
) {
113 t
->ch
= t
->font_size
;
115 while ((rt
= array_pop_ptr(&t
->data
))) {
116 array_free(&rt
->v
,0);
117 array_free(&rt
->t
,0);
119 glDeleteBuffers(2, rt
->vbo
);
120 glDeleteVertexArrays(1,&rt
->vao
);
127 /* clear and free a textbuffer */
128 void textbuffer_free(textbuffer_t
*t
)
134 array_free(&t
->data
,0);
140 /* adjust the settings of a textbuffer, and regen gl data if necessary */
141 int textbuffer_adjust(textbuffer_t
*t
, font_t
*f
, int size
, float x
, float y
, int max_x
, int max_y
, int max_c
, uint32_t options
)
150 if (f
&& f
!= t
->font
) {
158 if (t
->mx
!= max_x
) {
163 if (t
->my
!= max_y
) {
168 if (t
->mc
!= max_c
) {
174 if (size
!= t
->font_size
) {
179 if (t
->options
!= options
) {
180 if ((t
->options
&TB_OPT_NUMERIC
) == 0 && (options
&TB_OPT_NUMERIC
) == TB_OPT_NUMERIC
)
182 t
->options
= options
;
189 t
->ch
= t
->font_size
;
202 for (i
=0; i
<len
; i
++) {
203 textbuffer_addchar(t
,str
[i
]);
211 /* add a string to a textbuffer */
212 int textbuffer_addstr(textbuffer_t
*t
, char* str
)
220 while ((ch
= utf8_nextchar(str
,&i
))) {
221 r
= textbuffer_addchar(t
,ch
);
229 /* add a single utf32 character to a textbuffer */
230 int textbuffer_addchar(textbuffer_t
*t
, uint32_t ch
)
232 rendertext_t
*rt
= NULL
;
243 if ((t
->options
&TB_OPT_NUMERIC
) == TB_OPT_NUMERIC
&& !iswdigit(ch
))
246 if (t
->mc
&& t
->length
== t
->mc
)
251 if (t
->data
.length
) {
252 rt
= array_get_ptr(&t
->data
,t
->data
.length
-1);
253 if (rt
&& rt
->page
!= page
)
258 rt
= textbuffer_get_rendertext(t
,page
);
263 if (font_ttf_get_char(t
->font
,ch
,&fc
,&rt
->glid
))
266 sf
= (float)t
->font_size
/fc
->h
;
271 if (t
->mx
&& t
->nx
+w
> t
->mx
) {
273 t
->ny
+= t
->font_size
;
274 t
->ch
+= t
->font_size
;
277 if (t
->my
&& t
->ny
+h
> t
->my
)
284 array_push_v2t(&rt
->v
,&v
);
285 array_push_v2t(&rt
->t
,&tc
);
290 array_push_v2t(&rt
->v
,&v
);
291 array_push_v2t(&rt
->t
,&tc
);
296 array_push_v2t(&rt
->v
,&v
);
297 array_push_v2t(&rt
->t
,&tc
);
298 array_push_v2t(&rt
->v
,&v
);
299 array_push_v2t(&rt
->t
,&tc
);
304 array_push_v2t(&rt
->v
,&v
);
305 array_push_v2t(&rt
->t
,&tc
);
310 array_push_v2t(&rt
->v
,&v
);
311 array_push_v2t(&rt
->t
,&tc
);
317 if (t
->size
<= t
->length
+2) {
319 uint32_t nl
= t
->size
+16;
321 s
= realloc(t
->str
,sizeof(uint32_t)*nl
);
328 t
->str
[t
->length
++] = ch
;
329 t
->str
[t
->length
] = 0;
334 /* get the current draw dimensions for a textbuffer */
335 int textbuffer_get_dimensions(textbuffer_t
*t
, int sizes
[2])