initial import, >900commits predate this

This commit is contained in:
2020-09-29 13:47:50 +02:00
commit e74154ccee
352 changed files with 108120 additions and 0 deletions

View File

@ -0,0 +1,28 @@
cmake_minimum_required(VERSION 3.2)
project(common_components CXX)
add_library(common_components INTERFACE)
target_include_directories(common_components INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_link_libraries(common_components INTERFACE
entt
glm
)
##########################
add_library(common_components_serialize_json INTERFACE)
target_include_directories(common_components_serialize_json INTERFACE "${CMAKE_CURRENT_SOURCE_DIR}/src")
target_link_libraries(common_components_serialize_json INTERFACE
common_components
nlohmann_json::nlohmann_json
)
if(BUILD_TESTING)
add_subdirectory(test)
endif()

View File

@ -0,0 +1,10 @@
#pragma once
#include <glm/vec4.hpp>
namespace MM::Components {
struct Color {
glm::vec4 color {1.f, 1.f, 1.f, 1.f};
};
}

View File

@ -0,0 +1,11 @@
#pragma once
#include <string>
namespace MM::Components {
struct Name {
static const size_t max_str_len = 64;
std::string str;
};
}

View File

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

View File

@ -0,0 +1,16 @@
#pragma once
#include <glm/vec2.hpp>
#include <glm/vec3.hpp>
#include <glm/vec4.hpp>
#include <nlohmann/json.hpp>
namespace glm {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(vec2, x, y)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(vec3, x, y, z)
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(vec4, x, y, z, w)
}

View File

@ -0,0 +1,10 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/name.hpp>
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(Name, str)
} // MM::Components

View File

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

View File

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

View File

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

View File

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

View File

@ -0,0 +1,10 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/view_dir2d.hpp>
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ViewDir2D, dir)
} // MM::Components

View File

@ -0,0 +1,10 @@
#pragma once
#include <nlohmann/json.hpp>
#include <mm/components/view_dir3d.hpp>
namespace MM::Components {
NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE(ViewDir3D, yaw, pitch, roll)
} // MM::Components

View File

@ -0,0 +1,55 @@
//
// 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

@ -0,0 +1,37 @@
#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,16 @@
//
// 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,13 @@
#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

@ -0,0 +1,10 @@
#pragma once
namespace MM::Components {
struct ViewDir2D {
float dir = 0.f; // rad
};
} // MM::Components

View File

@ -0,0 +1,12 @@
#pragma once
namespace MM::Components {
struct ViewDir3D {
float yaw = 0.f; // rad
float pitch = 0.f; // rad
float roll = 0.f; // rad
};
} // MM::Components

View File

@ -0,0 +1,16 @@
add_executable(common_component_json_serialization_test
component_json_serialization_test.cpp
)
target_include_directories(common_component_json_serialization_test PRIVATE ".")
target_link_libraries(common_component_json_serialization_test
common_components_serialize_json
engine
gtest_main
)
add_test(NAME common_component_json_serialization_test COMMAND common_component_json_serialization_test)

View File

@ -0,0 +1,270 @@
#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_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)); \
} \
{ \
nlohmann::json j; \
[>EXPECT_NO_THROW({ j = comp; });<] \
ASSERT_EQ(json_string, 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;
const char* json_test_file = "{\"str\":\"test_name\"}";
{ // in
auto j = nlohmann::json::parse(json_test_file);
EXPECT_NO_THROW({
comp = j;
});
ASSERT_EQ(comp.str, "test_name");
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
TEST(common_components_json_serialization, in_out_name_fail) {
MM::Components::Name name_comp;
// intentional malformed json string
const char* json_test_file = "{\"strasdf\":\"test_name\"}";
{ // in
auto j = nlohmann::json::parse(json_test_file);
ASSERT_ANY_THROW({
name_comp = j;
});
}
}
// ##############################################################
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};
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(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(common_components_json_serialization, in_out_velocity2d) {
MM::Components::Velocity2D comp;
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);
EXPECT_NO_THROW({
comp = j;
});
ASSERT_EQ(comp.velocity.x, 42.f);
ASSERT_EQ(comp.velocity.y, 6.f);
ASSERT_EQ(comp.rotation, 99.f);
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
// ##############################################################
TEST(common_components_json_serialization, in_out_view_dir2d) {
MM::Components::ViewDir2D comp;
const char* json_test_file = R"({"dir":6.0})";
{ // in
auto j = nlohmann::json::parse(json_test_file);
EXPECT_NO_THROW({
comp = j;
});
ASSERT_EQ(comp.dir, 6.f);
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
// ##############################################################
TEST(common_components_json_serialization, in_out_view_dir3d) {
MM::Components::ViewDir3D comp;
const char* json_test_file = R"({"pitch":6.0,"roll":99.0,"yaw":42.0})";
{ // in
auto j = nlohmann::json::parse(json_test_file);
EXPECT_NO_THROW({
comp = j;
});
ASSERT_EQ(comp.yaw, 42.f);
ASSERT_EQ(comp.pitch, 6.f);
ASSERT_EQ(comp.roll, 99.f);
}
{ // out
nlohmann::json j;
EXPECT_NO_THROW({
j = comp;
});
ASSERT_EQ(json_test_file, j.dump());
}
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}