From f91e95eed85bdd74b219b20a435fa6f297069da1 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 13 Jan 2017 10:42:55 -0500 Subject: vector2 stuff, other things --- src/inventory.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/inventory.cpp') diff --git a/src/inventory.cpp b/src/inventory.cpp index 480c803..ed6028b 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -2,10 +2,10 @@ #include #include -#include -#include +//#include +//#include -constexpr const char* ICON_TEX_FILE_PATH = "config/invIcons.txt"; +constexpr const char* iconTexturePath = "config/invIcons.txt"; static std::vector iconTextures; @@ -16,7 +16,7 @@ void InventorySystem::configure(entityx::EventManager &ev) void InventorySystem::loadIcons(void) { iconTextures.clear(); - auto icons = readFileA(ICON_TEX_FILE_PATH); + auto icons = readFileA(iconTexturePath); for (const auto& s : icons) iconTextures.push_back(s); } -- cgit v1.2.3 From 3da25ab8c6ad1b52b808bfeffc0ad1b32621cfac Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 14 Jan 2017 09:56:58 -0500 Subject: particle improvements, inventory start --- config/items.xml | 2 +- include/inventory.hpp | 22 ++++++++----- include/particle.hpp | 16 ++++----- include/weather.hpp | 2 +- src/engine.cpp | 2 +- src/inventory.cpp | 89 ++++++++++++++++++++++++++++++++++++++++----------- src/particle.cpp | 48 ++++++++++++++------------- src/render.cpp | 12 ++++--- src/world.cpp | 13 +++----- 9 files changed, 132 insertions(+), 74 deletions(-) (limited to 'src/inventory.cpp') diff --git a/config/items.xml b/config/items.xml index 0abd40d..265dbf7 100644 --- a/config/items.xml +++ b/config/items.xml @@ -22,5 +22,5 @@ - + diff --git a/include/inventory.hpp b/include/inventory.hpp index fa24de4..15877e9 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -6,30 +6,36 @@ #include #include -struct Item { - GLuint icon; +struct InventoryEntry { + GLuint icon; + std::string name; + std::string type; + + int count; + int max; + vec2 loc; }; -using InventoryEntry = std::pair; - class InventorySystem : public entityx::System, public entityx::Receiver { private: entityx::Entity currentItemEntity; std::vector items; - unsigned int maxItemCount; public: - InventorySystem(unsigned int mic = 1) - : maxItemCount(mic) {} + InventorySystem(int size = 4) { + items.resize(size); + loadItems(); + } void configure(entityx::EventManager &ev); - void loadIcons(void); + void loadItems(void); void update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) override; void receive(const KeyDownEvent &kde); + void render(void); }; #endif // INVENTORY_HPP_ diff --git a/include/particle.hpp b/include/particle.hpp index 92ab7e4..55228c0 100644 --- a/include/particle.hpp +++ b/include/particle.hpp @@ -9,22 +9,22 @@ #include enum class ParticleType : char { - Drop, - Confetti, - SmallBlast, - SmallPoof + Drop = 1, + Confetti = 2, + SmallBlast = 4, + SmallPoof = 8 }; struct Particle { - vec2 location; + int timeLeft; vec2 velocity; + vec2 location; ParticleType type; - int timeLeft; vec2 color; // assets/colorIndex.png Particle(vec2 p, ParticleType t, int tl, vec2 c) - : location(p), type(t), timeLeft(tl), color(c) {} -};// __attribute__ ((packed)); + : timeLeft(tl), location(p), type(t), color(c) {} +}; class ParticleSystem : public entityx::System { private: diff --git a/include/weather.hpp b/include/weather.hpp index 0820871..8e148dd 100644 --- a/include/weather.hpp +++ b/include/weather.hpp @@ -56,7 +56,7 @@ public: newPartDelay = 0; partSystem.add(vec2(offset.x - game::SCREEN_WIDTH + randGet() % game::SCREEN_WIDTH * 2, offset.y + game::SCREEN_HEIGHT / 2 + 50), - ParticleType::Confetti, 6000, 0); + ParticleType::Confetti, 10000, 0); } break; default: diff --git a/src/engine.cpp b/src/engine.cpp index c7999ed..d334aea 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -23,7 +23,7 @@ void Engine::init(void) { systems.add(); systems.add(); systems.add(); - //systems.add(); + systems.add(); systems.add(); systems.add(); systems.add(); diff --git a/src/inventory.cpp b/src/inventory.cpp index ed6028b..de18104 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -2,23 +2,59 @@ #include #include -//#include -//#include +#include +#include -constexpr const char* iconTexturePath = "config/invIcons.txt"; +#include -static std::vector iconTextures; +#include +using namespace tinyxml2; + +constexpr const char* itemsPath = "config/items.xml"; + + +struct Item { + std::string name; + std::string type; + int value; + int stackSize; + Texture sprite; + + Item(void) + : value(0), stackSize(1) {} + + Item(XMLElement *e) { + name = e->StrAttribute("name"); + type = e->StrAttribute("type"); + + value = 0; + e->QueryIntAttribute("value", &value); + stackSize = 1; + e->QueryIntAttribute("maxStackSize", &stackSize); + + sprite = Texture(e->StrAttribute("sprite")); + } +}; + +static std::deque itemList; void InventorySystem::configure(entityx::EventManager &ev) { ev.subscribe(*this); } -void InventorySystem::loadIcons(void) { - iconTextures.clear(); - auto icons = readFileA(iconTexturePath); - for (const auto& s : icons) - iconTextures.push_back(s); +void InventorySystem::loadItems(void) { + XMLDocument doc; + doc.LoadFile(itemsPath); + + auto item = doc.FirstChildElement("item"); + if (item == nullptr) + UserError("No items found"); + + do { + itemList.emplace_back(item); + item = item->NextSiblingElement("item"); + } while (item != nullptr); } void InventorySystem::update(entityx::EntityManager &en, entityx::EventManager &ev, entityx::TimeDelta dt) @@ -26,20 +62,35 @@ void InventorySystem::update(entityx::EntityManager &en, entityx::EventManager & (void)en; (void)ev; (void)dt; +} - // TODO TODO TODO TODO until we do something - return; +void InventorySystem::render(void) +{ + Render::textShader.use(); + Render::textShader.enable(); + Colors::black.use(); + glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, .8); - //vec2 start = vec2(offset.x, 100);// - game::SCREEN_WIDTH / 2 + 20, game::SCREEN_HEIGHT - 40); + // calculate positions + items.front().loc = vec2(offset.x - 35 * items.size(), offset.y - game::SCREEN_HEIGHT / 2); + for (unsigned int i = 1; i < items.size(); i++) + items[i].loc = vec2(items[i - 1].loc.x + 70, items[i - 1].loc.y); - //std::cout << start.x << ' ' << start.y << std::endl; + // draw items + for (const auto& i : items) { + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, Colors::texCoord); + vec2 end = i.loc + 64; + GLfloat coords[18] = { + i.loc.x, i.loc.y, -9, end.x, i.loc.y, -9, end.x, end.y, -9, + end.x, end.y, -9, i.loc.x, end.y, -9, i.loc.x, i.loc.y, -9 + }; + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); + glDrawArrays(GL_TRIANGLES, 0, 6); + } - /*Render::textShader.use(); - glActiveTexture(GL_TEXTURE0); - Colors::black.use(); - Render::useShader(&Render::textShader); - Render::drawRect(start, start + 20, -9.9f); - Render::textShader.unuse();*/ + glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, 1); + Render::textShader.disable(); + Render::textShader.unuse(); } void InventorySystem::receive(const KeyDownEvent &kde) diff --git a/src/particle.cpp b/src/particle.cpp index 2d92409..198387c 100644 --- a/src/particle.cpp +++ b/src/particle.cpp @@ -91,52 +91,54 @@ void ParticleSystem::update(entityx::EntityManager &en, entityx::EventManager &e auto& p = parts[i]; // update timers - p.timeLeft -= dt; + if (p.timeLeft > 0) + p.timeLeft -= dt; + else + continue; // update movement + auto& vel = p.velocity; switch (p.type) { case ParticleType::Drop: - if (p.velocity.y > -.6) - p.velocity.y -= 0.001f; + if (vel.y > -.6) + vel.y -= 0.001f; break; case ParticleType::Confetti: - if (p.velocity.x > -0.01 && p.velocity.x < 0.01) { - p.velocity.x = randGet() % 12 / 30.0f - 0.2f; + if (vel.x > -0.01 && vel.x < 0.01) { + vel.x = randGet() % 12 / 30.0f - 0.2f; + vel.y = -0.15f; } else { - p.velocity.x += (p.velocity.x > 0) ? -0.002f : 0.002f; + vel.x += (vel.x > 0) ? -0.002f : 0.002f; } - p.velocity.y = -0.15f; - p.timeLeft = 1000; break; case ParticleType::SmallBlast: - if (p.velocity.x == 0) { + if (vel.x == 0) { int degree = randGet() % 100; - p.velocity.x = cos(degree) / 4.0f; - p.velocity.y = sin(degree) / 4.0f; + vel.x = cos(degree) / 4.0f; + vel.y = sin(degree) / 4.0f; } else { - p.velocity.x += (p.velocity.x > 0) ? -0.001f : 0.001f; - p.velocity.y += (p.velocity.y > 0) ? -0.001f : 0.001f; - if ((p.velocity.x > -0.01 && p.velocity.x < 0.01) && - (p.velocity.y > -0.01 && p.velocity.y < 0.01)) { + vel.x += (vel.x > 0) ? -0.001f : 0.001f; + vel.y += (vel.y > 0) ? -0.001f : 0.001f; + if ((vel.x > -0.01 && vel.x < 0.01) && + (vel.y > -0.01 && vel.y < 0.01)) { p.timeLeft = 0; } } - break; case ParticleType::SmallPoof: - if (p.velocity.x == 0) { - p.velocity.y = 0.1f; - p.velocity.x = randGet() % 10 / 20.0f - 0.25f; + if (vel.x == 0) { + vel.y = 0.1f; + vel.x = randGet() % 10 / 20.0f - 0.25f; } else { - p.velocity.x += (p.velocity.x > 0) ? -0.001f : 0.001f; - p.velocity.y -= 0.0015f; + vel.x += (vel.x > 0) ? -0.001f : 0.001f; + vel.y -= 0.0015f; } break; } // really update movement - p.location.x += p.velocity.x * dt; - p.location.y += p.velocity.y * dt; + p.location.x += vel.x * dt; + p.location.y += vel.y * dt; // world collision auto height = worldSystem.isAboveGround(p.location); diff --git a/src/render.cpp b/src/render.cpp index 4e32311..a63d63a 100644 --- a/src/render.cpp +++ b/src/render.cpp @@ -99,14 +99,16 @@ void render(const int& fps) } // namespace render -#include -#include -#include #include +#include +#include #include #include +#include +#include +#include + #include -#include void preRender(void) { @@ -172,6 +174,8 @@ void render(const int& fps) game::engine.getSystem()->render(); + game::engine.getSystem()->render(); + // draw the debug overlay if desired if (ui::debug) { auto pos = game::engine.getSystem()->getPosition(); diff --git a/src/world.cpp b/src/world.cpp index 8f4be8e..43b66cd 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -98,14 +98,6 @@ static const std::string buildPaths[] = { "brazzier.png" }; -// alpha-related values used for world drawing? nobody knows... -static const float bgDraw[4][3]={ - { 100, 240, 0.6 }, - { 150, 250, 0.4 }, - { 200, 255, 0.25 }, - { 255, 255, 0.1 } -}; - /* ---------------------------------------------------------------------------- ** Functions section ** --------------------------------------------------------------------------*/ @@ -827,9 +819,12 @@ void WorldSystem::render(void) Render::worldShader.unuse(); // draw the remaining layers + static const float alphas[4] = { + 0.6, 0.4, 0.25, 0.1 + }; for (int i = 0; i < 4; i++) { bgTex++; - auto xcoord = offset.x * bgDraw[i][2]; + auto xcoord = offset.x * alphas[i]; bg_items.clear(); bg_tex.clear(); -- cgit v1.2.3 From 8452b199d28bea53bf2c5e3b3d604064000fc73d Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Wed, 18 Jan 2017 06:46:36 -0500 Subject: inventory bar --- include/inventory.hpp | 32 ++++++++++++++++++---- main.cpp | 14 ++++++---- src/inventory.cpp | 76 +++++++++++++++++++++++++++------------------------ 3 files changed, 77 insertions(+), 45 deletions(-) (limited to 'src/inventory.cpp') diff --git a/include/inventory.hpp b/include/inventory.hpp index 15877e9..44ed148 100644 --- a/include/inventory.hpp +++ b/include/inventory.hpp @@ -6,20 +6,40 @@ #include #include -struct InventoryEntry { - GLuint icon; +struct Item { std::string name; std::string type; + int value; + int stackSize; + Texture sprite; + + Item(void) + : value(0), stackSize(1) {} + + Item(XMLElement *e) { + name = e->StrAttribute("name"); + type = e->StrAttribute("type"); + + value = 0; + e->QueryIntAttribute("value", &value); + stackSize = 1; + e->QueryIntAttribute("maxStackSize", &stackSize); + + sprite = Texture(e->StrAttribute("sprite")); + } +}; +struct InventoryEntry { + Item* item; int count; - int max; vec2 loc; + + InventoryEntry(void) + : item(nullptr), count(0) {} }; class InventorySystem : public entityx::System, public entityx::Receiver { private: - entityx::Entity currentItemEntity; - std::vector items; public: @@ -36,6 +56,8 @@ public: void receive(const KeyDownEvent &kde); void render(void); + + void add(const std::string& name, int count); }; #endif // INVENTORY_HPP_ diff --git a/main.cpp b/main.cpp index 49199a0..7e06d7d 100644 --- a/main.cpp +++ b/main.cpp @@ -20,29 +20,29 @@ using namespace std::literals::chrono_literals; #include #include #include +#include class GameThread : public entityx::Receiver { private: std::atomic_bool die; - bool running; std::thread thread; public: template GameThread(F&& func) { die.store(false); - running = true; thread = std::thread([this](F&& f) { while (!die.load()) f(); - running = false; }, std::forward(func)); } + ~GameThread(void) { + thread.join(); + } + void stop(void) { die.store(true); - while (running) - std::this_thread::sleep_for(2ms); } }; @@ -133,6 +133,10 @@ int main(int argc, char *argv[]) // // ///////////////////////////// + + game::engine.getSystem()->add("Debug", 3); + + std::list eventQueue; if (!worldDontReallyRun) { diff --git a/src/inventory.cpp b/src/inventory.cpp index de18104..354db9c 100644 --- a/src/inventory.cpp +++ b/src/inventory.cpp @@ -4,39 +4,16 @@ #include #include #include +#include -#include +#include #include using namespace tinyxml2; constexpr const char* itemsPath = "config/items.xml"; - -struct Item { - std::string name; - std::string type; - int value; - int stackSize; - Texture sprite; - - Item(void) - : value(0), stackSize(1) {} - - Item(XMLElement *e) { - name = e->StrAttribute("name"); - type = e->StrAttribute("type"); - - value = 0; - e->QueryIntAttribute("value", &value); - stackSize = 1; - e->QueryIntAttribute("maxStackSize", &stackSize); - - sprite = Texture(e->StrAttribute("sprite")); - } -}; - -static std::deque itemList; +static std::unordered_map itemList; void InventorySystem::configure(entityx::EventManager &ev) { @@ -52,7 +29,7 @@ void InventorySystem::loadItems(void) { UserError("No items found"); do { - itemList.emplace_back(item); + itemList.emplace(item->StrAttribute("name"), item); item = item->NextSiblingElement("item"); } while (item != nullptr); } @@ -66,11 +43,6 @@ void InventorySystem::update(entityx::EntityManager &en, entityx::EventManager & void InventorySystem::render(void) { - Render::textShader.use(); - Render::textShader.enable(); - Colors::black.use(); - glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, .8); - // calculate positions items.front().loc = vec2(offset.x - 35 * items.size(), offset.y - game::SCREEN_HEIGHT / 2); for (unsigned int i = 1; i < items.size(); i++) @@ -78,17 +50,40 @@ void InventorySystem::render(void) // draw items for (const auto& i : items) { + // draw the slot + Render::textShader.use(); + Render::textShader.enable(); + + Colors::black.use(); + glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, .8); glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, Colors::texCoord); vec2 end = i.loc + 64; GLfloat coords[18] = { - i.loc.x, i.loc.y, -9, end.x, i.loc.y, -9, end.x, end.y, -9, - end.x, end.y, -9, i.loc.x, end.y, -9, i.loc.x, i.loc.y, -9 + i.loc.x, i.loc.y, -7, end.x, i.loc.y, -7, end.x, end.y, -7, + end.x, end.y, -7, i.loc.x, end.y, -7, i.loc.x, i.loc.y, -7 }; glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); glDrawArrays(GL_TRIANGLES, 0, 6); + glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, 1); + + // draw the item + if (i.item != nullptr) { + i.item->sprite.use(); + static const GLfloat tex[12] = {0,1,1,1,1,0,1,0,0,0,0,1}; + glVertexAttribPointer(Render::textShader.tex, 2, GL_FLOAT, GL_FALSE, 0, tex); + vec2 end = i.loc + i.item->sprite.getDim(); + GLfloat coords[18] = { + i.loc.x, i.loc.y, -7.1, end.x, i.loc.y, -7.1, end.x, end.y, -7.1, + end.x, end.y, -7.1, i.loc.x, end.y, -7.1, i.loc.x, i.loc.y, -7.1 + }; + glVertexAttribPointer(Render::textShader.coord, 3, GL_FLOAT, GL_FALSE, 0, coords); + glDrawArrays(GL_TRIANGLES, 0, 6); + ui::setFontZ(-7.2); + ui::putText(i.loc.x, i.loc.y, std::to_string(i.count).c_str()); + ui::setFontZ(-6); + } } - glUniform4f(Render::textShader.uniform[WU_tex_color], 1, 1, 1, 1); Render::textShader.disable(); Render::textShader.unuse(); } @@ -97,3 +92,14 @@ void InventorySystem::receive(const KeyDownEvent &kde) { (void)kde; } + +void InventorySystem::add(const std::string& name, int count) +{ + for (auto& i : items) { + if (i.count == 0) { + i.item = &itemList[name]; + i.count = count; + break; + } + } +} -- cgit v1.2.3