remove old transform and velocity components and replace with new decomposed components

focus on 2D for now
This commit is contained in:
Green Sky 2022-01-04 22:33:59 +01:00
parent c36fa30cbc
commit 12b0a90ad0
71 changed files with 1114 additions and 528 deletions

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Position2D {
glm::vec2 pos {0.f, 0.f};
};
} // MM::Components

View File

@ -0,0 +1,11 @@
#pragma once
namespace MM::Components {
// used to lift 2D into 3D space. like a z-index in css/svg
struct Position2D_ZOffset {
float z_offset = 500.f; // default camera allows values to be between 0 and 1000
};
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec3.hpp>
namespace MM::Components {
struct Position3D {
glm::vec3 pos {0.f, 0.f, 0.f};
};
} // MM::Components

View File

@ -0,0 +1,10 @@
#pragma once
namespace MM::Components {
struct Rotation2D {
float rot {0.f};
};
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Scale2D {
glm::vec2 scale {1.f, 1.f};
};
} // MM::Components

View File

@ -3,6 +3,7 @@
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <glm/mat4x4.hpp>
#include <nlohmann/json.hpp>
@ -12,5 +13,26 @@ namespace glm {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(vec3, x, y, z)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(vec4, x, y, z, w)
//NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(mat4x4, [0], y, z, w)
inline void to_json(nlohmann::json& nlohmann_json_j, const mat4x4& nlohmann_json_t) {
// TODO: make 2d array?
nlohmann_json_j = nlohmann::json::array_t{};
nlohmann_json_j[0] = nlohmann_json_t[0];
nlohmann_json_j[1] = nlohmann_json_t[1];
nlohmann_json_j[2] = nlohmann_json_t[2];
nlohmann_json_j[3] = nlohmann_json_t[3];
}
inline void from_json(const nlohmann::json& nlohmann_json_j, mat4x4& nlohmann_json_t) {
if (!nlohmann_json_j.is_array()) {
//throw nlohmann::json::type_error::create(0, "", nlohmann_json_j);
assert(false && "expected array");
return; // TODO: dont fail silently
}
nlohmann_json_j.at(0).get_to(nlohmann_json_t[0]);
nlohmann_json_j.at(1).get_to(nlohmann_json_t[1]);
nlohmann_json_j.at(2).get_to(nlohmann_json_t[2]);
nlohmann_json_j.at(3).get_to(nlohmann_json_t[3]);
}
}

View File

@ -2,11 +2,11 @@
#include <nlohmann/json.hpp>
#include <mm/components/velocity2d.hpp>
#include <mm/components/position2d.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity2D, velocity, rotation)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Position2D, pos)
} // MM::Components

View File

@ -2,11 +2,11 @@
#include <nlohmann/json.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Transform2D, position, scale, rotation)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Position2D_ZOffset, z_offset)
} // MM::Components

View File

@ -2,11 +2,11 @@
#include <nlohmann/json.hpp>
#include <mm/components/velocity3d.hpp>
#include <mm/components/position3d.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity3D, velocity, rotation)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Position3D, pos)
} // MM::Components

View File

@ -2,11 +2,11 @@
#include <nlohmann/json.hpp>
#include <mm/components/transform3d.hpp>
#include <mm/components/rotation2d.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Transform3D, position, scale, rotation)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Rotation2D, rot)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/scale2d.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Scale2D, scale)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/transform4x4.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Transform4x4, trans)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/velocity2d_position.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity2DPosition, pos_vel)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/velocity2d_position_intent.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity2DPositionIntent, intent)
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/velocity2d_rotation.hpp>
#include "./json_glm.hpp"
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Velocity2DRotation, rot_vel)
} // MM::Components

View File

@ -1,55 +0,0 @@
//
// Created by FlaXxy on 29.07.2018.
//
#pragma once
#include <glm/vec2.hpp>
#include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
namespace MM::Components {
struct Transform2D {
glm::vec2 position {0.0f,0.0f};
glm::vec2 scale {1.0f,1.0f};
float rotation {0.0f};
glm::mat3x3 getTransform3(void) const {
//return transformationMatrix(scale, rotation, position);
glm::mat3x3 res(1);
res[2][0] = position.x;
res[2][1] = position.y;
float const s = sinf(rotation);
float const c = cosf(rotation);
res[0][0] = scale.x * c;
res[0][1] = scale.x * s;
res[1][0] = scale.y * -s;
res[1][1] = scale.y * c;
return res;
}
glm::mat4x4 getTransform4(float z = 500.f) const {
//return transformationMatrix(scale, rotation, position);
glm::mat4x4 res{1};
//res[2][0] = position.x;
//res[2][1] = position.y;
res = glm::translate(res, glm::vec3(position, z));
//float const s = sinf(rotation);
//float const c = cosf(rotation);
//res[0][0] = scale.x * c;
//res[0][1] = scale.x * s;
//res[1][0] = scale.y * -s;
//res[1][1] = scale.y * c;
res = glm::rotate(res, rotation, glm::vec3(0.f, 0.f, 1.f));
res = glm::scale(res, glm::vec3(scale, 1.f));
return res;
}
};
} // MM::Components

View File

@ -1,37 +0,0 @@
#pragma once
//#include <glm/mat3x3.hpp>
#include <glm/mat4x4.hpp>
#include <glm/gtc/matrix_transform.hpp>
//#include <entt/core/type_traits.hpp>
namespace MM::Components {
struct Transform3D {
glm::vec3 position {0.0f,0.0f,0.0f};
glm::vec3 scale {1.0f,1.0f,1.0f};
float rotation {0.0f};
glm::mat4x4 getTransform4(void) const {
//return transformationMatrix(scale, rotation, position);
glm::mat4x4 res{1};
//res[2][0] = position.x;
//res[2][1] = position.y;
res = glm::translate(res, position);
//float const s = sinf(rotation);
//float const c = cosf(rotation);
//res[0][0] = scale.x * c;
//res[0][1] = scale.x * s;
//res[1][0] = scale.y * -s;
//res[1][1] = scale.y * c;
res = glm::rotate(res, rotation, glm::vec3(0.f, 0.f, 1.f));
res = glm::scale(res, scale);
return res;
}
};
} // MM::Components

View File

@ -0,0 +1,15 @@
#pragma once
#include <glm/mat4x4.hpp>
namespace MM::Components {
// tag/flag to track dirty positional data, to reduce computation.
struct DirtyTransformTag {};
struct Transform4x4 {
glm::mat4x4 trans {1.f};
};
} // MM::Components

View File

@ -1,16 +0,0 @@
//
// Created by FlaXxy on 29.07.2018.
//
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Velocity2D {
glm::vec2 velocity;
float rotation {0.f};
};
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Velocity2DPosition {
glm::vec2 pos_vel {0.f, 0.f};
};
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
#include <glm/vec2.hpp>
namespace MM::Components {
struct Velocity2DPositionIntent {
glm::vec2 intent {0.f, 0.f};
};
} // MM::Components

View File

@ -0,0 +1,10 @@
#pragma once
namespace MM::Components {
struct Velocity2DRotation {
float rot_vel {0.f};
};
} // MM::Components

View File

@ -1,13 +0,0 @@
#pragma once
#include <glm/vec3.hpp>
namespace MM::Components {
struct Velocity3D {
glm::vec3 velocity {0.f, 0.f, 0.f};
glm::vec3 rotation {0.f, 0.f, 0.f};
};
} // MM::Components

View File

