aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndy <drumsetmonkey@gmail.com>2016-12-22 10:50:27 -0500
committerAndy <drumsetmonkey@gmail.com>2016-12-22 10:50:27 -0500
commit691411cdb214178f2d10ab589943993039fe080e (patch)
tree056acc0b536a05907b60fad86f9a3bc79254325d
parente472a62b750e57724a26d299bb682b4f39405d05 (diff)
Sprites flip and shit are good
-rw-r--r--assets/NPC.pngbin473 -> 555 bytes
-rw-r--r--assets/goodmouse.pngbin0 -> 6652 bytes
-rw-r--r--assets/mouse.pngbin540 -> 1100 bytes
-rw-r--r--include/common.hpp8
-rw-r--r--include/components.hpp31
-rw-r--r--include/engine.hpp1
-rw-r--r--main.cpp8
-rw-r--r--src/common.cpp8
-rw-r--r--src/components.cpp80
-rw-r--r--src/engine.cpp5
-rw-r--r--src/ui.cpp2
-rw-r--r--src/world.cpp25
-rw-r--r--xml/entities.xml83
13 files changed, 135 insertions, 116 deletions
diff --git a/assets/NPC.png b/assets/NPC.png
index 801ceb0..7c46dce 100644
--- a/assets/NPC.png
+++ b/assets/NPC.png
Binary files differ
diff --git a/assets/goodmouse.png b/assets/goodmouse.png
new file mode 100644
index 0000000..72c4409
--- /dev/null
+++ b/assets/goodmouse.png
Binary files differ
diff --git a/assets/mouse.png b/assets/mouse.png
index 9614317..d4c082e 100644
--- a/assets/mouse.png
+++ b/assets/mouse.png
Binary files differ
diff --git a/include/common.hpp b/include/common.hpp
index 3caa083..861905a 100644
--- a/include/common.hpp
+++ b/include/common.hpp
@@ -210,6 +210,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 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<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;
}
};
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<typename T>
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<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 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<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();
}
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<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)
{
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<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();
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>