nuclear@22: #include nuclear@22: #include nuclear@22: #include nuclear@22: #include "game.h" nuclear@22: #include "text.h" nuclear@22: #include "sdr.h" nuclear@22: #include "opengl.h" nuclear@22: #include "sanegl.h" nuclear@22: #include "drawtext/drawtext.h" nuclear@22: nuclear@22: #define FONT_NAME "consolas_s24.glyphmap" nuclear@22: #define FONT_SIZE 24 nuclear@22: nuclear@22: static struct dtx_font *font; nuclear@22: static unsigned int prog; nuclear@22: static int uloc_color = -1; nuclear@22: static int aloc_vertex, aloc_texcoords; nuclear@22: static float color[4]; nuclear@22: static int cpos[2]; nuclear@22: static int line_height; nuclear@22: nuclear@22: static int init(void) nuclear@22: { nuclear@22: if(font) return 0; /* already initialized */ nuclear@22: nuclear@22: if(!(font = dtx_open_font_glyphmap("data/" FONT_NAME))) { nuclear@22: fprintf(stderr, "failed to open font\n"); nuclear@22: return -1; nuclear@22: } nuclear@22: line_height = dtx_line_height(); nuclear@22: nuclear@22: if(!(prog = create_program_load("sdr/vertex.glsl", "sdr/font.p.glsl"))) { nuclear@22: dtx_close_font(font); nuclear@22: font = 0; nuclear@22: return -1; nuclear@22: } nuclear@22: aloc_vertex = glGetAttribLocation(prog, "attr_vertex"); nuclear@22: aloc_texcoords = glGetAttribLocation(prog, "attr_texcoord"); nuclear@22: uloc_color = glGetUniformLocation(prog, "color"); nuclear@22: nuclear@22: return 0; nuclear@22: } nuclear@22: nuclear@22: void text_color(float r, float g, float b, float a) nuclear@22: { nuclear@22: color[0] = r; nuclear@22: color[1] = g; nuclear@22: color[2] = b; nuclear@22: color[3] = a; nuclear@22: } nuclear@22: nuclear@22: void text_position(int x, int y) nuclear@22: { nuclear@22: cpos[0] = x; nuclear@22: cpos[1] = y; nuclear@22: } nuclear@22: nuclear@22: static int pre_print(void) nuclear@22: { nuclear@22: int top = win_height - line_height; nuclear@22: nuclear@22: if(init() == -1) { nuclear@22: return -1; nuclear@22: } nuclear@22: nuclear@22: gl_matrix_mode(GL_TEXTURE); nuclear@22: gl_push_matrix(); nuclear@22: gl_load_identity(); nuclear@22: gl_matrix_mode(GL_PROJECTION); nuclear@22: gl_push_matrix(); nuclear@22: gl_load_identity(); nuclear@22: gl_ortho(0, win_width, 0, win_height, -1, 1); nuclear@22: gl_matrix_mode(GL_MODELVIEW); nuclear@22: gl_push_matrix(); nuclear@22: gl_load_identity(); nuclear@22: gl_translatef(cpos[0] * dtx_glyph_width(' '), top - cpos[1] * line_height, 0); nuclear@22: nuclear@22: glUseProgram(prog); nuclear@22: glUniform4fv(uloc_color, 1, color); nuclear@22: nuclear@22: gl_apply_xform(prog); nuclear@22: nuclear@22: dtx_use_font(font, FONT_SIZE); nuclear@22: dtx_vertex_attribs(aloc_vertex, aloc_texcoords); nuclear@22: return 0; nuclear@22: } nuclear@22: nuclear@22: static void post_print(void) nuclear@22: { nuclear@22: gl_matrix_mode(GL_TEXTURE); nuclear@22: gl_pop_matrix(); nuclear@22: gl_matrix_mode(GL_PROJECTION); nuclear@22: gl_pop_matrix(); nuclear@22: gl_matrix_mode(GL_MODELVIEW); nuclear@22: gl_pop_matrix(); nuclear@22: } nuclear@22: nuclear@22: void text_print(const char *s) nuclear@22: { nuclear@22: if(pre_print() == -1) { nuclear@22: return; nuclear@22: } nuclear@22: nuclear@22: dtx_string(s); nuclear@22: nuclear@22: post_print(); nuclear@22: } nuclear@22: nuclear@22: void text_printf(const char *fmt, ...) nuclear@22: { nuclear@22: va_list ap; nuclear@22: int buf_size; nuclear@22: char *buf, tmp; nuclear@22: nuclear@22: if(pre_print() == -1) { nuclear@22: return; nuclear@22: } nuclear@22: nuclear@22: va_start(ap, fmt); nuclear@22: buf_size = vsnprintf(&tmp, 0, fmt, ap); nuclear@22: va_end(ap); nuclear@22: nuclear@22: if(buf_size == -1) { nuclear@22: buf_size = 512; nuclear@22: } nuclear@22: nuclear@22: buf = alloca(buf_size + 1); nuclear@22: va_start(ap, fmt); nuclear@22: vsnprintf(buf, buf_size + 1, fmt, ap); nuclear@22: va_end(ap); nuclear@22: nuclear@22: dtx_string(buf); nuclear@22: nuclear@22: post_print(); nuclear@22: }