@ -1,33 +1,37 @@
#include "mm/components/position2d.hpp"
#include <gtest/gtest.h>
#include <mm/components/serialize/json_name.hpp>
#include <mm/components/serialize/json_color.hpp>
#include <mm/components/serialize/json_transform2d.hpp>
#include <mm/components/serialize/json_transform3d.hpp>
#include <mm/components/serialize/json_velocity2d.hpp>
#include <mm/components/serialize/json_position2d.hpp>
#include <mm/components/serialize/json_position2d_zoffset.hpp>
#include <mm/components/serialize/json_position3d.hpp>
#include <mm/components/serialize/json_rotation2d.hpp>
#include <mm/components/serialize/json_scale2d.hpp>
#include <mm/components/serialize/json_transform4x4.hpp>
#include <mm/components/serialize/json_velocity2d_position.hpp>
#include <mm/components/serialize/json_velocity2d_position_intent.hpp>
#include <mm/components/serialize/json_velocity2d_rotation.hpp>
#include <mm/components/serialize/json_view_dir2d.hpp>
#include <mm/components/serialize/json_view_dir3d.hpp>
/*#define PARSE_TEST_MACRO(type, json_string, comp_val) \
TEST(common_components_json_serialization, type) { \
MM::Components::type comp; \
{ \
auto j = nlohmann::json::parse(json_string); \
EXPECT_NO_THROW({ comp = j; }); \
ASSERT_EQ(comp, (comp_val)); \
#define TEST_JSON_SERL_EXPAND(x) x
#define TEST_JSON_SERL_IN_OUT(TYPE, JSON_STR, TEST_CORPUS) \
TEST(common_components_json_serialization, in_out_##TYPE) { \
MM::Components::TYPE comp; \
{ /* in */ \
auto j = nlohmann::json::parse(JSON_STR); \
comp = j; \
TEST_CORPUS \
} \
{ \
{ /* out */ \
nlohmann::json j; \
[>EXPECT_NO_THROW({ j = comp; });<] \
ASSERT_EQ(json_string, j.dump()); \
j = comp; \
ASSERT_EQ(JSON_STR, j.dump()); \
} \
}
PARSE_TEST_MACRO(
Name,
"{\"str\":\"test_name\"}",
MM::Components::Name{"test_name"}
); */
TEST(common_components_json_serialization, in_out_name) {
MM::Components::Name comp;
@ -72,137 +76,113 @@ TEST(common_components_json_serialization, in_out_name_fail) {
// ##############################################################
TEST(common_components_json_serialization, in_out_color) {
MM::Components::Color comp;
const char* json_test_file = R"({"color":{"w":1337.0,"x":0.0,"y":1.0,"z":3.0}})";
{ // in
auto j = nlohmann::json::parse(json_test_file);
EXPECT_NO_THROW({
comp = j;
});
glm::vec4 comp_val{0.f, 1.f, 3.f, 1337.f};
TEST_JSON_SERL_IN_OUT(
Color,
R"({"color":{"w":1337.0,"x":0.0,"y":1.0,"z":3.0}})",
TEST_JSON_SERL_EXPAND({
glm::vec4 comp_val(0.f, 1.f, 3.f, 1337.f);
ASSERT_EQ(comp.color.x, comp_val.x);
ASSERT_EQ(comp.color.y, comp_val.y);
ASSERT_EQ(comp.color.z, comp_val.z);
ASSERT_EQ(comp.color.w, comp_val.w);
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
})
)
// ##############################################################
TEST(common_components_json_serialization, in_out_transform2d) {
MM::Components::Transform2D comp;
const char* json_test_file = R"({"position":{"x":42.0,"y":6.0},"rotation":99.0,"scale":{"x":1337.0,"y":68.0}})";
{ // in
auto j = nlohmann::json::parse(json_test_file);
EXPECT_NO_THROW({
comp = j;
});
ASSERT_EQ(comp.position.x, 42.f);
ASSERT_EQ(comp.position.y, 6.f);
ASSERT_EQ(comp.scale.x, 1337.f);
ASSERT_EQ(comp.scale.y, 68.f);
ASSERT_EQ(comp.rotation, 99.f);
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
TEST_JSON_SERL_IN_OUT(
Position2D,
R"({"pos":{"x":42.0,"y":6.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.pos.x, 42.f);
ASSERT_EQ(comp.pos.y, 6.f);
)
)
// ##############################################################
TEST(common_components_json_serialization, in_out_transform3d) {
MM::Components::Transform3D comp;
const char* json_test_file = R"({"position":{"x":42.0,"y":6.0,"z":66.0},"rotation":99.0,"scale":{"x":1337.0,"y":68.0,"z":60.0}})";
{ // in
auto j = nlohmann::json::parse(json_test_file);
EXPECT_NO_THROW({
comp = j;
});
ASSERT_EQ(comp.position.x, 42.f);
ASSERT_EQ(comp.position.y, 6.f);
ASSERT_EQ(comp.position.z, 66.f);
ASSERT_EQ(comp.scale.x, 1337.f);
ASSERT_EQ(comp.scale.y, 68.f);
ASSERT_EQ(comp.scale.z, 60.f);
// TODO: prob needs 3 rotations...
ASSERT_EQ(comp.rotation, 99.f);
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
TEST_JSON_SERL_IN_OUT(
Position2D_ZOffset,
R"({"z_offset":3.0})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.z_offset, 3.f);
)
)
// ##############################################################
TEST(common_components_json_serialization, in_out_velocity2d) {
MM::Components::Velocity2D comp;
TEST_JSON_SERL_IN_OUT(
Position3D,
R"({"pos":{"x":42.0,"y":6.0,"z":44.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.pos.x, 42.f);
ASSERT_EQ(comp.pos.y, 6.f);
ASSERT_EQ(comp.pos.z, 44.f);
)
)
const char* json_test_file = R"({"rotation":99.0,"velocity":{"x":42.0,"y":6.0}})";
// ##############################################################
{ // in
auto j = nlohmann::json::parse(json_test_file);
TEST_JSON_SERL_IN_OUT(
Rotation2D,
R"({"rot":42.0})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.rot, 42.f);
)
)
EXPECT_NO_THROW({
comp = j;
});
// ##############################################################
ASSERT_EQ(comp.velocity.x, 42.f);
ASSERT_EQ(comp.velocity.y, 6.f);
TEST_JSON_SERL_IN_OUT(
Scale2D,
R"({"scale":{"x":42.0,"y":6.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.scale.x, 42.f);
ASSERT_EQ(comp.scale.y, 6.f);
)
)
ASSERT_EQ(comp.rotation, 99.f);
}
// ##############################################################
{ // out
nlohmann::json j;
TEST_JSON_SERL_IN_OUT(
Transform4x4,
R"({"trans":[{"w":0.0,"x":1.0,"y":0.0,"z":0.0},{"w":0.0,"x":0.0,"y":1.0,"z":0.0},{"w":0.0,"x":0.0,"y":0.0,"z":1.0},{"w":1.0,"x":0.0,"y":0.0,"z":0.0}]})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.trans, glm::mat4x4{1.f});
)
)
EXPECT_NO_THROW({
j = comp;
});
// ##############################################################
ASSERT_EQ(json_test_file, j.dump());
}
}
TEST_JSON_SERL_IN_OUT(
Velocity2DPosition,
R"({"pos_vel":{"x":42.0,"y":6.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.pos_vel.x, 42.f);
ASSERT_EQ(comp.pos_vel.y, 6.f);
)
)
// ##############################################################
TEST_JSON_SERL_IN_OUT(
Velocity2DPositionIntent,
R"({"intent":{"x":42.0,"y":6.0}})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.intent.x, 42.f);
ASSERT_EQ(comp.intent.y, 6.f);
)
)
// ##############################################################
TEST_JSON_SERL_IN_OUT(
Velocity2DRotation,
R"({"rot_vel":42.0})",
TEST_JSON_SERL_EXPAND(
ASSERT_EQ(comp.rot_vel, 42.f);
)
)
// ##############################################################

View File

@ -36,9 +36,13 @@ add_library(imgui_widgets
./src/mm/imgui/widgets/auto_wrap.hpp
./src/mm/imgui/widgets/components/name.hpp
./src/mm/imgui/widgets/components/transform2d.hpp
./src/mm/imgui/widgets/components/transform3d.hpp
./src/mm/imgui/widgets/components/velocity2d.hpp
./src/mm/imgui/widgets/components/position2d.hpp
./src/mm/imgui/widgets/components/position2d_zoffset.hpp
./src/mm/imgui/widgets/components/position3d.hpp
./src/mm/imgui/widgets/components/rotation2d.hpp
./src/mm/imgui/widgets/components/scale2d.hpp
./src/mm/imgui/widgets/components/velocity2d_position.hpp
./src/mm/imgui/widgets/components/velocity2d_rotation.hpp
./src/mm/imgui/widgets/components/view_dir2d.hpp
./src/mm/imgui/widgets/components/view_dir3d.hpp
./src/mm/imgui/widgets/components/color.hpp
@ -57,9 +61,13 @@ add_library(imgui_widgets
./src/mm/imgui/widgets/texture_resource_manager.cpp
./src/mm/imgui/widgets/components/name.cpp
./src/mm/imgui/widgets/components/transform2d.cpp
./src/mm/imgui/widgets/components/transform3d.cpp
./src/mm/imgui/widgets/components/velocity2d.cpp
./src/mm/imgui/widgets/components/position2d.cpp
./src/mm/imgui/widgets/components/position2d_zoffset.cpp
./src/mm/imgui/widgets/components/position3d.cpp
./src/mm/imgui/widgets/components/rotation2d.cpp
./src/mm/imgui/widgets/components/scale2d.cpp
./src/mm/imgui/widgets/components/velocity2d_position.cpp
./src/mm/imgui/widgets/components/velocity2d_rotation.cpp
./src/mm/imgui/widgets/components/view_dir2d.cpp
./src/mm/imgui/widgets/components/view_dir3d.cpp
./src/mm/imgui/widgets/components/color.cpp

View File

@ -4,7 +4,8 @@
#include "./entity.hpp"
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/position3d.hpp>
#include <entt/entity/registry.hpp>
@ -37,10 +38,12 @@ void Camera3D(MM::Scene& scene) {
MM::ImGuiWidgets::Entity(tracking, scene);
if (scene.valid(tracking)) {
if (scene.all_of<MM::Components::Transform2D>(tracking)) {
camera->translation = {scene.get<MM::Components::Transform2D>(tracking).position, 0.f};
if (scene.all_of<MM::Components::Position2D>(tracking)) {
camera->translation = {scene.get<MM::Components::Position2D>(tracking).pos, 0.f};
} else if (scene.all_of<MM::Components::Position3D>(tracking)) { // "3d" fallback
camera->translation = scene.get<MM::Components::Position3D>(tracking).pos;
} else {
ImGui::TextUnformatted("error: Entity has no Transform");
ImGui::TextUnformatted("error: Entity has neither Position2D nor Position3D");
}
}
} else {

View File

@ -0,0 +1,21 @@
#include "./position2d.hpp"
#include <imgui/imgui.h>
namespace MM::ImGuiWidgets::Components {
void Position(MM::Components::Position2D& p) {
ImGui::DragFloat2("position (x,y)##Position2D", &p.pos.x, 0.1f);
}
} // MM::ImGuiWidgets::Components
namespace MM {
template <>
void ComponentEditorWidget<Components::Position2D>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::Position(reg.get<Components::Position2D>(e));
}
} // MM

View File

@ -1,15 +1,15 @@
#pragma once
#include <mm/components/velocity2d.hpp>
#include <mm/imgui/imgui_entt_entity_editor.hpp>
#include <mm/components/position2d.hpp>
#include <mm/services/scene_service_interface.hpp>
#include <mm/imgui/imgui_entt_entity_editor.hpp>
namespace MM::ImGuiWidgets::Components {
void Velocity(MM::Components::Velocity2D& v);
void Position(MM::Components::Position2D& p);
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Velocity2D>(MM::Scene& reg, MM::Entity e);
void ComponentEditorWidget<Components::Position2D>(MM::Scene& reg, MM::Entity e);
}

View File

@ -0,0 +1,21 @@
#include "./position2d_zoffset.hpp"
#include <imgui/imgui.h>
namespace MM::ImGuiWidgets::Components {
void Position_ZOffset(MM::Components::Position2D_ZOffset& z) {
ImGui::SliderFloat("zoffset##Position2D_ZOffset", &z.z_offset, 0.f, 1000.f); // TODO: this depends on the camera far and near plane... somewhat
}
} // MM::ImGuiWidgets::Components
namespace MM {
template <>
void ComponentEditorWidget<Components::Position2D_ZOffset>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::Position_ZOffset(reg.get<Components::Position2D_ZOffset>(e));
}
} // MM

View File

@ -0,0 +1,15 @@
#pragma once
#include <mm/components/position2d_zoffset.hpp>
#include <mm/services/scene_service_interface.hpp>
#include <mm/imgui/imgui_entt_entity_editor.hpp>
namespace MM::ImGuiWidgets::Components {
void Position_ZOffset(MM::Components::Position2D_ZOffset& t);
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Position2D_ZOffset>(MM::Scene& reg, MM::Entity e);
}

View File

@ -0,0 +1,26 @@
#include "./position3d.hpp"
#include <imgui/imgui.h>
//#include <glm/gtc/constants.hpp>
namespace MM::ImGuiWidgets::Components {
void Transform(MM::Components::Position3D& p) {
//ImGui::DragFloat3("position (x,y,z)##Transform3D", &t.position.x, 0.1f);
ImGui::DragFloat3("position (x,y,z)##Position3D", &p.pos.x, 0.1f);
//ImGui::SliderFloat("rotation##Transform3D", &t.rotation, 0.f, glm::pi<float>() * 2.f);
//ImGui::DragFloat3("scale (x,y,z)##Transform3D", &t.scale.x, 1.f, 0.f);
}
} // MM::ImGuiWidgets::Components
namespace MM {
template <>
void ComponentEditorWidget<Components::Position3D>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::Transform(reg.get<Components::Position3D>(e));
}
} // MM

View File

@ -1,15 +1,15 @@
#pragma once
#include <mm/components/transform2d.hpp>
#include <mm/services/scene_service_interface.hpp>
#include <mm/components/position3d.hpp>
#include <mm/imgui/imgui_entt_entity_editor.hpp>
#include <mm/services/scene_service_interface.hpp>
namespace MM::ImGuiWidgets::Components {
void Transform(MM::Components::Transform2D& t);
void Transform(MM::Components::Position3D& p);
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Transform2D>(MM::Scene& reg, MM::Entity e);
void ComponentEditorWidget<Components::Position3D>(MM::Scene& reg, MM::Entity e);
}

View File

@ -0,0 +1,23 @@
#include "./rotation2d.hpp"
#include <imgui/imgui.h>
#include <glm/gtc/constants.hpp>
namespace MM::ImGuiWidgets::Components {
void Rotation(MM::Components::Rotation2D& r) {
ImGui::SliderFloat("rotation##Rotation2D", &r.rot, 0.f, glm::two_pi<float>());
}
} // MM::ImGuiWidgets::Components
namespace MM {
template <>
void ComponentEditorWidget<Components::Rotation2D>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::Rotation(reg.get<Components::Rotation2D>(e));
}
} // MM

View File

@ -1,15 +1,15 @@
#pragma once
#include <mm/components/transform3d.hpp>
#include <mm/imgui/imgui_entt_entity_editor.hpp>
#include <mm/components/rotation2d.hpp>
#include <mm/services/scene_service_interface.hpp>
#include <mm/imgui/imgui_entt_entity_editor.hpp>
namespace MM::ImGuiWidgets::Components {
void Transform(MM::Components::Transform3D& t);
void Rotation(MM::Components::Rotation2D& r);
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Transform3D>(MM::Scene& reg, MM::Entity e);
void ComponentEditorWidget<Components::Rotation2D>(MM::Scene& reg, MM::Entity e);
}

View File

@ -0,0 +1,21 @@
#include "./scale2d.hpp"
#include <imgui/imgui.h>
namespace MM::ImGuiWidgets::Components {
void Scale(MM::Components::Scale2D& s) {
ImGui::DragFloat2("scale (x,y)##Scale2D", &s.scale.x, 0.01f);
}
} // MM::ImGuiWidgets::Components
namespace MM {
template <>
void ComponentEditorWidget<Components::Scale2D>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::Scale(reg.get<Components::Scale2D>(e));
}
} // MM

View File

@ -0,0 +1,15 @@
#pragma once
#include <mm/components/scale2d.hpp>
#include <mm/services/scene_service_interface.hpp>
#include <mm/imgui/imgui_entt_entity_editor.hpp>
namespace MM::ImGuiWidgets::Components {
void Scale(MM::Components::Scale2D& s);
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Scale2D>(MM::Scene& reg, MM::Entity e);
}

View File

@ -9,8 +9,6 @@
namespace MM::ImGuiWidgets::Components {
void TilemapRenderable(MM::OpenGL::TilemapRenderable& tm_r) {
ImGui::InputFloat("z", &tm_r.z);
for (size_t i = 0; i < tm_r.sprite_layer.size(); i++) {
ImGui::Separator();
std::string label = "sprite_sheet##";
@ -19,7 +17,7 @@ void TilemapRenderable(MM::OpenGL::TilemapRenderable& tm_r) {
}
}
}
} // MM::ImGuiWidgets::Components
namespace MM {

View File

@ -1,25 +0,0 @@
#include "./transform2d.hpp"
#include <imgui/imgui.h>
#include <glm/gtc/constants.hpp>
namespace MM::ImGuiWidgets::Components {
void Transform(MM::Components::Transform2D& t) {
ImGui::DragFloat2("position (x,y)##Transform", &t.position.x, 0.1f);
ImGui::SliderFloat("rotation##Transform", &t.rotation, 0.f, glm::pi<float>() * 2.f);
ImGui::DragFloat2("scale (x,y)##Transform", &t.scale.x, 1.f, 0.f);
}
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Transform2D>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::Transform(reg.get<Components::Transform2D>(e));
}
} // MM

View File

@ -1,25 +0,0 @@
#include "./transform3d.hpp"
#include <imgui/imgui.h>
#include <glm/gtc/constants.hpp>
namespace MM::ImGuiWidgets::Components {
void Transform(MM::Components::Transform3D& t) {
ImGui::DragFloat3("position (x,y,z)##Transform3D", &t.position.x, 0.1f);
ImGui::SliderFloat("rotation##Transform3D", &t.rotation, 0.f, glm::pi<float>() * 2.f);
ImGui::DragFloat3("scale (x,y,z)##Transform3D", &t.scale.x, 1.f, 0.f);
}
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Transform3D>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::Transform(reg.get<Components::Transform3D>(e));
}
} // MM

View File

@ -1,24 +0,0 @@
#include "./velocity2d.hpp"
#include <imgui/imgui.h>
#include <glm/gtc/constants.hpp>
namespace MM::ImGuiWidgets::Components {
void Velocity(MM::Components::Velocity2D& v) {
ImGui::DragFloat2("velocity (x,y)##Velocity", &v.velocity.x, 0.1f);
ImGui::SliderFloat("rotation##Velocity", &v.rotation, -glm::two_pi<float>(), glm::two_pi<float>());
}
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Velocity2D>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::Velocity(reg.get<Components::Velocity2D>(e));
}
} // MM

View File

@ -0,0 +1,21 @@
#include "./velocity2d_position.hpp"
#include <imgui/imgui.h>
namespace MM::ImGuiWidgets::Components {
void VelocityPosition(MM::Components::Velocity2DPosition& v) {
ImGui::DragFloat2("velocity (x,y)##Velocity2DPosition", &v.pos_vel.x, 0.1f);
}
} // MM::ImGuiWidgets::Components
namespace MM {
template <>
void ComponentEditorWidget<Components::Velocity2DPosition>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::VelocityPosition(reg.get<Components::Velocity2DPosition>(e));
}
} // MM

View File

@ -0,0 +1,15 @@
#pragma once
#include <mm/components/velocity2d_position.hpp>
#include <mm/imgui/imgui_entt_entity_editor.hpp>
#include <mm/services/scene_service_interface.hpp>
namespace MM::ImGuiWidgets::Components {
void VelocityPosition(MM::Components::Velocity2DPosition& v);
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Velocity2DPosition>(MM::Scene& reg, MM::Entity e);
}

View File

@ -0,0 +1,21 @@
#include "./velocity2d_rotation.hpp"
#include <imgui/imgui.h>
namespace MM::ImGuiWidgets::Components {
void VelocityRotation(MM::Components::Velocity2DRotation& v) {
ImGui::DragFloat("rotation##Velocity2DRotation", &v.rot_vel);
}
} // MM::ImGuiWidgets::Components
namespace MM {
template <>
void ComponentEditorWidget<Components::Velocity2DRotation>(MM::Scene& reg, MM::Entity e) {
ImGuiWidgets::Components::VelocityRotation(reg.get<Components::Velocity2DRotation>(e));
}
} // MM

View File

@ -0,0 +1,15 @@
#pragma once
#include <mm/components/velocity2d_rotation.hpp>
#include <mm/imgui/imgui_entt_entity_editor.hpp>
#include <mm/services/scene_service_interface.hpp>
namespace MM::ImGuiWidgets::Components {
void VelocityRotation(MM::Components::Velocity2DRotation& v);
}
namespace MM {
template <>
void ComponentEditorWidget<Components::Velocity2DRotation>(MM::Scene& reg, MM::Entity e);
}

