Skip to content

Commit 7072dc2

Browse files
wip bump thirdparty deps (HFSM2, entt), add RoamingStateMachineComponent
HFSM2: 2.2.1 -> 2.3.0 entt: v3.9.0 -> v3.12.2
1 parent 0458721 commit 7072dc2

12 files changed

+210
-164
lines changed

examples/CMakeLists.txt

+6
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,9 @@ target_link_libraries(hyperspace_fsm_example rendering resources audio
55
add_executable(motionstate_control_example motionstate_control_example.cpp)
66
target_link_libraries(motionstate_control_example ecs urdf rendering resources
77
control)
8+
9+
find_package(ompl)
10+
11+
add_executable(ompl_example ompl_example.cpp)
12+
target_include_directories(ompl_example PUBLIC ${OMPL_INCLUDE_DIRS})
13+
target_link_libraries(ompl_example ${OMPL_LIBRARIES})

examples/hyperspace_fsm_example.cpp

+18-15
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ inline float current_time()
1919
struct Context
2020
{
2121
ecs::ResourceManager* resource_manager;
22-
entt::resource_handle<const rendering::ShaderProgram> tunnel_shader;
23-
entt::resource_handle<const rendering::ShaderProgram> transition_shader;
22+
entt::resource<const rendering::ShaderProgram> tunnel_shader;
23+
entt::resource<const rendering::ShaderProgram> transition_shader;
2424

2525
float last_transition_time = 0.0f;
2626

@@ -66,8 +66,9 @@ struct IdleState : FSM::State
6666
context.last_transition_time = current_time();
6767
context.transition_shader->use();
6868

69-
context.sound_source_idle->play(
70-
context.resource_manager->get_sound("Computer_Drone_10.wav").get(), 0.1);
69+
context.sound_source_idle->play(*context.resource_manager->get_sound("Computer_Drone_10."
70+
"wav"),
71+
0.1);
7172

7273
std::cout << " IdleState" << std::endl;
7374
}
@@ -85,8 +86,8 @@ struct EnteringStateStartup : FSM::State
8586
auto& context = control.context();
8687
context.last_transition_time = current_time();
8788

88-
context.sound_source_effect1->play(
89-
context.resource_manager->get_sound("Fighter_Startup_2.wav").get());
89+
context.sound_source_effect1->play(*context.resource_manager->get_sound("Fighter_Startup_2."
90+
"wav"));
9091

9192
std::cout << " EnteringStateStartup" << std::endl;
9293
}
@@ -111,8 +112,8 @@ struct EnteringStateHyperspaceStart : FSM::State
111112
{
112113
auto& context = control.context();
113114

114-
context.sound_source_effect2->play(
115-
context.resource_manager->get_sound("Hyperspace_Start.wav").get());
115+
context.sound_source_effect2->play(*context.resource_manager->get_sound("Hyperspace_Start."
116+
"wav"));
116117

117118
std::cout << " EnteringStateHyperspaceStart" << std::endl;
118119
}
@@ -137,10 +138,10 @@ struct EnteringStateHyperspaceBreakthrough : FSM::State
137138
{
138139
auto& context = control.context();
139140

140-
context.sound_source_idle->play(context.resource_manager->get_sound("XW_ENG_11.wav").get(),
141+
context.sound_source_idle->play(*context.resource_manager->get_sound("XW_ENG_11.wav"),
141142
0.25);
142-
context.sound_source_effect1->play(
143-
context.resource_manager->get_sound("Deflector_Hit_1B.wav").get());
143+
context.sound_source_effect1->play(*context.resource_manager->get_sound("Deflector_Hit_1B."
144+
"wav"));
144145

145146
std::cout << " EnteringStateHyperspaceBreakthrough" << std::endl;
146147
}
@@ -195,10 +196,12 @@ struct ExitingState : FSM::State
195196

196197
context.transition_shader->use();
197198

198-
context.sound_source_idle->play(
199-
context.resource_manager->get_sound("Computer_Drone_10.wav").get(), 0.1);
200-
context.sound_source_effect1->play(
201-
context.resource_manager->get_sound("Laser_Whoosh_4A.wav").get(), 0.25);
199+
context.sound_source_idle->play(*context.resource_manager->get_sound("Computer_Drone_10."
200+
"wav"),
201+
0.1);
202+
context.sound_source_effect1->play(*context.resource_manager->get_sound("Laser_Whoosh_4A."
203+
"wav"),
204+
0.25);
202205

203206
std::cout << " ExitingState" << std::endl;
204207
}

include/ecs/components.h

