[Inventory|InventoryWidget|ItemWidget] Added. Need to add ITransform to go further.

This commit is contained in:
Quentin Bazin 2018-06-21 01:32:55 +02:00
parent 2892475ecc
commit 05957d4552
9 changed files with 196 additions and 13 deletions

View File

@ -1,4 +1,4 @@
# Generated by YCM Generator at 2018-06-20 23:22:48.494654 # Generated by YCM Generator at 2018-06-21 01:05:00.362117
# This file is NOT licensed under the GPLv3, which is the license for the rest # This file is NOT licensed under the GPLv3, which is the license for the rest
# of YouCompleteMe. # of YouCompleteMe.
@ -41,6 +41,7 @@ flags = [
'-I/home/bazin_q/rendu/Perso/KubKraft/include/gl', '-I/home/bazin_q/rendu/Perso/KubKraft/include/gl',
'-I/home/bazin_q/rendu/Perso/KubKraft/include/gui', '-I/home/bazin_q/rendu/Perso/KubKraft/include/gui',
'-I/home/bazin_q/rendu/Perso/KubKraft/include/hud', '-I/home/bazin_q/rendu/Perso/KubKraft/include/hud',
'-I/home/bazin_q/rendu/Perso/KubKraft/include/inventory',
'-I/home/bazin_q/rendu/Perso/KubKraft/include/states', '-I/home/bazin_q/rendu/Perso/KubKraft/include/states',
'-I/home/bazin_q/rendu/Perso/KubKraft/include/system', '-I/home/bazin_q/rendu/Perso/KubKraft/include/system',
'-I/home/bazin_q/rendu/Perso/KubKraft/include/utils', '-I/home/bazin_q/rendu/Perso/KubKraft/include/utils',

View File

@ -0,0 +1,32 @@
/*
* =====================================================================================
*
* Filename: InventoryWidget.hpp
*
* Description:
*
* Created: 21/06/2018 01:08:40
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef INVENTORYWIDGET_HPP_
#define INVENTORYWIDGET_HPP_
#include <vector>
#include "Inventory.hpp"
#include "ItemWidget.hpp"
class InventoryWidget : public IDrawable {
public:
void update(const Inventory &inventory);
private:
void draw(RenderTarget &target, RenderStates states) const override;
std::vector<ItemWidget> m_itemWidgets;
};
#endif // INVENTORYWIDGET_HPP_

View File

@ -0,0 +1,30 @@
/*
* =====================================================================================
*
* Filename: ItemWidget.hpp
*
* Description:
*
* Created: 21/06/2018 01:10:13
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef ITEMWIDGET_HPP_
#define ITEMWIDGET_HPP_
#include "Image.hpp"
class ItemWidget : public IDrawable {
public:
ItemWidget(u16 id);
private:
void draw(RenderTarget &target, RenderStates states) const override;
Texture m_texture;
Image m_image;
};
#endif // ITEMWIDGET_HPP_

View File

@ -14,9 +14,11 @@
#ifndef WORKBENCHWIDGET_HPP_ #ifndef WORKBENCHWIDGET_HPP_
#define WORKBENCHWIDGET_HPP_ #define WORKBENCHWIDGET_HPP_
#include "Image.hpp" #include "InventoryWidget.hpp"
#include "SDLHeaders.hpp" #include "SDLHeaders.hpp"
#include "Inventory.hpp"
class WorkbenchWidget : public IDrawable { class WorkbenchWidget : public IDrawable {
public: public:
WorkbenchWidget(); WorkbenchWidget();
@ -29,7 +31,8 @@ class WorkbenchWidget : public IDrawable {
Texture m_backgroundTexture; Texture m_backgroundTexture;
Image m_background; Image m_background;
Texture m_blocksTexture; Inventory m_inventory{9, 3};
InventoryWidget m_inventoryWidget;
}; };
#endif // WORKBENCHWIDGET_HPP_ #endif // WORKBENCHWIDGET_HPP_

View File

@ -0,0 +1,40 @@
/*
* =====================================================================================
*
* Filename: Inventory.hpp
*
* Description:
*
* Created: 21/06/2018 00:59:26
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef INVENTORY_HPP_
#define INVENTORY_HPP_
#include <vector>
#include "Types.hpp"
class Inventory {
public:
Inventory(u16 width, u16 height)
: m_width(width), m_height(height) {}
void addItem(u16 id);
u16 width() const { return m_width; }
u16 height() const { return m_height; }
const std::vector<u16> &items() const { return m_items; }
private:
u16 m_width;
u16 m_height;
std::vector<u16> m_items;
};
#endif // INVENTORY_HPP_

View File

@ -0,0 +1,29 @@
/*
* =====================================================================================
*
* Filename: InventoryWidget.cpp
*
* Description:
*
* Created: 21/06/2018 01:09:20
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "InventoryWidget.hpp"
void InventoryWidget::update(const Inventory &inventory) {
m_itemWidgets.clear();
for (u16 id : inventory.items()) {
// TODO: Set the right position
m_itemWidgets.emplace_back(id);
}
}
void InventoryWidget::draw(RenderTarget &target, RenderStates states) const {
for (auto &it : m_itemWidgets)
target.draw(it, states);
}

27
source/gui/ItemWidget.cpp Normal file
View File

@ -0,0 +1,27 @@
/*
* =====================================================================================
*
* Filename: ItemWidget.cpp
*
* Description:
*
* Created: 21/06/2018 01:11:11
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "ItemWidget.hpp"
ItemWidget::ItemWidget(u16 id) {
m_texture.load("textures/blocks.png");
m_image.load(m_texture);
m_image.setClipRect(id * 16, 0, 16, 16);
m_image.setPosRect(10, 10, 16 * 2, 16 * 2);
}
void ItemWidget::draw(RenderTarget &target, RenderStates states) const {
target.draw(m_image, states);
}

View File

@ -23,7 +23,10 @@ WorkbenchWidget::WorkbenchWidget() {
m_background.clipRect().width * 3, m_background.clipRect().width * 3,
m_background.clipRect().height * 3); m_background.clipRect().height * 3);
m_blocksTexture.load("textures/blocks.png"); for (u16 i = 1 ; i < 10 ; ++i)
m_inventory.addItem(i);
m_inventoryWidget.update(m_inventory);
} }
void WorkbenchWidget::onEvent(const SDL_Event &event) { void WorkbenchWidget::onEvent(const SDL_Event &event) {
@ -31,15 +34,14 @@ void WorkbenchWidget::onEvent(const SDL_Event &event) {
void WorkbenchWidget::draw(RenderTarget &target, RenderStates states) const { void WorkbenchWidget::draw(RenderTarget &target, RenderStates states) const {
target.draw(m_background, states); target.draw(m_background, states);
target.draw(m_inventoryWidget, states);
for (u16 i = 1 ; i < 10 ; ++i) { // Image image;
Image image; // image.load(m_blocksTexture);
image.load(m_blocksTexture); // image.setClipRect(i * 16, 0, 16, 16);
image.setClipRect(i * 16, 0, 16, 16); // image.setPosRect(m_background.posRect().x + 10.5 * 3 + (i - 1) * 27 * 2,
image.setPosRect(m_background.posRect().x + 10.5 * 3 + (i - 1) * 27 * 2, // m_background.posRect().y + 86.5 * 3,
m_background.posRect().y + 86.5 * 3, // 16 * 2, 16 * 2);
16 * 2, 16 * 2); // target.draw(image, states);
target.draw(image, states);
}
} }

View File

@ -0,0 +1,19 @@
/*
* =====================================================================================
*
* Filename: Inventory.cpp
*
* Description:
*
* Created: 21/06/2018 01:03:48
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "Inventory.hpp"
void Inventory::addItem(u16 id) {
m_items.emplace_back(id);
}