# HG changeset patch # User John Tsiombikas # Date 1434162727 -10800 # Node ID d7fe157c402d9d4b23171ad928e68e041bab7004 # Parent 4ca4e3c5a754f9e4c4fbda799f2755ea449b41b5 fonts diff -r 4ca4e3c5a754 -r d7fe157c402d android/manifest.xml.in --- a/android/manifest.xml.in Thu Jun 11 04:56:33 2015 +0300 +++ b/android/manifest.xml.in Sat Jun 13 05:32:07 2015 +0300 @@ -14,7 +14,8 @@ + android:screenOrientation="landscape" + android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen" > diff -r 4ca4e3c5a754 -r d7fe157c402d libs/drawtext/drawtext.h --- a/libs/drawtext/drawtext.h Thu Jun 11 04:56:33 2015 +0300 +++ b/libs/drawtext/drawtext.h Sat Jun 13 05:32:07 2015 +0300 @@ -20,6 +20,7 @@ #include #include +#include "assman.h" struct dtx_font; struct dtx_glyphmap; @@ -90,7 +91,7 @@ * freetype support. */ struct dtx_glyphmap *dtx_load_glyphmap(const char *fname); -struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp); +struct dtx_glyphmap *dtx_load_glyphmap_stream(ass_file *fp); int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap); int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap); diff -r 4ca4e3c5a754 -r d7fe157c402d libs/drawtext/font.c --- a/libs/drawtext/font.c Thu Jun 11 04:56:33 2015 +0300 +++ b/libs/drawtext/font.c Sat Jun 13 05:32:07 2015 +0300 @@ -31,6 +31,7 @@ #include #include FT_FREETYPE_H #endif +#include "assman.h" #include "drawtext.h" #include "drawtext_impl.h" @@ -333,18 +334,18 @@ struct dtx_glyphmap *dtx_load_glyphmap(const char *fname) { - FILE *fp; + ass_file *fp; struct dtx_glyphmap *gmap; - if(!(fp = fopen(fname, "rb"))) { + if(!(fp = ass_fopen(fname, "rb"))) { return 0; } gmap = dtx_load_glyphmap_stream(fp); - fclose(fp); + ass_fclose(fp); return gmap; } -struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp) +struct dtx_glyphmap *dtx_load_glyphmap_stream(ass_file *fp) { char buf[512]; int hdr_lines = 0; @@ -364,7 +365,7 @@ while(hdr_lines < 3) { char *line = buf; - if(!fgets(buf, sizeof buf, fp)) { + if(!ass_fgets(buf, sizeof buf, fp)) { fperror("unexpected end of file"); goto err; } @@ -471,13 +472,13 @@ } for(i=0; ipixels[i] = 255 * c / max_pixval; - fseek(fp, 2, SEEK_CUR); + ass_fseek(fp, 2, SEEK_CUR); } gmap->cstart = min_code; diff -r 4ca4e3c5a754 -r d7fe157c402d sdr/font.p.glsl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sdr/font.p.glsl Sat Jun 13 05:32:07 2015 +0300 @@ -0,0 +1,12 @@ +precision mediump float; + +uniform sampler2D tex; +uniform vec4 color; + +varying vec4 tex_coords; + +void main() +{ + vec4 texel = texture2D(tex, tex_coords.xy); + gl_FragColor = vec4(color.xyz, texel.a); +} diff -r 4ca4e3c5a754 -r d7fe157c402d sdr/vertex.glsl --- a/sdr/vertex.glsl Thu Jun 11 04:56:33 2015 +0300 +++ b/sdr/vertex.glsl Sat Jun 13 05:32:07 2015 +0300 @@ -1,12 +1,13 @@ -attribute vec4 attr_vertex, attr_texcoord; +attribute vec4 attr_vertex, attr_texcoord, attr_color; uniform mat4 matrix_modelview, matrix_projection, matrix_texture; -varying vec4 tex_coords; +varying vec4 tex_coords, color; void main() { mat4 mvp = matrix_projection * matrix_modelview; gl_Position = mvp * attr_vertex; tex_coords = matrix_texture * attr_texcoord; + color = attr_color; } diff -r 4ca4e3c5a754 -r d7fe157c402d src/assman.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/assman.c Sat Jun 13 05:32:07 2015 +0300 @@ -0,0 +1,30 @@ +#include "assman.h" + +int ass_fgetc(ass_file *fp) +{ + char c; + + if(ass_fread(&c, 1, 1, fp) < 1) { + return -1; + } + return c; +} + +char *ass_fgets(char *s, int size, ass_file *fp) +{ + int i, c; + char *ptr = s; + + if(!size) return 0; + + for(i=0; i::const_iterator it = sdrdb.find(name); diff -r 4ca4e3c5a754 -r d7fe157c402d src/shader.h --- a/src/shader.h Thu Jun 11 04:56:33 2015 +0300 +++ b/src/shader.h Sat Jun 13 05:32:07 2015 +0300 @@ -2,6 +2,7 @@ #define SHADER_H_ #include +#include "vmath/vmath.h" enum SdrDefaultAttrib { SDR_ATTR_VERTEX, @@ -42,6 +43,25 @@ bool bind() const; + // helper functions for setting uniforms + void set_uniform(const char *name, int count, const int *val) const; + void set_uniform(const char *name, int count, const float *val) const; + void set_uniform(const char *name, const Vector4 &v) const; + + void set_uniform1i(const char *name, int x) const; + void set_uniform2i(const char *name, int x, int y) const; + void set_uniform3i(const char *name, int x, int y, int z) const; + void set_uniform4i(const char *name, int x, int y, int z, int w) const; + + void set_uniform1f(const char *name, float x) const; + void set_uniform2f(const char *name, float x, float y) const; + void set_uniform3f(const char *name, float x, float y, float z) const; + void set_uniform4f(const char *name, float x, float y, float z, float w) const; + + void set_uniform_matrix(const char *name, const float *m) const; + void set_uniform_matrix(const char *name, const Matrix4x4 &m) const; + + unsigned int get_globj() const { return prog; } }; diff -r 4ca4e3c5a754 -r d7fe157c402d src/text.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/text.c Sat Jun 13 05:32:07 2015 +0300 @@ -0,0 +1,135 @@ +#include +#include +#include +#include "game.h" +#include "text.h" +#include "sdr.h" +#include "opengl.h" +#include "sanegl.h" +#include "drawtext/drawtext.h" + +#define FONT_NAME "consolas_s24.glyphmap" +#define FONT_SIZE 24 + +static struct dtx_font *font; +static unsigned int prog; +static int uloc_color = -1; +static int aloc_vertex, aloc_texcoords; +static float color[4]; +static int cpos[2]; +static int line_height; + +static int init(void) +{ + if(font) return 0; /* already initialized */ + + if(!(font = dtx_open_font_glyphmap("data/" FONT_NAME))) { + fprintf(stderr, "failed to open font\n"); + return -1; + } + line_height = dtx_line_height(); + + if(!(prog = create_program_load("sdr/vertex.glsl", "sdr/font.p.glsl"))) { + dtx_close_font(font); + font = 0; + return -1; + } + aloc_vertex = glGetAttribLocation(prog, "attr_vertex"); + aloc_texcoords = glGetAttribLocation(prog, "attr_texcoord"); + uloc_color = glGetUniformLocation(prog, "color"); + + return 0; +} + +void text_color(float r, float g, float b, float a) +{ + color[0] = r; + color[1] = g; + color[2] = b; + color[3] = a; +} + +void text_position(int x, int y) +{ + cpos[0] = x; + cpos[1] = y; +} + +static int pre_print(void) +{ + int top = win_height - line_height; + + if(init() == -1) { + return -1; + } + + gl_matrix_mode(GL_TEXTURE); + gl_push_matrix(); + gl_load_identity(); + gl_matrix_mode(GL_PROJECTION); + gl_push_matrix(); + gl_load_identity(); + gl_ortho(0, win_width, 0, win_height, -1, 1); + gl_matrix_mode(GL_MODELVIEW); + gl_push_matrix(); + gl_load_identity(); + gl_translatef(cpos[0] * dtx_glyph_width(' '), top - cpos[1] * line_height, 0); + + glUseProgram(prog); + glUniform4fv(uloc_color, 1, color); + + gl_apply_xform(prog); + + dtx_use_font(font, FONT_SIZE); + dtx_vertex_attribs(aloc_vertex, aloc_texcoords); + return 0; +} + +static void post_print(void) +{ + gl_matrix_mode(GL_TEXTURE); + gl_pop_matrix(); + gl_matrix_mode(GL_PROJECTION); + gl_pop_matrix(); + gl_matrix_mode(GL_MODELVIEW); + gl_pop_matrix(); +} + +void text_print(const char *s) +{ + if(pre_print() == -1) { + return; + } + + dtx_string(s); + + post_print(); +} + +void text_printf(const char *fmt, ...) +{ + va_list ap; + int buf_size; + char *buf, tmp; + + if(pre_print() == -1) { + return; + } + + va_start(ap, fmt); + buf_size = vsnprintf(&tmp, 0, fmt, ap); + va_end(ap); + + if(buf_size == -1) { + buf_size = 512; + } + + buf = alloca(buf_size + 1); + va_start(ap, fmt); + vsnprintf(buf, buf_size + 1, fmt, ap); + va_end(ap); + + dtx_string(buf); + + post_print(); +} diff -r 4ca4e3c5a754 -r d7fe157c402d src/text.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/text.h Sat Jun 13 05:32:07 2015 +0300 @@ -0,0 +1,17 @@ +#ifndef TEXT_H_ +#define TEXT_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +void text_color(float r, float g, float b, float a); +void text_position(int x, int y); +void text_print(const char *s); +void text_printf(const char *fmt, ...); + +#ifdef __cplusplus +} +#endif + +#endif /* TEXT_H_ */