From cf0deda5f30eb3bf6b4ea6a1d47aa7dad115b799 Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 6 Jan 2017 08:46:14 -0500 Subject: [PATCH] Sprite sheets and flipping --- assets/NPC_Sheet.png | Bin 0 -> 531 bytes include/components.hpp | 29 ++++++++++++++++++----------- src/components.cpp | 23 ++++++++++++++--------- src/world.cpp | 22 +++++++++++++--------- xml/entities.xml | 23 ++++++++++++----------- 5 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 assets/NPC_Sheet.png diff --git a/assets/NPC_Sheet.png b/assets/NPC_Sheet.png new file mode 100644 index 0000000000000000000000000000000000000000..5537b5e1601c356fa3c0bfac332082fcae0e4286 GIT binary patch literal 531 zcmV+u0_^>XP)00006VoOIv0RI60 z0RN!9r;`8x010qNS#tmY3ljhU3ljkVnw%H_000McNliru;Q<8?3kPk!uuuR103B&m zSad^gZEa<4bN~PV002XBWnpw>WFU8GbZ8()Nlj2>E@cM*00DPNL_t(I%f*#3OT$1E z$N#S_R?;aY9ZKd7E!0IET)Wk!2RhX(wpnx+6!c4olhmpZoI9l-z;EDpkV0GPAQctD zb1--0n$jvH_=m^47asiXasS+rWg6(+JwnP3eC+Jsu&f7z0oj3%rt4ucn}(M4t%s)Ega_C3f!pVs8lQoUJMd#0 zu}lMLh%D3KMZ-n)lHG$e8~{qvbeYL)PVP}#87qV+u*{($k;<9Z%UK8^P~QAR)Ah7X zNDX@ql05fZw5XS}0Dvv)E7sVk#NfPEdx*&4H5C>`H2cy6Op>; std::vector 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>; + //TODO struct Animate { // COMMENT - std::vector> frame; + Limb frame; // COMMENT uint index; diff --git a/src/components.cpp b/src/components.cpp index 7322208..05f4714 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -80,9 +80,21 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, Render::worldShader.use(); en.each([dt](entityx::Entity entity, Visible &visible, Sprite &sprite, Position &pos) { - (void)entity; // Verticies and shit float its = 0; + + float sz; + if (entity.has_component()) { + sz = entity.component()->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(); diff --git a/src/world.cpp b/src/world.cpp index 6b18542..a26a4fa 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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().get()->getSpriteSize() * game::HLINE; + dim = entity.component()->getSpriteSize(); - float cdat[2] = {dim.x, dim.y}; + float cdat[2] = {dim.x * game::HLINE, dim.y * game::HLINE}; entity.assign(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(); } } diff --git a/xml/entities.xml b/xml/entities.xml index e1d7549..23b9358 100644 --- a/xml/entities.xml +++ b/xml/entities.xml @@ -3,21 +3,22 @@ - image="assets/NPC.png" + - assets/NPC.png - assets/NPC.png - assets/items/ironSword.png + assets/NPC_Sheet.png + assets/NPC_Sheet.png - - - assets/NPC.png - - - assets/NPC_Walk.png - + + + + assets/NPC.png + + + assets/NPC_Walk.png + + -- 2.39.5