diff options
-rw-r--r-- | assets/NPC.png | bin | 473 -> 555 bytes | |||
-rw-r--r-- | assets/goodmouse.png | bin | 0 -> 6652 bytes | |||
-rw-r--r-- | assets/mouse.png | bin | 540 -> 1100 bytes | |||
-rw-r--r-- | include/common.hpp | 8 | ||||
-rw-r--r-- | include/components.hpp | 40 | ||||
-rw-r--r-- | include/engine.hpp | 1 | ||||
-rw-r--r-- | main.cpp | 12 | ||||
-rw-r--r-- | src/common.cpp | 8 | ||||
-rw-r--r-- | src/components.cpp | 80 | ||||
-rw-r--r-- | src/engine.cpp | 5 | ||||
-rw-r--r-- | src/ui.cpp | 2 | ||||
-rw-r--r-- | src/world.cpp | 25 | ||||
-rw-r--r-- | xml/entities.xml | 83 |
13 files changed, 146 insertions, 118 deletions
diff --git a/assets/NPC.png b/assets/NPC.png Binary files differindex 801ceb0..7c46dce 100644 --- a/assets/NPC.png +++ b/assets/NPC.png diff --git a/assets/goodmouse.png b/assets/goodmouse.png Binary files differnew file mode 100644 index 0000000..72c4409 --- /dev/null +++ b/assets/goodmouse.png diff --git a/assets/mouse.png b/assets/mouse.png Binary files differindex 9614317..d4c082e 100644 --- a/assets/mouse.png +++ b/assets/mouse.png diff --git a/include/common.hpp b/include/common.hpp index 56928b5..19af420 100644 --- a/include/common.hpp +++ b/include/common.hpp @@ -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. * diff --git a/include/components.hpp b/include/components.hpp index bbf153a..5c067dd 100644 --- a/include/components.hpp +++ b/include/components.hpp @@ -125,12 +125,36 @@ struct Solid { }; struct SpriteData { + + SpriteData(std::string path, vec2 off): + offset(off) { + tex = Texture(path); + size = tex.getDim(); + + 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 off, vec2 si): + size(si), offset(off) { + tex = Texture(path); + vec2 tmpsize = tex.getDim(); - SpriteData(std::string path, vec2 offset) - : tex(path), offset(offset) {} + 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; + } Texture tex; + vec2 size; vec2 offset; + + vec2 offset_tex; + vec2 size_tex; }; using Frame = std::vector<std::pair<SpriteData, vec2>>; @@ -212,7 +236,7 @@ struct Sprite { //TODO struct Animate { // COMMENT - std::vector<Frame> frame; + std::vector<std::pair<uint, Frame>> frame; // COMMENT uint index; @@ -221,17 +245,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; } }; diff --git a/include/engine.hpp b/include/engine.hpp index 52569e7..2b03696 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -44,6 +44,7 @@ public: * @param dt the delta time */ void render(entityx::TimeDelta dt); + void resetRender(entityx::TimeDelta dt); /** * Updates all logic systems. @@ -131,7 +131,9 @@ int main(int argc, char *argv[]) game::briceLoad(); game::briceUpdate(); - // + // load sprites used in the inventory menu. See src/inventory.cpp + //initInventorySprites(); + // get a world // @@ -218,7 +220,7 @@ int main(int argc, char *argv[]) const bool &run = game::engine.shouldRun; while (run) { render(); - game::engine.render(0); + game::engine.resetRender(0); } // on game end, get back together @@ -254,7 +256,7 @@ int main(int argc, char *argv[]) } void render() { - static const Texture mouseTex ("assets/mouse.png"); + static const Texture mouseTex ("assets/goodmouse.png"); static const glm::mat4 view = glm::lookAt( glm::vec3(0.0f, 0.0f, 0.0f), // pos glm::vec3(0.0f, 0.0f, -10.0f), // looking at @@ -313,6 +315,8 @@ void render() { // draw the player's inventory //player->inv->draw(); + game::engine.render(0); + // draw the fade overlay ui::drawFade(); @@ -337,6 +341,6 @@ void render() { glActiveTexture(GL_TEXTURE0); mouseTex.use(); 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(); } diff --git a/src/common.cpp b/src/common.cpp index 706bc3c..6539a05 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -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; diff --git a/src/components.cpp b/src/components.cpp index f95f46f..7322208 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -21,11 +21,11 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e position.x += direction.x * dt; position.y += direction.y * dt; - if (entity.has_component<Animate>() && entity.has_component<Sprite>()) { + /*if (entity.has_component<Animate>() && entity.has_component<Sprite>()) { auto animate = entity.component<Animate>(); entity.component<Sprite>()->sprite = (direction.x != 0) ? animate->nextFrame() : animate->firstFrame(); - } + }*/ if (entity.has_component<Dialog>() && entity.component<Dialog>()->talking) { direction.x = 0; } else { @@ -82,34 +82,36 @@ 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, - - 1.0, 1.0, - 0.0, 1.0, - 0.0, 0.0}; + float its = 0; + for (auto &S : sprite.sprite) { + auto sp = S.first; + auto size = sp.size * game::HLINE; + vec2 drawOffset(HLINES(S.second.x), HLINES(S.second.y)); + vec2 loc(pos.x + drawOffset.x, pos.y + drawOffset.y); - GLfloat tex_coordL[] = {1.0, 0.0, - 0.0, 0.0, - 0.0, 1.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, - 0.0, 1.0, - 1.0, 1.0, - 1.0, 0.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}; - for (auto &S : sprite.sprite) { - const auto& size = S.first.tex.getDim() * game::HLINE; + 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)); - vec2 loc = vec2(pos.x + S.first.offset.x, pos.y + S.first.offset.y); + 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, - loc.x + size.x, loc.y, visible.z, - loc.x + size.x, loc.y + size.y, visible.z, + GLfloat coords[] = {loc.x, loc.y, visible.z + its, + loc.x + size.x, loc.y, visible.z + its, + loc.x + size.x, loc.y + size.y, visible.z + its, - loc.x + size.x, loc.y + size.y, visible.z, - loc.x, loc.y + size.y, visible.z, - loc.x, loc.y, visible.z}; + loc.x + size.x, loc.y + size.y, visible.z + its, + loc.x, loc.y + size.y, visible.z + its, + loc.x, loc.y, visible.z + its}; // make the entity hit flash red @@ -119,19 +121,19 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, glUniform4f(Render::worldShader.uniform[WU_tex_color], 1.0, flashAmt, flashAmt, 1.0); }*/ - S.first.tex.use(); + sp.tex.use(); glUniform1i(Render::worldShader.uniform[WU_texture], 0); 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; } }); @@ -263,6 +265,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) { @@ -270,12 +276,24 @@ 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))); + if (sname == "src") { + 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(); } tmp.push_back(tmpf); diff --git a/src/engine.cpp b/src/engine.cpp index b42b162..48e384d 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -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) { @@ -683,7 +683,7 @@ namespace ui { c2.x, c1.y + boxCornerDim2.y, z, 1.0f, 0.5f, c2.x, c1.y + boxCornerDim2.y, z, 1.0f, 0.5f, - c2.x - boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.0f, 0.5f, + c2.x - boxCornerDim2.x, c1.y + boxCornerDim2.y, z, 0.5f, 0.5f, c2.x - boxCornerDim2.x, c1.y, z, 0.5f, 0.0f}; GLfloat box_l[] = {c1.x, c1.y + boxCornerDim2.y, z, 0.0f, 0.0f, diff --git a/src/world.cpp b/src/world.cpp index d0727e2..6b18542 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -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; @@ -331,10 +326,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") { @@ -381,12 +376,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(); diff --git a/xml/entities.xml b/xml/entities.xml index b447dc3..e1d7549 100644 --- a/xml/entities.xml +++ b/xml/entities.xml @@ -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> @@ -82,7 +31,11 @@ <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 /> @@ -91,7 +44,11 @@ <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> |