istereo2

view 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 source
1 #include <map>
2 #include <string>
3 #include "goatkit/goatkit.h"
4 #include "opengl.h"
5 #include "drawtext.h"
6 #include "sdr.h"
8 #define VIS_THRES 0.005
10 using namespace goatkit;
12 extern int view_xsz, view_ysz;
13 extern unsigned int prog_ui, prog_font;
14 extern struct dtx_font *font;
16 static void draw_label(const Widget *w);
17 static void draw_text(float x, float y, const char *text);
19 static struct {
20 const char *name;
21 WidgetDrawFunc func;
22 } widget_funcs[] = {
23 { "label", draw_label },
24 {0, 0}
25 };
27 static std::map<std::string, WidgetDrawFunc> funcmap;
29 extern "C" WidgetDrawFunc get_widget_func(const char *name)
30 {
31 static bool initialized;
33 if(!initialized) {
34 for(int i=0; widget_funcs[i].func; i++) {
35 funcmap[widget_funcs[i].name] = widget_funcs[i].func;
36 }
37 initialized = true;
38 }
39 return funcmap[name];
40 }
42 static void begin_drawing(const Widget *w)
43 {
44 Vec2 pos = w->get_position();
46 glEnable(GL_BLEND);
47 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
49 gl_matrix_mode(GL_MODELVIEW);
50 gl_push_matrix();
51 gl_load_identity();
52 gl_translatef(pos.x, pos.y, 0);
53 }
55 static void end_drawing(const Widget *w)
56 {
57 gl_matrix_mode(GL_MODELVIEW);
58 gl_pop_matrix();
59 }
61 static void draw_label(const Widget *w)
62 {
63 Vec2 pos = w->get_position();
64 float vis = w->get_visibility();
65 if(vis < VIS_THRES) return;
67 begin_drawing(w);
68 draw_text(pos.x, pos.y, w->get_text());
69 end_drawing(w);
70 }
72 static void draw_text(float x, float y, const char *text)
73 {
74 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap_idx(font, 0);
75 dtx_use_font(font, dtx_get_glyphmap_ptsize(gmap));
77 float aspect = (float)view_xsz / (float)view_ysz;
78 float virt_xsz = 420.0 * aspect;
79 float virt_ysz = 420.0;
81 gl_matrix_mode(GL_PROJECTION);
82 gl_push_matrix();
83 gl_load_identity();
84 gl_ortho(0, virt_xsz, 0, virt_ysz, -1, 1);
86 gl_matrix_mode(GL_MODELVIEW);
87 gl_push_matrix();
88 gl_load_identity();
89 gl_translatef(x * virt_xsz, y * virt_ysz, 0);
91 bind_program(prog_font);
92 set_uniform_float4(prog_font, "ucolor", 1.0, 1.0, 1.0, 1.0);
93 gl_apply_xform(prog_ui);
95 dtx_string(text);
97 gl_matrix_mode(GL_PROJECTION);
98 gl_pop_matrix();
99 gl_matrix_mode(GL_MODELVIEW);
100 gl_pop_matrix();