]> code.bitgloo.com Git - clyne/gamedev2.git/commitdiff
Textures can now load from lua tables as well as strings
authorAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 14 Sep 2019 06:50:32 +0000 (02:50 -0400)
committerAndy Belle-Isle <drumsetmonkey@gmail.com>
Sat, 14 Sep 2019 06:50:32 +0000 (02:50 -0400)
src/texture.cpp
src/texture.hpp

index d2b2627f364d26892068dd37c3c9d733545956e1..2870b9f78e4c4216c4e4f29a53ea56501122ee38 100644 (file)
 #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;
+        }
+    }
 }
index 8cfd68c640c06666808d49c53132f06d4a661f0c..3daebbd17afef816338e8a4a0853602d53b2f49e 100644 (file)
@@ -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>
 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_