libdrawtext

diff src/drawgl.c @ 15:462bcb69de15

added dtx_printf
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 28 Feb 2013 18:51:33 +0200
parents 55c034e70809
children baec6ad7b2dd
line diff
     1.1 --- a/src/drawgl.c	Sun Aug 26 04:43:09 2012 +0300
     1.2 +++ b/src/drawgl.c	Thu Feb 28 18:51:33 2013 +0200
     1.3 @@ -16,6 +16,7 @@
     1.4  along with this program.  If not, see <http://www.gnu.org/licenses/>.
     1.5  */
     1.6  #ifndef NO_OPENGL
     1.7 +#include <stdarg.h>
     1.8  #include <math.h>
     1.9  #include <ctype.h>
    1.10  
    1.11 @@ -117,9 +118,31 @@
    1.12  	dtx_flush();
    1.13  }
    1.14  
    1.15 +static const char *put_char(const char *str, float *pos_x, float *pos_y, int *should_flush)
    1.16 +{
    1.17 +	struct dtx_glyphmap *gmap;
    1.18 +	float px, py;
    1.19 +	int code = dtx_utf8_char_code(str);
    1.20 +	str = dtx_utf8_next_char((char*)str);
    1.21 +
    1.22 +	if(buf_mode == DTX_LBF && code == '\n') {
    1.23 +		*should_flush = 1;
    1.24 +	}
    1.25 +
    1.26 +	px = *pos_x;
    1.27 +	py = *pos_y;
    1.28 +
    1.29 +	if((gmap = dtx_proc_char(code, pos_x, pos_y))) {
    1.30 +		int idx = code - gmap->cstart;
    1.31 +
    1.32 +		set_glyphmap_texture(gmap);
    1.33 +		add_glyph(gmap->glyphs + idx, px, py);
    1.34 +	}
    1.35 +	return str;
    1.36 +}
    1.37 +
    1.38  void dtx_string(const char *str)
    1.39  {
    1.40 -	struct dtx_glyphmap *gmap;
    1.41  	int should_flush = buf_mode == DTX_NBF;
    1.42  	float pos_x = 0.0f;
    1.43  	float pos_y = 0.0f;
    1.44 @@ -129,23 +152,7 @@
    1.45  	}
    1.46  
    1.47  	while(*str) {
    1.48 -		float px, py;
    1.49 -		int code = dtx_utf8_char_code(str);
    1.50 -		str = dtx_utf8_next_char((char*)str);
    1.51 -
    1.52 -		if(buf_mode == DTX_LBF && code == '\n') {
    1.53 -			should_flush = 1;
    1.54 -		}
    1.55 -
    1.56 -		px = pos_x;
    1.57 -		py = pos_y;
    1.58 -
    1.59 -		if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
    1.60 -			int idx = code - gmap->cstart;
    1.61 -
    1.62 -			set_glyphmap_texture(gmap);
    1.63 -			add_glyph(gmap->glyphs + idx, px, py);
    1.64 -		}
    1.65 +		str = put_char(str, &pos_x, &pos_y, &should_flush);
    1.66  	}
    1.67  
    1.68  	if(should_flush) {
    1.69 @@ -153,6 +160,32 @@
    1.70  	}
    1.71  }
    1.72  
    1.73 +void dtx_printf(const char *fmt, ...)
    1.74 +{
    1.75 +	va_list ap;
    1.76 +	int buf_size;
    1.77 +	char *buf, tmp;
    1.78 +
    1.79 +	if(!dtx_font) {
    1.80 +		return;
    1.81 +	}
    1.82 +
    1.83 +	va_start(ap, fmt);
    1.84 +	buf_size = vsnprintf(&tmp, 0, fmt, ap);
    1.85 +	va_end(ap);
    1.86 +
    1.87 +	if(buf_size == -1) {
    1.88 +		buf_size = 512;
    1.89 +	}
    1.90 +
    1.91 +	buf = alloca(buf_size + 1);
    1.92 +	va_start(ap, fmt);
    1.93 +	vsnprintf(buf, buf_size + 1, fmt, ap);
    1.94 +	va_end(ap);
    1.95 +
    1.96 +	dtx_string(buf);
    1.97 +}
    1.98 +
    1.99  static void qvertex(struct vertex *v, float x, float y, float s, float t)
   1.100  {
   1.101  	v->x = x;