[Image] Added. [Hotbar] WIP.

This commit is contained in:
Quentin Bazin 2018-06-20 05:43:52 +02:00
parent 46fb26ab76
commit d9038e6cb5
13 changed files with 348 additions and 8 deletions

8
Notes Normal file
View File

@ -0,0 +1,8 @@
Notes
• GUI Texture:
• Button: 200x20 (0;46, 0;66, 0;86)
• Hotbar: 182x22
• Selection: 24x24 (0;22)

2
TODO
View File

@ -9,7 +9,7 @@ TODO
• TODO: Remplacer les anciennes classes par leur upgrade:
◦ DONE: `ApplicationState` / `ApplicationStateStack`
◦ DONE: `GameClock`
◦ TODO: `Window` (depuis `ZeldaOOL`)
◦ TODO: `Window` et `Texture` (depuis `ZeldaOOL`)
◦ TODO: `Debug` / `Exception`
◦ TODO: Input system (Mouse + Keyboard)
• TODO: Catch SDLLoader exception

115
include/core/Rect.hpp Normal file
View File

@ -0,0 +1,115 @@
/*
* =====================================================================================
*
* Filename: Rect.hpp
*
* Description:
*
* Created: 15/09/2014 22:13:37
*
* Author: Quentin Bazin, <gnidmoo@gmail.com>
*
* =====================================================================================
*/
#ifndef RECT_HPP_
#define RECT_HPP_
#include <algorithm>
// #include "Vector2.hpp"
template<typename T>
class Rect {
public:
Rect() = default;
Rect(T _x, T _y, T _width, T _height) {
reset(_x, _y, _width, _height);
}
// Rect(const Vector2<T> &_position, const Vector2<T> &_size) {
// reset(_position.x, _position.y, _size.x, _size.y);
// }
template<typename U>
Rect(const Rect<U> &rect)
: Rect(rect.x, rect.y, rect.width, rect.height) {}
void reset(T _x, T _y, T _width, T _height) {
x = _x;
y = _y;
width = _width;
height = _height;
}
void reset(Rect<T> rect) { reset(rect.x, rect.y, rect.width, rect.height); }
void move(T _x, T _y) { x += _x; y += _y; }
// void move(Vector2<T> d) { move(d.x, d.y); }
bool intersects(const Rect<T> &rect) const {
T r1MinX = std::min(x, static_cast<T>(x + width));
T r1MaxX = std::max(x, static_cast<T>(x + width));
T r1MinY = std::min(y, static_cast<T>(y + height));
T r1MaxY = std::max(y, static_cast<T>(y + height));
T r2MinX = std::min(rect.x, static_cast<T>(rect.x + rect.width));
T r2MaxX = std::max(rect.x, static_cast<T>(rect.x + rect.width));
T r2MinY = std::min(rect.y, static_cast<T>(rect.y + rect.height));
T r2MaxY = std::max(rect.y, static_cast<T>(rect.y + rect.height));
T interLeft = std::max(r1MinX, r2MinX);
T interTop = std::max(r1MinY, r2MinY);
T interRight = std::min(r1MaxX, r2MaxX);
T interBottom = std::min(r1MaxY, r2MaxY);
return interLeft < interRight && interTop < interBottom;
}
T intersectionDirection(const Rect<T> &rect) const {
T r1MinX = std::min(x, static_cast<T>(x + width));
T r1MaxX = std::max(x, static_cast<T>(x + width));
T r1MinY = std::min(y, static_cast<T>(y + height));
T r1MaxY = std::max(y, static_cast<T>(y + height));
T r2MinX = std::min(rect.x, static_cast<T>(rect.x + rect.width));
T r2MaxX = std::max(rect.x, static_cast<T>(rect.x + rect.width));
T r2MinY = std::min(rect.y, static_cast<T>(rect.y + rect.height));
T r2MaxY = std::max(rect.y, static_cast<T>(rect.y + rect.height));
T interLeft = std::max(r1MinX, r2MinX);
T interTop = std::max(r1MinY, r2MinY);
T interRight = std::min(r1MaxX, r2MaxX);
T interBottom = std::min(r1MaxY, r2MaxY);
if(interLeft < interRight && interTop < interBottom) {
if(interRight - interLeft < interBottom - interTop) {
return 1;
} else {
return 2;
}
} else {
return 0;
}
}
// Vector2<T> position() const { return {x, y}; }
// void setPosition(Vector2<T> vector2) { x = vector2.x; y = vector2.y; }
// Rect operator+(const Vector2<T> &vector2) const { return Rect{x + vector2.x, y + vector2.y, width, height}; }
// Rect operator-(const Vector2<T> &vector2) const { return Rect{x - vector2.x, y - vector2.y, width, height}; }
//
// Rect &operator+=(const Vector2<T> &vector2) { *this = operator+(vector2); return *this; }
// Rect &operator-=(const Vector2<T> &vector2) { *this = operator-(vector2); return *this; }
T x = 0;
T y = 0;
T width = 0;
T height = 0;
};
using IntRect = Rect<int>;
using FloatRect = Rect<float>;
#endif // RECT_HPP_

