summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorVegard Storheil Eriksen <zyp@jvnv.net>2011-01-28 22:08:37 +0100
committerVegard Storheil Eriksen <zyp@jvnv.net>2011-01-28 22:08:37 +0100
commitf84b25fc8e0619ad24974c4dc520449827f8d193 (patch)
tree71c623805bbb10b8e06a44ee40176e304a42f62f
parent7ebb0e91f63203753420b5a4b75d09d170f204f4 (diff)
Split out image loading from Texture into Image.
-rw-r--r--image.cpp50
-rw-r--r--image.h27
-rw-r--r--texture.cpp26
-rw-r--r--texture.h8
4 files changed, 90 insertions, 21 deletions
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 <stdint.h>
+#include <string>
+
+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 <stdexcept>
#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 <stdint.h>
-#include <string>
+#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;