From 7c290476afd877cf4be48f458a6d2666a2562c8c Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Mon, 17 Sep 2012 01:37:56 +0200 Subject: menu/background_*: vertices are only recalculated if the size changed The size of the parent component is cached by the backgrounds and the vertices to draw the backgrounds are only calculated again, if the size of the parent component changed. --- src/menu/background_color.cpp | 29 ++++++++++++++++++----------- src/menu/background_color.hpp | 14 ++++++++++++++ src/menu/background_image.cpp | 24 +++++++++++++++--------- src/menu/background_image.hpp | 19 +++++++++++++++++++ 4 files changed, 66 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/menu/background_color.cpp b/src/menu/background_color.cpp index f55763b7..031ffe26 100644 --- a/src/menu/background_color.cpp +++ b/src/menu/background_color.cpp @@ -28,26 +28,33 @@ namespace usdx { BackgroundColor::BackgroundColor(const DrawableControl &control) - : Background(control), color(0,0,0) + : Background(control), color(0,0,0), size(0, 0) { } BackgroundColor::BackgroundColor(const DrawableControl &control, const RgbColor &color) : - Background(control), color(color) + Background(control), color(color), size(0, 0) { } - void BackgroundColor::draw() + void BackgroundColor::updateVertices() { + vertices[0] = 0; + vertices[1] = get_parent().get_height(); + vertices[2] = get_parent().get_width(); + vertices[3] = get_parent().get_height(); + vertices[4] = get_parent().get_width(); + vertices[5] = 0; + vertices[6] = 0; + vertices[7] = 0; + } + + void BackgroundColor::draw(void) { - this->vertices[0] = 0; - this->vertices[1] = get_parent().get_height(); - this->vertices[2] = get_parent().get_width(); - this->vertices[3] = get_parent().get_height(); - this->vertices[4] = get_parent().get_width(); - this->vertices[5] = 0; - this->vertices[6] = 0; - this->vertices[7] = 0; + if (size != get_parent().get_size()) { + this->updateVertices(); + size = get_parent().get_size(); + } glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); diff --git a/src/menu/background_color.hpp b/src/menu/background_color.hpp index 7913fe30..4ba040cb 100644 --- a/src/menu/background_color.hpp +++ b/src/menu/background_color.hpp @@ -29,6 +29,7 @@ #include "background.hpp" #include "utils/rgb_color.hpp" +#include "utils/dimension.hpp" namespace usdx { @@ -37,6 +38,19 @@ namespace usdx private: GLint vertices[8]; RgbColor color; + + /** + * Cache for the size of the parent component, to be able to detect + * changes. + */ + Dimension size; + + /** + * Update the vertices for drawing the background, if the size if the + * parent component changes. + */ + void updateVertices(); + protected: void draw(void); diff --git a/src/menu/background_image.cpp b/src/menu/background_image.cpp index bb398304..451587f7 100644 --- a/src/menu/background_image.cpp +++ b/src/menu/background_image.cpp @@ -30,7 +30,7 @@ namespace usdx { BackgroundImage::BackgroundImage(const DrawableControl &control, boost::filesystem::wpath filename) : - Background(control) + Background(control), texture(NULL), size(0,0) { texture = new Disposer(new Texture(filename)); } @@ -42,18 +42,24 @@ namespace usdx texture = NULL; } } + + void BackgroundImage::updateVertices() { + vertices[0] = 0; + vertices[1] = get_parent().get_height(); + vertices[2] = get_parent().get_width(); + vertices[3] = get_parent().get_height(); + vertices[4] = get_parent().get_width(); + vertices[5] = 0; + vertices[6] = 0; + vertices[7] = 0; } void BackgroundImage::draw(void) { - this->vertices[0] = 0; - this->vertices[1] = get_parent().get_height(); - this->vertices[2] = get_parent().get_width(); - this->vertices[3] = get_parent().get_height(); - this->vertices[4] = get_parent().get_width(); - this->vertices[5] = 0; - this->vertices[6] = 0; - this->vertices[7] = 0; + if (size != get_parent().get_size()) { + this->updateVertices(); + size = get_parent().get_size(); + } Activator t(texture->get()); diff --git a/src/menu/background_image.hpp b/src/menu/background_image.hpp index 23b1a68e..8b28a2e3 100644 --- a/src/menu/background_image.hpp +++ b/src/menu/background_image.hpp @@ -31,6 +31,7 @@ #include "background.hpp" #include "texture.hpp" #include "utils/disposer.hpp" +#include "utils/dimension.hpp" namespace usdx { @@ -46,8 +47,26 @@ namespace usdx */ Disposer* texture; + /** + * Vertices to draw the background rectangle filled with the + * texture. Cached until size of the parent component changes. + * + * @see updateVertices() + */ GLint vertices[8]; + /** + * Cache for the size of the parent component, to be able to detect + * changes. + */ + Dimension size; + + /** + * Update the vertices for drawing the background, if the size if the + * parent component changes. + */ + void updateVertices(); + protected: void draw(void); -- cgit v1.2.3