aboutsummaryrefslogtreecommitdiffstats
path: root/src/imageloader.cpp
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-04-04 12:19:41 +0100
committerYann Herklotz <ymherklotz@gmail.com>2017-04-04 12:19:41 +0100
commit55a1e0ad7c9d2661c266b2e767bfcb2f944e859f (patch)
treefe3ac90ab853e2f87bd33294976a3fc56e144988 /src/imageloader.cpp
parentaa67c8bb56cb750ac83ecbd361439f5ecb5e12d9 (diff)
downloadYAGE-55a1e0ad7c9d2661c266b2e767bfcb2f944e859f.tar.gz
YAGE-55a1e0ad7c9d2661c266b2e767bfcb2f944e859f.zip
Adding spritebatch class
Diffstat (limited to 'src/imageloader.cpp')
-rw-r--r--src/imageloader.cpp45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/imageloader.cpp b/src/imageloader.cpp
new file mode 100644
index 00000000..8325b7f6
--- /dev/null
+++ b/src/imageloader.cpp
@@ -0,0 +1,45 @@
+#include "imageloader.hpp"
+#include "iomanager.hpp"
+#include "picopng.hpp"
+
+#include <stdexcept>
+
+namespace yage
+{
+
+GlTexture ImageLoader::loadPng(const std::string &file_path)
+{
+ GlTexture texture = {};
+
+ std::vector<unsigned char> in;
+ std::vector<unsigned char> out;
+ unsigned long width, height;
+
+ if(!IoManager::readFileToBuffer(file_path, in))
+ throw std::runtime_error("Failed to load '"+file_path+"' to buffer");
+
+ int error_code = decodePNG(out, width, height, &in[0], in.size());
+ if(error_code != 0)
+ throw std::runtime_error("Failed to load '"+file_path+"' to png with error code"+std::to_string(error_code));
+
+ glGenTextures(1, &texture.id);
+
+ glBindTexture(GL_TEXTURE_2D, texture.id);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, &out[0]);
+
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
+
+ glGenerateMipmap(GL_TEXTURE_2D);
+
+ glBindTexture(GL_TEXTURE_2D, 0);
+
+ texture.width = (int)width;
+ texture.height = (int)height;
+
+ return texture;
+}
+
+} // yage