diff --git a/lua/builder_test.lua b/lua/builder_test.lua index 5376677..47c6b42 100644 --- a/lua/builder_test.lua +++ b/lua/builder_test.lua @@ -10,7 +10,7 @@ config{ slide{ h1 "Here's Mine", image{ - src="test.jpg" + src="assets/test.jpg" } } diff --git a/src/document.hpp b/src/document.hpp index 021db7d..1216d48 100644 --- a/src/document.hpp +++ b/src/document.hpp @@ -4,6 +4,8 @@ #include #include #include +#include +#include "image.hpp" enum class NodeType { TEXT, @@ -28,6 +30,7 @@ struct Node { NodeType type = NodeType::ERROR; std::string content = ""; std::unordered_map config{}; + std::shared_ptr image = nullptr; }; struct Slide { diff --git a/src/image.cpp b/src/image.cpp index cb42c94..68619bb 100644 --- a/src/image.cpp +++ b/src/image.cpp @@ -1,6 +1,18 @@ #include "image.hpp" #include +// these never change so just put them here +float VERTICES[24] = { + // positions // texCoords + -1.0f, 1.0f, 0.0f, 1.0f, // top left + -1.0f, -1.0f, 0.0f, 0.0f, // bottom left + 1.0f, -1.0f, 1.0f, 0.0f, // bottom right + + -1.0f, 1.0f, 0.0f, 1.0f, // top left + 1.0f, -1.0f, 1.0f, 0.0f, // bottom right + 1.0f, 1.0f, 1.0f, 1.0f, // top right +}; + unsigned int image_to_texture(unsigned char *data, int width, int height, int nrComponents) { unsigned int textureID; glGenTextures(1, &textureID); @@ -31,33 +43,17 @@ unsigned int image_to_texture(unsigned char *data, int width, int height, int nr return textureID; } -void Image::load(Node& node) { - dbc::check(node.type == NodeType::IMAGE, "That's not an image idiot."); - - if(!node.config.contains("_image_data")) { - dbc::check(node.config.contains("src"), "Node doesn't have an image src."); - std::string filename = std::any_cast(node.config.at("src")); - - int width = 0; - int height = 0; - int nrComponents = 0; - stbi_set_flip_vertically_on_load(true); - unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0); - dbc::check(data != NULL, "Failed to stb_load image."); - - unsigned int texture_id = image_to_texture(data, width, height, nrComponents); +void Image::load(const std::string& filename) { + stbi_set_flip_vertically_on_load(true); + unsigned char *data = stbi_load(filename.c_str(), &width, &height, &nrComponents, 0); + dbc::check(data != NULL, "Failed to stb_load image."); - node.config.try_emplace("_image_data", texture_id); + texture_id = image_to_texture(data, width, height, nrComponents); - stbi_image_free(data); - } + stbi_image_free(data); } -void Image::render(Node& node, float xpos, float ypos, float w, float h) { - dbc::check(node.config.contains("_image_data"), "Node isn't configured for images, did you Image::load?"); - - unsigned int texture_id = std::any_cast(node.config.at("_image_data")); - +void Image::render() { shader.use(); shader.setInt("texture1", texture_id); glBindVertexArray(image_vao); @@ -71,7 +67,7 @@ Image::Image() { glBindVertexArray(image_vao); glBindBuffer(GL_ARRAY_BUFFER, image_vbo); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); + glBufferData(GL_ARRAY_BUFFER, sizeof(VERTICES), &VERTICES, GL_STATIC_DRAW); // position attrib glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), (void*)0); diff --git a/src/image.hpp b/src/image.hpp index 89c71f0..56a35e7 100644 --- a/src/image.hpp +++ b/src/image.hpp @@ -1,25 +1,18 @@ #pragma once -#include "document.hpp" #include "shader.hpp" struct Image { Shader shader{{"shaders/image.vert.glsl", "shaders/image.frag.glsl", ""}}; unsigned int image_vao = UINT_MAX; unsigned int image_vbo = UINT_MAX; - - float vertices[24] = { - // positions // texCoords - -0.5f, 0.5f, 0.0f, 1.0f, // top left - -0.5f, -0.5f, 0.0f, 0.0f, // bottom left - 0.5f, -0.5f, 1.0f, 0.0f, // bottom right - - -0.5f, 0.5f, 0.0f, 1.0f, // top left - 0.5f, -0.5f, 1.0f, 0.0f, // bottom right - 0.5f, 0.5f, 1.0f, 1.0f, // top right - }; + unsigned int texture_id = UINT_MAX; + int width = 0; + int height = 0; + int nrComponents = 0; + // scale, rotate, position Image(); - void load(Node& node); - void render(Node& node, float xpos, float ypos, float w, float h); + void load(const std::string& filename); + void render(); }; diff --git a/src/render.cpp b/src/render.cpp index 65919e1..f5d33da 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -31,10 +31,13 @@ void Engine::render() { if(node.type == NodeType::TEXT || node.type == NodeType::H1 || node.type == NodeType::UL) { text.render(node.content, 25.0f, SCR_HEIGHT - y, 1.0f, glm::vec3(1.0f, 1.0f, 1.0f)); } else if(node.type == NodeType::IMAGE) { - Image img; - img.load(node); - img.render(node, -1.0f, -1.0f, 1.0f, 1.0f); - // it's an image, skip it + if(node.image == nullptr) { + node.image = std::make_shared(); + std::string src = std::any_cast(node.config.at("src")); + node.image->load(src); + } + + node.image->render(); } else { dbc::sentinel("Unknown NodeType"); }