Skip to content
Snippets Groups Projects
Commit 97171b7b authored by BATON Theau's avatar BATON Theau
Browse files

Refactoring code

parent bd4dfe88
No related branches found
No related tags found
No related merge requests found
Showing
with 110 additions and 211 deletions
#pragma once
#include "Module.hpp"
#include <engine/graphics/back/buffers/VertexArray.hpp>
#include <engine/graphics/back/buffers/VerticeBuffer.hpp>
#include <engine/graphics/back/shaders/Program.hpp>
#include <engine/graphics/front/object/Text.hpp>
#include <engine/utility/ref_set.hpp>
namespace megu {
class Text_Module : public Module<Text> {
public:
Text_Module();
virtual void draw(const Text &, const Camera &, const Window &) const override;
private:
VertexArray _vao;
VerticeBuffer _vbo;
Program _program;
};
}
\ No newline at end of file
...@@ -34,15 +34,15 @@ namespace megu { ...@@ -34,15 +34,15 @@ namespace megu {
size_t count = 0; size_t count = 0;
for(size_t x = 0; x < vertexArray.width(); ++x) { for(size_t x = 0; x < vertexArray.row(); ++x) {
for(size_t y = 0; y < vertexArray.height(); ++y) { for(size_t y = 0; y < vertexArray.collumn(); ++y) {
uOffsets.push_back(glm::translate(glm::mat4(1.f), {x, y, 0.f})); uOffsets.push_back(glm::translate(glm::mat4(1.f), {x, y, 0.f}));
uUvs.push_back(vertexArray.uvs()[x][y]); uUvs.push_back(vertexArray.uvs()[x][y]);
if(count > 128) { if(count > 128) {
this->_program.setUniform("uOffsets", uOffsets); this->_program.setUniform("uOffsets", uOffsets);
this->_program.setUniform("uUvs", uUvs); this->_program.setUniform("uUvs", uUvs);
glDrawArraysInstanced(TileArray::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(vertexArray.width() * vertexArray.height())); glDrawArraysInstanced(TileArray::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(vertexArray.row() * vertexArray.collumn()));
uOffsets.clear(); uOffsets.clear();
uUvs.clear(); uUvs.clear();
...@@ -56,7 +56,7 @@ namespace megu { ...@@ -56,7 +56,7 @@ namespace megu {
this->_program.setUniform("uOffsets", uOffsets); this->_program.setUniform("uOffsets", uOffsets);
this->_program.setUniform("uUvs", uUvs); this->_program.setUniform("uUvs", uUvs);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawArraysInstanced(TileArray::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(vertexArray.width() * vertexArray.height())); glDrawArraysInstanced(TileArray::Primitive(), 0, static_cast<GLsizei>(this->_vbo.size()), static_cast<GLsizei>(vertexArray.row() * vertexArray.collumn()));
//glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); //glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
} }
} }
......
...@@ -30,12 +30,12 @@ namespace megu { ...@@ -30,12 +30,12 @@ namespace megu {
} }
void Image::load(const TextureBuffer & buffer) { void Image::load(const TextureBuffer & buffer) {
this->setSize({buffer.width(), buffer.height()}); this->setSize(static_cast<float>(buffer.width()), static_cast<float>(buffer.height()));
this->_texture.store(buffer); this->_texture.store(buffer);
} }
void Image::link(const Texture & texture) { void Image::link(const Texture & texture) {
this->setSize({texture.width(), texture.height()}); this->setSize(static_cast<float>(texture.width()), static_cast<float>(texture.height()));
this->_texture = texture; this->_texture = texture;
} }
} }
\ No newline at end of file
...@@ -19,25 +19,23 @@ namespace megu { ...@@ -19,25 +19,23 @@ namespace megu {
Image(const Image &) = default; Image(const Image &) = default;
Image operator=(const Image &); Image operator=(const Image &);
Image operator=(const Image &&); Image operator=(const Image &&);
~Image() = default; virtual ~Image() = default;
inline Vec2 getPosition() const {return Vec2(this->_transformation.x(), this->_transformation.y());} inline Vec2 getPosition() const {return Vec2(this->_transformation.x(), this->_transformation.y());}
inline Vec2 getOrigine() const {return Vec2(this->_transformation.origine().x, this->_transformation.origine().y);} inline Vec2 getOrigine() const {return Vec2(this->_transformation.origine().x, this->_transformation.origine().y);}
inline Vec2 getSize() const {return Vec2(this->_transformation.scaling().x, this->_transformation.scaling().y);} inline Vec2 getSize() const {return Vec2(this->_transformation.scaling().x, this->_transformation.scaling().y);}
inline float getRotation() const {return this->_transformation.roll();} inline float getRotation() const {return this->_transformation.roll();}
inline float getLayer() const {return this->_transformation.z();}
inline const Texture & texture() const {return this->_texture;} inline const Texture & texture() const {return this->_texture;}
inline const Transformable & transformation() const {return this->_transformation;} inline const Transformable & transformation() const {return this->_transformation;}
inline void setPosition(const Vec2 & pos) {this->_transformation.setPosition(pos.x, pos.y);} inline void setPosition(float x, float y) {this->_transformation.setPosition(x, y);}
inline void setOrigine(const Vec2 & pos) {this->_transformation.setOrigine(pos.x, pos.y);} inline void setOrigine(float x, float y) {this->_transformation.setOrigine(x, y);}
inline void setSize(const Vec2 & size) {this->_transformation.setScaling(size.x, size.y);} inline void setSize(float w, float h) {this->_transformation.setScaling(w, h);}
inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);} inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);}
inline void setLayer(float l) {this->_transformation.setZ(l);}
inline void move(const Vec2 & pos) {this->_transformation.move({pos.x, pos.y, 0.f});} inline void move(float x, float y) {this->_transformation.move({x, y, 0.f});}
inline void scale(const Vec2 & size) {this->_transformation.scale({size.x, size.y, 0.f});} inline void scale(float w, float h) {this->_transformation.scale({w, h, 0.f});}
inline void rotate(float a) {this->_transformation.rotate(a, Transformable::Axis::Z);} inline void rotate(float a) {this->_transformation.rotate(a, Transformable::Axis::Z);}
void load(const std::filesystem::path &, bool = false); void load(const std::filesystem::path &, bool = false);
......
#include "Sprite.hpp" #include "Sprite.hpp"
namespace megu { namespace megu {
Sprite::Sprite()
: _frame(0, 0, 0, 0) {}
Sprite::Sprite(const std::filesystem::path & path, bool flip) Sprite::Sprite(const std::filesystem::path & path, bool flip)
: _frame(0, 0, 0, 0) { : _frame(0, 0, 0, 0) {
this->load(path, flip); this->load(path, flip);
...@@ -18,12 +21,12 @@ namespace megu { ...@@ -18,12 +21,12 @@ namespace megu {
} }
void Sprite::load(const TextureBuffer & buffer) { void Sprite::load(const TextureBuffer & buffer) {
this->setSize({buffer.width(), buffer.height()}); this->setSize(static_cast<float>(buffer.width()), static_cast<float>(buffer.height()));
this->_texture.store(buffer); this->_texture.store(buffer);
} }
void Sprite::link(const Texture & texture) { void Sprite::link(const Texture & texture) {
this->setSize({texture.width(), texture.height()}); this->setSize(static_cast<float>(texture.width()), static_cast<float>(texture.height()));
this->_texture = texture; this->_texture = texture;
} }
} }
\ No newline at end of file
...@@ -13,35 +13,32 @@ namespace megu { ...@@ -13,35 +13,32 @@ namespace megu {
public: public:
using Frame = glm::vec4; using Frame = glm::vec4;
Sprite();
Sprite(const std::filesystem::path &, bool = true); Sprite(const std::filesystem::path &, bool = true);
Sprite(const std::filesystem::path &, const Frame &, bool = true); Sprite(const std::filesystem::path &, const Frame &, bool = true);
virtual ~Sprite() = default;
inline void setFrame(const Frame & frame) {this->_frame = frame;} inline const Vec2 & getPosition() const {return Vec2(this->_transformation.x(), this->_transformation.y());}
inline const Vec2 & getOrigine() const {return Vec2(this->_transformation.origine().x, this->_transformation.origine().y);}
inline const Vec2 & getSize() const {return Vec2(this->_transformation.scaling().x, this->_transformation.scaling().y);}
inline const Frame & getFrame() const {return this->_frame;}
inline float getrotation() const {return this->_transformation.rotation().z;}
inline void setPosition(const Vec2 & pos) {this->_transformation.setPosition(pos.x, pos.y);} inline const Texture & texture() const {return this->_texture;}
inline void setOrigine(const Vec2 & pos) {this->_transformation.setOrigine(pos.x, pos.y);} inline const Transformable & transformation() const {return this->_transformation;}
inline void setSize(const Vec2 & size) {this->_transformation.setScaling(size.x, size.y);}
inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);}
inline const Vec3 & position() const {return this->_transformation.position();} inline void setPosition(float x, float y) {this->_transformation.setPosition(x, y);}
inline const Vec3 & origine() const {return this->_transformation.origine();} inline void setOrigine(float x, float y) {this->_transformation.setOrigine(x, y);}
inline const Vec3 & rotation() const {return this->_transformation.rotation();} inline void setSize(float w, float h) {this->_transformation.setScaling(w, h);}
inline const Vec2 & size() const {return Vec2(this->_transformation.scaling().x, this->_transformation.scaling().y);} inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);}
inline void setFrame(const Frame & frame) {this->_frame = frame;}
inline float x() const {return this->_transformation.x();} inline void setFrame(float x, float y, float w, float h) {this->_frame = Frame(x, y, w ,h);}
inline float y() const {return this->_transformation.y();}
inline float z() const {return this->_transformation.z();}
inline void move(const Vec2 & pos) {this->_transformation.move({pos.x, pos.y, 0.f});} inline void move(float x, float y) {this->_transformation.move({x, y, 0.f});}
inline void scale(const Vec2 & size) {this->_transformation.scale({size.x, size.y, 0.f});} inline void scale(float w, float h) {this->_transformation.scale({w, h, 0.f});}
inline void rotate(float a) {this->_transformation.rotate(a, Transformable::Axis::Z);} inline void rotate(float a) {this->_transformation.rotate(a, Transformable::Axis::Z);}
inline void setSize(float w, float h) {this->_transformation.setScaling(w, h);}
inline const Frame & frame() const {return this->_frame;}
inline const Texture & texture() const {return this->_texture;}
inline const Transformable & transformation() const {return this->_transformation;}
void load(const std::filesystem::path &, bool = false); void load(const std::filesystem::path &, bool = false);
void load(const TextureBuffer &); void load(const TextureBuffer &);
void link(const Texture &); void link(const Texture &);
......
#include "Text.hpp"
#include <exception>
#include <iostream>
namespace megu {
Text::Text(const std::string & text)
: _text(text) {}
Text::Text(const Text & src)
: _text(src._text), _characters(src._characters), _transformation(src._transformation) {}
Text & Text::operator=(const Text & src) {
this->_text = src._text;
this->_characters = src._characters;
this->_transformation = src._transformation;
return *this;
}
void Text::loadFont(const std::filesystem::path & file, bool smoothing) {
if(!std::filesystem::exists(file) || std::filesystem::is_directory(file)) {
throw std::runtime_error("Cannot load font file" + file.string() + ".");
}
TextureBuffer buffer(file);
this->_texture.store(buffer);
}
}
#pragma once
#include <utility/Identifiable.hpp>
#include <string>
#include <filesystem>
#include <map>
#include <engine/graphics/back/geometry/Transformable.hpp>
#include <engine/graphics/back/textures/Texture.hpp>
#include <engine/graphics/front/geometry/Quads.hpp>
#include <engine/utility/type.hpp>
namespace megu {
class Text : public Quads {
public:
using Frame = Vec4Int;
using CharacterMap = std::map<unsigned char, Frame>;
Text(const std::string & = "");
Text(const Text &);
Text & operator=(const Text &);
~Text() = default;
inline Vec2 getPosition() const {return Vec2(this->_transformation.x(), this->_transformation.y());}
inline Vec2 getOrigine() const {return Vec2(this->_transformation.origine().x, this->_transformation.origine().y);}
inline Vec2 getSize() const {return Vec2(this->_transformation.scaling().x, this->_transformation.scaling().y);}
inline float getRotation() const {return this->_transformation.roll();}
inline float getLayer() const {return this->_transformation.z();}
inline const Texture & texture() const {return this->_texture;}
inline const Transformable & transformation() const {return this->_transformation;}
inline const std::string & text() const {return this->_text;}
inline const CharacterMap & characters() const {return this->_characters;}
inline void setPosition(const Vec2 & pos) {this->_transformation.setPosition(pos.x, pos.y);}
inline void setOrigine(const Vec2 & pos) {this->_transformation.setOrigine(pos.x, pos.y);}
inline void setSize(const Vec2 & size) {this->_transformation.setScaling(size.x, size.y);}
inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);}
inline void setLayer(float l) {this->_transformation.setZ(l);}
inline void move(const Vec2 & pos) {this->_transformation.move({pos.x, pos.y, 0.f});}
inline void scale(const Vec2 & size) {this->_transformation.scale({size.x, size.y, 0.f});}
inline void rotate(float a) {this->_transformation.rotate(a, Transformable::Axis::Z);}
inline void addGlyph(unsigned char c, Frame f) {this->_characters.insert(std::pair<unsigned char, Frame>(c, f));}
inline void removeGlyph(unsigned char c) {this->_characters.erase(c);}
inline void clearGlyph() {return this->_characters.clear();}
void loadFont(const std::filesystem::path &, bool = true);
inline const Frame & operator[](unsigned char c) const {return this->_characters.at(c);}
private:
std::string _text;
Texture _texture;
CharacterMap _characters;
Transformable _transformation;
};
}
\ No newline at end of file
...@@ -3,17 +3,17 @@ ...@@ -3,17 +3,17 @@
#include <iostream> #include <iostream>
namespace megu { namespace megu {
TileArray::TileArray(const std::filesystem::path & path, size_t width, size_t height, float tileSize, size_t lenght) TileArray::TileArray(const std::filesystem::path & path, size_t row, size_t col, float tileSize, size_t lenght)
: _width(width), _height(height), _tileSize(tileSize) { : _row(row), _col(col), _tileSize(tileSize) {
megu::TextureBuffer buffer(path); megu::TextureBuffer buffer(path);
this->_texture.store(buffer); this->_texture.store(buffer);
float twidth = static_cast<float>(this->_texture.width()); float twidth = static_cast<float>(this->_texture.width());
float theight = static_cast<float>(this->_texture.height()); float theight = static_cast<float>(this->_texture.height());
for(size_t x = 0; x < width; ++x) { for(size_t x = 0; x < row; ++x) {
std::vector<glm::vec4> rows; std::vector<glm::vec4> rows;
for(size_t y = 0; y < height; ++y) { for(size_t y = 0; y < col; ++y) {
rows.push_back({0.f, this->_texture.height() - lenght, lenght, lenght}); rows.push_back({0.f, this->_texture.height() - lenght, lenght, lenght});
} }
this->_uvs.push_back(rows); this->_uvs.push_back(rows);
......
...@@ -13,35 +13,45 @@ ...@@ -13,35 +13,45 @@
namespace megu { namespace megu {
class TileArray : public Quads { class TileArray : public Quads {
public: public:
TileArray() = delete;
TileArray(const std::filesystem::path &, size_t, size_t, float, size_t); TileArray(const std::filesystem::path &, size_t, size_t, float, size_t);
virtual ~TileArray() = default;
inline void setPosition(const Vec2 & pos) {this->_transformation.setPosition(pos.x, pos.y);} inline Vec2 getPosition() const {return Vec2(this->_transformation.x(), this->_transformation.y());}
inline void setOrigine(const Vec2 & pos) {this->_transformation.setOrigine(pos.x, pos.y);} inline Vec2 getOrigine() const {return Vec2(this->_transformation.origine().x, this->_transformation.origine().y);}
inline void setSize(const Vec2 & size) {this->_transformation.setScaling(size.x, size.y);} inline Vec2 getSize() const {return Vec2(this->_transformation.scaling().x, this->_transformation.scaling().y);}
inline float getrotation() const {return this->_transformation.rotation().z;}
inline void setPosition(float x, float y) {this->_transformation.setPosition(x, y);}
inline void setOrigine(float x, float y) {this->_transformation.setOrigine(x, y);}
inline void setSize(float w, float h) {this->_transformation.setScaling(w, h);}
inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);} inline void setRotation(float a) {this->_transformation.setRotation(a, Transformable::Axis::Z);}
inline void setLayer(float l) {this->_transformation.setZ(l);} inline void setLayer(float l) {this->_transformation.setZ(l);}
inline void move(const Vec2 & pos) {this->_transformation.move({pos.x, pos.y, 0.f});} inline void move(float x, float y) {this->_transformation.move(x, y);}
inline void scale(const Vec2 & size) {this->_transformation.scale({size.x, size.y, 0.f});} inline void scale(float w, float h) {this->_transformation.scale(w, h);}
inline void rotate(float a) {this->_transformation.rotate(a, Transformable::Axis::Z);} inline void rotate(float a) {this->_transformation.rotate(a, Transformable::Axis::Z);}
inline size_t width() const {return this->_width;} inline size_t row() const {return this->_row;}
inline size_t height() const {return this->_height;} inline size_t collumn() const {return this->_col;}
inline float getTileSize() const {return this->_tileSize;} inline float tileSize() const {return this->_tileSize;}
inline const Vec3 & getSize() const {return this->_transformation.scaling();}
inline void setUv(size_t x, size_t y, const glm::vec4 & uv) {this->_uvs[y][x] = uv;} inline void setUv(size_t x, size_t y, const glm::vec4 & uv) {this->_uvs[y][x] = uv;}
inline const glm::vec4 & getUv(size_t x, size_t y) const {return this->_uvs[y][x];}
inline const Transformable & transformation() const {return this->_transformation;} inline const Transformable & transformation() const {return this->_transformation;}
inline const Texture & texture() const {return this->_texture;} inline const Texture & texture() const {return this->_texture;}
inline GLint getTextureWidth() const {return this->_texture.width();}
inline GLint getTextureHeight() const {return this->_texture.height();}
inline const std::vector<std::vector<glm::vec4>> & uvs() const {return this->_uvs;} inline const std::vector<std::vector<glm::vec4>> & uvs() const {return this->_uvs;}
std::vector<glm::vec4> & operator[](size_t index) {return this->_uvs[index];} std::vector<glm::vec4> & operator[](size_t index) {return this->_uvs[index];}
private: private:
Transformable _transformation; Transformable _transformation;
Texture _texture; Texture _texture;
size_t _width, _height; size_t _row, _col;
float _tileSize; float _tileSize;
std::vector<std::vector<glm::vec4>> _uvs; std::vector<std::vector<glm::vec4>> _uvs;
}; };
......
...@@ -18,10 +18,6 @@ namespace megu { ...@@ -18,10 +18,6 @@ namespace megu {
return *this; return *this;
} }
void Position::setPosition(const glm::vec3 & position) {
this->_position = position;
}
void Position::move(const glm::vec3 & direction) { void Position::move(const glm::vec3 & direction) {
this->_position.x += direction.x; this->_position.x += direction.x;
this->_position.y += direction.y; this->_position.y += direction.y;
......
...@@ -10,15 +10,14 @@ namespace megu { ...@@ -10,15 +10,14 @@ namespace megu {
Position(const glm::vec3 &); Position(const glm::vec3 &);
Position(const Position &); Position(const Position &);
Position operator=(const Position &); Position operator=(const Position &);
virtual ~Position() = default;
inline float x() const {return this->_position.x;} inline float x() const {return this->_position.x;}
inline float y() const {return this->_position.y;} inline float y() const {return this->_position.y;}
inline float z() const {return this->_position.z;} inline float z() const {return this->_position.z;}
inline const glm::vec3 & position() const {return this->_position;} inline void set(const glm::vec3 & position) {this->_position = position;}
inline void set(float x, float y, float z) {this->set({x, y, z});}
void setPosition(const glm::vec3 &);
inline void setPosition(float x, float y, float z) {this->setPosition({x, y, z});}
void move(const glm::vec3 &); void move(const glm::vec3 &);
inline void move(float x, float y, float z) {this->move({x, y, z});} inline void move(float x, float y, float z) {this->move({x, y, z});}
......
...@@ -15,27 +15,13 @@ namespace megu { ...@@ -15,27 +15,13 @@ namespace megu {
return *this; return *this;
} }
Cube SquareBox::asCube() const {
return {
this->_position,
this->_position + Position(this->_dimension.x, 0.f, 0.f),
this->_position + Position(0.f, 0.f, this->_dimension.z),
this->_position + Position(this->_dimension.x, 0.f, this->_dimension.z),
this->_position + Position(0.f, this->_dimension.y, 0.f),
this->_position + Position(this->_dimension.x, this->_dimension.y, 0.f),
this->_position + Position(0.f, this->_dimension.y, this->_dimension.z),
this->_position + Position(this->_dimension.x, this->_dimension.y, this->_dimension.z)
};
}
void SquareBox::move(const Direction & direction) { void SquareBox::move(const Direction & direction) {
this->_position.move(direction.x(), direction.y(), direction.z()); this->_position.move(direction.x(), direction.y(), direction.z());
} }
bool SquareBox::intersect(const SquareBox & squareBox) const { bool SquareBox::intersect(const SquareBox & squareBox) const {
const Position & a = squareBox.position(); const Position & a = squareBox._position;
const Dimension & as = squareBox.dimension(); const Dimension & as = squareBox._dimension;
const Position & b = this->_position; const Position & b = this->_position;
const Dimension & bs = this->_dimension; const Dimension & bs = this->_dimension;
...@@ -57,4 +43,8 @@ namespace megu { ...@@ -57,4 +43,8 @@ namespace megu {
bool SquareBox::operator!=(const SquareBox & squareBox) const { bool SquareBox::operator!=(const SquareBox & squareBox) const {
return !(*this == squareBox); return !(*this == squareBox);
} }
bool SquareBox::operator|(const SquareBox & squareBox) const {
return this->intersect(squareBox);
}
} }
\ No newline at end of file
...@@ -6,7 +6,6 @@ ...@@ -6,7 +6,6 @@
namespace megu { namespace megu {
using Dimension = glm::vec3; using Dimension = glm::vec3;
using Cube = std::array<Position, 8>;
class SquareBox { class SquareBox {
public: public:
...@@ -15,23 +14,31 @@ namespace megu { ...@@ -15,23 +14,31 @@ namespace megu {
SquareBox(const SquareBox &); SquareBox(const SquareBox &);
SquareBox operator=(const SquareBox &); SquareBox operator=(const SquareBox &);
inline const Position & position() const {return this->_position;} inline const Position & getPosition() const {return this->_position;}
inline const Dimension & dimension() const {return this->_dimension;} inline const Dimension & getDimension() const {return this->_dimension;}
inline float x() const {return this->_position.x();}
inline float y() const {return this->_position.y();}
inline float z() const {return this->_position.z();}
inline float width() const {return this->_dimension.x;} inline float width() const {return this->_dimension.x;}
inline float height() const {return this->_dimension.y;} inline float height() const {return this->_dimension.y;}
inline float depth() const {return this->_dimension.z;} inline float depth() const {return this->_dimension.z;}
inline void setPosition(const Position & position) {this->_position = position;} inline void setPosition(const Position & position) {this->_position = position;}
inline void setDimension(const Dimension & dimension) {this->_dimension = dimension;} inline void setPosition(float x, float y, float z = 0.f) {this->_position = Position(x, y, z);}
Cube asCube() const; inline void setDimension(const Dimension & dimension) {this->_dimension = dimension;}
inline void setDimension(float x, float y, float z = 0.f) {this->_dimension = Dimension(x, y, z);}
void move(const Direction &); void move(const Direction &);
inline void move(float x, float y, float z = 0.f) {this->move(Direction(x, y, z));}
bool intersect(const SquareBox &) const; bool intersect(const SquareBox &) const;
bool operator==(const SquareBox &) const; bool operator==(const SquareBox &) const;
bool operator!=(const SquareBox &) const; bool operator!=(const SquareBox &) const;
bool operator|(const SquareBox &) const;
private: private:
Position _position; Position _position;
......
...@@ -48,24 +48,24 @@ namespace megu { ...@@ -48,24 +48,24 @@ namespace megu {
void PhysicEngine::step(double delta, Priority priority) { void PhysicEngine::step(double delta, Priority priority) {
for(auto & source : this->_dynamic[priority]) { for(auto & source : this->_dynamic[priority]) {
source.get().update_physic(delta); source.get().updatePhysic(delta);
for(auto & target : this->_statics[priority]) { for(auto & target : this->_statics[priority]) {
if(source.get().isColliding(target)) { if(source.get() | target) {
this->_collisions.insert(Collision(source, target)); this->_collisions.insert(Collision(source, target));
this->_collisions.insert(Collision(target, source)); this->_collisions.insert(Collision(target, source));
} }
} }
for(auto & target : this->_dynamic[priority]) { for(auto & target : this->_dynamic[priority]) {
if(source.get().isColliding(target)) { if(source.get() | target) {
this->_collisions.insert(Collision(source, target)); this->_collisions.insert(Collision(source, target));
} }
} }
} }
} }
std::optional<SquareBox> PhysicEngine::makeCollision(const SquareBox & box, Priority priority) const { std::optional<SquareBox> PhysicEngine::checkCollision(const SquareBox & box, Priority priority) const {
for(const auto & source : this->_statics.at(priority)) { for(const auto & source : this->_statics.at(priority)) {
auto obox = source.get().isColliding(box); auto obox = source.get().isColliding(box);
if(obox.has_value()) { if(obox.has_value()) {
......
...@@ -28,7 +28,7 @@ namespace megu { ...@@ -28,7 +28,7 @@ namespace megu {
void step(double); void step(double);
void step(double, Priority); void step(double, Priority);
std::optional<SquareBox> makeCollision(const SquareBox &, Priority) const; std::optional<SquareBox> checkCollision(const SquareBox &, Priority) const;
std::optional<std::reference_wrapper<const Tangible>> get(const Identifiable &) const; std::optional<std::reference_wrapper<const Tangible>> get(const Identifiable &) const;
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
#include <iostream> #include <iostream>
namespace megu { namespace megu {
Tangible::Tangible()
: _box({0, 0, 0}, {1, 1, 1}) {}
Tangible::Tangible(const Position & position, const Dimension & dimension) Tangible::Tangible(const Position & position, const Dimension & dimension)
: _box(position, dimension) {} : _box(position, dimension) {}
...@@ -25,7 +28,11 @@ namespace megu { ...@@ -25,7 +28,11 @@ namespace megu {
return this->_box == entity._box; return this->_box == entity._box;
} }
/*bool Tangible::operator!=(const Tangible & entity) const { std::optional<SquareBox> Tangible::operator|(const Tangible & tangible) const {
return !(*this == entity); return this->isColliding(tangible);
}*/ }
std::optional<SquareBox> Tangible::operator|(const SquareBox & box) const {
return this->isColliding(box);
}
} }
\ No newline at end of file
...@@ -10,18 +10,20 @@ ...@@ -10,18 +10,20 @@
namespace megu { namespace megu {
class Tangible : virtual public Identifiable { class Tangible : virtual public Identifiable {
public: public:
Tangible() = delete; Tangible();
Tangible(const Position &, const Dimension &); Tangible(const Position &, const Dimension &);
virtual ~Tangible() = default; virtual ~Tangible() = default;
inline const Position & getPosition() const {return this->_box.position();} inline const Position & getPosition() const {return this->_box.getPosition();}
inline const SquareBox & getBox() const {return this->_box;} inline const SquareBox & getBox() const {return this->_box;}
inline float x() const {return this->_box.position().x();} inline float x() const {return this->_box.getPosition().x();}
inline float y() const {return this->_box.position().y();} inline float y() const {return this->_box.getPosition().y();}
inline float z() const {return this->_box.position().z();} inline float z() const {return this->_box.getPosition().z();}
inline void setPosition(float x, float y, float z = 0.f) {this->_box.setPosition(x, y, z);}
inline void setDimension(float x, float y, float z = 0.f) {this->_box.setDimension(x, y, z);}
inline void setPosition(const Position & position) {this->_box.setPosition(position);}
inline void move(const Direction & direction) {this->_box.move(direction);} inline void move(const Direction & direction) {this->_box.move(direction);}
inline void move(float x, float y, float z = 0.f) {return this->move(Direction(x, y, z));} inline void move(float x, float y, float z = 0.f) {return this->move(Direction(x, y, z));}
...@@ -29,6 +31,8 @@ namespace megu { ...@@ -29,6 +31,8 @@ namespace megu {
virtual std::optional<SquareBox> isColliding(const SquareBox &) const; virtual std::optional<SquareBox> isColliding(const SquareBox &) const;
bool operator==(const Tangible &) const; bool operator==(const Tangible &) const;
std::optional<SquareBox> operator|(const Tangible &) const;
std::optional<SquareBox> operator|(const SquareBox &) const;
using UpdateLambda = std::function<void(double)>; using UpdateLambda = std::function<void(double)>;
......
...@@ -5,6 +5,6 @@ namespace megu { ...@@ -5,6 +5,6 @@ namespace megu {
: Tangible(position, dimension) {} : Tangible(position, dimension) {}
void TangibleMovable::update(double delta) { void TangibleMovable::update(double delta) {
this->update_physic(delta); this->updatePhysic(delta);
} }
} }
\ No newline at end of file
...@@ -9,7 +9,7 @@ namespace megu { ...@@ -9,7 +9,7 @@ namespace megu {
TangibleMovable(const Position &, const Dimension &); TangibleMovable(const Position &, const Dimension &);
virtual ~TangibleMovable() = default; virtual ~TangibleMovable() = default;
virtual void update_physic(double) = 0; virtual void updatePhysic(double) = 0;
protected: protected:
void update(double) override; void update(double) override;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment