added scr_lvled, a bunch of libraries, and improved framework code
[raydungeon] / libs / drawtext / 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_draw_buffering(int mode)
49 {
50         if(mode >= DTX_NBF && mode <= DTX_FBF) {
51                 dtx_buf_mode = mode;
52         }
53 }
54
55 void dtx_string(const char *str)
56 {
57         dtx_substring(str, 0, strlen(str));
58 }
59
60 void dtx_substring(const char *str, int start, int end)
61 {
62         int should_flush = dtx_buf_mode == DTX_NBF;
63         float pos_x = dtx_cur_offset[0];
64         float pos_y = dtx_cur_offset[1];
65
66         if(!dtx_font) {
67                 return;
68         }
69
70         /* skip start characters */
71         while(*str && start > 0) {
72                 str = dtx_utf8_next_char((char*)str);
73                 --start;
74                 --end;
75         }
76
77         while(*str && --end >= 0) {
78                 str = dtx_drawchar(str, &pos_x, &pos_y, &should_flush);
79         }
80
81         if(should_flush) {
82                 dtx_drawflush();
83         }
84 }
85
86 #ifdef _MSC_VER
87 #define vsnprintf _vsnprintf
88 #endif
89
90 void dtx_printf(const char *fmt, ...)
91 {
92         va_list ap;
93         int buf_size;
94         char *buf, tmp;
95
96         if(!dtx_font) {
97                 return;
98         }
99
100         va_start(ap, fmt);
101         buf_size = vsnprintf(&tmp, 0, fmt, ap);
102         va_end(ap);
103
104         if(buf_size == -1) {
105                 buf_size = 512;
106         }
107
108         buf = alloca(buf_size + 1);
109         va_start(ap, fmt);
110         vsnprintf(buf, buf_size + 1, fmt, ap);
111         va_end(ap);
112
113         dtx_string(buf);
114 }
115
116 void dtx_flush(void)
117 {
118         dtx_drawflush();
119 }