From 691411cdb214178f2d10ab589943993039fe080e Mon Sep 17 00:00:00 2001 From: Andy Date: Thu, 22 Dec 2016 10:50:27 -0500 Subject: Sprites flip and shit are good --- assets/NPC.png | Bin 473 -> 555 bytes assets/goodmouse.png | Bin 0 -> 6652 bytes assets/mouse.png | Bin 540 -> 1100 bytes include/common.hpp | 8 +++++ include/components.hpp | 31 ++++++++++++++---- include/engine.hpp | 1 + main.cpp | 8 +++-- src/common.cpp | 8 +++++ src/components.cpp | 80 +++++++++++++++++++++++++++++------------------ src/engine.cpp | 5 ++- src/ui.cpp | 2 +- src/world.cpp | 25 ++++++++------- xml/entities.xml | 83 ++++++++++++------------------------------------- 13 files changed, 135 insertions(+), 116 deletions(-) create mode 100644 assets/goodmouse.png diff --git a/assets/NPC.png b/assets/NPC.png index 801ceb0..7c46dce 100644 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 index 0000000..72c4409 Binary files /dev/null and b/assets/goodmouse.png differ diff --git a/assets/mouse.png b/assets/mouse.png index 9614317..d4c082e 100644 Binary files a/assets/mouse.png and b/assets/mouse.png differ diff --git a/include/common.hpp b/include/common.hpp index 3caa083..861905a 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 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 d630f83..2a8680d 100644 --- a/include/components.hpp +++ b/include/components.hpp @@ -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>; @@ -214,7 +231,7 @@ struct Sprite { //TODO struct Animate { // COMMENT - std::vector frame; + std::vector> 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; } }; diff --git a/include/engine.hpp b/include/engine.hpp index c842b59..1c5f510 100644 --- a/include/engine.hpp +++ b/include/engine.hpp @@ -20,6 +20,7 @@ public: void init(void); void render(entityx::TimeDelta dt); + void resetRender(entityx::TimeDelta dt); void update(entityx::TimeDelta dt); template diff --git a/main.cpp b/main.cpp index a3901a3..6631736 100644 --- 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(); } diff --git a/src/common.cpp b/src/common.cpp index 01a7db8..098cd84 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -22,6 +22,14 @@ unsigned int millis(void) { return std::chrono::duration_cast(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 StringTokenizer(const std::string& str, char delim) { std::vector tokens; diff --git a/src/components.cpp b/src/components.cpp index 612e522..323dfc8 100644 --- a/src/components.cpp +++ b/src/components.cpp @@ -21,9 +21,9 @@ void MovementSystem::update(entityx::EntityManager &en, entityx::EventManager &e if (entity.has_component() && entity.has_component()) { if (direction.x) { - entity.component().get()->sprite = entity.component().get()->nextFrame(); + entity.component()->nextFrame(entity.component()->sprite); } else { - entity.component().get()->sprite = entity.component().get()->firstFrame(); + entity.component()->firstFrame(entity.component()->sprite); } } if (entity.has_component() && entity.component()->talking) { @@ -82,38 +82,41 @@ void RenderSystem::update(entityx::EntityManager &en, entityx::EventManager &ev, en.each([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()) - sprite.sprite = entity.component()->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 developFrame(XMLElement* xml) Frame tmpf; std::vector tmp; + vec2 foffset; + vec2 fsize; + vec2 fdraw; + // this is the xml elements first child. It will only be the tag auto framexml = xml->FirstChildElement(); while (framexml) { @@ -271,12 +278,23 @@ std::vector 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(); } diff --git a/src/engine.cpp b/src/engine.cpp index e5b2b36..7287f7d 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(dt); - systems.update(dt); //systems.update(dt); // doesn't do anything... ui::fadeUpdate(); } +void Engine::resetRender(entityx::TimeDelta dt) +{ + systems.update(dt); +} void Engine::update(entityx::TimeDelta dt) { diff --git a/src/ui.cpp b/src/ui.cpp index 1994382..84bb55f 100644 --- a/src/ui.cpp +++ b/src/ui.cpp @@ -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, diff --git a/src/world.cpp b/src/world.cpp index 4a7e284..d61ed41 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(); - 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(abcd->FloatAttribute("value")); } else if (tname == "Sprite") { auto sprite = entity.assign(); - 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(wxml->StrAttribute("inside")); } else if (tname == "Solid") { @@ -382,12 +377,20 @@ void WorldSystem::load(const std::string& file) } else if (tname == "Wander") { entity.assign(); } else if (tname == "Animation") { - entity.assign(); + auto entan = entity.assign(); auto animx = abcd->FirstChildElement(); + uint idtc = 0; while (animx) { std::string animType = animx->Name(); if (animType == "movement") { - entity.component().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 @@ - + image="assets/NPC.png" + + assets/NPC.png + assets/NPC.png + assets/items/ironSword.png + + - + - assets/NPC.png + assets/NPC.png - assets/NPC_Walk.png - - - assets/NPC_Walk2.png - - - assets/NPC_Walk3.png - - - assets/NPC_Walk4.png - - - assets/NPC_Walk5.png - - - assets/NPC_Walk6.png - - - assets/NPC_Walk7.png - - - assets/NPC_Walk8.png - - - assets/NPC_Walk9.png - - - assets/NPC_Walk0.png - - - assets/NPC_Walk9.png - - - assets/NPC_Walk8.png - - - assets/NPC_Walk7.png - - - assets/NPC_Walk6.png - - - assets/NPC_Walk5.png - - - assets/NPC_Walk4.png - - - assets/NPC_Walk3.png - - - assets/NPC_Walk2.png - - - assets/NPC_Walk.png - - - assets/NPC.png + assets/NPC_Walk.png @@ -82,7 +31,11 @@ - + + + assets/style/classic/house1.png + + @@ -91,7 +44,11 @@ - + + + assets/chest.png + + -- cgit v1.2.3