aboutsummaryrefslogtreecommitdiffstats
path: root/yage/core/imageloader.cpp
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-10-31 22:11:18 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-10-31 22:11:18 +0000
commitf776df6076725d14679b31168e3ede53c966182e (patch)
treeb843d7ef0d0722a1b3571ee09aa1345d19c60e7f /yage/core/imageloader.cpp
parent1bb0ef8960c71ef505a351702bec54c01ba15e22 (diff)
downloadYAGE-f776df6076725d14679b31168e3ede53c966182e.tar.gz
YAGE-f776df6076725d14679b31168e3ede53c966182e.zip
renaming base folder
Diffstat (limited to 'yage/core/imageloader.cpp')
-rw-r--r--yage/core/imageloader.cpp64
1 files changed, 64 insertions, 0 deletions
diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp
new file mode 100644
index 00000000..e1d54dc1
--- /dev/null
+++ b/yage/core/imageloader.cpp
@@ -0,0 +1,64 @@
+/* ----------------------------------------------------------------------------
+ * imageloader.cpp
+ *
+ * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
+ * See file LICENSE for more details
+ * ----------------------------------------------------------------------------
+ */
+
+#include "imageloader.h"
+
+#ifndef UNIT_TESTS
+#include <glad/glad.h>
+#endif
+
+#include <yage/base/iomanager.h>
+#include <yage/base/picopng.h>
+
+#include <iostream>
+#include <stdexcept>
+
+namespace yage
+{
+
+Texture ImageLoader::loadPng(const std::string &file_path)
+{
+ 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));
+ }
+
+ Texture texture(0, static_cast<int>(width), static_cast<int>(height));
+ std::cout << "Geometry: " << texture.width << "x" << texture.height << "\n";
+
+ 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);
+
+ return texture;
+}
+
+} // namespace yage