istereo2

annotate libs/goatkit/theme.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 3bccfc7d10fe
children 64e15874f3bd
rev   line source
nuclear@6 1 /*
nuclear@6 2 GoatKit - a themable/animated widget toolkit for games
nuclear@6 3 Copyright (C) 2014 John Tsiombikas <nuclear@member.fsf.org>
nuclear@6 4
nuclear@6 5 This program is free software: you can redistribute it and/or modify
nuclear@6 6 it under the terms of the GNU Lesser General Public License as published by
nuclear@6 7 the Free Software Foundation, either version 3 of the License, or
nuclear@6 8 (at your option) any later version.
nuclear@6 9
nuclear@6 10 This program is distributed in the hope that it will be useful,
nuclear@6 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@6 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@6 13 GNU Lesser General Public License for more details.
nuclear@6 14
nuclear@6 15 You should have received a copy of the GNU Lesser General Public License
nuclear@6 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@6 17 */
nuclear@6 18 #include "config.h"
nuclear@6 19 #include <stdio.h>
nuclear@6 20 #include <vector>
nuclear@6 21 #include <string>
nuclear@6 22 #include <map>
nuclear@6 23 #include <algorithm>
nuclear@6 24 #include "theme.h"
nuclear@6 25 #include "widget.h"
nuclear@6 26
nuclear@6 27 #ifdef WIN32
nuclear@6 28 #include <windows.h>
nuclear@6 29
nuclear@6 30 static void *dlopen(const char *name, int flags);
nuclear@6 31 static void dlclose(void *so);
nuclear@6 32 static void *dlsym(void *so, const char *symbol);
nuclear@6 33 #else
nuclear@6 34 #include <unistd.h>
nuclear@6 35 #include <dlfcn.h>
nuclear@6 36 #endif
nuclear@6 37
nuclear@6 38 #ifdef HAVE_OPENGL_H
nuclear@6 39 #include "opengl.h"
nuclear@6 40
nuclear@6 41 #else
nuclear@6 42
nuclear@6 43 #ifdef __APPLE__
nuclear@6 44 #include <OpenGL/gl.h>
nuclear@6 45 #else
nuclear@6 46 #include <GL/gl.h>
nuclear@6 47 #endif
nuclear@6 48
nuclear@6 49 #endif /* HAVE_OPENGL_H_ */
nuclear@6 50
nuclear@6 51 #ifndef PREFIX
nuclear@6 52 #define PREFIX "/usr/local"
nuclear@6 53 #endif
nuclear@6 54
nuclear@6 55 namespace goatkit {
nuclear@6 56
nuclear@6 57 struct ThemeImpl {
nuclear@6 58 void *so;
nuclear@6 59 WidgetDrawFunc (*lookup_theme_draw_func)(const char*);
nuclear@6 60 mutable std::map<std::string, WidgetDrawFunc> func_cache;
nuclear@6 61 };
nuclear@6 62
nuclear@6 63 Theme *theme;
nuclear@6 64 static std::vector<std::string> search_paths;
nuclear@6 65 static const char *fallback_paths[] = {
nuclear@6 66 PREFIX "/share/goatkit",
nuclear@6 67 0
nuclear@6 68 };
nuclear@6 69
nuclear@6 70 void add_theme_path(const char *path)
nuclear@6 71 {
nuclear@6 72 if(!path || !*path) return;
nuclear@6 73
nuclear@6 74 std::string s = path;
nuclear@6 75 int last = s.length() - 1;
nuclear@6 76 if(s[last] == '/' || s[last] == '\\') {
nuclear@6 77 s.erase(last);
nuclear@6 78 }
nuclear@6 79
nuclear@6 80 if(std::find(search_paths.begin(), search_paths.end(), s) != search_paths.end()) {
nuclear@6 81 return;
nuclear@6 82 }
nuclear@6 83
nuclear@6 84 search_paths.push_back(s);
nuclear@6 85 }
nuclear@6 86
nuclear@6 87 Theme::Theme()
nuclear@6 88 {
nuclear@6 89 impl = new ThemeImpl;
nuclear@6 90 impl->so = 0;
nuclear@6 91 impl->lookup_theme_draw_func = 0;
nuclear@6 92 }
nuclear@6 93
nuclear@6 94 Theme::~Theme()
nuclear@6 95 {
nuclear@6 96 unload();
nuclear@6 97 delete impl;
nuclear@6 98 }
nuclear@6 99
nuclear@6 100 typedef WidgetDrawFunc (*LookupFunc)(const char*);
nuclear@6 101
nuclear@6 102 bool Theme::load(const char *name)
nuclear@6 103 {
nuclear@6 104 unload();
nuclear@6 105
nuclear@7 106 if(strcmp(name, "GOATKIT_THEME_BUILTIN") == 0) {
nuclear@7 107 impl->so = RTLD_DEFAULT;
nuclear@7 108 } else {
nuclear@7 109 std::string fname = std::string(name) + ".gtheme";
nuclear@7 110 if(!(impl->so = dlopen(fname.c_str(), RTLD_LAZY))) {
nuclear@7 111 for(size_t i=0; i<search_paths.size(); i++) {
nuclear@7 112 std::string path = search_paths[i] + "/" + fname;
nuclear@6 113
nuclear@6 114 if((impl->so = dlopen(path.c_str(), RTLD_LAZY))) {
nuclear@6 115 break;
nuclear@6 116 }
nuclear@6 117 }
nuclear@6 118
nuclear@7 119 // try the fallback paths
nuclear@7 120 if(!impl->so) {
nuclear@7 121 for(int i=0; fallback_paths[i]; i++) {
nuclear@7 122 std::string path = std::string(fallback_paths[i]) + "/" + fname;
nuclear@7 123
nuclear@7 124 if((impl->so = dlopen(path.c_str(), RTLD_LAZY))) {
nuclear@7 125 break;
nuclear@7 126 }
nuclear@7 127 }
nuclear@7 128 }
nuclear@7 129
nuclear@7 130 if(!impl->so) {
nuclear@7 131 fprintf(stderr, "%s: failed to load theme plugin: %s\n", __func__, name);
nuclear@7 132 return false;
nuclear@7 133 }
nuclear@6 134 }
nuclear@6 135 }
nuclear@6 136
nuclear@6 137 // loaded the shared object, now get the lookup function
nuclear@6 138 impl->lookup_theme_draw_func = (LookupFunc)dlsym(impl->so, "get_widget_func");
nuclear@6 139 if(!impl->lookup_theme_draw_func) {
nuclear@6 140 fprintf(stderr, "%s: invalid theme plugin %s\n", __func__, name);
nuclear@6 141 unload();
nuclear@6 142 return false;
nuclear@6 143 }
nuclear@6 144
nuclear@6 145 return true;
nuclear@6 146 }
nuclear@6 147
nuclear@6 148 void Theme::unload()
nuclear@6 149 {
nuclear@6 150 if(impl->so) {
nuclear@7 151 if(impl->so != RTLD_DEFAULT) {
nuclear@7 152 dlclose(impl->so);
nuclear@7 153 }
nuclear@6 154 impl->so = 0;
nuclear@6 155 }
nuclear@6 156 impl->func_cache.clear();
nuclear@6 157 }
nuclear@6 158
nuclear@6 159 WidgetDrawFunc Theme::get_draw_func(const char *type) const
nuclear@6 160 {
nuclear@6 161 std::map<std::string, WidgetDrawFunc>::const_iterator it = impl->func_cache.find(type);
nuclear@6 162 if(it == impl->func_cache.end()) {
nuclear@6 163 // don't have it cached, try to look it up
nuclear@6 164 WidgetDrawFunc func;
nuclear@6 165 if(impl->lookup_theme_draw_func && (func = impl->lookup_theme_draw_func(type))) {
nuclear@6 166 impl->func_cache[type] = func;
nuclear@6 167 return func;
nuclear@6 168 }
nuclear@6 169
nuclear@6 170 // can't look it up, return the default
nuclear@6 171 return default_draw_func;
nuclear@6 172 }
nuclear@6 173 return it->second;
nuclear@6 174 }
nuclear@6 175
nuclear@6 176 #define LERP(a, b, t) ((a) + ((b) - (a)) * t)
nuclear@6 177 #define DEF_TEX_SZ 32
nuclear@6 178 void default_draw_func(const Widget *w)
nuclear@6 179 {
nuclear@6 180 static unsigned int tex;
nuclear@6 181
nuclear@6 182 if(!tex) {
nuclear@6 183 unsigned char *pixels = new unsigned char[DEF_TEX_SZ * DEF_TEX_SZ * 3];
nuclear@6 184 unsigned char *ptr = pixels;
nuclear@6 185 for(int i=0; i<DEF_TEX_SZ; i++) {
nuclear@6 186 for(int j=0; j<DEF_TEX_SZ; j++) {
nuclear@6 187 bool stripe = (((j + i) / 8) & 1) == 1;
nuclear@6 188 ptr[0] = ptr[1] = ptr[2] = stripe ? 255 : 0;
nuclear@6 189 ptr += 3;
nuclear@6 190 }
nuclear@6 191 }
nuclear@6 192
nuclear@6 193 glGenTextures(1, &tex);
nuclear@6 194 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@6 195 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@6 196 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@6 197 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, DEF_TEX_SZ, DEF_TEX_SZ, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
nuclear@6 198 delete [] pixels;
nuclear@6 199 }
nuclear@6 200
nuclear@6 201 Vec2 pos = w->get_position();
nuclear@6 202 Vec2 sz = w->get_size();
nuclear@6 203 float aspect = sz.x / sz.y;
nuclear@6 204
nuclear@6 205 #if !defined(GL_ES_VERSION_2_0)
nuclear@6 206 glPushAttrib(GL_ENABLE_BIT);
nuclear@6 207 glEnable(GL_TEXTURE_2D);
nuclear@6 208 #endif
nuclear@6 209 glBindTexture(GL_TEXTURE_2D, tex);
nuclear@6 210
nuclear@6 211 float offs = w->get_pressed() * 0.1 * sz.y;
nuclear@6 212 glMatrixMode(GL_MODELVIEW);
nuclear@6 213 glPushMatrix();
nuclear@6 214 glTranslatef(offs, -offs, 0);
nuclear@6 215
nuclear@6 216 float active = w->get_active();
nuclear@6 217 float hover = w->get_under_mouse();
nuclear@6 218
nuclear@6 219 float rg = LERP(0.4, 1.0, hover);
nuclear@6 220 float b = LERP(rg, 0, active);
nuclear@6 221 glColor3f(rg, rg, b);
nuclear@6 222
nuclear@6 223 glBegin(GL_QUADS);
nuclear@6 224 glTexCoord2f(0, 1);
nuclear@6 225 glVertex2f(pos.x, pos.y);
nuclear@6 226 glTexCoord2f(aspect, 1);
nuclear@6 227 glVertex2f(pos.x + sz.x, pos.y);
nuclear@6 228 glTexCoord2f(aspect, 0);
nuclear@6 229 glVertex2f(pos.x + sz.x, pos.y + sz.y);
nuclear@6 230 glTexCoord2f(0, 0);
nuclear@6 231 glVertex2f(pos.x, pos.y + sz.y);
nuclear@6 232 glEnd();
nuclear@6 233
nuclear@6 234 glPopMatrix();
nuclear@6 235
nuclear@6 236 #ifndef GL_ES_VERSION_2_0
nuclear@6 237 glPopAttrib();
nuclear@6 238 #endif
nuclear@6 239 }
nuclear@6 240
nuclear@6 241 } // namespace goatkit
nuclear@6 242
nuclear@6 243 #ifdef WIN32
nuclear@6 244 // XXX untested
nuclear@6 245 static void *dlopen(const char *name, int flags)
nuclear@6 246 {
nuclear@6 247 return LoadLibrary(name);
nuclear@6 248 }
nuclear@6 249
nuclear@6 250 static void dlclose(void *so)
nuclear@6 251 {
nuclear@6 252 FreeLibrary(so);
nuclear@6 253 }
nuclear@6 254
nuclear@6 255 static void *dlsym(void *so, const char *symbol)
nuclear@6 256 {
nuclear@7 257 if(so == RTLD_DEFAULT) {
nuclear@7 258 so = GetModuleHandle(0);
nuclear@7 259 }
nuclear@6 260 return (void*)GetProcAddress(so, symbol);
nuclear@6 261 }
nuclear@6 262 #endif