]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
Sprites flip and shit are good
authorAndy <drumsetmonkey@gmail.com>
Thu, 22 Dec 2016 15:50:27 +0000 (10:50 -0500)
committerAndy <drumsetmonkey@gmail.com>
Thu, 22 Dec 2016 15:50:27 +0000 (10:50 -0500)
13 files changed:
assets/NPC.png
assets/goodmouse.png [new file with mode: 0644]
assets/mouse.png
include/common.hpp
include/components.hpp
include/engine.hpp
main.cpp
src/common.cpp
src/components.cpp
src/engine.cpp
src/ui.cpp
src/world.cpp
xml/entities.xml

index 801ceb0649cc0851b4a5ad6f5e72be6ddb85bc5b..7c46dce49a8c108ed827d89202faebc6815b373f 100644 (file)
Binary files a/assets/NPC.png and b/assets/NPC.png differ
diff --git a/assets/goodmouse.png b/assets/goodmouse.png
new file mode 100644 (file)
index 0000000..72c4409
Binary files /dev/null and b/assets/goodmouse.png differ
index 9614317ae7f52f046f90e42b0d60a787066e2769..d4c082eaf57aa512fd8ef918a8adf597383038d1 100644 (file)
Binary files a/assets/mouse.png and b/assets/mouse.png differ
index 3caa083869842f82d82894c6065bceecffaaa47a..861905a33650a55b281cfa35fc50a757982e470f 100644 (file)
@@ -209,6 +209,14 @@ constexpr const float MSEC_PER_TICK = 1000.0f / TICKS_PER_SEC;
  */
 std::vector<std::string> StringTokenizer(const std::string& str, char delim);
 
