diff options
author | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-10-05 07:00:04 +0200 |
---|---|---|
committer | Vegard Storheil Eriksen <zyp@jvnv.net> | 2010-10-05 07:00:04 +0200 |
commit | f88411abc7150ed209efcb6180503f0c6f7c8ef6 (patch) | |
tree | 3c5b38c16a125ad26e6aef74c8a8c5cb85d43205 | |
parent | 4e5f45f47fa3568f6b081d56f9e7119367fc775c (diff) |
Moved texture loading into base Texture class.
-rw-r--r-- | texture.cpp | 27 | ||||
-rw-r--r-- | texture.h | 6 | ||||
-rw-r--r-- | texturestbi.cpp | 20 | ||||
-rw-r--r-- | texturestbi.h | 8 |
4 files changed, 32 insertions, 29 deletions
diff --git a/texture.cpp b/texture.cpp index 4c9cb6d..ba7d9c7 100644 --- a/texture.cpp +++ b/texture.cpp @@ -1,7 +1,33 @@ #include <stdexcept> #include "texture.h" +#include "stb_image.h" #include "gl.h" +Texture::Texture() { + glGenTextures(1, &texture); +} + +Texture::Texture(const std::string& filename) { + glGenTextures(1, &texture); + load(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::bind() const { glBindTexture(GL_TEXTURE_2D, texture); } @@ -26,7 +52,6 @@ void Texture::build(void* data, unsigned int format, unsigned int w, unsigned in width = w; height = h; - glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); @@ -2,9 +2,15 @@ #define _TEXTURE_H_ #include <stdint.h> +#include <string> class Texture { public: + Texture(); + Texture(const std::string& filename); + + void load(const std::string& filename); + void bind() const; unsigned int tex() const; unsigned int w() const; diff --git a/texturestbi.cpp b/texturestbi.cpp deleted file mode 100644 index abd802e..0000000 --- a/texturestbi.cpp +++ /dev/null @@ -1,20 +0,0 @@ -#include <stdexcept> -#include <SFML/Window/OpenGL.hpp> -#include "texturestbi.h" -#include "stb_image.h" - -TextureSTBI::TextureSTBI(const char* filename) { - int w, h, ch; - uint8_t* data = stbi_load(filename, &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); -} diff --git a/texturestbi.h b/texturestbi.h deleted file mode 100644 index ebb6d9d..0000000 --- a/texturestbi.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef _TEXTURESOIL_H_ -#define _TEXTURESOIL_H_ -#include "texture.h" -class TextureSTBI : public Texture { - public: - TextureSTBI(const char* filename); -}; -#endif // _TEXTURESOIL_H_ |