Replace large background with two computed textures. Object size is reduced and UI can be scaled properly. Add optional frame to container.

master
Peter Nelson 2013-02-03 19:54:40 +00:00
parent 94b6ed53b6
commit ebcf58aa91
4 changed files with 81 additions and 19 deletions

View File

@ -12,11 +12,11 @@ Container::~Container()
void Container::OnPaint() const
{
if (this->back) {
float tw = (float)w / tex->w[TEX_BACK];
float th = (float)h / tex->h[TEX_BACK];
float tw = (float)w / tex->w[TEX_BACKTILE];
float th = (float)h / tex->h[TEX_BACKTILE];
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex->tex[TEX_BACK]);
glBindTexture(GL_TEXTURE_2D, tex->tex[TEX_BACKTILE]);
glBegin(GL_QUADS);
glColor4f(colour.r, colour.g, colour.b, colour.a);
glTexCoord2f(.0, .0);
@ -28,6 +28,20 @@ void Container::OnPaint() const
glTexCoord2f(tw, .0);
glVertex2f(w + x, y);
glEnd();
glBindTexture(GL_TEXTURE_2D, tex->tex[TEX_BACKGLOW]);
glBegin(GL_QUADS);
glColor4f(colour.r, colour.g, colour.b, colour.a * 0.3f);
glTexCoord2f(.0, .0);
glVertex2f( x, y);
glTexCoord2f(.0, 1);
glVertex2f( x, h + y);
glTexCoord2f(1, 1);
glVertex2f(w + x, h + y);
glTexCoord2f(1, .0);
glVertex2f(w + x, y);
glEnd();
glDisable(GL_TEXTURE_2D);
} else if (this->colour.a) {
glBegin(GL_QUADS);
@ -39,13 +53,34 @@ void Container::OnPaint() const
glEnd();
}
/* glBegin(GL_LINE_LOOP);
glColor4f(1, 1, 1, 0.25f);
glVertex2f( x + .5f, y + .5f);
glVertex2f( x + .5f, h + y - .5f);
glVertex2f(w + x - .5f, h + y - .5f);
glVertex2f(w + x - .5f, y + .5f);
glEnd();*/
if (this->frame.a) {
if (true) {
glBegin(GL_LINE_LOOP);
glColor4f(frame.r, frame.g, frame.b, frame.a);
glVertex2f( x + .5f, y + .5f);
glVertex2f( x + .5f, h + y - .5f);
glVertex2f(w + x - .5f, h + y - .5f);
glVertex2f(w + x - .5f, y + .5f);
glEnd();
} else {
glBegin(GL_LINE_LOOP);
glColor4f(1, 1, 1, frame.a);
glVertex2f( x + 1.5f, y + 1.5f);
glVertex2f( x + 1.5f, h + y - .5f);
glVertex2f(w + x - .5f, h + y - .5f);
glVertex2f(w + x - .5f, y + 1.5f);
glEnd();
glBegin(GL_LINE_LOOP);
glColor4f(0, 0, 0, frame.a);
glVertex2f( x + .5f, y + .5f);
glVertex2f( x + .5f, h + y - 1.5f);
glVertex2f(w + x - 1.5f, h + y - 1.5f);
glVertex2f(w + x - 1.5f, y + .5f);
glEnd();
}
}
std::list<Widget *>::const_iterator it;
for (it = children.begin(); it != children.end(); ++it) {

View File

@ -6,6 +6,7 @@
struct Container : Widget {
std::list<Widget *> children;
Colour colour;
Colour frame;
bool back;
~Container();

View File

@ -1,7 +1,8 @@
#include <cstdlib>
#include <cmath>
#include <pugl/pugl.h>
#include "pui.h"
#include "textures/background.c"
#include "textures/slider1-vert.c"
#include "textures/slider1-vert-prelight.c"
#include "textures/trough1-vertical.c"
@ -10,14 +11,14 @@
#include "textures/trough2-vertical.c"
#include "textures/knob4.c"
void PTextures::BindTexture(int texture, int width, int height, const unsigned char *data)
void PTextures::BindTexture(int texture, int width, int height, const unsigned char *data, int format)
{
glBindTexture(GL_TEXTURE_2D, tex[texture]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 0);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, format, GL_UNSIGNED_BYTE, data);
w[texture] = width;
h[texture] = height;
@ -30,10 +31,34 @@ void PTextures::InitTextures()
glGenTextures(TEX_END, this->tex);
BindTexture(TEX_BACK,
background.width,
background.height,
background.pixel_data);
/* Texture buffer for generated textures */
int size = 128;
w[TEX_BACKTILE] = h[TEX_BACKTILE] = size;
uint8_t *buffer = new uint8_t[size * size];
/* Create background tile */
for (int i = 0; i < size * size; i++) {
/* Normalised noise */
int r = 0;
for (int j = 0; j < 4; j++) r += (rand() & 0x1F);
buffer[i] = (r >> 2) + 0x30;
}
BindTexture(TEX_BACKTILE, size, size, buffer, GL_LUMINANCE);
/* Create glow, focused at 1/3 down the window */
int cx = size / 2;
int cy = size / 3;
for (int y = 0; y < size; y++) {
for (int x = 0; x < size; x++) {
float b = 196 - sqrtf((cx - x) * (cx - x) + (cy - y) * (cy - y)) * 256 / size;
if (b < 0) b = 0;
buffer[x + y * size] = b;
}
}
BindTexture(TEX_BACKGLOW, size, size, buffer, GL_LUMINANCE);
delete buffer;
BindTexture(TEX_SLIDER1,
slider1_vert.width,
slider1_vert.height,

View File

@ -2,7 +2,8 @@
#define TEXTURES_H
enum {
TEX_BACK,
TEX_BACKTILE,
TEX_BACKGLOW,
TEX_SLIDER1,
TEX_SLIDER1_PRE,
@ -25,7 +26,7 @@ struct PTextures {
int h[TEX_END];
void InitTextures();
void BindTexture(int texture, int width, int height, const unsigned char *data);
void BindTexture(int texture, int width, int height, const unsigned char *data, int format = GL_RGBA);
};
#endif /* TEXTURES_H */