From f84b25fc8e0619ad24974c4dc520449827f8d193 Mon Sep 17 00:00:00 2001 From: Vegard Storheil Eriksen Date: Fri, 28 Jan 2011 22:08:37 +0100 Subject: Split out image loading from Texture into Image. --- image.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ image.h | 27 +++++++++++++++++++++++++++ texture.cpp | 26 +++++++++----------------- texture.h | 8 ++++---- 4 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 image.cpp create mode 100644 image.h diff --git a/image.cpp b/image.cpp new file mode 100644 index 0000000..db385db --- /dev/null +++ b/image.cpp @@ -0,0 +1,50 @@ +#include "image.h" + +#include "gl.h" + +#include "stb_image.h" + +Image::Image() { + data = 0; +} + +Image::Image(const std::string& filename) { + load(filename); +} + +Image::~Image() { + if(data) { + stbi_image_free(data); + } +} + +void Image::load(const std::string& filename) { + int w, h, ch; + data = stbi_load(filename.c_str(), &w, &h, &ch, 4); + + width = w; + height = h; + + unsigned int format; + if(ch == 4) { + format = GL_RGBA; + } else { + format = GL_RGB; + } +} + +unsigned int Image::w() const { + return width; +} + +unsigned int Image::h() const { + return height; +} + +unsigned int Image::f() const { + return format; +} + +const void* Image::d() const { + return data; +} diff --git a/image.h b/image.h new file mode 100644 index 0000000..41b0ed2 --- /dev/null +++ b/image.h @@ -0,0 +1,27 @@ +#ifndef IMAGE_H +#define IMAGE_H + +#include +#include + +class Image { + public: + Image(); + Image(const std::string& filename); + ~Image(); + + void load(const std::string& filename); + + unsigned int w() const; + unsigned int h() const; + unsigned int f() const; + const void* d() const; + + protected: + unsigned int width; + unsigned int height; + unsigned int format; + void* data; +}; + +#endif diff --git a/texture.cpp b/texture.cpp index ba7d9c7..df84961 100644 --- a/texture.cpp +++ b/texture.cpp @@ -1,31 +1,23 @@ #include #include "texture.h" -#include "stb_image.h" #include "gl.h" Texture::Texture() { glGenTextures(1, &texture); } +Texture::Texture(const Image& image) { + glGenTextures(1, &texture); + load(image); +} + Texture::Texture(const std::string& filename) { glGenTextures(1, &texture); - load(filename); + load(Image(filename)); } -void Texture::load(const std::string& filename) { - int w, h, ch; - uint8_t* data = stbi_load(filename.c_str(), &w, &h, &ch, 4); - - unsigned int format; - if(ch == 4) { - format = GL_RGBA; - } else { - format = GL_RGB; - } - - build(data, format, w, h); - - stbi_image_free(data); +void Texture::load(const Image& image) { + build(image.d(), image.f(), image.w(), image.h()); } void Texture::bind() const { @@ -44,7 +36,7 @@ unsigned int Texture::h() const { return height; } -void Texture::build(void* data, unsigned int format, unsigned int w, unsigned int h) { +void Texture::build(const void* data, unsigned int format, unsigned int w, unsigned int h) { if(!data) { throw(std::runtime_error("No texture data")); } diff --git a/texture.h b/texture.h index 9fea06f..e63feb2 100644 --- a/texture.h +++ b/texture.h @@ -1,22 +1,22 @@ #ifndef _TEXTURE_H_ #define _TEXTURE_H_ -#include -#include +#include "image.h" class Texture { public: Texture(); + Texture(const Image& image); Texture(const std::string& filename); - void load(const std::string& filename); + void load(const Image& image); void bind() const; unsigned int tex() const; unsigned int w() const; unsigned int h() const; protected: - void build(void* data, unsigned int format, unsigned int w, unsigned int h); + void build(const void* data, unsigned int format, unsigned int w, unsigned int h); unsigned int width; unsigned int height; unsigned int texture; -- cgit v1.2.3