aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorClyne Sullivan <tullivan99@gmail.com>2017-01-18 06:46:36 -0500
committerClyne Sullivan <tullivan99@gmail.com>2017-01-18 06:46:36 -0500
commit8452b199d28bea53bf2c5e3b3d604064000fc73d (patch)
treefd3f6f2dbbf64329a1a17dee0cfdcddce4814f27 /src
parentd20633705e53a122467fb39fdbb2de3cfec279f7 (diff)
inventory bar
Diffstat (limited to 'src')
-rw-r--r--src/inventory.cpp76
1 files changed, 41 insertions, 35 deletions
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 <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;
+ }
+ }
+}