#include <SDL/SDL_image.h>
#include "Dispatcher.hh"
+#include "Log.hh"
#include "Mippleton.hh"
#include "OpenGL.hh"
#include "Texture.hh"
* objects and avoid having duplicate textures loaded to GL.
*/
-class Texture::TextureImpl : public Mippleton<TextureImpl>
+class Texture::Impl : public Mippleton<Impl>
{
/**
* to cache it if the client has plenty of RAM.
*/
- void contextRecreated(const Notification& note)
+ void contextRecreated(const Notification* note)
{
object_ = globalObject_ = 0;
uploadToGL();
* Construction is initialization.
*/
- explicit TextureImpl(const std::string& name) :
- Mippleton<TextureImpl>(name),
+ explicit Impl(const std::string& name) :
+ Mippleton<Impl>(name),
surface_(0),
width_(0),
height_(0),
loadFromFile();
// we want to know when the GL context is recreated
- Dispatcher::instance().addHandler("video.context_recreated",
- boost::bind(&TextureImpl::contextRecreated, this, _1), this);
+ Dispatcher::getInstance().addHandler("video.context_recreated",
+ boost::bind(&Impl::contextRecreated, this, _1), this);
}
- ~TextureImpl()
+ ~Impl()
{
if (surface_)
{
unloadFromGL();
- Dispatcher::instance().removeHandler(this);
+ Dispatcher::getInstance().removeHandler(this);
}
{
SDL_Surface* surface;
- surface = IMG_Load(Texture::getPathToResource(getName()).c_str());
+ surface = IMG_Load(Texture::getPath(getName()).c_str());
if (!surface)
{
- throw Texture::Exception("loading from file failed");
+ logWarning("texture not found: %s", getName().c_str());
+ throw Exception(Exception::FILE_NOT_FOUND);
}
SDL_Surface* temp = prepareImageForGL(surface);
if (!temp)
{
- throw Texture::Exception("uploading to opengl failed");
+ throw Exception(Exception::OPENGL_ERROR);
}
if (temp->format->BytesPerPixel == 3)
else
{
SDL_FreeSurface(temp);
- throw Texture::Exception("incompatible color mode");
+ throw Exception(Exception::BAD_IMAGE_FORMAT);
}
width_ = temp->w;
static GLuint globalObject_; ///< Global GL texture handle.
};
-GLuint Texture::TextureImpl::globalObject_ = 0;
+GLuint Texture::Impl::globalObject_ = 0;
Texture::Texture(const std::string& name) :
// pass through
- impl_(Texture::TextureImpl::retain(name), &Texture::TextureImpl::release)
-{}
+ impl_(Texture::Impl::getInstance(name)) {}
/**
* Bind the GL texture for mapping, etc.
*/
-void Texture::bind()
+void Texture::bind() const
{
// pass through
impl_->bind();
* Get the texture object, for the curious.
*/
-GLuint Texture::getObject()
+GLuint Texture::getObject() const
{
// pass through
return impl_->object_;
}
-unsigned Texture::getWidth()
+void Texture::resetBind()
+{
+ glBindTexture(GL_TEXTURE_2D, 0);
+ Impl::globalObject_ = 0;
+}
+
+
+unsigned Texture::getWidth() const
{
// pass through
return impl_->width_;
}
-unsigned Texture::getHeight()
+unsigned Texture::getHeight() const
{
// pass through
return impl_->height_;
}
-std::string Texture::getPathToResource(const std::string& name)
+std::string Texture::getPath(const std::string& name)
{
- // TODO named resources must be png for now
- return Resource::getPathToResource("textures/" + name + ".png");
+ std::string path = Resource::getPath("textures/" + name + ".png");
+ return path;
}