aboutsummaryrefslogtreecommitdiffstats
path: root/yage/core
diff options
context:
space:
mode:
Diffstat (limited to 'yage/core')
-rw-r--r--yage/core/imageloader.cpp36
-rw-r--r--yage/core/window.cpp11
2 files changed, 25 insertions, 22 deletions
diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp
index 7b34312d..5a60e83b 100644
--- a/yage/core/imageloader.cpp
+++ b/yage/core/imageloader.cpp
@@ -8,10 +8,10 @@
#include "imageloader.h"
#include "../data/texture.h"
+#include "logger.h"
+#include "stb_image.h"
#include <glad/glad.h>
-#include <yage/core/iomanager.h>
-#include <yage/core/picopng.h>
#include <iostream>
#include <stdexcept>
@@ -21,30 +21,22 @@ 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));
- }
+ int width, height, num_channels;
+ unsigned char *data =
+ stbi_load(file_path.c_str(), &width, &height, &num_channels, 0);
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]);
+
+ if (num_channels == 4) {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
+ GL_UNSIGNED_BYTE, data);
+ } else {
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGB,
+ GL_UNSIGNED_BYTE, data);
+ }
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
@@ -54,7 +46,7 @@ Texture ImageLoader::loadPng(const std::string &file_path)
glGenerateMipmap(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, 0);
+ yLog << "Successfully loaded texture: " << file_path;
return texture;
}
diff --git a/yage/core/window.cpp b/yage/core/window.cpp
index 5db75074..711e1294 100644
--- a/yage/core/window.cpp
+++ b/yage/core/window.cpp
@@ -19,11 +19,20 @@ using std::runtime_error;
namespace yage
{
+namespace {
+
void key_callback(GLFWwindow *window, int key, int scanCode, int action,
int mods)
{
}
+void framebuffer_size_callback(GLFWwindow *window, int width, int height)
+{
+ glViewport(0, 0, width, height);
+}
+
+} // namespace
+
Window::Window() = default;
Window::~Window()
@@ -56,6 +65,8 @@ void Window::create(std::string window_name, int width, int height)
// set key callback
glfwSetKeyCallback(window_, key_callback);
+ // set resize callback
+ glfwSetFramebufferSizeCallback(window_, framebuffer_size_callback);
// set vsync on
glfwSwapInterval(1);