gameui

view src/theme.cc @ 3:f1014234dece

transitions in gui elements are awesome :)
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 21 Mar 2014 03:37:16 +0200
parents
children e0916bb20b7f
line source
1 #include <stdio.h>
2 #include "theme.h"
3 #include "widget.h"
5 #ifdef WIN32
6 #include <windows.h>
7 #endif
8 #ifdef __APPLE__
9 #include <OpenGL/gl.h>
10 #else
11 #include <GL/gl.h>
12 #endif
15 namespace gameui {
17 Theme *theme;
19 Theme::Theme()
20 {
21 so = 0;
22 }
24 bool Theme::load(const char *name)
25 {
26 fprintf(stderr, "theme loading not implemented yet!\n");
27 return false;
28 }
30 widget_draw_func Theme::get_draw_func(const char *type) const
31 {
32 std::map<std::string, widget_draw_func>::const_iterator it = draw_func.find(type);
33 if(it == draw_func.end()) {
34 return default_draw_func;
35 }
36 return it->second;
37 }
39 #define LERP(a, b, t) ((a) + ((b) - (a)) * t)
40 #define DEF_TEX_SZ 32
41 void default_draw_func(const Widget *w)
42 {
43 static unsigned int tex;
45 if(!tex) {
46 unsigned char *pixels = new unsigned char[DEF_TEX_SZ * DEF_TEX_SZ * 3];
47 unsigned char *ptr = pixels;
48 for(int i=0; i<DEF_TEX_SZ; i++) {
49 for(int j=0; j<DEF_TEX_SZ; j++) {
50 bool stripe = (((j + i) / 8) & 1) == 1;
51 ptr[0] = ptr[1] = ptr[2] = stripe ? 255 : 0;
52 ptr += 3;
53 }
54 }
56 glGenTextures(1, &tex);
57 glBindTexture(GL_TEXTURE_2D, tex);
58 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
59 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
60 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, DEF_TEX_SZ, DEF_TEX_SZ, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
61 delete [] pixels;
62 }
64 Vec2 pos = w->get_position();
65 Vec2 sz = w->get_size();
66 float aspect = sz.x / sz.y;
68 glPushAttrib(GL_ENABLE_BIT);
69 glEnable(GL_TEXTURE_2D);
70 glBindTexture(GL_TEXTURE_2D, tex);
72 float offs = w->get_pressed() * 0.1 * sz.y;
73 glMatrixMode(GL_MODELVIEW);
74 glPushMatrix();
75 glTranslatef(offs, -offs, 0);
77 float active = w->get_active();
78 float hover = w->get_under_mouse();
80 float rg = LERP(0.4, 1.0, hover);
81 float b = LERP(rg, 0, active);
82 glColor3f(rg, rg, b);
84 glBegin(GL_QUADS);
85 glTexCoord2f(0, 1);
86 glVertex2f(pos.x, pos.y);
87 glTexCoord2f(aspect, 1);
88 glVertex2f(pos.x + sz.x, pos.y);
89 glTexCoord2f(aspect, 0);
90 glVertex2f(pos.x + sz.x, pos.y + sz.y);
91 glTexCoord2f(0, 0);
92 glVertex2f(pos.x, pos.y + sz.y);
93 glEnd();
95 glPopMatrix();
97 glPopAttrib();
98 }
100 } // namespace gameui