istereo2
view src/uitheme.cc @ 8:661bf09db398
- replaced Quartz timer with cross-platform timer code
- protected goatkit builtin theme function from being optimized out
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 24 Sep 2015 07:09:37 +0300 |
parents | a3c4fcc9f8f3 |
children | 64e15874f3bd |
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 __attribute__ ((used))
30 extern "C" WidgetDrawFunc get_widget_func(const char *name)
31 {
32 static bool initialized;
34 if(!initialized) {
35 for(int i=0; widget_funcs[i].func; i++) {
36 funcmap[widget_funcs[i].name] = widget_funcs[i].func;
37 }
38 initialized = true;
39 }
40 return funcmap[name];
41 }
43 static void begin_drawing(const Widget *w)
44 {
45 Vec2 pos = w->get_position();
47 glEnable(GL_BLEND);
48 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
50 gl_matrix_mode(GL_MODELVIEW);
51 gl_push_matrix();
52 gl_load_identity();
53 gl_translatef(pos.x, pos.y, 0);
54 }
56 static void end_drawing(const Widget *w)
57 {
58 gl_matrix_mode(GL_MODELVIEW);
59 gl_pop_matrix();
60 }
62 static void draw_label(const Widget *w)
63 {
64 Vec2 pos = w->get_position();
65 float vis = w->get_visibility();
66 if(vis < VIS_THRES) return;
68 begin_drawing(w);
69 draw_text(pos.x, pos.y, w->get_text());
70 end_drawing(w);
71 }
73 static void draw_text(float x, float y, const char *text)
74 {
75 struct dtx_glyphmap *gmap = dtx_get_font_glyphmap_idx(font, 0);
76 dtx_use_font(font, dtx_get_glyphmap_ptsize(gmap));
78 float aspect = (float)view_xsz / (float)view_ysz;
79 float virt_xsz = 420.0 * aspect;
80 float virt_ysz = 420.0;
82 gl_matrix_mode(GL_PROJECTION);
83 gl_push_matrix();
84 gl_load_identity();
85 gl_ortho(0, virt_xsz, 0, virt_ysz, -1, 1);
87 gl_matrix_mode(GL_MODELVIEW);
88 gl_push_matrix();
89 gl_load_identity();
90 gl_translatef(x * virt_xsz, y * virt_ysz, 0);
92 bind_program(prog_font);
93 set_uniform_float4(prog_font, "ucolor", 1.0, 1.0, 1.0, 1.0);
94 gl_apply_xform(prog_ui);
96 dtx_string(text);
98 gl_matrix_mode(GL_PROJECTION);
99 gl_pop_matrix();
100 gl_matrix_mode(GL_MODELVIEW);
101 gl_pop_matrix();
102 }