+21-9
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,22 @@
1212
#include "urdf/fighter_input.h"
1313
#include "urdf/fighter_model.h"
1414
#include "audio/audio.h"
15+
#include "sm/roaming_state_machine.h"
1516

1617
using MotionStateComponent = geometry::MotionState;
1718

19+
struct RoamingStateMachineComponent
20+
{
21+
RoamingStateMachineComponent(std::shared_ptr<sm::RoamingStateMachineContext> context)
22+
: context(context), instance(*context)
23+
{
24+
std::ignore = context;
25+
}
26+
27+
std::shared_ptr<sm::RoamingStateMachineContext> context;
28+
sm::RoamingStateMachine::Instance instance;
29+
};
30+
1831
struct CameraComponent
1932
{
2033
Eigen::Matrix4f perspective;
@@ -31,11 +44,10 @@ struct CameraComponent
3144

3245
struct FighterComponent
3346
{
34-
FighterComponent(const std::string& name,
35-
entt::resource_handle<const urdf::FighterModel> model);
47+
FighterComponent(const std::string& name, entt::resource<const urdf::FighterModel> model);
3648

3749
std::string name;
38-
entt::resource_handle<const urdf::FighterModel> model;
50+
entt::resource<const urdf::FighterModel> model;
3951
urdf::FighterInput input;
4052
int current_fire_mode = 0;
4153
int current_spawn_idx = 0;
@@ -68,27 +80,27 @@ struct FighterComponent
6880
struct LaserComponent
6981
{
7082
entt::entity producer;
71-
entt::resource_handle<const urdf::FighterModel> fighter_model;
83+
entt::resource<const urdf::FighterModel> fighter_model;
7284
float length;
7385
};
7486

7587
struct SkyboxComponent
7688
{
77-
entt::resource_handle<const rendering::Texture> texture;
78-
entt::resource_handle<const rendering::Model> model;
89+
entt::resource<const rendering::Texture> texture;
90+
entt::resource<const rendering::Model> model;
7991
};
8092

8193
struct VisualComponent
8294
{
83-
entt::resource_handle<const rendering::Model> model;
84-
std::optional<std::vector<entt::resource_handle<const rendering::Texture>>> textures;
95+
entt::resource<const rendering::Model> model;
96+
std::optional<std::vector<entt::resource<const rendering::Texture>>> textures;
8597
std::optional<Eigen::Vector3f> color = std::nullopt;
8698
std::optional<Eigen::Vector3f> size = std::nullopt;
8799
};
88100

89101
struct SoundEffectComponent
90102
{
91-
entt::resource_handle<const audio::AudioBuffer> buffer;
103+
entt::resource<const audio::AudioBuffer> buffer;
92104
std::unique_ptr<audio::AudioSource> sound_source;
93105
};
94106

include/ecs/resource_manager.h

+100-13
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,99 @@
77
#include "rendering/shader_program.h"
88
#include "urdf/fighter_model.h"
99
#include "audio/audio.h"
10-
10+
#include "resources/load_model.h"
11+
#include "resources/load_texture.h"
12+
#include "resources/load_geometry.h"
13+
#include "resources/locator.h"
14+
#include "rendering/compile_shader_program.h"
15+
#include "rendering/primitives.h"
16+
#include "urdf/parsing.h"
1117
#include <entt/entt.hpp>
1218

1319
namespace ecs
1420
{
21+
struct model_loader final
22+
{
23+
using result_type = std::shared_ptr<rendering::Model>;
24+
25+
result_type operator()(const std::string& uri) const
26+
{
27+
auto meshes = std::vector<rendering::Mesh>();
28+
for (const auto& mesh_data : resources::load_model(uri))
29+
{
30+
meshes.emplace_back(mesh_data);
31+
}
32+
33+
return std::make_shared<rendering::Model>(uri, std::move(meshes));
34+
}
35+
36+
result_type operator()(std::function<rendering::Model()> load_fn) const
37+
{
38+
return std::make_shared<rendering::Model>(load_fn());
39+
}
40+
};
41+
42+
struct primitive_loader final
43+
{
44+
using result_type = std::shared_ptr<rendering::Model>;
45+
46+
result_type operator()(std::function<rendering::Model()> load_fn) const
47+
{
48+
return std::make_shared<rendering::Model>(load_fn());
49+
}
50+
};
51+
52+
struct texture_loader final
53+
{
54+
using result_type = std::shared_ptr<rendering::Texture>;
55+
56+
result_type operator()(const std::string& uri, const bool as_cubemap) const
57+
{
58+
if (as_cubemap)
59+
{
60+
return std::make_shared<rendering::Texture>(uri, resources::load_cubemap_texture(uri));
61+
}
62+
else
63+
{
64+
return std::make_shared<rendering::Texture>(resources::load_texture(uri));
65+
}
66+
}
67+
};
68+
69+
struct shader_loader final
70+
{
71+
using result_type = std::shared_ptr<rendering::ShaderProgram>;
72+
73+
result_type operator()(const std::string& uri,
74+
const std::string& vert_filename,
75+
const std::string& frag_filename,
76+
const std::optional<std::string>& geom_filename) const
77+
{
78+
return rendering::compileShaders(uri, vert_filename, frag_filename, geom_filename);
79+
}
80+
};
81+
82+
struct fighter_model_loader final
83+
{
84+
using result_type = std::shared_ptr<urdf::FighterModel>;
85+
86+
result_type operator()(const std::string& uri) const
87+
{
88+
return std::make_shared<urdf::FighterModel>(urdf::parse_fighter_urdf(uri));
89+
}
90+
};
91+
92+
struct sound_loader final
93+
{
94+
using result_type = std::shared_ptr<audio::AudioBuffer>;
95+
96+
result_type operator()(const std::string& uri) const
97+
{
98+
return std::make_shared<audio::AudioBuffer>(std::string(resources::locator::SOUNDS_PATH) +
99+
uri);
100+
}
101+
};
102+
15103
class ResourceManager
16104
{
17105
public:
@@ -28,20 +116,19 @@ class ResourceManager
28116
void load_fighter_model(const std::string& uri);
29117
void load_sound(const std::string& uri);
30118

31-
void
32-
update_shaders(std::function<void(entt::resource_handle<const rendering::ShaderProgram>)> fn);
119+
void update_shaders(std::function<void(const entt::resource<rendering::ShaderProgram>&)> fn);
33120

34-
entt::resource_handle<const rendering::Model> get_model(const std::string& uri) const;
35-
entt::resource_handle<const rendering::Texture> get_texture(const std::string& uri) const;
36-
entt::resource_handle<const rendering::ShaderProgram> get_shader(const std::string& uri) const;
37-
entt::resource_handle<const urdf::FighterModel> get_fighter_model(const std::string& uri) const;
38-
entt::resource_handle<const audio::AudioBuffer> get_sound(const std::string& uri) const;
121+
entt::resource<const rendering::Model> get_model(const std::string& uri) const;
122+
entt::resource<const rendering::Texture> get_texture(const std::string& uri) const;
123+
entt::resource<const rendering::ShaderProgram> get_shader(const std::string& uri) const;
124+
entt::resource<const urdf::FighterModel> get_fighter_model(const std::string& uri) const;
125+
entt::resource<const audio::AudioBuffer> get_sound(const std::string& uri) const;
39126

40127
private:
41-
entt::resource_cache<rendering::Model> model_cache;
42-
entt::resource_cache<rendering::Texture> texture_cache;
43-
entt::resource_cache<rendering::ShaderProgram> shader_cache;
44-
entt::resource_cache<urdf::FighterModel> fighter_model_cache;
45-
entt::resource_cache<audio::AudioBuffer> sound_cache;
128+
entt::resource_cache<rendering::Model, model_loader> model_cache;
129+
entt::resource_cache<rendering::Texture, texture_loader> texture_cache;
130+
entt::resource_cache<rendering::ShaderProgram, shader_loader> shader_cache;
131+
entt::resource_cache<urdf::FighterModel, fighter_model_loader> fighter_model_cache;
132+
entt::resource_cache<audio::AudioBuffer, sound_loader> sound_cache;
46133
};
47134
} // namespace ecs

include/ecs/scene.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Scene
2323
entt::entity register_camera(const Eigen::Matrix4f& perspective);
2424
entt::entity register_laser(const Eigen::Vector3f& position,
2525
const Eigen::Quaternionf& orientation,
26-
const entt::resource_handle<const urdf::FighterModel> model,
26+
const entt::resource<const urdf::FighterModel> model,
2727
const Eigen::Vector3f& size,
2828
const Eigen::Vector3f color,
2929
const float speed,

src/ecs/components.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ CameraComponent::get_target_state(const geometry::MotionState& tracked_entity_st
1616
}
1717

1818
FighterComponent::FighterComponent(const std::string& name,
19-
entt::resource_handle<const urdf::FighterModel> model)
19+
entt::resource<const urdf::FighterModel> model)
2020
: name(name),
2121
model(model),
2222
fire_sound_source(std::make_unique<audio::AudioSource>(1.0f, false)),

0 commit comments

Comments
 (0)