]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Sprite sheets and flipping
authorAndy <drumsetmonkey@gmail.com>
Fri, 6 Jan 2017 13:46:14 +0000 (08:46 -0500)
committerAndy <drumsetmonkey@gmail.com>
Fri, 6 Jan 2017 13:46:14 +0000 (08:46 -0500)
assets/NPC_Sheet.png [new file with mode: 0644]
include/components.hpp
src/components.cpp
src/world.cpp
xml/entities.xml

diff --git a/assets/NPC_Sheet.png b/assets/NPC_Sheet.png
new file mode 100644 (file)
index 0000000..5537b5e
Binary files /dev/null and b/assets/NPC_Sheet.png differ
index 5c067dd24717c80241b2d75ede544d634a95686c..ed9a13606097cab35d8ba6b3c2068f76f7365b28 100644 (file)
@@ -127,10 +127,11 @@ struct Solid {
 struct SpriteData {
        
        SpriteData(std::string path, vec2 off):
-               offset(off) {
+               offset(off)     {
                        tex = Texture(path);
                        size = tex.getDim();
-                       
+                       offset = vec2(0.0f, 0.0f);
+
                        size_tex = vec2(1.0, 1.0);
                        
                        offset_tex.x = offset.x/size.x;
@@ -155,13 +156,14 @@ struct SpriteData {
        
        vec2 offset_tex;
        vec2 size_tex;
+
+       uint limb;
 };
 
 using Frame = std::vector<std::pair<SpriteData, vec2>>;
 
 std::vector<Frame> developFrame(XMLElement*);
 
-//TODO
 /**
  * @struct Sprite
  * @brief If an entity is visible we want to be able to see it.
@@ -202,30 +204,33 @@ struct Sprite {
        }
 
        vec2 getSpriteSize() {
-               vec2 st; /** the start location of the sprite */
+               vec2 st; /** the start location of the sprite (bottom left)*/
+               vec2 ed; /** the end ofthe location of the sprite (bottom right)*/
                vec2 dim; /** how wide the sprite is */
 
                if (sprite.size()) {
                        st.x = sprite[0].second.x;
                        st.y = sprite[0].second.y;
+
+                       ed.x = sprite[0].second.x + sprite[0].first.size.x;
+                       ed.y = sprite[0].second.y + sprite[0].first.size.y;
                } else {
                        return vec2(0.0f, 0.0f);
                }
 
                for (auto &s : sprite) {
-                       const auto& size = s.first.tex.getDim();
-
                        if (s.second.x < st.x)
                                st.x = s.second.x;
                        if (s.second.y < st.y)
                                st.y = s.second.y;
 
-                       if (s.second.x + size.x > dim.x)
-                               dim.x = s.second.x + size.x;
-                       if (s.second.y + size.y > dim.y)
-                               dim.y = s.second.y + size.y;
+                       if (s.second.x + s.first.size.x > ed.x)
+                               ed.x = s.second.x + s.first.size.x;
+                       if (s.second.y + s.first.size.y > ed.y)
+                               ed.y = s.second.y + s.first.size.y;
                }
 
+               dim = vec2(ed.x - st.x, ed.y - st.y);
                return dim;
        }
 
@@ -233,10 +238,12 @@ struct Sprite {
        bool faceLeft;
 };
 
+using Limb = std::vector<std::pair<uint, Frame>>;
+
 //TODO
 struct Animate {
        // COMMENT
-       std::vector<std::pair<uint, Frame>> frame;
+       Limb frame;
        // COMMENT      
        uint index;
 
index 7322208386fa64ed8108bdf4c6913e4b6eb18260..05f4714552d178b5df6a2f0fb04b25e2a011c707 100644 (file)
@@ -80,9 +80,21 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
        Render::worldShader.use();
 
        en.each<Visible, Sprite, Position>([dt](entityx::Entity entity, Visible &visible, Sprite &sprite, Position &pos) {
-               (void)entity;
                // Verticies and shit
                float its = 0;
+               
+               float sz;
+               if (entity.has_component<Solid>()) {
+                       sz = entity.component<Solid>()->width;
+               }
+               if(sprite.faceLeft) {
+                       glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(-1.0f,1.0f,1.0f));
+                       glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f - sz - pos.x * 2.0f, 0.0f, 0.0f));
+
+                       glm::mat4 mov = scale * translate;
+                       glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(mov));
+               }
+               
                for (auto &S : sprite.sprite) {
                        auto sp = S.first;
                        auto size = sp.size * game::HLINE;
@@ -97,13 +109,6 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
                                                                   sp.offset_tex.x,                             sp.offset_tex.y + sp.size_tex.y,
                                                                   sp.offset_tex.x,                             sp.offset_tex.y};
 
-                       if(sprite.faceLeft) {
-                               glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(-1.0f,1.0f,1.0f));
-                               glm::mat4 translate = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f - size.x - pos.x * 2.0f, 0.0f, 0.0f));
-
-                               glm::mat4 mov = scale * translate;
-                               glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(mov));
-                       }
 
                        GLfloat coords[] = {loc.x,                      loc.y,                  visible.z + its,
                                                                loc.x + size.x, loc.y,                  visible.z + its,
@@ -130,11 +135,11 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
                        glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coord);
                        glDrawArrays(GL_TRIANGLES, 0, 6);
 
