gameui

diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/theme.cc	Fri Mar 21 03:37:16 2014 +0200
     1.3 @@ -0,0 +1,100 @@
     1.4 +#include <stdio.h>
     1.5 +#include "theme.h"
     1.6 +#include "widget.h"
     1.7 +
     1.8 +#ifdef WIN32
     1.9 +#include <windows.h>
    1.10 +#endif
    1.11 +#ifdef __APPLE__
    1.12 +#include <OpenGL/gl.h>
    1.13 +#else
    1.14 +#include <GL/gl.h>
    1.15 +#endif
    1.16 +
    1.17 +
    1.18 +namespace gameui {
    1.19 +
    1.20 +Theme *theme;
    1.21 +
    1.22 +Theme::Theme()
    1.23 +{
    1.24 +	so = 0;
    1.25 +}
    1.26 +
    1.27 +bool Theme::load(const char *name)
    1.28 +{
    1.29 +	fprintf(stderr, "theme loading not implemented yet!\n");
    1.30 +	return false;
    1.31 +}
    1.32 +
    1.33 +widget_draw_func Theme::get_draw_func(const char *type) const
    1.34 +{
    1.35 +	std::map<std::string, widget_draw_func>::const_iterator it = draw_func.find(type);
    1.36 +	if(it == draw_func.end()) {
    1.37 +		return default_draw_func;
    1.38 +	}
    1.39 +	return it->second;
    1.40 +}
    1.41 +
    1.42 +#define LERP(a, b, t)	((a) + ((b) - (a)) * t)
    1.43 +#define DEF_TEX_SZ	32
    1.44 +void default_draw_func(const Widget *w)
    1.45 +{
    1.46 +	static unsigned int tex;
    1.47 +
    1.48 +	if(!tex) {
    1.49 +		unsigned char *pixels = new unsigned char[DEF_TEX_SZ * DEF_TEX_SZ * 3];
    1.50 +		unsigned char *ptr = pixels;
    1.51 +		for(int i=0; i<DEF_TEX_SZ; i++) {
    1.52 +			for(int j=0; j<DEF_TEX_SZ; j++) {
    1.53 +				bool stripe = (((j + i) / 8) & 1) == 1;
    1.54 +				ptr[0] = ptr[1] = ptr[2] = stripe ? 255 : 0;
    1.55 +				ptr += 3;
    1.56 +			}
    1.57 +		}
    1.58 +
    1.59 +		glGenTextures(1, &tex);
    1.60 +		glBindTexture(GL_TEXTURE_2D, tex);
    1.61 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.62 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.63 +		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, DEF_TEX_SZ, DEF_TEX_SZ, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);
    1.64 +		delete [] pixels;
    1.65 +	}
    1.66 +
    1.67 +	Vec2 pos = w->get_position();
    1.68 +	Vec2 sz = w->get_size();
    1.69 +	float aspect = sz.x / sz.y;
    1.70 +
    1.71 +	glPushAttrib(GL_ENABLE_BIT);
    1.72 +	glEnable(GL_TEXTURE_2D);
    1.73 +	glBindTexture(GL_TEXTURE_2D, tex);
    1.74 +
    1.75 +	float offs = w->get_pressed() * 0.1 * sz.y;
    1.76 +	glMatrixMode(GL_MODELVIEW);
    1.77 +	glPushMatrix();
    1.78 +	glTranslatef(offs, -offs, 0);
    1.79 +
    1.80 +	float active = w->get_active();
    1.81 +	float hover = w->get_under_mouse();
    1.82 +
    1.83 +	float rg = LERP(0.4, 1.0, hover);
    1.84 +	float b = LERP(rg, 0, active);
    1.85 +	glColor3f(rg, rg, b);
    1.86 +
    1.87 +	glBegin(GL_QUADS);
    1.88 +	glTexCoord2f(0, 1);
    1.89 +	glVertex2f(pos.x, pos.y);
    1.90 +	glTexCoord2f(aspect, 1);
    1.91 +	glVertex2f(pos.x + sz.x, pos.y);
    1.92 +	glTexCoord2f(aspect, 0);
    1.93 +	glVertex2f(pos.x + sz.x, pos.y + sz.y);
    1.94 +	glTexCoord2f(0, 0);
    1.95 +	glVertex2f(pos.x, pos.y + sz.y);
    1.96 +	glEnd();
    1.97 +
    1.98 +	glPopMatrix();
    1.99 +
   1.100 +	glPopAttrib();
   1.101 +}
   1.102 +
   1.103 +}	// namespace gameui