istereo2
diff src/uitheme.cc @ 7:a3c4fcc9f8f3
- started a goatkit UI theme
- font rendering with drawtext and shaders
- asset manager (only used by drawtext for now, will replace respath eventually)
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 24 Sep 2015 06:49:25 +0300 |
parents | |
children | 661bf09db398 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/uitheme.cc Thu Sep 24 06:49:25 2015 +0300 1.3 @@ -0,0 +1,101 @@ 1.4 +#include <map> 1.5 +#include <string> 1.6 +#include "goatkit/goatkit.h" 1.7 +#include "opengl.h" 1.8 +#include "drawtext.h" 1.9 +#include "sdr.h" 1.10 + 1.11 +#define VIS_THRES 0.005 1.12 + 1.13 +using namespace goatkit; 1.14 + 1.15 +extern int view_xsz, view_ysz; 1.16 +extern unsigned int prog_ui, prog_font; 1.17 +extern struct dtx_font *font; 1.18 + 1.19 +static void draw_label(const Widget *w); 1.20 +static void draw_text(float x, float y, const char *text); 1.21 + 1.22 +static struct { 1.23 + const char *name; 1.24 + WidgetDrawFunc func; 1.25 +} widget_funcs[] = { 1.26 + { "label", draw_label }, 1.27 + {0, 0} 1.28 +}; 1.29 + 1.30 +static std::map<std::string, WidgetDrawFunc> funcmap; 1.31 + 1.32 +extern "C" WidgetDrawFunc get_widget_func(const char *name) 1.33 +{ 1.34 + static bool initialized; 1.35 + 1.36 + if(!initialized) { 1.37 + for(int i=0; widget_funcs[i].func; i++) { 1.38 + funcmap[widget_funcs[i].name] = widget_funcs[i].func; 1.39 + } 1.40 + initialized = true; 1.41 + } 1.42 + return funcmap[name]; 1.43 +} 1.44 + 1.45 +static void begin_drawing(const Widget *w) 1.46 +{ 1.47 + Vec2 pos = w->get_position(); 1.48 + 1.49 + glEnable(GL_BLEND); 1.50 + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 1.51 + 1.52 + gl_matrix_mode(GL_MODELVIEW); 1.53 + gl_push_matrix(); 1.54 + gl_load_identity(); 1.55 + gl_translatef(pos.x, pos.y, 0); 1.56 +} 1.57 + 1.58 +static void end_drawing(const Widget *w) 1.59 +{ 1.60 + gl_matrix_mode(GL_MODELVIEW); 1.61 + gl_pop_matrix(); 1.62 +} 1.63 + 1.64 +static void draw_label(const Widget *w) 1.65 +{ 1.66 + Vec2 pos = w->get_position(); 1.67 + float vis = w->get_visibility(); 1.68 + if(vis < VIS_THRES) return; 1.69 + 1.70 + begin_drawing(w); 1.71 + draw_text(pos.x, pos.y, w->get_text()); 1.72 + end_drawing(w); 1.73 +} 1.74 + 1.75 +static void draw_text(float x, float y, const char *text) 1.76 +{ 1.77 + struct dtx_glyphmap *gmap = dtx_get_font_glyphmap_idx(font, 0); 1.78 + dtx_use_font(font, dtx_get_glyphmap_ptsize(gmap)); 1.79 + 1.80 + float aspect = (float)view_xsz / (float)view_ysz; 1.81 + float virt_xsz = 420.0 * aspect; 1.82 + float virt_ysz = 420.0; 1.83 + 1.84 + gl_matrix_mode(GL_PROJECTION); 1.85 + gl_push_matrix(); 1.86 + gl_load_identity(); 1.87 + gl_ortho(0, virt_xsz, 0, virt_ysz, -1, 1); 1.88 + 1.89 + gl_matrix_mode(GL_MODELVIEW); 1.90 + gl_push_matrix(); 1.91 + gl_load_identity(); 1.92 + gl_translatef(x * virt_xsz, y * virt_ysz, 0); 1.93 + 1.94 + bind_program(prog_font); 1.95 + set_uniform_float4(prog_font, "ucolor", 1.0, 1.0, 1.0, 1.0); 1.96 + gl_apply_xform(prog_ui); 1.97 + 1.98 + dtx_string(text); 1.99 + 1.100 + gl_matrix_mode(GL_PROJECTION); 1.101 + gl_pop_matrix(); 1.102 + gl_matrix_mode(GL_MODELVIEW); 1.103 + gl_pop_matrix(); 1.104 +} 1.105 \ No newline at end of file