+/**
+ * Seperates a string like, "23,12" to a vec2.
+ * 
+ * @param s the string to parse
+ * @return the vec2 of the values passed in the string
+ */
+vec2 str2coord(std::string s);
+
 /**
  * A function to draw a colored box for OpenGL.
  * To use it, the lower left hand and upper right hand coords are given.
index d630f83d0c9f223e504a2069c493218617708163..2a8680d1b22053c8084b7b615c30e2cf7b1b05ef 100644 (file)
@@ -130,11 +130,28 @@ struct SpriteData {
                offset(offset) {
                        pic = Texture::loadTexture(path);
                        size = Texture::imageDim(path);
-               }
+                       size_tex = vec2(1.0, 1.0);
+                       
+                       offset_tex.x = offset.x/size.x;
+                       offset_tex.y = offset.y/size.y;
+       }
+
+       SpriteData(std::string path, vec2 offset, vec2 size):
+               offset(offset), size(size) {
+                       pic = Texture::loadTexture(path);
+                       vec2 tmpsize = Texture::imageDim(path);
+
+                       size_tex.x = size.x/tmpsize.x;
+                       size_tex.y = size.y/tmpsize.y;
+                       offset_tex.x = offset.x/tmpsize.x;
+                       offset_tex.y = offset.y/tmpsize.y;
+       }
 
        GLuint pic;
        vec2 offset;
+       vec2 offset_tex;
        vec2 size;
+       vec2 size_tex;
 };
 
 using Frame = std::vector<std::pair<SpriteData, vec2>>;
@@ -214,7 +231,7 @@ struct Sprite {
 //TODO
 struct Animate {
        // COMMENT
-       std::vector<Frame> frame;
+       std::vector<std::pair<uint, Frame>> frame;
        // COMMENT      
        uint index;
 
@@ -223,17 +240,19 @@ struct Animate {
        }
 
        // COMMENT
-       Frame nextFrame() {
+       void nextFrame(Frame sprite) {
                if (index < frame.size() - 1) {
                        index++;
                } else {
                        index = 0;
                }
-               return frame.at(index);
+               auto fa = frame.at(index);
+               if (sprite.size() > fa.first-1)
+                       sprite.at(fa.first) = fa.second.at(fa.first);
        }
 
-       Frame firstFrame() {
-               return frame.front();
+       void firstFrame(Frame sprite) {
+               sprite = frame.at(0).second;
        }
 };
 
index c842b599333d9cce38e0c68e64d9dbd994f20083..1c5f5100ea7b45766de2661b1ef2b7733290412d 100644 (file)
@@ -20,6 +20,7 @@ public:
 
        void init(void);
     void render(entityx::TimeDelta dt);
+       void resetRender(entityx::TimeDelta dt);
        void update(entityx::TimeDelta dt);
 
        template<typename T>
index a3901a3dc3792bc491abbfa0dd6df5755b04f7b9..663173661e692ebb0aa4da0bd81e79158800cbb4 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -129,7 +129,7 @@ int main(int argc, char *argv[])
        //initInventorySprites();
 
        // load mouse texture, and other inventory textures
-       mouseTex = Texture::loadTexture("assets/mouse.png");
+       mouseTex = Texture::loadTexture("assets/goodmouse.png");
 
        // get a world
        if (xmlFolder.empty())
@@ -227,7 +227,7 @@ int main(int argc, char *argv[])
                const bool &run = game::engine.shouldRun;
                while (run) {
                        render();
-                       game::engine.render(0);
+                       game::engine.resetRender(0);
                }
 
                thMain.join();
@@ -307,6 +307,8 @@ void render() {
        // draw the player's inventory
        //player->inv->draw();
 
+       game::engine.render(0);
+       
        // draw the fade overlay
        ui::drawFade();
 
@@ -351,7 +353,7 @@ void render() {
                glActiveTexture(GL_TEXTURE0);
                glBindTexture(GL_TEXTURE_2D, mouseTex);
                Render::useShader(&Render::textShader);
-               Render::drawRect(ui::mouse, ui::mouse + 15, -9.9);
+               Render::drawRect(vec2(ui::mouse.x, ui::mouse.y - 64), vec2(ui::mouse.x + 64, ui::mouse.y), -9.9);
        Render::textShader.unuse();
 }
 
index 01a7db87f4f632f6edc37a09fccf861cf19a3d6e..098cd84157ccef01d17e94a07c4b29ed2202bf64 100644 (file)
@@ -22,6 +22,14 @@ unsigned int millis(void) {
        return std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
 }
 
+
+vec2 str2coord(std::string s)
+{
+       auto cpos = s.find(',');
+       s[cpos] = '\0';
+       return vec2 (std::stof(s), std::stof(s.substr(cpos + 1)));
+}
+
 std::vector<std::string> StringTokenizer(const std::string& str, char delim)
 {
        std::vector<std::string> tokens;
index 612e52249c2accecf41acb9814689dc12701d7ac..323dfc85008a8f8b1f38556b43956600819d10d8 100644 (file)
@@ -21,9 +21,9 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e
 
                if (entity.has_component<Animate>() && entity.has_component<Sprite>()) {
                        if (direction.x) {
-                               entity.component<Sprite>().get()->sprite = entity.component<Animate>().get()->nextFrame();
+                               entity.component<Animate>()->nextFrame(entity.component<Sprite>()->sprite);
                        } else {
-                               entity.component<Sprite>().get()->sprite = entity.component<Animate>().get()->firstFrame();
+                               entity.component<Animate>()->firstFrame(entity.component<Sprite>()->sprite);
                        }
                }
                if (entity.has_component<Dialog>() && entity.component<Dialog>()->talking) {
@@ -82,38 +82,41 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
        en.each<Visible, Sprite, Position>([dt](entityx::Entity entity, Visible &visible, Sprite &sprite, Position &pos) {
                (void)entity;
                // Verticies and shit
-               GLfloat tex_coord[] = {0.0, 0.0,
-                                                          1.0, 0.0,
-                                                          1.0, 1.0,
+               float its = 0;
+               for (auto &S : sprite.sprite) {
+                       auto sp = S.first;
+                       float width = HLINES(S.first.size.x);
+                       float height = HLINES(S.first.size.y);
+                       vec2 drawOffset(HLINES(S.second.x), HLINES(S.second.y));
+                       vec2 loc(pos.x + drawOffset.x, pos.y + drawOffset.y);
 
-                                                          1.0, 1.0,
-                                                          0.0, 1.0,
-                                                          0.0, 0.0};
+                       GLfloat tex_coord[] = {sp.offset_tex.x,                                 sp.offset_tex.y,
+                                                                  sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y,
+                                                                  sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y + sp.size_tex.y,
 
-               GLfloat tex_coordL[] = {1.0, 0.0,
-                                                               0.0, 0.0,
-                                                               0.0, 1.0,
+                                                                  sp.offset_tex.x + sp.size_tex.x, sp.offset_tex.y + sp.size_tex.y,
+                                                                  sp.offset_tex.x,                             sp.offset_tex.y + sp.size_tex.y,
+                                                                  sp.offset_tex.x,                             sp.offset_tex.y};
 
-                                                               0.0, 1.0,
-                                                               1.0, 1.0,
-                                                               1.0, 0.0};
+                       if(sprite.faceLeft) {
 
-               if (entity.has_component<Animate>())
-                       sprite.sprite = entity.component<Animate>()->nextFrame();
+                               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-width-pos.x*2, 0.0f, 0.0f));
 
-               for (auto &S : sprite.sprite) {
-                       float width = HLINES(S.first.size.x);
-                       float height = HLINES(S.first.size.y);
+                               glm::mat4 mov = scale * translate;
+                               glUniformMatrix4fv(Render::worldShader.uniform[WU_transform], 1, GL_FALSE, glm::value_ptr(mov));
 
-                       vec2 loc = vec2(pos.x + S.first.offset.x, pos.y + S.first.offset.y);
+                       } else {
+                               
+                       }
 
-                       GLfloat coords[] = {loc.x,                      loc.y,                  visible.z,
-                                                               loc.x + width,  loc.y,                  visible.z,
-                                                               loc.x + width,  loc.y + height, visible.z,
+                       GLfloat coords[] = {loc.x,                      loc.y,                  visible.z + its,
+                                                               loc.x + width,  loc.y,                  visible.z + its,
+                                                               loc.x + width,  loc.y + height, visible.z + its,
 
-                                                               loc.x + width,  loc.y + height, visible.z,
-                                                               loc.x,                  loc.y + height, visible.z,
-                                                               loc.x,                  loc.y,                  visible.z};
+                                                               loc.x + width,  loc.y + height, visible.z + its,
+                                                               loc.x,                  loc.y + height, visible.z + its,
+                                                               loc.x,                  loc.y,                  visible.z + its};
 
 
                        // make the entity hit flash red
@@ -129,13 +132,13 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev,
                        Render::worldShader.enable();
 
                        glVertexAttribPointer(Render::worldShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords);
-                       if (sprite.faceLeft)
-                               glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coordL);
-                       else
-                               glVertexAttribPointer(Render::worldShader.tex, 2, GL_FLOAT, GL_FALSE, 0 ,tex_coord);
+                       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;
                }
        });
 
@@ -264,6 +267,10 @@ std::vector<Frame> developFrame(XMLElement* xml)
        Frame tmpf;
        std::vector<Frame> tmp;
 
+       vec2 foffset;
+       vec2 fsize;
+       vec2 fdraw;
+
        // this is the xml elements first child. It will only be the <frame> tag
        auto framexml = xml->FirstChildElement();
        while (framexml) {
@@ -271,12 +278,23 @@ std::vector<Frame> developFrame(XMLElement* xml)
                std::string defframe = framexml->Name();
                if (defframe == "frame") {
                        tmpf.clear();
+                       tmp.clear();
                        // the xml element to parse each src of the frames
                        auto sxml = framexml->FirstChildElement();
                        while (sxml) {
                                std::string sname = sxml->Name();
                                if (sname == "src") {
-                                       tmpf.push_back(std::make_pair(SpriteData(sxml->GetText(), vec2(0,0)), vec2(0,0)));
+                                       foffset = (sxml->Attribute("offset") != nullptr) ? 
+                                               str2coord(sxml->Attribute("offset")) : vec2(0,0);
+                                       fdraw = (sxml->Attribute("drawOffset") != nullptr) ?
+                                               str2coord(sxml->Attribute("drawOffset")) : vec2(0,0);
+
+                                       if (sxml->Attribute("size") != nullptr) {
+                                               fsize = str2coord(sxml->Attribute("size"));
+                                               tmpf.push_back(std::make_pair(SpriteData(sxml->GetText(), foffset, fsize), fdraw));
+                                       } else {
+                                               tmpf.push_back(std::make_pair(SpriteData(sxml->GetText(), foffset), fdraw));
+                                       }
                                }
                                sxml = sxml->NextSiblingElement();
                        }
index e5b2b36391324dc24fa4a373ed44e8659be5c342..7287f7d086b93c6d5f89623fa1777ed9fc0bb437 100644 (file)
@@ -40,11 +40,14 @@ void Engine::init(void) {
 void Engine::render(entityx::TimeDelta dt)
 {
     systems.update<RenderSystem>(dt);
-       systems.update<WindowSystem>(dt);
     //systems.update<InventorySystem>(dt); // doesn't do anything...
 
        ui::fadeUpdate();
 }
+void Engine::resetRender(entityx::TimeDelta dt)
+{
+       systems.update<WindowSystem>(dt);
+}
 
 void Engine::update(entityx::TimeDelta dt)
 {
index 1994382b3cf882baa0e4ce055b955d9460b938f2..84bb55f0ca02b4727c4ebef62b857271e58f1970 100644 (file)
@@ -747,7 +747,7 @@ namespace ui {
                                c2.x,                                           c1.y + box_corner_dim.y, z,     1.0f, 0.5f,
 
                                c2.x,                                           c1.y + box_corner_dim.y, z, 1.0f, 0.5f,
-                                       c2.x - box_corner_dim.x,        c1.y + box_corner_dim.y, z, 0.0f, 0.5f,
+                                       c2.x - box_corner_dim.x,        c1.y + box_corner_dim.y, z, 0.5f, 0.5f,
                                c2.x - box_corner_dim.x,        c1.y,                                    z,     0.5f, 0.0f};
 
                GLfloat box_l[] =  {c1.x,                                               c1.y + box_corner_dim.y, z, 0.0f, 0.0f,
index 4a7e284edf5c251724ca6ab817311c770be1fae5..d61ed419078476173ccc67956dc26afe8542ae22 100644 (file)
@@ -182,11 +182,6 @@ bool WorldSystem::save(void)
 void WorldSystem::load(const std::string& file)
 {
        auto& render = *game::engine.getSystem<RenderSystem>();
-       auto str2coord = [](std::string s) -> vec2 {
-               auto cpos = s.find(',');
-               s[cpos] = '\0';
-               return vec2 (std::stof(s), std::stof(s.substr(cpos + 1)));
-       };
 
        entityx::Entity entity;
 
@@ -332,10 +327,10 @@ void WorldSystem::load(const std::string& file)
                                                entity.assign<Visible>(abcd->FloatAttribute("value"));
                                        } else if (tname == "Sprite") {
                                                auto sprite = entity.assign<Sprite>();
-                                               auto tex = abcd->Attribute("image");
-                                               sprite->addSpriteSegment(SpriteData(tex,
-                                                                                   vec2(0, 0)),
-                                                                                               vec2(0, 0));
+                                               auto sprx = abcd;
+                                               auto frames = developFrame(sprx);
+                                               if (frames.size())
+                                                       sprite->sprite = frames.at(0);
                                        } else if (tname == "Portal") {
                                                entity.assign<Portal>(wxml->StrAttribute("inside"));
                                        } else if (tname == "Solid") {
@@ -382,12 +377,20 @@ void WorldSystem::load(const std::string& file)
                                        } else if (tname == "Wander") {
                                                entity.assign<Wander>();
                                        } else if (tname == "Animation") {
-                                               entity.assign<Animate>();
+                                               auto entan = entity.assign<Animate>();
                                                auto animx = abcd->FirstChildElement();
+                                               uint idtc = 0;
                                                while (animx) { 
                                                        std::string animType = animx->Name();
                                                        if (animType == "movement") {
-                                                               entity.component<Animate>().get()->frame = developFrame(animx);
+                                                               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]));
+                                                               }
                                                        }
                                                        
                                                        animx = animx->NextSiblingElement();
index b447dc30d0746d16bb2886d8b87a11b87260f3f9..e1d75498bec146e16f3c3394b91749f487f291c1 100644 (file)
@@ -3,71 +3,20 @@
 <npc>
        <Position value="0.0,100.0" />
        <Visible value="0.2" />
-       <Sprite image="assets/NPC.png" />
+       <Sprite> image="assets/NPC.png"
+               <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>
+               </frame>
+       </Sprite>       
        <Animation>
-               <movement>
+               <movement update="1" changeId="1">
                        <frame>
-                               <src>assets/NPC.png</src>               
+                               <src offset="0,21" size="15,9" drawOffset="0,13">assets/NPC.png</src>           
                        </frame>
                        <frame>
-                               <src>assets/NPC_Walk.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk2.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk3.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk4.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk5.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk6.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk7.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk8.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk9.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk0.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk9.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk8.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk7.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk6.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk5.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk4.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk3.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk2.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC_Walk.png</src>
-                       </frame>
-                       <frame>
-                               <src>assets/NPC.png</src>
+                               <src offset="0,21" size="15,9" drawOffset="0,13">assets/NPC_Walk.png</src>
                        </frame>
                </movement>
        </Animation>
 <structure>
        <Position value="0.0,100.0" />
        <Visible value="0.25" />
-       <Sprite image="assets/style/classic/house1.png" />
+       <Sprite>
+               <frame>
+                       <src>assets/style/classic/house1.png</src>
+               </frame>
+       </Sprite>
        <Portal />
        <Solid />
        <Grounded />
 <chest>
        <Position value="0.0,100.0" />
        <Visible value="0.15" />
-       <Sprite image="assets/chest.png" />
+       <Sprite>
+               <frame>
+                       <src>assets/chest.png</src>
+               </frame>
+       </Sprite>
        <Solid />
        <Grounded />
 </chest>