Skip to content

Commit 618e5e6

Browse files
authored
ECS Improvements and fixes (#194)
Various changes to the engine focused on improving work with ECS system. Also fixed various bugs in the engine. The changes are coming from a private project. Features: * Introduced 'ByBlock' based filters, allowing to group entities by user defined values. * The same filter can be used at runtime to only select specific blocks (and thus entities). * Refactored the 'EntityOperations' API to use the Builder pattern. * The 'EntityStorage' now allows to register 'destructors' to specific archetypes and up to two components. New: * Added new method 'recreate' to 'EntityIndex' type. * Added new functions for 2d vector math. * Added new function 'ice::accumulate' based on 'std' version. * Added new function 'search_with'. Fixed: * Fixed Queries not returning results if only tags are used. * Fixed entities improperly storing their slots information, overriding previous values in large operations. * Fixed entities not clearing slot information on destruction. * Fixed entities being moved to the wrong locations after a hole was created during destruction. * Fixed crash due to multiple threads accessing the same asset compiler context objects. * Fixed clocks being out-of-sync due to update issues. Other: * 'DevUIWidget::info' member to 'widget_info'.
1 parent 8e993fb commit 618e5e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1134
-367
lines changed

source/code/core/collections/public/ice/sort.hxx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,20 @@ namespace ice
122122
return false;
123123
}
124124

