diff options
author | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-09-14 02:50:32 -0400 |
---|---|---|
committer | Andy Belle-Isle <drumsetmonkey@gmail.com> | 2019-09-14 02:50:32 -0400 |
commit | 9583f32bc760576d250e78a79a812ec95ebd0f8e (patch) | |
tree | 3d004daa972d11dba3518c9439c8c949913dbf48 /src | |
parent | 0a4352f7422030a2fef71e90d81d09a184883622 (diff) |
Textures can now load from lua tables as well as strings
Diffstat (limited to 'src')
-rw-r--r-- | src/texture.cpp | 68 | ||||
-rw-r--r-- | src/texture.hpp | 11 |
2 files changed, 70 insertions, 9 deletions
diff --git a/src/texture.cpp b/src/texture.cpp index d2b2627..2870b9f 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -24,12 +24,21 @@ #include <unordered_map> #include <iostream> +struct TextureData +{ + GLuint tex = 0; + int width = 0; + int height= 0; +}; + // Stores a list of all textures we've already loaded. This makes sure we don't // waste our precious CPU cycles reloading a texture. -std::unordered_map<std::string, GLuint> textureCache; +std::unordered_map<std::string, TextureData> textureCache; -Texture::Texture(std::string filename) +TextureData loadTexture(std::string filename) { + TextureData tex; + // Search to see if this texture has already been loading auto cacheSearch = textureCache.find(filename); @@ -37,17 +46,17 @@ Texture::Texture(std::string filename) // If this texture hasn't been loading unsigned char* image = SOIL_load_image(filename.c_str(), - &width, &height, 0, + &(tex.width), &(tex.height), 0, SOIL_LOAD_RGBA); - glGenTextures(1, &tex); - glBindTexture(GL_TEXTURE_2D, tex); + glGenTextures(1, &(tex.tex)); + glBindTexture(GL_TEXTURE_2D, tex.tex); glPixelStoref(GL_UNPACK_ALIGNMENT, 1); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tex.width, tex.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image); SOIL_free_image_data(image); @@ -59,4 +68,51 @@ Texture::Texture(std::string filename) // If this texture has been loaded, just return the loaded texture tex = cacheSearch->second; } + + return tex; +} + +void Texture::loadFromString(std::string filename) +{ + TextureData d = loadTexture(filename); + + tex = d.tex; + width = d.width; + height = d.height; +} + +Texture::Texture(std::string filename) +{ + loadFromString(filename); +} + +Texture::Texture(sol::object param) +{ + if (param.get_type() == sol::type::string) { + loadFromString(param.as<std::string>()); + } else if (param.get_type() == sol::type::table) { + sol::table tab = param; + + // If there is a filename given, load that file to get image data + if (tab["file"] == sol::type::string) + loadFromString(tab.get<std::string>("file")); + else + return; // If we don't have image data just return a null image + + if (tab["offset"] == sol::type::table) { + sol::table off = tab["offset"]; + if (off["x"] == sol::type::number) + offset.x = off.get<float>("x")/width; + if (off["y"] == sol::type::number) + offset.y = off.get<float>("y")/height; + } + + if (tab["size"] == sol::type::table) { + sol::table siz = tab["size"]; + if (siz["x"] == sol::type::number) + size.x = siz.get<float>("x")/width; + if (siz["y"] == sol::type::number) + size.y = siz.get<float>("y")/height; + } + } } diff --git a/src/texture.hpp b/src/texture.hpp index 8cfd68c..3daebbd 100644 --- a/src/texture.hpp +++ b/src/texture.hpp @@ -22,6 +22,8 @@ #include <soil/SOIL.h> +#include <sol/sol.hpp> + #include <GL/glew.h> #include <SDL2/SDL_opengl.h> #include <glm/glm.hpp> @@ -31,16 +33,19 @@ class Texture { private: + void loadFromString(std::string); public: GLuint tex = 0; - int width; - int height; glm::vec2 offset = glm::vec2(0); - glm::vec2 offsetSize = glm::vec2(1); + glm::vec2 size = glm::vec2(1); + + int width; + int height; Texture() {}; Texture(std::string); + Texture(sol::object); }; #endif//TEXTURE_HPP_ |