summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--texture.cpp27
-rw-r--r--texture.h6
-rw-r--r--texturestbi.cpp20
-rw-r--r--texturestbi.h8
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);
diff --git a/texture.h b/texture.h
index ca7fef1..9fea06f 100644
--- a/texture.h
+++ b/texture.h
@@ -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_