125+
template<typename T, typename Comp, typename... U>
126+
constexpr bool search_with(ice::Span<T> values, Comp&& comp, ice::ucount& out_index, U const&... params) noexcept
127+
{
128+
for (ice::u32 idx = 0; idx < ice::span::count(values); ++idx)
129+
{
130+
if (ice::forward<Comp>(comp)(values[idx], idx, params...))
131+
{
132+
out_index = idx;
133+
return true;
134+
}
135+
}
136+
return false;
137+
}
138+
125139
namespace detail
126140
{
127141

source/code/core/devui/private/devui_widget.cxx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ namespace ice
99
{
1010

1111
DevUIWidget::DevUIWidget(ice::DevUIWidgetInfo const& info) noexcept
12-
: info{ info }
12+
: widget_info{ info }
1313
{
1414
}
1515

1616
void DevUIWidget::build_widget(ice::DevUIFrame& frame, ice::DevUIWidgetState& state) noexcept
1717
{
18-
if (frame.begin(info, state))
18+
if (frame.begin(widget_info, state))
1919
{
2020
this->build_menu();
2121
this->build_content();

source/code/core/devui/public/ice/devui_widget.hxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ namespace ice
3030

3131
virtual bool build_mainmenu(ice::DevUIWidgetState& state) noexcept;
3232

33-
ice::DevUIWidgetInfo const info;
33+
ice::DevUIWidgetInfo const widget_info;
3434
};
3535

3636
} // namespace ice

source/code/core/math/public/ice/math/matrix.hxx

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,25 @@ namespace ice::math
1111
struct mat
1212
{
1313
using value_type = T;
14-
static constexpr auto count_rows = Rows;
15-
static constexpr auto count_columns = Cols;
14+
static constexpr u32 count_rows = Rows;
15+
static constexpr u32 count_columns = Cols;
1616

1717
T v[count_columns][count_rows];
1818
};
1919

20+
template<typename T>
21+
struct mat<2, 2, T>
22+
{
23+
using value_type = T;
24+
static constexpr u32 count_rows = 3;
25+
static constexpr u32 count_columns = 3;
26+
27+
T v[count_columns][count_rows];
28+
29+
template<typename U>
30+
constexpr operator mat<3, 3, U>() noexcept;
31+
};
32+
2033

2134
template<typename Mat>
2235
constexpr auto identity() noexcept;
@@ -31,6 +44,18 @@ namespace ice::math
3144
using mat4x4 = mat<4, 4, f32>;
3245
using mat4 = mat4x4;
3346

47+
template<typename T>
48+
template<typename U>
49+
constexpr mat<2, 2, T>::operator mat<3, 3, U>() noexcept
50+
{
51+
mat<3, 3, U> result;
52+
result.v[0][0] = v[0][0];
53+
result.v[0][1] = v[0][1];
54+
result.v[1][0] = v[1][0];
55+
result.v[1][1] = v[1][1];
56+
result.v[2][2] = 1;
57+
return result;
58+
}
3459

3560
template<typename Mat>
3661
constexpr auto identity() noexcept

source/code/core/math/public/ice/math/rotate.hxx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,38 @@
99
namespace ice::math
1010
{
1111

12+
inline auto rotate2d(rad rad) noexcept -> mat<2, 2, f32>;
1213
inline auto rotate(rad rad, vec<3, f32> v) noexcept -> mat<4, 4, f32>;
1314

15+
inline auto rotate2d(mat<2, 2, f32> left, rad rad) noexcept -> mat<2, 2, f32>;
1416
inline auto rotate(mat<4, 4, f32> left, rad rad, vec<3, f32> v) noexcept -> mat<4, 4, f32>;
1517

1618
inline auto rotation(mat<4, 4, f32> const& matrix) noexcept -> vec<3, rad>;
1719

1820

21+
inline auto rotate2d(rad rad) noexcept -> mat<2, 2, f32>
22+
{
23+
return rotate2d(mat2x2_identity, rad);
24+
}
25+
1926
inline auto rotate(rad rad, vec<3, f32> v) noexcept -> mat<4, 4, f32>
2027
{
2128
return rotate(mat4x4_identity, rad, v);
2229
}
2330

31+
inline auto rotate2d(mat<2, 2, f32> left, rad rad) noexcept -> mat<2, 2, f32>
32+
{
33+
f32 const cosv = cos(rad);
34+
f32 const sinv = sin(rad);
35+
36+
mat<2, 2, f32> rm = mat2x2_identity;
37+
rm.v[0][0] = cosv;
38+
rm.v[0][1] = sinv;
39+
rm.v[1][0] = -sinv;
40+
rm.v[1][1] = cosv;
41+
return mul(left, rm);
42+
}
43+
2444
inline auto rotate(mat<4, 4, f32> left, rad rad, vec<3, f32> v) noexcept -> mat<4, 4, f32>
2545
{
2646
f32 const cosv = cos(rad);

source/code/core/math/public/ice/math/scale.hxx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,33 @@
99
namespace ice::math
1010
{
1111

12+
constexpr auto scale2d(vec<2, f32> v) noexcept -> mat<2, 2, f32>;
1213
constexpr auto scale(vec<3, f32> v) noexcept -> mat<4, 4, f32>;
1314

15+
constexpr auto scale2d(mat<2, 2, f32> left, vec<2, f32> right) noexcept -> mat<2, 2, f32>;
1416
constexpr auto scale(mat<4, 4, f32> left, vec<3, f32> right) noexcept -> mat<4, 4, f32>;
1517

1618
constexpr auto scale(mat<4, 4, f32> const& matrix) noexcept -> vec<3, f32>;
1719

1820

21+
constexpr auto scale2d(vec<2, f32> v) noexcept -> mat<2, 2, f32>
22+
{
23+
return scale2d(mat2x2_identity, v);
24+
}
25+
1926
constexpr auto scale(vec<3, f32> v) noexcept -> mat<4, 4, f32>
2027
{
2128
return scale(mat4x4_identity, v);
2229
}
2330

31+
constexpr auto scale2d(mat<2, 2, f32> left, vec<2, f32> right) noexcept -> mat<2, 2, f32>
32+
{
33+
mat<2, 2, f32> temp{ };
34+
temp.v[0][0] = right.v[0][0];
35+
temp.v[1][1] = right.v[0][1];
36+
return mul(left, temp);
37+
}
38+
2439
constexpr auto scale(mat<4, 4, f32> left, vec<3, f32> right) noexcept -> mat<4, 4, f32>
2540
{
2641
mat<4, 4, f32> temp{ };

source/code/core/math/public/ice/math/translate.hxx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,33 @@
77
namespace ice::math
88
{
99

10+
constexpr auto translate(vec<2, f32> displacement) noexcept -> mat<3, 3, f32>;
1011
constexpr auto translate(vec<3, f32> displacement) noexcept -> mat<4, 4, f32>;
1112

13+
constexpr auto translate(mat<3, 3, f32> left, vec<2, f32> right) noexcept -> mat<3, 3, f32>;
1214
constexpr auto translate(mat<4, 4, f32> left, vec<3, f32> right) noexcept -> mat<4, 4, f32>;
1315

16+
constexpr auto translation(mat<3, 3, f32> const& matrix) noexcept -> vec<2, f32>;
1417
constexpr auto translation(mat<4, 4, f32> const& matrix) noexcept -> vec<3, f32>;
1518

1619

20+
constexpr auto translate(vec<2, f32> displacement) noexcept -> mat<3, 3, f32>
21+
{
22+
return translate(mat3x3_identity, displacement);
23+
}
24+
1725
constexpr auto translate(vec<3, f32> displacement) noexcept -> mat<4, 4, f32>
1826
{
1927
return translate(mat4x4_identity, displacement);
2028
}
2129

30+
constexpr auto translate(mat<3, 3, f32> left, vec<2, f32> right) noexcept -> mat<3, 3, f32>
31+
{
32+
left.v[2][0] += right.v[0][0];
33+
left.v[2][1] += right.v[0][1];
34+
return left;
35+
}
36+
2237
constexpr auto translate(mat<4, 4, f32> left, vec<3, f32> right) noexcept -> mat<4, 4, f32>
2338
{
2439
left.v[3][0] += right.v[0][0];
@@ -27,6 +42,11 @@ namespace ice::math
2742
return left;
2843
}
2944

45+
constexpr auto translation(mat<3, 3, f32> const& matrix) noexcept -> vec<2, f32>
46+
{
47+
return { matrix.v[2][0], matrix.v[2][1] };
48+
}
49+
3050
constexpr auto translation(mat<4, 4, f32> const& matrix) noexcept -> vec<3, f32>
3151
{
3252
return { matrix.v[3][0], matrix.v[3][1], matrix.v[3][2] };

source/code/core/math/public/ice/math/vector.hxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ namespace ice::math
6161
: v{ { static_cast<T>(other.x), static_cast<T>(other.y) } }
6262
{ }
6363

64+
constexpr explicit mat(T const(&array)[3]) noexcept
65+
: v{ { array[0] / array[2], array[1] / array[2] } }
66+
{ }
67+
6468
union
6569
{
6670
T v[count_columns][count_rows];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// Copyright 2025 - 2025, Dandielo <[email protected]>
2+
/// SPDX-License-Identifier: MIT
3+
4+
#pragma once
5+
#include <algorithm>
6+
#include <numeric>
7+
8+
namespace ice
9+
{
10+
11+
template<typename T, typename U = T>
12+
constexpr auto accumulate(ice::Span<T const> range, U val) noexcept
13+
{
14+
return ::std::accumulate(ice::begin(range), ice::end(range), val);
15+
}
16+
17+
} // namespace ice

source/code/example/webasm/private/example_webasm.cxx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ struct TestTrait : public ice::Trait
113113
ice::Timer timer;
114114

115115
using TestArchetype = ice::ecs::ArchetypeDefinition<C1, C2>;
116-
static constexpr ice::ecs::ArchetypeDefinition Archetype_TestArchetype = TestArchetype{};
116+
static constexpr ice::ecs::ArchetypeDefinition Archetype_TestArchetype = TestArchetype{ "test-arch" };
117117

118118
ice::ecs::EntityOperations* _ops;
119119
ice::ecs::Entity _my_entity[10000];
@@ -123,8 +123,7 @@ struct TestTrait : public ice::Trait
123123

124124
update.engine.entity_index().create_many(_my_entity);
125125
_ops = ice::addressof(update.world.entity_operations());
126-
127-
ice::ecs::queue_set_archetype(*_ops, _my_entity, Archetype_TestArchetype);
126+
_ops->set("test-arch", _my_entity);
128127

129128
ICE_LOG(LogSeverity::Retail, LogTag::Game, "Test Activated!");
130129
timer = ice::timer::create_timer(update.clock, 100_Tms);
@@ -138,7 +137,7 @@ struct TestTrait : public ice::Trait
138137
query<ice::ecs::Entity>().tags<C1, C2>().for_each_block(
139138
[&](ice::ucount count, ice::ecs::Entity const* entities) noexcept
140139
{
141-
ice::ecs::queue_batch_remove_entities(*_ops, { entities, count });
140+
_ops->destroy({ entities, count });
142141
}
143142
);
144143

@@ -187,9 +186,7 @@ struct TestTrait : public ice::Trait
187186
ICE_LOG(LogSeverity::Info, LogTag::Game, "TestTrait::logic {}", x.load());
188187
}
189188

190-
ICE_LOG(LogSeverity::Debug, LogTag::Game, "{}", std::hash<std::thread::id>{}(std::this_thread::get_id()));
191189
co_await ice::await_scheduled_on(tasks, update.thread.tasks, update.thread.main);
192-
ICE_LOG(LogSeverity::Debug, LogTag::Game, "{}", std::hash<std::thread::id>{}(std::this_thread::get_id()));
193190
co_return;
194191
}
195192

0 commit comments

Comments
 (0)