]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
inventory bar
authorClyne Sullivan <tullivan99@gmail.com>
Wed, 18 Jan 2017 11:46:36 +0000 (06:46 -0500)
committerClyne Sullivan <tullivan99@gmail.com>
Wed, 18 Jan 2017 11:46:36 +0000 (06:46 -0500)
include/inventory.hpp
main.cpp
src/inventory.cpp

index 15877e93978975a4a15c31e8882e8b5bfc8a7e8d..44ed148c06fa655bd31cae56843a05d0f8970add 100644 (file)
@@ -6,20 +6,40 @@
 #include <components.hpp>
 #include <events.hpp>
 
-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<InventorySystem>, public entityx::Receiver<InventorySystem> {
 private:
-    entityx::Entity currentItemEntity;
-
     std::vector<InventoryEntry> 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_
index 49199a0b2d5800cd4299a16c8e92e14b1ebfb21f..7e06d7de902eec841729767b1dfc4cdd61656ca6 100644 (file)
--- a/main.cpp
+++ b/main.cpp
@@ -20,29 +20,29 @@ using namespace std::literals::chrono_literals;
 #include <world.hpp>
 #include <render.hpp>
 #include <ui.hpp>
+#include <inventory.hpp>
 
 class GameThread : public entityx::Receiver<GameThread> {
 private:
        std::atomic_bool die;
-       bool running;
        std::thread thread;
 
 public:
        template<typename F>
        GameThread(F&& func) {
                die.store(false);
-               running = true;
                thread = std::thread([this](F&& f) {
                        while (!die.load())
                                f();
-                       running = false;
                }, std::forward<F>(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<InventorySystem>()->add("Debug", 3);
+
+
        std::list<SDL_Event> eventQueue;
 
        if (!worldDontReallyRun) {
index de18104c204baca871acb815297b1f766de11f69..354db9c2ecc9e63d4881b6ac98f2d444583f443b 100644 (file)
@@ -4,39 +4,16 @@
 #include <events.hpp>
 #include <texture.hpp>
 #include <render.hpp>
+#include <ui.hpp>
 
-#include <deque>
+#include <unordered_map>
 
 #include <tinyxml2.h>
 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<Item> itemList;
+static std::unordered_map<std::string, Item> 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;
+               }
+       }
+}