From 11e4937c9f9af5e064ad7434b2e4bf47b5fe31f6 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Mon, 17 Sep 2012 01:33:32 +0200 Subject: menu/texture: implement GlDelayedAllocation interface Textures are now initialized during their first use, destroyed after their last used and could be use with the Activator. --- src/menu/background_image.cpp | 25 +++++++++---------------- src/menu/background_image.hpp | 7 +++++-- src/menu/software_mouse_pointer.cpp | 21 +++++---------------- src/menu/software_mouse_pointer.hpp | 6 +++--- src/menu/texture.cpp | 34 ++++++++++++++++++++++++++++------ src/menu/texture.hpp | 10 ++++++++-- 6 files changed, 58 insertions(+), 45 deletions(-) diff --git a/src/menu/background_image.cpp b/src/menu/background_image.cpp index 4db81967..15229284 100644 --- a/src/menu/background_image.cpp +++ b/src/menu/background_image.cpp @@ -24,6 +24,7 @@ #include "background_image.hpp" #include "drawable_control.hpp" +#include "utils/activator.hpp" namespace usdx { @@ -31,21 +32,16 @@ namespace usdx boost::filesystem::wpath filename) : Background(control), color(255,255,255) { - texture = new Texture(filename); - - this->tex[0] = 0.0f; - this->tex[1] = 1.0f; - this->tex[2] = 1.0f; - this->tex[3] = 1.0f; - this->tex[4] = 1.0f; - this->tex[5] = 0.0f; - this->tex[6] = 0.0f; - this->tex[7] = 0.0f; + texture = new Disposer(new Texture(filename)); } BackgroundImage::~BackgroundImage() { - delete texture; + if (texture != NULL) { + delete texture; + texture = NULL; + } + } } void BackgroundImage::draw(void) @@ -59,8 +55,7 @@ namespace usdx this->vertices[6] = 0; this->vertices[7] = 0; - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, texture->get_texture()); + Activator t(texture->get()); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); @@ -68,14 +63,12 @@ namespace usdx glVertexPointer(2, GL_INT, 0, vertices); glColorPointer(3, GL_UNSIGNED_BYTE, 0, color.get_array(4)); - glTexCoordPointer(2, GL_FLOAT, 0, tex); + glTexCoordPointer(2, GL_FLOAT, 0, Texture::default_vertices); glDrawArrays(GL_QUADS, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_COLOR_ARRAY); - - glDisable(GL_TEXTURE_2D); } }; diff --git a/src/menu/background_image.hpp b/src/menu/background_image.hpp index 1112aa54..d7475007 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/rgb_color.hpp" +#include "utils/disposer.hpp" namespace usdx { @@ -41,11 +42,13 @@ namespace usdx class BackgroundImage : public Background { private: - Texture* texture; + /** + * Texture containing the image, that should be used as background. + */ + Disposer* texture; GLint vertices[8]; RgbColor color; - GLfloat tex[8]; protected: void draw(void); diff --git a/src/menu/software_mouse_pointer.cpp b/src/menu/software_mouse_pointer.cpp index e9b3b603..a3594479 100644 --- a/src/menu/software_mouse_pointer.cpp +++ b/src/menu/software_mouse_pointer.cpp @@ -25,6 +25,7 @@ #include "software_mouse_pointer.hpp" #include #include +#include "utils/activator.hpp" namespace usdx { @@ -42,20 +43,11 @@ namespace usdx this->vertices[6] = 0.0f; this->vertices[7] = 0.0f; - this->texture[0] = 0.0f; - this->texture[1] = 1.0f; - this->texture[2] = 1.0f; - this->texture[3] = 1.0f; - this->texture[4] = 1.0f; - this->texture[5] = 0.0f; - this->texture[6] = 0.0f; - this->texture[7] = 0.0f; - set_position(0, 0); set_size(40, 40); - texture_normal = new Texture("game/themes/Deluxe/interface/cursor.png"); - texture_pressed = new Texture("game/themes/Deluxe/interface/cursor_pressed.png"); + texture_normal = new Disposer(new Texture("game/themes/Deluxe/interface/cursor.png")); + texture_pressed = new Disposer(new Texture("game/themes/Deluxe/interface/cursor_pressed.png")); mouse_move_connection = event_manager->mouse_move.connect( boost::bind(&SoftwareMousePointer::on_mouse_move, this, _1, _2)); @@ -83,8 +75,7 @@ namespace usdx return; } - glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, texture_normal->get_texture()); + Activator a(texture_normal->get()); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); @@ -92,7 +83,7 @@ namespace usdx glVertexPointer(2, GL_FLOAT, 0, vertices); glColorPointer(4, GL_UNSIGNED_BYTE, 0, color.get_array(4)); - glTexCoordPointer(2, GL_FLOAT, 0, texture); + glTexCoordPointer(2, GL_FLOAT, 0, Texture::default_vertices); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -104,8 +95,6 @@ namespace usdx glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_COLOR_ARRAY); - - glDisable(GL_TEXTURE_2D); } void SoftwareMousePointer::on_mouse_move(int x, int y) diff --git a/src/menu/software_mouse_pointer.hpp b/src/menu/software_mouse_pointer.hpp index bb4db608..2706e335 100644 --- a/src/menu/software_mouse_pointer.hpp +++ b/src/menu/software_mouse_pointer.hpp @@ -31,6 +31,7 @@ #include "texture.hpp" #include "timer.hpp" #include "utils/rgba_color.hpp" +#include "utils/disposer.hpp" #include #include @@ -41,11 +42,10 @@ namespace usdx { private: GLfloat vertices[8]; - GLfloat texture[8]; RgbaColor color; - Texture* texture_normal; - Texture* texture_pressed; + Disposer* texture_normal; + Disposer* texture_pressed; Timer* fade_inactive; diff --git a/src/menu/texture.cpp b/src/menu/texture.cpp index bae6dc2c..4ad0c39e 100644 --- a/src/menu/texture.cpp +++ b/src/menu/texture.cpp @@ -29,13 +29,24 @@ namespace usdx log4cpp::Category& Texture::log = log4cpp::Category::getInstance("usdx.base.texture"); + const GLfloat Texture::default_vertices[] = { 0.0f, 1.0f, + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.0f, 0.0f }; + Texture::Texture(boost::filesystem::wpath filename) : filename(filename), texture(0), size(0, 0), rotation(0) { - Image image(filename); + } - size.set_width(image.get_surface()->w); - size.set_height(image.get_surface()->h); + Texture::~Texture() + { + } + + void Texture::gl_initialize() + { + Image image(filename); + size = image.get_size(); // // Check that the image dimensions are a power of 2 // if ((image.get_surface()->w & (image.get_surface()->w - 1)) != 0 ) { @@ -98,15 +109,26 @@ namespace usdx image.get_surface()->pixels); } - Texture::~Texture() + void Texture::gl_cleanup() { if (glIsTexture(texture)) { glDeleteTextures(1, &texture); } } - GLuint Texture::get_texture(void) const + void Texture::enable(void) + { + GlDelayedAllocation::enable(); + + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, texture); + + } + + void Texture::disable(void) { - return texture; + GlDelayedAllocation::disable(); + + glDisable(GL_TEXTURE_2D); } }; diff --git a/src/menu/texture.hpp b/src/menu/texture.hpp index 12a32e46..9dfa2fa1 100644 --- a/src/menu/texture.hpp +++ b/src/menu/texture.hpp @@ -34,6 +34,7 @@ #include "utils/rectangle.hpp" #include "utils/image.hpp" #include "drawable.hpp" +#include "gl_delayed_allocation.hpp" namespace usdx { @@ -64,7 +65,7 @@ namespace usdx virtual ~TextureColorDepthException() {}; }; - class Texture + class Texture : public GlDelayedAllocation { private: static log4cpp::Category& log; @@ -78,11 +79,16 @@ namespace usdx float rotation; ///< radiant (0 - 2*pi) + virtual void gl_initialize(); + virtual void gl_cleanup(); public: Texture(boost::filesystem::wpath filename); virtual ~Texture(); - GLuint get_texture(void) const; + const static GLfloat default_vertices[]; + + virtual void enable(void); + virtual void disable(void); }; }; -- cgit v1.2.3