View File

@ -1,8 +1,5 @@
#include "./scene_tools.hpp"
//#include <mm/services/opengl_renderer.hpp>
//#include <mm/resource_manager.hpp>
#include "mm/components/velocity2d_rotation.hpp"
#include <mm/engine.hpp>
@ -10,18 +7,18 @@
#include <imgui/imgui.h>
#include <mm/components/name.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/velocity2d.hpp>
#include <mm/components/time_delta.hpp>
//#include <mm/imgui/widgets/texture_resource_manager.hpp>
#include <mm/imgui/widgets/entity.hpp>
#include <mm/imgui/widgets/components/name.hpp>
#include <mm/imgui/widgets/components/transform2d.hpp>
#include <mm/imgui/widgets/components/transform3d.hpp>
#include <mm/imgui/widgets/components/velocity2d.hpp>
#include <mm/imgui/widgets/components/position2d.hpp>
#include <mm/imgui/widgets/components/position2d_zoffset.hpp>
#include <mm/imgui/widgets/components/position3d.hpp>
#include <mm/imgui/widgets/components/rotation2d.hpp>
#include <mm/imgui/widgets/components/scale2d.hpp>
#include <mm/imgui/widgets/components/velocity2d_position.hpp>
#include <mm/imgui/widgets/components/velocity2d_rotation.hpp>
#include <mm/imgui/widgets/components/view_dir2d.hpp>
#include <mm/imgui/widgets/components/view_dir3d.hpp>
#include <mm/imgui/widgets/components/color.hpp>
@ -29,9 +26,6 @@
#include <mm/imgui/widgets/camera.hpp>
//#include <mm/imgui/widgets/filesystem.hpp>
//#include <mm/services/filesystem.hpp>
#include <mm/logger.hpp>
#define LOGIGS(x) LOG("ImGuiSceneToolsService", x)
@ -48,9 +42,13 @@ namespace MM::Services {
_entity_editor.show_window = false;
_entity_editor.registerComponent<MM::Components::Name>("Name");
_entity_editor.registerComponent<MM::Components::Transform2D>("Transform2D");
_entity_editor.registerComponent<MM::Components::Transform3D>("Transform3D");
_entity_editor.registerComponent<MM::Components::Velocity2D>("Velocity2D");
_entity_editor.registerComponent<MM::Components::Position2D>("Position2D");
_entity_editor.registerComponent<MM::Components::Position2D_ZOffset>("Position2D_ZOffset");
_entity_editor.registerComponent<MM::Components::Position3D>("Position3D");
_entity_editor.registerComponent<MM::Components::Rotation2D>("Rotation2D");
_entity_editor.registerComponent<MM::Components::Scale2D>("Scale2D");
_entity_editor.registerComponent<MM::Components::Velocity2DPosition>("Velocity2DPosition");
_entity_editor.registerComponent<MM::Components::Velocity2DRotation>("Velocity2DRotation");
_entity_editor.registerComponent<MM::Components::ViewDir2D>("ViewDir2D");
_entity_editor.registerComponent<MM::Components::ViewDir3D>("ViewDir3D");
_entity_editor.registerComponent<MM::Components::Color>("Color");

View File

@ -1,7 +1,5 @@
#include "./batched_spritesheet.hpp"
#include <memory>
#include <mm/opengl/shader.hpp>
#include <mm/opengl/buffer.hpp>
#include <mm/opengl/vertex_array_object.hpp>
@ -13,11 +11,9 @@
#include <mm/services/scene_service_interface.hpp>
#include <entt/entity/registry.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/components/color.hpp>
#include "./spritesheet_renderable.hpp"
#include <mm/services/opengl_renderer.hpp>
#include <tracy/Tracy.hpp>
#ifndef MM_OPENGL_3_GLES
@ -67,18 +63,17 @@ BatchedSpriteSheet::~BatchedSpriteSheet(void) {
void BatchedSpriteSheet::render(Services::OpenGLRenderer& rs, Engine& engine) {
ZoneScopedN("MM::OpenGL::RenderTasks::BatchedSpriteSheet::render");
auto* scene_ss = engine.tryService<MM::Services::SceneServiceInterface>();
// no scene
if (scene_ss == nullptr) {
return;
auto* ssi = engine.tryService<MM::Services::SceneServiceInterface>();
if (ssi == nullptr) {
return; // nothing to draw
}
auto& scene = scene_ss->getScene();
auto& scene = ssi->getScene();
struct sp_data {
SpriteSheet sp;
struct instance_data {
MM::Components::Transform2D* trans = nullptr;
const MM::Components::Transform4x4* trans = nullptr;
glm::vec4* color = nullptr;
uint32_t tile_index = 0;
};
@ -87,13 +82,11 @@ void BatchedSpriteSheet::render(Services::OpenGLRenderer& rs, Engine& engine) {
// HACK: assume same sp for same texture
std::unordered_map<MM::OpenGL::Texture::handle_t, sp_data> batch_map;
auto view = scene.view<Components::Transform2D, SpriteSheetRenderable>();
view.each([&](auto e, Components::Transform2D& t, SpriteSheetRenderable& spr) {
// if off screen, early out
if (false) { // TODO:
return;
}
scene.view<const Components::Transform4x4, SpriteSheetRenderable>().each([this, &scene, &batch_map](entt::entity e, const auto& t, auto& spr) {
//// if off screen, early out
//if (false) { // TODO:
//return;
//}
assert(spr.sp.tex); // debug
@ -139,7 +132,8 @@ void BatchedSpriteSheet::render(Services::OpenGLRenderer& rs, Engine& engine) {
auto* inst_memory = gl_inst_buffer->map(sp_ent.second.instances.size(), GL_DYNAMIC_DRAW);
for (auto& inst : sp_ent.second.instances) {
inst_memory->pos_trans = inst.trans->getTransform4(inst.trans->position.y/10.f + 500.f);
//inst_memory->pos_trans = inst.trans->getTransform4(inst.trans->position.y/10.f + 500.f);
inst_memory->pos_trans = inst.trans->trans; // TODO: this is ugly
inst_memory->color = *inst.color;
inst_memory->tile_index = inst.tile_index;
@ -149,6 +143,8 @@ void BatchedSpriteSheet::render(Services::OpenGLRenderer& rs, Engine& engine) {
static_assert(std::is_standard_layout<gl_instance_data>::value); // check if offsetof() is usable
// TODO: optimize, dont call attrib pointer each draw
// mat4, oof
// attptr 1-4
for (size_t i = 0; i < 4; i++) {

View File

@ -11,7 +11,7 @@
#include <mm/services/scene_service_interface.hpp>
#include <entt/entity/registry.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/components/color.hpp>
#include <tracy/Tracy.hpp>
@ -56,6 +56,13 @@ SimpleRect::~SimpleRect(void) {
void SimpleRect::render(Services::OpenGLRenderer& rs, Engine& engine) {
ZoneScopedN("MM::OpenGL::RenderTasks::SimpleRect::render");
auto* ssi = engine.tryService<MM::Services::SceneServiceInterface>();
if (ssi == nullptr) {
return; // nothing to draw
}
auto& scene = ssi->getScene();
rs.targets[target_fbo]->bind(FrameBufferObject::RW);
glEnable(GL_DEPTH_TEST);
@ -64,8 +71,6 @@ void SimpleRect::render(Services::OpenGLRenderer& rs, Engine& engine) {
_shader->bind();
_vao->bind();
auto& scene = engine.tryService<MM::Services::SceneServiceInterface>()->getScene();
Camera3D* cam = scene.try_ctx<Camera3D>();
if (!cam) {
cam = &default_cam;
@ -73,12 +78,8 @@ void SimpleRect::render(Services::OpenGLRenderer& rs, Engine& engine) {
auto vp = cam->getViewProjection();
auto view = scene.view<MM::Components::Transform2D>();
for (auto& e : view) {
auto& t = view.get<Components::Transform2D>(e);
_shader->setUniformMat4f("_WVP", vp * t.getTransform4(t.position.y/10.f + 500.f));
scene.view<const Components::Transform4x4>().each([this, &scene, &vp](entt::entity e, const auto& t) {
_shader->setUniformMat4f("_WVP", vp * t.trans);
if (scene.all_of<Components::Color>(e)) {
_shader->setUniform4f("_color", scene.get<Components::Color>(e).color);
@ -86,9 +87,8 @@ void SimpleRect::render(Services::OpenGLRenderer& rs, Engine& engine) {
_shader->setUniform4f("_color", default_color);
}
glDrawArrays(GL_TRIANGLES, 0, 6);
}
});
_vao->unbind();
_shader->unbind();
@ -106,7 +106,7 @@ in vec2 _vertexPosition;
uniform mat4 _WVP;
void main() {
gl_Position = _WVP * vec4(_vertexPosition, 0, 1);
gl_Position = _WVP * vec4(_vertexPosition, 0.0, 1.0);
})")
FS_CONST_MOUNT_FILE(fragmentPath,

View File

@ -11,7 +11,7 @@
#include <mm/services/scene_service_interface.hpp>
#include <entt/entity/registry.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/opengl/components/texture.hpp>
#include <mm/components/color.hpp>
@ -62,6 +62,13 @@ SimpleSprite::~SimpleSprite(void) {
void SimpleSprite::render(Services::OpenGLRenderer& rs, Engine& engine) {
ZoneScopedN("MM::OpenGL::RenderTasks::SimpleSprite::render");
auto* ssi = engine.tryService<MM::Services::SceneServiceInterface>();
if (ssi == nullptr) {
return; // nothing to draw
}
auto& scene = ssi->getScene();
rs.targets[target_fbo]->bind(FrameBufferObject::W);
glEnable(GL_DEPTH_TEST);
@ -72,8 +79,6 @@ void SimpleSprite::render(Services::OpenGLRenderer& rs, Engine& engine) {
_vao->bind();
auto& scene = engine.tryService<MM::Services::SceneServiceInterface>()->getScene();
auto* cam = scene.try_ctx<Camera3D>();
if (!cam) {
cam = &default_cam;
@ -81,14 +86,12 @@ void SimpleSprite::render(Services::OpenGLRenderer& rs, Engine& engine) {
auto vp = cam->getViewProjection();
auto view = scene.view<Components::Transform2D, Components::OpenGL::Texture>();
view.each([&](auto e, Components::Transform2D& t, Components::OpenGL::Texture& tex) {
scene.view<const Components::Transform4x4, Components::OpenGL::Texture>().each([this, &scene, &vp](entt::entity e, const auto& t, auto& tex) {
assert(tex.tex); // debug
tex.tex->bind(0);
_shader->setUniformMat4f("_WVP", vp * t.getTransform4(t.position.y/10.f + 500.f));
_shader->setUniformMat4f("_WVP", vp * t.trans);
if (scene.all_of<Components::Color>(e)) {
_shader->setUniform4f("_color", scene.get<Components::Color>(e).color);

View File

@ -11,7 +11,7 @@
#include <mm/services/scene_service_interface.hpp>
#include <entt/entity/registry.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/components/color.hpp>
#include "./spritesheet_renderable.hpp"
@ -69,6 +69,13 @@ SimpleSpriteSheet::~SimpleSpriteSheet(void) {
void SimpleSpriteSheet::render(Services::OpenGLRenderer& rs, Engine& engine) {
ZoneScopedN("MM::OpenGL::RenderTasks::SimpleSpriteSheet::render");
auto* ssi = engine.tryService<MM::Services::SceneServiceInterface>();
if (ssi == nullptr) {
return; // nothing to draw
}
auto& scene = ssi->getScene();
rs.targets[target_fbo]->bind(FrameBufferObject::W);
glEnable(GL_DEPTH_TEST);
@ -78,9 +85,6 @@ void SimpleSpriteSheet::render(Services::OpenGLRenderer& rs, Engine& engine) {
_vertexBuffer->bind(GL_ARRAY_BUFFER);
_vao->bind();
auto& scene = engine.tryService<MM::Services::SceneServiceInterface>()->getScene();
auto* cam = scene.try_ctx<Camera3D>();
if (!cam) {
cam = &default_cam;
@ -88,16 +92,14 @@ void SimpleSpriteSheet::render(Services::OpenGLRenderer& rs, Engine& engine) {
auto vp = cam->getViewProjection();
auto view = scene.view<Components::Transform2D, SpriteSheetRenderable>();
view.each([&](auto e, Components::Transform2D& t, SpriteSheetRenderable& spr) {
scene.view<const Components::Transform4x4, SpriteSheetRenderable>().each([this, &scene, &vp](entt::entity e, const auto& t, auto& spr) {
assert(spr.sp.tex); // debug
TracyGpuZone("MM::OpenGL::Renderers::SimpleSpriteSheetRenderer::render.each");
spr.sp.tex->bind(0);
_shader->setUniformMat4f("_WVP", vp * t.getTransform4(t.position.y/10.f + 500.f));
_shader->setUniformMat4f("_WVP", vp * t.trans);
_shader->setUniform2ui("_tileCount", spr.sp.tile_count.x, spr.sp.tile_count.y);
_shader->setUniform1ui("_atlasIndex", spr.tile_index);

View File

@ -1,3 +1,5 @@
#pragma once
#include <mm/opengl/spritesheet.hpp>
namespace MM::OpenGL {

View File

@ -11,7 +11,7 @@
#include <mm/services/scene_service_interface.hpp>
#include <entt/entity/registry.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/opengl/components/texture.hpp>
#include <mm/components/color.hpp>
@ -87,9 +87,10 @@ void Tilemap::render(MM::Services::OpenGLRenderer& rs, MM::Engine& engine) {
_shader->setUniform3f("_ambient_color", ambient_color);
auto view = scene.view<MM::Components::Transform2D, OpenGL::TilemapRenderable>();
view.each([&](auto, MM::Components::Transform2D& t, OpenGL::TilemapRenderable& tilemap) {
_shader->setUniformMat4f("_WVP", vp * t.getTransform4(tilemap.z + 500.f));
scene.view<MM::Components::Transform4x4, OpenGL::TilemapRenderable>()
.each([&](auto, MM::Components::Transform4x4& t, OpenGL::TilemapRenderable& tilemap) {
//_shader->setUniformMat4f("_WVP", vp * t.getTransform4(tilemap.z + 500.f));
_shader->setUniformMat4f("_WVP", vp * t.trans);
// for each sprite layer
for (auto& sp_layer : tilemap.sprite_layer) {

View File

@ -42,7 +42,7 @@ struct TilemapRenderable {
};
std::vector<SpriteLayer> sprite_layer;
float z = 0.f;
//float z = 0.f; // TODO: use Components::Position2D_ZOffset instead
};
} // MM::OpenGL

View File

@ -30,6 +30,7 @@ target_link_libraries(simple_rect_render_task_test
simple_rect_render_task
simple_velocity_system
transform_system
random
@ -48,6 +49,9 @@ target_link_libraries(simple_sprite_render_task_test
simple_sprite_render_task
simple_velocity_system
transform_system
random
gtest_main
)
@ -64,6 +68,7 @@ target_link_libraries(simple_spritesheet_render_task_test
simple_spritesheet_render_task
#simple_velocity_system
transform_system
gtest_main
)
@ -80,6 +85,7 @@ target_link_libraries(batched_spritesheet_render_task_test
batched_spritesheet_render_task
#simple_velocity_system
transform_system
gtest_main
)
@ -101,6 +107,7 @@ target_link_libraries(blur_render_task_test
imgui_render_task
simple_velocity_system
transform_system
random
@ -119,6 +126,8 @@ target_link_libraries(tilemap_render_task_test
imgui_service
tilemap_render_task
transform_system
gtest_main
)

View File

@ -14,11 +14,17 @@
#include <mm/opengl/render_tasks/batched_spritesheet.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/scale2d.hpp>
#include <mm/components/position3d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/components/color.hpp>
#include <mm/components/time_delta.hpp>
#include <mm/opengl/render_tasks/spritesheet_renderable.hpp>
#include <mm/systems/transform.hpp>
#include <physfs.h>
#include "res/textures.zip.h"
@ -69,10 +75,27 @@ TEST(batched_spritesheet_render_task, it) {
rs.addRenderTask<MM::OpenGL::RenderTasks::BatchedSpriteSheet>(engine);
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position2D_ZOffset>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position3D>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Transform4x4>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D_ZOffset>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position3D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Scale2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
// setup systems
scene.set<float>(0.f); // accu
auto& org = scene.set<entt::organizer>();
org.emplace<&update_spritesheet_animation>("update_spritesheet_animation");
org.emplace<MM::Systems::position3d_from_2d>("position3d_from_2d");
org.emplace<MM::Systems::transform3d_translate>("transform3d_translate");
org.emplace<MM::Systems::transform3d_rotate2d>("transform3d_rotate2d");
org.emplace<MM::Systems::transform3d_scale2d>("transform3d_scale2d");
org.emplace<MM::Systems::transform_clear_dirty>("transform_clear_dirty");
// HACK: instead you would switch to this scene
engine.getService<MM::Services::OrganizerSceneService>().updateOrganizerVertices(scene);
@ -83,10 +106,13 @@ TEST(batched_spritesheet_render_task, it) {
{
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
t.position.x = -1.f;
t.scale.x = 1.5f;
t.scale.y = 2.f;
auto& p = scene.emplace<MM::Components::Position2D>(e);
p.pos.x = -1.f;
// zoffset is created by event
auto& s = scene.emplace<MM::Components::Scale2D>(e);
s.scale = {1.5f,2.f};
auto& spr = scene.emplace<MM::OpenGL::SpriteSheetRenderable>(e);
spr.sp.tex = rm_t.get("anim_run"_hs);
@ -97,10 +123,13 @@ TEST(batched_spritesheet_render_task, it) {
{
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
t.position.x = 1.f;
t.scale.x = 1.5f;
t.scale.y = 2.f;
auto& p = scene.emplace<MM::Components::Position2D>(e);
p.pos.x = 1.f;
// zoffset is created by event
auto& s = scene.emplace<MM::Components::Scale2D>(e);
s.scale = {1.5f,2.f};
auto& spr = scene.emplace<MM::OpenGL::SpriteSheetRenderable>(e);
spr.sp.tex = rm_t.get("anim_idle"_hs);

View File

@ -17,10 +17,17 @@
#include <mm/opengl/render_tasks/blur.hpp>
#include <mm/opengl/render_tasks/imgui.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/scale2d.hpp>
#include <mm/components/rotation2d.hpp>
#include <mm/components/velocity2d_rotation.hpp>
#include <mm/components/position3d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/components/color.hpp>
#include <mm/systems/simple_velocity_system2d.hpp>
#include <mm/systems/transform.hpp>
#include <mm/opengl/fbo_builder.hpp>
#include <mm/opengl/texture_loader.hpp>
@ -110,10 +117,27 @@ TEST(blur_render_task, it) {
.finish();
}
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position2D_ZOffset>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position3D>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Transform4x4>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D_ZOffset>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position3D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Scale2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Rotation2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>(); // in this example only rotation is touched
// setup v system
auto& org = scene.set<entt::organizer>();
org.emplace<&MM::Systems::simple_velocity>("simple_velocity");
org.emplace<MM::Systems::simple_rotational_velocity_patching>("simple_rotational_velocity_patching");
org.emplace<MM::Systems::position3d_from_2d>("position3d_from_2d");
org.emplace<MM::Systems::transform3d_translate>("transform3d_translate");
org.emplace<MM::Systems::transform3d_rotate2d>("transform3d_rotate2d");
org.emplace<MM::Systems::transform3d_scale2d>("transform3d_scale2d");
org.emplace<MM::Systems::transform_clear_dirty>("transform_clear_dirty");
// HACK: instead you would switch to this scene
engine.getService<MM::Services::OrganizerSceneService>().updateOrganizerVertices(scene);
@ -122,12 +146,18 @@ TEST(blur_render_task, it) {
for (int i = 0; i < 10; i++) {
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
t.position.x = float(i) * 9.f - 40.f;
t.scale = {5,5};
auto& p = scene.emplace<MM::Components::Position2D>(e);
p.pos.x = i * 9.f - 40;
auto& v = scene.emplace<MM::Components::Velocity2D>(e);
v.rotation = float(i) * 0.3f;
// zoffset is created by event
auto& s = scene.emplace<MM::Components::Scale2D>(e);
s.scale = {5,5};
scene.emplace<MM::Components::Rotation2D>(e);
auto& v = scene.emplace<MM::Components::Velocity2DRotation>(e);
v.rot_vel = i * 0.3f;
if (rng.roll(0.5f)) {
auto& col = scene.emplace<MM::Components::Color>(e);

View File

@ -12,10 +12,17 @@
#include <mm/opengl/render_tasks/simple_rect.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/scale2d.hpp>
#include <mm/components/rotation2d.hpp>
#include <mm/components/velocity2d_rotation.hpp>
#include <mm/components/position3d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/components/color.hpp>
#include <mm/systems/simple_velocity_system2d.hpp>
#include <mm/systems/transform.hpp>
#include <mm/random/srng.hpp>
@ -44,9 +51,25 @@ TEST(simple_rect_render_task, it) {
rs.addRenderTask<MM::OpenGL::RenderTasks::SimpleRect>(engine);
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position2D_ZOffset>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position3D>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Transform4x4>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D_ZOffset>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position3D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Scale2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Rotation2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>(); // in this example only rotation is touched
// setup v system
auto& org = scene.set<entt::organizer>();
org.emplace<&MM::Systems::simple_velocity>("simple_velocity");
org.emplace<MM::Systems::simple_rotational_velocity_patching>("simple_rotational_velocity_patching");
org.emplace<MM::Systems::position3d_from_2d>("position3d_from_2d");
org.emplace<MM::Systems::transform3d_translate>("transform3d_translate");
org.emplace<MM::Systems::transform3d_rotate2d>("transform3d_rotate2d");
org.emplace<MM::Systems::transform3d_scale2d>("transform3d_scale2d");
org.emplace<MM::Systems::transform_clear_dirty>("transform_clear_dirty");
// HACK: instead you would switch to this scene
engine.getService<MM::Services::OrganizerSceneService>().updateOrganizerVertices(scene);
@ -56,13 +79,19 @@ TEST(simple_rect_render_task, it) {
for (int y = 0; y < 10; y++) {
for (int i = 0; i < 10; i++) {
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
t.position.x = i * 9.f - 40;
t.position.y = -y * 6.f + 25;
t.scale = {5,5};
auto& p = scene.emplace<MM::Components::Position2D>(e);
p.pos.x = i * 9.f - 40;
p.pos.y = -y * 6.f + 25;
auto& v = scene.emplace<MM::Components::Velocity2D>(e);
v.rotation = i * 0.3f;
// zoffset is created by event
auto& s = scene.emplace<MM::Components::Scale2D>(e);
s.scale = {5,5};
scene.emplace<MM::Components::Rotation2D>(e);
auto& v = scene.emplace<MM::Components::Velocity2DRotation>(e);
v.rot_vel = i * 0.3f;
if (rng.roll(0.5f)) {
auto& col = scene.emplace<MM::Components::Color>(e);

View File

@ -12,13 +12,20 @@
#include <mm/opengl/render_tasks/simple_sprite.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/scale2d.hpp>
#include <mm/components/rotation2d.hpp>
#include <mm/components/velocity2d_rotation.hpp>
#include <mm/components/position3d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/opengl/components/texture.hpp>
#include <mm/components/color.hpp>
#include <mm/systems/simple_velocity_system2d.hpp>
#include <mm/systems/transform.hpp>
#include <random>
#include <mm/random/srng.hpp>
using namespace entt::literals;
@ -47,37 +54,58 @@ TEST(simple_sprite_render_task, it) {
rs.addRenderTask<MM::OpenGL::RenderTasks::SimpleSprite>(engine);
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position2D_ZOffset>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position3D>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Transform4x4>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D_ZOffset>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position3D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Scale2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Rotation2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>(); // in this example only rotation is touched
// setup v system
auto& org = scene.set<entt::organizer>();
org.emplace<&MM::Systems::simple_velocity>("simple_velocity");
org.emplace<MM::Systems::simple_rotational_velocity_patching>("simple_rotational_velocity_patching");
org.emplace<MM::Systems::position3d_from_2d>("position3d_from_2d");
org.emplace<MM::Systems::transform3d_translate>("transform3d_translate");
org.emplace<MM::Systems::transform3d_rotate2d>("transform3d_rotate2d");
org.emplace<MM::Systems::transform3d_scale2d>("transform3d_scale2d");
org.emplace<MM::Systems::transform_clear_dirty>("transform_clear_dirty");
// HACK: instead you would switch to this scene
engine.getService<MM::Services::OrganizerSceneService>().updateOrganizerVertices(scene);
auto& rm_t = MM::ResourceManager<MM::OpenGL::Texture>::ref();
std::mt19937 mt(42);
MM::Random::SRNG rng{42};
for (int y = 0; y < 10; y++) {
for (int i = 0; i < 10; i++) {
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
t.position.x = i * 9.f - 40;
t.position.y = -y * 6.f + 25;
t.scale = {5,5};
auto& p = scene.emplace<MM::Components::Position2D>(e);
p.pos.x = i * 9.f - 40;
p.pos.y = -y * 6.f + 25;
auto& v = scene.emplace<MM::Components::Velocity2D>(e);
v.rotation = i * 0.3f;
// zoffset is created by event
auto& s = scene.emplace<MM::Components::Scale2D>(e);
s.scale = {5,5};
scene.emplace<MM::Components::Rotation2D>(e);
auto& v = scene.emplace<MM::Components::Velocity2DRotation>(e);
v.rot_vel = i * 0.3f;
auto& tex = scene.emplace<MM::Components::OpenGL::Texture>(e);
tex.tex = rm_t.get("errig"_hs);
if (mt() % 2) {
if (rng.roll(0.5f)) {
auto& col = scene.emplace<MM::Components::Color>(e);
auto rc = [&mt]() -> float {
return (mt() % 1001) / 1000.f ;
};
col.color = {rc(),rc(),rc(),1};
col.color = {rng.zeroToOne(), rng.zeroToOne(), rng.zeroToOne(), 1.f};
}
}
}

View File

@ -14,11 +14,17 @@
#include <mm/opengl/render_tasks/simple_spritesheet.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/scale2d.hpp>
#include <mm/components/position3d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/components/color.hpp>
#include <mm/components/time_delta.hpp>
#include <mm/opengl/render_tasks/spritesheet_renderable.hpp>
#include <mm/systems/transform.hpp>
#include <physfs.h>
#include "res/textures.zip.h"
@ -65,10 +71,26 @@ TEST(simple_spritesheet_render_task, it) {
rs.addRenderTask<MM::OpenGL::RenderTasks::SimpleSpriteSheet>(engine);
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position2D_ZOffset>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position3D>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Transform4x4>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D_ZOffset>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position3D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Scale2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
// setup systems
scene.set<float>(0.f); // accu
auto& org = scene.set<entt::organizer>();
org.emplace<&update_spritesheet_animation>("update_spritesheet_animation");
org.emplace<MM::Systems::position3d_from_2d>("position3d_from_2d");
org.emplace<MM::Systems::transform3d_translate>("transform3d_translate");
org.emplace<MM::Systems::transform3d_rotate2d>("transform3d_rotate2d");
org.emplace<MM::Systems::transform3d_scale2d>("transform3d_scale2d");
org.emplace<MM::Systems::transform_clear_dirty>("transform_clear_dirty");
// HACK: instead you would switch to this scene
engine.getService<MM::Services::OrganizerSceneService>().updateOrganizerVertices(scene);
@ -79,10 +101,13 @@ TEST(simple_spritesheet_render_task, it) {
{
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
t.position.x = -1.f;
t.scale.x = 1.5f;
t.scale.y = 2.f;
auto& p = scene.emplace<MM::Components::Position2D>(e);
p.pos.x = -1.f;
// zoffset is created by event
auto& s = scene.emplace<MM::Components::Scale2D>(e);
s.scale = {1.5f,2.f};
auto& spr = scene.emplace<MM::OpenGL::SpriteSheetRenderable>(e);
spr.sp.tex = rm_t.get("anim_run"_hs);
@ -93,10 +118,13 @@ TEST(simple_spritesheet_render_task, it) {
{
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
t.position.x = 1.f;
t.scale.x = 1.5f;
t.scale.y = 2.f;
auto& p = scene.emplace<MM::Components::Position2D>(e);
p.pos.x = 1.f;
// zoffset is created by event
auto& s = scene.emplace<MM::Components::Scale2D>(e);
s.scale = {1.5f,2.f};
auto& spr = scene.emplace<MM::OpenGL::SpriteSheetRenderable>(e);
spr.sp.tex = rm_t.get("anim_idle"_hs);

View File

@ -8,13 +8,18 @@
#include <mm/services/opengl_renderer.hpp>
#include <entt/entity/registry.hpp>
#include <entt/entity/organizer.hpp>
#include <mm/opengl/camera_3d.hpp>
#include <mm/opengl/render_tasks/tilemap.hpp>
#include <mm/opengl/render_tasks/tilemap_renderable.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/position3d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/systems/transform.hpp>
#include <mm/opengl/texture_loader.hpp>
using namespace entt::literals;
@ -48,11 +53,30 @@ TEST(tilemap_render_task_test, it) {
rs.addRenderTask<MM::OpenGL::RenderTasks::Tilemap>(engine);
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position2D_ZOffset>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Position3D>>();
scene.on_construct<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::Transform4x4>>();
scene.on_construct<MM::Components::Position3D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>(); // fun
// "useless" in this example
scene.on_update<MM::Components::Position2D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position2D_ZOffset>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
scene.on_update<MM::Components::Position3D>().connect<&entt::registry::emplace_or_replace<MM::Components::DirtyTransformTag>>();
auto& org = scene.set<entt::organizer>();
org.emplace<MM::Systems::position3d_from_2d>("position3d_from_2d");
org.emplace<MM::Systems::transform3d_translate>("transform3d_translate");
org.emplace<MM::Systems::transform_clear_dirty>("transform_clear_dirty");
// HACK: instead you would switch to this scene
engine.getService<MM::Services::OrganizerSceneService>().updateOrganizerVertices(scene);
auto& rm_t = MM::ResourceManager<MM::OpenGL::Texture>::ref();
{
auto e = scene.create();
scene.emplace<MM::Components::Transform2D>(e);
scene.emplace<MM::Components::Position2D>(e);
auto& tm = scene.emplace<MM::OpenGL::TilemapRenderable>(e);

View File

@ -8,7 +8,9 @@
#include <mm/services/scene_service_interface.hpp>
#include <entt/entity/registry.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/components/name.hpp>
#include <nlohmann/json.hpp>
@ -95,9 +97,9 @@ bool Tilemap::parseTiled_LayerRenderable(MM::Engine&, MM::Scene& scene, nlohmann
uint32_t layer_width = jlayer["width"];
uint32_t layer_height = jlayer["height"];
auto& tilemap_rend = scene.emplace<OpenGL::TilemapRenderable>(e);
scene.get_or_emplace<Components::Position2D_ZOffset>(e).z_offset = Tiled_get_z_prop(jlayer) + 500.f; // TODO: magic
tilemap_rend.z = Tiled_get_z_prop(jlayer);
auto& tilemap_rend = scene.emplace<OpenGL::TilemapRenderable>(e);
// TODO: inefficient
for (auto& it : _sprite_sheet_map) {
@ -165,10 +167,11 @@ bool Tilemap::parseTiled_Layer(MM::Engine& engine, MM::Scene& scene, nlohmann::j
name.str = jlayer["name"];
}
auto& t = scene.emplace<MM::Components::Transform2D>(e);
t.position = {0, 0};
t.scale = {1, 1};
scene.emplace<MM::Components::Position2D>(e);
// in case there is no auto system
scene.emplace_or_replace<MM::Components::Transform4x4>(e);
scene.emplace_or_replace<MM::Components::DirtyTransformTag>(e);
if (Tiled_is_collision_layer(jlayer)) {
return parseTiled_LayerCollision(engine, scene, jlayer, e);

View File

@ -21,6 +21,7 @@ target_link_libraries(mm_logo_screen
simple_sprite_render_task
common_components
transform_system
random
)

View File

@ -1,6 +1,5 @@
#include "./mm_logo_screen.hpp"
#include <glm/gtc/constants.hpp>
#include <mm/services/organizer_scene.hpp>
#include <mm/services/opengl_renderer.hpp>
@ -13,14 +12,24 @@
#include <mm/opengl/texture_loader.hpp>
#include <mm/opengl/camera_3d.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/position3d.hpp>
#include <mm/components/scale2d.hpp>
#include <mm/components/transform4x4.hpp>
#include <mm/components/color.hpp>
#include <mm/components/time_delta.hpp>
#include <mm/opengl/components/texture.hpp>
#include <mm/systems/transform.hpp>
#include <glm/gtc/constants.hpp>
#include <glm/trigonometric.hpp>
#include <mm/random/srng.hpp>
#include <random>
namespace MM::Screens {
namespace Components {
@ -51,8 +60,8 @@ namespace Systems {
}
// elastic scale easing
void elasic_scale_easing(entt::view<entt::get_t<MM::Components::Transform2D, Components::easing>> view, const MM::Components::TimeDelta& td) {
view.each([&td](auto& t, auto& easing_comp) {
void elasic_scale_easing(entt::view<entt::get_t<MM::Components::Scale2D, Components::easing>> view, const MM::Components::TimeDelta& td) {
view.each([&td](auto& s, auto& easing_comp) {
easing_comp.accumulator += td.tickDelta;
// taken from https://github.com/warrenm/AHEasing
@ -61,8 +70,8 @@ namespace Systems {
return glm::sin(-13.f * glm::half_pi<float>() * (x + 1.f)) * glm::pow(2.f, -10.f * x) + 1.f;
};
t.scale.x = glm::mix(easing_comp.start.x, easing_comp.end.x, elasticOut(easing_comp.accumulator / easing_comp.duration));
t.scale.y = glm::mix(easing_comp.start.y, easing_comp.end.y, elasticOut(easing_comp.accumulator / easing_comp.duration));
s.scale.x = glm::mix(easing_comp.start.x, easing_comp.end.x, elasticOut(easing_comp.accumulator / easing_comp.duration));
s.scale.y = glm::mix(easing_comp.start.y, easing_comp.end.y, elasticOut(easing_comp.accumulator / easing_comp.duration));
});
}
@ -117,9 +126,14 @@ void create_mm_logo(MM::Engine& engine, MM::Services::ScreenDirector::Screen& sc
scene.set<Components::screen_timer>(0.f, screen_duration, next_screen);
org.emplace<&Systems::screen_timer_system>("screen_timer_system");
org.emplace<Systems::screen_timer_system>("screen_timer_system");
org.emplace<&Systems::elasic_scale_easing>("elasic_scale_easing");
org.emplace<Systems::elasic_scale_easing>("elasic_scale_easing");
org.emplace<MM::Systems::position3d_from_2d>("position3d_from_2d");
org.emplace<MM::Systems::transform3d_translate>("transform3d_translate");
//org.emplace<MM::Systems::transform3d_rotate2d>("transform3d_rotate2d");
org.emplace<MM::Systems::transform3d_scale2d>("transform3d_scale2d");
//org.emplace<MM::Systems::transform_clear_dirty>("transform_clear_dirty");
auto& cam = scene.set<MM::OpenGL::Camera3D>();
cam.horizontalViewPortSize = 89.f;
@ -128,8 +142,12 @@ void create_mm_logo(MM::Engine& engine, MM::Services::ScreenDirector::Screen& sc
{
auto e_logo = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e_logo);
t.scale = {0,0};
scene.emplace<MM::Components::Position2D>(e_logo);
scene.emplace<MM::Components::Position2D_ZOffset>(e_logo);
scene.emplace<MM::Components::Position3D>(e_logo);
scene.emplace<MM::Components::Scale2D>(e_logo, glm::vec2{0.f, 0.f});
scene.emplace<MM::Components::Transform4x4>(e_logo);
scene.emplace<MM::Components::DirtyTransformTag>(e_logo);
auto& easing_comp = scene.emplace<Components::easing>(e_logo);
easing_comp.start = {0.f, 0.f};
@ -161,7 +179,6 @@ void create_mm_logo(MM::Engine& engine, MM::Services::ScreenDirector::Screen& sc
1.f,
};
//std::mt19937_64 mt{std::random_device{}()};
MM::Random::SRNG rng{std::random_device{}(), 0};
std::vector<glm::vec3> colors {color1, color2, color3};

View File

@ -1,6 +1,7 @@
cmake_minimum_required(VERSION 3.8)
project(systems CXX)
add_subdirectory(transform)
add_subdirectory(simple_velocity)
if(NOT MM_HEADLESS)

View File

@ -6,22 +6,13 @@
namespace MM::Systems {
void player_velocity2d(entt::view<entt::get_t<MM::Input::PlayerID, MM::Components::Velocity2D>> view, const MM::Engine* engine) {
void player_velocity2d(entt::view<entt::get_t<const MM::Input::PlayerID, MM::Components::Velocity2DPositionIntent>> view, const MM::Engine* engine) {
ZoneScopedN("MM::Systems::PlayerVelocity2D");
auto& input_ss = engine->getService<MM::Services::InputService>();
view.each([&input_ss](const MM::Input::PlayerID p_id, MM::Components::Velocity2D& v) {
//const float movement_speed = 8.f; // apply via post processing
auto vec_force = input_ss.getMoveForce(p_id);
if (vec_force >= 0.01f) {
v.velocity = input_ss.getMoveVec(p_id);
//v.velocity *= vec_force;
} else {
v.velocity = {0.f, 0.f};
}
view.each([&input_ss](const MM::Input::PlayerID p_id, MM::Components::Velocity2DPositionIntent& v) {
v.intent = input_ss.getMoveVec(p_id);
});
}

View File

@ -1,14 +1,13 @@
#pragma once
#include <mm/engine_fwd.hpp>
#include <entt/entity/utility.hpp>
#include <mm/services/input_service.hpp>
#include <mm/components/velocity2d.hpp>
#include <mm/components/velocity2d_position_intent.hpp>
namespace MM::Systems {
// this system transforms the input from the input_service into velocity
void player_velocity2d(entt::view<entt::get_t<MM::Input::PlayerID, MM::Components::Velocity2D>> view, const MM::Engine* engine);
// this system transforms the input from the input_service into velocity intent
void player_velocity2d(entt::view<entt::get_t<const MM::Input::PlayerID, MM::Components::Velocity2DPositionIntent>> view, const MM::Engine* engine);
} // MM::Systems

View File

@ -1,21 +1,66 @@
#include "./simple_velocity_system2d.hpp"
#include "mm/components/rotation2d.hpp"
#include "mm/components/velocity2d_position.hpp"
#include "mm/components/velocity2d_rotation.hpp"
#include <entt/entity/registry.hpp>
namespace MM::Systems {
void simple_velocity(
void simple_positional_velocity(
entt::view<entt::get_t<
Components::Transform2D,
const Components::Velocity2D
Components::Position2D,
const Components::Velocity2DPosition
>> view,
const Components::TimeDelta& td
) {
view.each([delta = td.tickDelta](auto, auto& t, auto& v) {
t.position += v.velocity * delta;
t.rotation += v.rotation * delta;
view.each([delta = td.tickDelta](auto& t, auto& v) {
t.pos += v.pos_vel * delta;
});
}
void simple_positional_velocity_patching(
entt::registry& scene,
entt::view<entt::get_t<
Components::Position2D,
const Components::Velocity2DPosition
>> view,
const Components::TimeDelta& td
) {
view.each([&scene, delta = td.tickDelta](const auto e, auto& t, auto& v) {
if (v.pos_vel.x != 0.f || v.pos_vel.y != 0.f) {
t.pos += v.pos_vel * delta;
scene.patch<MM::Components::Position2D>(e);
}
);
});
}
void simple_rotational_velocity(
entt::view<entt::get_t<
Components::Rotation2D,
const Components::Velocity2DRotation
>> view,
const Components::TimeDelta& td
) {
view.each([delta = td.tickDelta](auto& r, auto& v) {
r.rot += v.rot_vel * delta;
});
}
void simple_rotational_velocity_patching(
entt::registry& scene,
entt::view<entt::get_t<
Components::Rotation2D,
const Components::Velocity2DRotation
>> view,
const Components::TimeDelta& td
) {
view.each([&scene, delta = td.tickDelta](const auto e, auto& r, auto& v) {
if (v.rot_vel != 0.f) {
r.rot += v.rot_vel * delta;
scene.patch<MM::Components::Rotation2D>(e);
}
});
}
} // MM::Systems

View File

@ -1,17 +1,49 @@
#pragma once
#include <mm/components/velocity2d.hpp>
#include <mm/components/transform2d.hpp>
#include <mm/components/velocity2d_position.hpp>
#include <mm/components/velocity2d_rotation.hpp>
#include <mm/components/position2d.hpp>
#include <mm/components/rotation2d.hpp>
#include <mm/components/time_delta.hpp>
#include <mm/services/scene_service_interface.hpp>
#include <entt/fwd.hpp>
namespace MM::Systems {
void simple_velocity(
// non patching (on_update())
void simple_positional_velocity(
entt::view<entt::get_t<
Components::Transform2D,
const Components::Velocity2D
Components::Position2D,
const Components::Velocity2DPosition
>> view,
const Components::TimeDelta& td
);
// patching (on_update())
void simple_positional_velocity_patching(
entt::registry& scene,
entt::view<entt::get_t<
Components::Position2D,
const Components::Velocity2DPosition
>> view,
const Components::TimeDelta& td
);
// non patching (on_update())
void simple_rotational_velocity(
entt::view<entt::get_t<
Components::Rotation2D,
const Components::Velocity2DRotation
>> view,
const Components::TimeDelta& td
);
// patching (on_update())
void simple_rotational_velocity_patching(
entt::registry& scene,
entt::view<entt::get_t<
Components::Rotation2D,
const Components::Velocity2DRotation
>> view,
const Components::TimeDelta& td
);

View File

@ -12,7 +12,8 @@ TEST(simple_velocity_2d, basic_run) {
// setup v system
auto& org = scene.set<entt::organizer>();
org.emplace<&MM::Systems::simple_velocity>("simple_velocity");
org.emplace<&MM::Systems::simple_positional_velocity>("simple_positional_velocity");
org.emplace<&MM::Systems::simple_rotational_velocity>("simple_rotational_velocity");
auto graph = org.graph();
// setup delta
@ -21,19 +22,21 @@ TEST(simple_velocity_2d, basic_run) {
// setup test entity
auto e = scene.create();
auto& t = scene.emplace<MM::Components::Transform2D>(e);
auto& v = scene.emplace<MM::Components::Velocity2D>(e);
t.position = { 0.f, 0.f };
t.rotation = 0.f;
auto& p = scene.emplace<MM::Components::Position2D>(e);
auto& r = scene.emplace<MM::Components::Rotation2D>(e);
auto& vp = scene.emplace<MM::Components::Velocity2DPosition>(e);
auto& vr = scene.emplace<MM::Components::Velocity2DRotation>(e);
p.pos = { 0.f, 0.f };
r.rot = 0.f;
v.velocity = { 1.f, 1.f };
v.rotation = 0.f;
vp.pos_vel = { 1.f, 1.f };
vr.rot_vel = 0.f;
// run all systems
for (auto&& vert : graph) {
vert.callback()(vert.data(), scene);
}
ASSERT_EQ(t.position.x, 1.f * 1.f/60.f);
ASSERT_EQ(p.pos.x, 1.f * 1.f/60.f);
}

View File

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.2)
project(transfrom_system CXX)
add_library(transform_system
src/mm/systems/transform.hpp
src/mm/systems/transform.cpp
)
target_include_directories(transform_system PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_link_libraries(transform_system
entt
glm
engine
common_components
tracy_client
)
#if (BUILD_TESTING)
#add_subdirectory(test)
#endif()

View File

@ -0,0 +1,43 @@
#include "./transform.hpp"
#include <glm/gtc/matrix_transform.hpp>
#include <tracy/Tracy.hpp>
namespace MM::Systems {
void position3d_from_2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Position2D, const Components::Position2D_ZOffset, Components::Position3D>> view) {
ZoneScoped;
view.each([](const Components::Position2D& pos2, const Components::Position2D_ZOffset& zoff, Components::Position3D& pos3) {
pos3.pos = glm::vec3(pos2.pos, zoff.z_offset + pos2.pos.y/10.f);
});
}
void transform3d_translate(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Position3D, Components::Transform4x4>> view) {
ZoneScoped;
view.each([](const Components::Position3D& pos, Components::Transform4x4& trans) {
trans.trans = glm::translate(glm::mat4x4{1.f}, pos.pos);
});
}
void transform3d_rotate2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Rotation2D, Components::Transform4x4>> view) {
ZoneScoped;
view.each([](const Components::Rotation2D& rot, Components::Transform4x4& trans) {
trans.trans = glm::rotate(trans.trans, rot.rot, glm::vec3(0.f, 0.f, 1.f));
});
}
void transform3d_scale2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Scale2D, Components::Transform4x4>> view) {
ZoneScoped;
view.each([](const Components::Scale2D& scale, Components::Transform4x4& trans) {
trans.trans = glm::scale(trans.trans, glm::vec3(scale.scale, 1.f));
});
}
void transform_clear_dirty(entt::registry& scene, entt::view<entt::get_t<Components::DirtyTransformTag>> view) {
ZoneScoped;
scene.remove<Components::DirtyTransformTag>(view.begin(), view.end());
}
} // MM::Systems

View File

@ -0,0 +1,53 @@
#pragma once
#include <mm/components/position2d.hpp>
#include <mm/components/position2d_zoffset.hpp>
#include <mm/components/position3d.hpp>
#include <mm/components/scale2d.hpp>
#include <mm/components/rotation2d.hpp>
#include <mm/components/transform4x4.hpp>
#include <entt/entity/registry.hpp>
namespace MM::Systems {
// 2D usage:
//
// auto create dependencies, eg:
// scene.on_construct<Components::Position2D>().connect<&entt::registry::emplace_or_replace<Components::Position3D>>();
// scene.on_construct<Components::Position2D>().connect<&entt::registry::emplace_or_replace<Components::Transform4x4>>();
// scene.on_construct<Components::Position2D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
//
// mark as dirty on change, eg:
// scene.on_update<Components::Position2D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
// scene.on_update<Components::Position2D_ZOffset>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
// scene.on_update<Components::Position3D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
// scene.on_update<Components::Scale2D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
// scene.on_update<Components::Rotation2D>().connect<&entt::registry::emplace_or_replace<Components::DirtyTransformTag>>();
//
// optinally cleanup on destruction, eg:
// scene.on_destroy<Components::Position2D>().connect<&entt::registry::remove<
// Components::DirtyTransformTag,
// Components::Position2D_ZOffset,
// Components::Position3D,
// Components::Scale2D,
// Components::Rotation2D,
// Components::Transform4x4
// >>();
// TODO: impl for no zoffset comp
void position3d_from_2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Position2D, const Components::Position2D_ZOffset, Components::Position3D>> view);
void transform3d_translate(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Position3D, Components::Transform4x4>> view);
void transform3d_rotate2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Rotation2D, Components::Transform4x4>> view);
void transform3d_scale2d(entt::view<entt::get_t<const Components::DirtyTransformTag, const Components::Scale2D, Components::Transform4x4>> view);
void transform_clear_dirty(entt::registry& scene, entt::view<entt::get_t<Components::DirtyTransformTag>> view);
} // MM::Systems