libdrawtext

diff src/drawgl.c @ 20:c091833c2354

merged
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 18 Mar 2013 06:06:26 +0200
parents 462bcb69de15
children 00f98842608f
line diff
     1.1 --- a/src/drawgl.c	Mon Aug 27 05:04:19 2012 +0300
     1.2 +++ b/src/drawgl.c	Mon Mar 18 06:06:26 2013 +0200
     1.3 @@ -16,11 +16,18 @@
     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  #include <stdlib.h>
    1.12  
    1.13 +#ifndef _MSC_VER
    1.14 +#include <alloca.h>
    1.15 +#else
    1.16 +#include <malloc.h>
    1.17 +#endif
    1.18 +
    1.19  #ifdef TARGET_IPHONE
    1.20  #include <OpenGLES/ES2/gl.h>
    1.21  #else
    1.22 @@ -117,9 +124,31 @@
    1.23  	dtx_flush();
    1.24  }
    1.25  
    1.26 +static const char *put_char(const char *str, float *pos_x, float *pos_y, int *should_flush)
    1.27 +{
    1.28 +	struct dtx_glyphmap *gmap;
    1.29 +	float px, py;
    1.30 +	int code = dtx_utf8_char_code(str);
    1.31 +	str = dtx_utf8_next_char((char*)str);
    1.32 +
    1.33 +	if(buf_mode == DTX_LBF && code == '\n') {
    1.34 +		*should_flush = 1;
    1.35 +	}
    1.36 +
    1.37 +	px = *pos_x;
    1.38 +	py = *pos_y;
    1.39 +
    1.40 +	if((gmap = dtx_proc_char(code, pos_x, pos_y))) {
    1.41 +		int idx = code - gmap->cstart;
    1.42 +
    1.43 +		set_glyphmap_texture(gmap);
    1.44 +		add_glyph(gmap->glyphs + idx, px, py);
    1.45 +	}
    1.46 +	return str;
    1.47 +}
    1.48 +
    1.49  void dtx_string(const char *str)
    1.50  {
    1.51 -	struct dtx_glyphmap *gmap;
    1.52  	int should_flush = buf_mode == DTX_NBF;
    1.53  	float pos_x = 0.0f;
    1.54  	float pos_y = 0.0f;
    1.55 @@ -129,23 +158,7 @@
    1.56  	}
    1.57  
    1.58  	while(*str) {
    1.59 -		float px, py;
    1.60 -		int code = dtx_utf8_char_code(str);
    1.61 -		str = dtx_utf8_next_char((char*)str);
    1.62 -
    1.63 -		if(buf_mode == DTX_LBF && code == '\n') {
    1.64 -			should_flush = 1;
    1.65 -		}
    1.66 -
    1.67 -		px = pos_x;
    1.68 -		py = pos_y;
    1.69 -
    1.70 -		if((gmap = dtx_proc_char(code, &pos_x, &pos_y))) {
    1.71 -			int idx = code - gmap->cstart;
    1.72 -
    1.73 -			set_glyphmap_texture(gmap);
    1.74 -			add_glyph(gmap->glyphs + idx, px, py);
    1.75 -		}
    1.76 +		str = put_char(str, &pos_x, &pos_y, &should_flush);
    1.77  	}
    1.78  
    1.79  	if(should_flush) {
    1.80 @@ -153,6 +166,32 @@
    1.81  	}
    1.82  }
    1.83  
    1.84 +void dtx_printf(const char *fmt, ...)
    1.85 +{
    1.86 +	va_list ap;
    1.87 +	int buf_size;
    1.88 +	char *buf, tmp;
    1.89 +
    1.90 +	if(!dtx_font) {
    1.91 +		return;
    1.92 +	}
    1.93 +
    1.94 +	va_start(ap, fmt);
    1.95 +	buf_size = vsnprintf(&tmp, 0, fmt, ap);
    1.96 +	va_end(ap);
    1.97 +
    1.98 +	if(buf_size == -1) {
    1.99 +		buf_size = 512;
   1.100 +	}
   1.101 +
   1.102 +	buf = alloca(buf_size + 1);
   1.103 +	va_start(ap, fmt);
   1.104 +	vsnprintf(buf, buf_size + 1, fmt, ap);
   1.105 +	va_end(ap);
   1.106 +
   1.107 +	dtx_string(buf);
   1.108 +}
   1.109 +
   1.110  static void qvertex(struct vertex *v, float x, float y, float s, float t)
   1.111  {
   1.112  	v->x = x;