View File

@ -21,9 +21,9 @@
class Texture {
public:
Texture();
Texture() = default;
Texture(const std::string &filename);
~Texture();
~Texture() noexcept;
void load(const std::string &filename);

30
include/hud/Hotbar.hpp Normal file
View File

@ -0,0 +1,30 @@
/*
* =====================================================================================
*
* Filename: Hotbar.hpp
*
* Description:
*
* Created: 20/06/2018 05:40:05
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#ifndef HOTBAR_HPP_
#define HOTBAR_HPP_
#include "Image.hpp"
class Hotbar : public IDrawable {
public:
Hotbar();
private:
void draw(RenderTarget &target, RenderStates states) const override;
Texture m_texture;
Image m_background;
};
#endif // HOTBAR_HPP_

51
include/hud/Image.hpp Normal file
View File

@ -0,0 +1,51 @@
/*
* =====================================================================================
*
* Filename: Image.hpp
*
* Description:
*
* Created: 20/09/2014 16:21:56
*
* Author: Quentin Bazin, <gnidmoo@gmail.com>
*
* =====================================================================================
*/
#ifndef IMAGE_HPP_
#define IMAGE_HPP_
#include "IDrawable.hpp"
#include "Rect.hpp"
#include "Texture.hpp"
#include "VertexBuffer.hpp"
class Image : public IDrawable {
public:
Image() = default;
Image(const Texture &texture);
void load(const Texture &texture);
void setClipRect(float x, float y, u16 width, u16 height);
void setPosRect(float x, float y, u16 width, u16 height);
u16 width() const { return m_width; }
u16 height() const { return m_height; }
private:
void updateVertexBuffer() const;
void draw(RenderTarget &target, RenderStates states) const override;
const Texture *m_texture = nullptr;
u16 m_width = 0;
u16 m_height = 0;
FloatRect m_clipRect;
FloatRect m_posRect;
VertexBuffer m_vbo;
};
#endif // IMAGE_HPP_

View File

@ -20,6 +20,7 @@
#include "BlockCursor.hpp"
#include "Camera.hpp"
#include "Crosshair.hpp"
#include "Hotbar.hpp"
#include "Skybox.hpp"
#include "World.hpp"
@ -54,6 +55,9 @@ class GameState : public ApplicationState {
glm::vec4 m_selectedBlock{0, 0, 0, -1};
BlockCursor m_blockCursor{m_camera, m_world, m_viewMatrix, m_projectionMatrix};
Crosshair m_crosshair;
Texture m_widgetTexture;
Hotbar m_hotbar;
};
#endif // GAMESTATE_HPP_

View File

@ -16,7 +16,7 @@ vec4 fog(vec4 color, float fogCoord, float fogStart, float fogEnd);
void main() {
// Discard if the pixel is too far away
if(v_dist > u_renderDistance) discard;
if(v_blockID != -1 && v_dist > u_renderDistance) discard;
vec4 color = getColor();
if (v_blockID == 8) {

View File

@ -15,14 +15,12 @@
#include "SDLHeaders.hpp"
#include "Texture.hpp"
Texture::Texture() {
}
Texture::Texture(const std::string &filename) {
load(filename);
}
Texture::~Texture() {
Texture::~Texture() noexcept {
glDeleteTextures(1, &m_texture);
}
void Texture::load(const std::string &filename) {

27
source/hud/Hotbar.cpp Normal file
View File

@ -0,0 +1,27 @@
/*
* =====================================================================================
*
* Filename: Hotbar.cpp
*
* Description:
*
* Created: 20/06/2018 05:40:47
*
* Author: Quentin Bazin, <quent42340@gmail.com>
*
* =====================================================================================
*/
#include "Config.hpp"
#include "Hotbar.hpp"
Hotbar::Hotbar() {
m_texture.load("textures/widgets.png");
m_background.load(m_texture);
m_background.setClipRect(0, 0, 182, 22);
m_background.setPosRect(SCREEN_WIDTH / 2 - 182 * 3 / 2, SCREEN_HEIGHT - 22 * 3, 182 * 3, 22 * 3);
}
void Hotbar::draw(RenderTarget &target, RenderStates states) const {
target.draw(m_background, states);
}

106
source/hud/Image.cpp Normal file
View File

@ -0,0 +1,106 @@
/*
* =====================================================================================
*
* Filename: Image.cpp
*
* Description:
*
* Created: 20/09/2014 16:22:12
*
* Author: Quentin Bazin, <gnidmoo@gmail.com>
*
* =====================================================================================
*/
#define GLM_FORCE_RADIANS
#include <glm/gtc/matrix_transform.hpp>
#include "Application.hpp"
#include "Config.hpp"
#include "Image.hpp"
#include "Vertex.hpp"
Image::Image(const Texture &texture) {
load(texture);
}
void Image::load(const Texture &texture) {
m_texture = &texture;
m_width = m_texture->width();
m_height = m_texture->height();
m_posRect = FloatRect(0, 0, m_width, m_height);
m_clipRect = FloatRect(0, 0, m_width, m_height);
updateVertexBuffer();
}
void Image::setClipRect(float x, float y, u16 width, u16 height) {
m_clipRect.reset(x, y, width, height);
updateVertexBuffer();
}
void Image::setPosRect(float x, float y, u16 width, u16 height) {
m_posRect.reset(x, y, width, height);
updateVertexBuffer();
}
void Image::updateVertexBuffer() const {
Vertex vertices[4] = {
{{m_posRect.width, 0, 0, -1}},
{{0, 0, 0, -1}},
{{0, m_posRect.height, 0, -1}},
{{m_posRect.width, m_posRect.height, 0, -1}},
};
FloatRect texRect = FloatRect(
m_clipRect.x / float(m_width),
m_clipRect.y / float(m_height),
m_clipRect.width / float(m_width),
m_clipRect.height / float(m_height)
);
vertices[0].texCoord[0] = texRect.x + texRect.width;
vertices[0].texCoord[1] = texRect.y;
vertices[1].texCoord[0] = texRect.x;
vertices[1].texCoord[1] = texRect.y;
vertices[2].texCoord[0] = texRect.x;
vertices[2].texCoord[1] = texRect.y + texRect.height;
vertices[3].texCoord[0] = texRect.x + texRect.width;
vertices[3].texCoord[1] = texRect.y + texRect.height;
// GLubyte indices[] = {
// 0, 1, 3,
// 3, 1, 2
// };
VertexBuffer::bind(&m_vbo);
m_vbo.setData(sizeof(vertices), vertices, GL_DYNAMIC_DRAW);
VertexBuffer::bind(nullptr);
}
void Image::draw(RenderTarget &target, RenderStates states) const {
states.viewMatrix = nullptr;
glm::mat4 projectionMatrix = glm::ortho(0.0f, (float)SCREEN_WIDTH, (float)SCREEN_HEIGHT, 0.0f);
states.projectionMatrix = &projectionMatrix;
glm::mat4 modelMatrix = glm::translate(glm::mat4{1.0f}, glm::vec3{m_posRect.x, m_posRect.y, 0});
states.modelMatrix = &modelMatrix;
states.texture = m_texture;
glDisable(GL_BLEND);
glDisable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
// glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, indices);
target.draw(m_vbo, GL_QUADS, 0, 4, states);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
}

View File

@ -82,5 +82,6 @@ void GameState::draw(RenderTarget &target, RenderStates states) const {
target.draw(m_world, states);
target.draw(m_crosshair, states);
target.draw(m_blockCursor, states);
target.draw(m_hotbar, states);
}

BIN
textures/widgets.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB