- added libdrawtext
[demo_prior] / libs / drawtext / src / draw.c
1 /*
2 libdrawtext - a simple library for fast text rendering in OpenGL
3 Copyright (C) 2011-2016  John Tsiombikas <nuclear@member.fsf.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License
16 along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdarg.h>
21 #if defined(WIN32) || defined(__WIN32__)
22 #include <malloc.h>
23 #else
24 #include <alloca.h>
25 #endif
26 #include "drawtext.h"
27 #include "drawtext_impl.h"
28
29 void dtx_position(float x, float y)
30 {
31         dtx_cur_offset[0] = x;
32         dtx_cur_offset[1] = y;
33 }
34
35 void dtx_color(float r, float g, float b, float a)
36 {
37         dtx_cur_color[0] = r;
38         dtx_cur_color[1] = g;
39         dtx_cur_color[2] = b;
40         dtx_cur_color[3] = a;
41
42         dtx_cur_color_int[0] = r > 1.0 ? 255 : (int)(r * 255.0);
43         dtx_cur_color_int[1] = g > 1.0 ? 255 : (int)(g * 255.0);
44         dtx_cur_color_int[2] = b > 1.0 ? 255 : (int)(b * 255.0);
45         dtx_cur_color_int[3] = a > 1.0 ? 255 : (int)(a * 255.0);
46 }
47
48 void dtx_string(const char *str)
49 {
50         dtx_substring(str, 0, strlen(str));
51 }
52
53 void dtx_substring(const char *str, int start, int end)
54 {
55         int should_flush = dtx_buf_mode == DTX_NBF;
56         float pos_x = dtx_cur_offset[0];
57         float pos_y = dtx_cur_offset[1];
58
59         if(!dtx_font) {
60                 return;
61         }
62
63         /* skip start characters */
64         while(*str && start > 0) {
65                 str = dtx_utf8_next_char((char*)str);
66                 --start;
67                 --end;
68         }
69
70         while(*str && --end >= 0) {
71                 str = dtx_drawchar(str, &pos_x, &pos_y, &should_flush);
72         }
73
74         if(should_flush) {
75                 dtx_drawflush();
76         }
77 }
78
79 void dtx_printf(const char *fmt, ...)
80 {
81         va_list ap;
82         int buf_size;
83         char *buf, tmp;
84
85         if(!dtx_font) {
86                 return;
87         }
88
89         va_start(ap, fmt);
90         buf_size = vsnprintf(&tmp, 0, fmt, ap);
91         va_end(ap);
92
93         if(buf_size == -1) {
94                 buf_size = 512;
95         }
96
97         buf = alloca(buf_size + 1);
98         va_start(ap, fmt);
99         vsnprintf(buf, buf_size + 1, fmt, ap);
100         va_end(ap);
101
102         dtx_string(buf);
103 }
104
105 void dtx_flush(void)
106 {
107         dtx_drawflush();
108 }