-                       glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(glm::mat4(1.0f)));
                        glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, 1.0, 1.0, 1.0);
                        
                        its-=.01;
                }
+               glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(glm::mat4(1.0f)));
        });
 
        Render::worldShader.disable();
index 6b1854207bd4e22436c610e5290876af10bbb9e3..a26a4faa74139cee5ad5652bd935077313d65a22 100644 (file)
@@ -338,9 +338,9 @@ void WorldSystem::load(const std::string& file)
                                                if (abcd->Attribute("value") != nullptr)
                                                        dim = str2coord(abcd->StrAttribute("value"));
                                                else
-                                                       dim = entity.component<Sprite>().get()->getSpriteSize() * game::HLINE;
+                                                       dim = entity.component<Sprite>()->getSpriteSize();
 
-                                               float cdat[2] = {dim.x, dim.y};
+                                               float cdat[2] = {dim.x * game::HLINE, dim.y * game::HLINE};
                                                entity.assign<Solid>(cdat[0], cdat[1]);
                                        } else if (tname == "Direction") {
                                                vec2 dir;
@@ -382,13 +382,17 @@ void WorldSystem::load(const std::string& file)
                                                while (animx) { 
                                                        std::string animType = animx->Name();
                                                        if (animType == "movement") {
-                                                               auto frames = developFrame(animx);
-                                                               if (animx->UnsignedAttribute("changeID") != XML_NO_ERROR)
-                                                                       idtc = 0;
-                                                               else 
-                                                                       idtc = animx->UnsignedAttribute("changeID");
-                                                               for (uint i = 0; i < frames.size(); i++) {
-                                                                       entan->frame.push_back(std::make_pair(idtc, frames[i]));
+                                                               auto limbx = animx->FirstChildElement();
+                                                               while (limbx) {
+                                                                       auto frames = developFrame(limbx);
+                                                                       if (limbx->UnsignedAttribute("changeID") != XML_NO_ERROR)
+                                                                               idtc = 0;
+                                                                       else 
+                                                                               idtc = limbx->UnsignedAttribute("changeID");
+                                                                       for (uint i = 0; i < frames.size(); i++) {
+                                                                               entan->frame.push_back(std::make_pair(idtc, frames[i]));
+                                                                       }
+                                                               limbx = limbx->NextSiblingElement();
                                                                }
                                                        }
                                                        
index e1d75498bec146e16f3c3394b91749f487f291c1..23b93587025fee5a63feb655809505af3fc3db43 100644 (file)
@@ -3,21 +3,22 @@
 <npc>
        <Position value="0.0,100.0" />
        <Visible value="0.2" />
-       <Sprite> image="assets/NPC.png"
+       <Sprite>
                <frame>
-                       <src id="0" offset="0,0" size="15,20" drawOffset="0,0">assets/NPC.png</src>
-                       <src id="1" offset="0,21" size="15,9" drawOffset="-1,14">assets/NPC.png</src>
-                       <src id="2" drawOffset="13,6">assets/items/ironSword.png</src>
+                       <src limb="0" offset="0,0" size="13,15" drawOffset="0,5">assets/NPC_Sheet.png</src>
+                       <src limb="1" offset="13,0" size="9,6" drawOffset="3,0">assets/NPC_Sheet.png</src>
                </frame>
        </Sprite>       
        <Animation>
-               <movement update="1" changeId="1">
-                       <frame>
-                               <src offset="0,21" size="15,9" drawOffset="0,13">assets/NPC.png</src>           
-                       </frame>
-                       <frame>
-                               <src offset="0,21" size="15,9" drawOffset="0,13">assets/NPC_Walk.png</src>
-                       </frame>
+               <movement>
+                       <limb update="1" id="0">
+                               <frame>
+                                       <src offset="0,21" size="15,9" drawOffset="0,13">assets/NPC.png</src>           
+                               </frame>
+                               <frame>
+                                       <src offset="0,21" size="15,9" drawOffset="0,13">assets/NPC_Walk.png</src>
+                               </frame>
+                       </limb>
                </movement>
        </Animation>
        <Direction />