Skip to content

Commit 3898f8d

Browse files
committed
#839 Ensure on_add hooks are invoked when using bulk_init
1 parent 3451ca3 commit 3898f8d

File tree

10 files changed

+193
-163
lines changed

10 files changed

+193
-163
lines changed

examples/c/systems/system_ctx/project.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
"flecs"
77
],
88
"public": false
9+
},
10+
"lang.c": {
11+
"${os linux}": {
12+
"lib": ["m"]
13+
}
914
}
10-
}
15+
}

examples/c/systems/system_ctx/src/main.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,12 @@ typedef struct {
1616
double value;
1717
} Radius;
1818

19-
double distance(const Position *p1, const Position *p2) {
20-
return sqrt(pow(p2->x - p1->x, 2) + pow(p2->y - p1->y, 2));
19+
double sqr(double value) {
20+
return value * value;
21+
}
22+
23+
double distance_sqr(const Position *p1, const Position *p2) {
24+
return sqr(p2->x - p1->x) + sqr(p2->y - p1->y);
2125
}
2226

2327
void Collide(ecs_iter_t *it) {
@@ -47,11 +51,10 @@ void Collide(ecs_iter_t *it) {
4751
}
4852

4953
// Check for collision
50-
double d = distance(&p1[i], &p2[j]);
51-
double r = r1[i].value + r2[j].value;
52-
if (r > d) {
53-
printf("%u (r: %.0f) and %u (r: %.0f) collided! (d = %.2f)\n",
54-
(uint32_t)e1, r1[i].value, (uint32_t)e2, r2[j].value, d);
54+
double d_sqr = distance_sqr(&p1[i], &p2[j]);
55+
double r_sqr = sqr(r1[i].value + r2[j].value);
56+
if (r_sqr > d_sqr) {
57+
printf("%u and %u collided!\n", (uint32_t)e1, (uint32_t)e2);
5558
}
5659
}
5760
}
@@ -96,8 +99,8 @@ int main(int argc, char *argv[]) {
9699
return ecs_fini(ecs);
97100

98101
// Output
99-
// 508 (r: 6) and 505 (r: 4) collided! (d = 9.22)
100-
// 510 (r: 10) and 508 (r: 6) collided! (d = 14.32)
101-
// 513 (r: 10) and 506 (r: 3) collided! (d = 3.61)
102-
// 514 (r: 6) and 512 (r: 8) collided! (d = 11.40)
102+
// 508 and 505 collided!
103+
// 510 and 508 collided!
104+
// 513 and 506 collided!
105+
// 514 and 512 collided!
103106
}

examples/c/systems/time_interval/src/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include <time_interval.h>
2-
#include <basics.h>
32
#include <stdio.h>
43

54
// This example shows how to run a system at a specified time interval.

examples/cpp/systems/system_ctx/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
"public": false,
99
"language": "c++"
1010
}
11-
}
11+
}

examples/cpp/systems/system_ctx/src/main.cpp

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <system_ctx.h>
22
#include <iostream>
3-
#include <math.h>
43

54
// Applications can pass context data to a system. A common use case where this
65
// comes in handy is when a system needs to iterate more than one query. The
@@ -15,8 +14,12 @@ struct Radius {
1514
double value;
1615
};
1716

18-
double distance(const Position& p1, const Position& p2) {
19-
return sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2));
17+
double sqr(double value) {
18+
return value * value;
19+
}
20+
21+
double distance_sqr(const Position& p1, const Position& p2) {
22+
return sqr(p2.x - p1.x) + sqr(p2.y - p1.y);
2023
}
2124

2225
double randd(int max) {
@@ -33,10 +36,10 @@ int main(int, char *[]) {
3336
auto sys = ecs.system<const Position, const Radius>("Collide")
3437
.ctx(&q_collide)
3538
.each([](flecs::iter& it, size_t i, const Position& p1, const Radius& r1) {
36-
CollisionQuery *q_collide = it.ctx<CollisionQuery>();
39+
CollisionQuery *q = it.ctx<CollisionQuery>();
3740
flecs::entity e1 = it.entity(i);
3841

39-
q_collide->each([&](flecs::entity e2, const Position& p2, const Radius& r2) {
42+
q->each([&](flecs::entity e2, const Position& p2, const Radius& r2) {
4043
if (e1 == e2) {
4144
// don't collide with self
4245
return;
@@ -49,12 +52,10 @@ int main(int, char *[]) {
4952
}
5053

5154
// Check for collision
52-
double d = distance(p1, p2);
53-
double r = r1.value + r2.value;
54-
if (r > d) {
55-
std::cout << e1 << " (r: " << r1.value << ") and "
56-
<< e2 << " (r: " << r2.value << ") collided! "
57-
<< "(d = " << d << "\n";
55+
double d_sqr = distance_sqr(p1, p2);
56+
double r_sqr = sqr(r1.value + r2.value);
57+
if (r_sqr > d_sqr) {
58+
std::cout << e1 << " and " << e2 << " collided!\n";
5859
}
5960
});
6061
});
@@ -70,8 +71,8 @@ int main(int, char *[]) {
7071
sys.run();
7172

7273
// Output
73-
// 526 (r: 4) and 529 (r: 6) collided! (d = 9.21954
74-
// 527 (r: 3) and 534 (r: 10) collided! (d = 3.60555
75-
// 529 (r: 6) and 531 (r: 10) collided! (d = 14.3178
76-
// 533 (r: 8) and 535 (r: 6) collided! (d = 11.4018
74+
// 526 and 529 collided!
75+
// 527 and 534 collided!
76+
// 529 and 531 collided!
77+
// 533 and 535 collided!
7778
}

flecs.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3819,9 +3819,7 @@ int32_t flecs_table_grow_data(
38193819
/* Initialize entity ids and record ptrs */
38203820
int32_t i;
38213821
if (ids) {
3822-
for (i = 0; i < to_add; i ++) {
3823-
e[i] = ids[i];
3824-
}
3822+
ecs_os_memcpy_n(e, ids, ecs_entity_t, to_add);
38253823
} else {
38263824
ecs_os_memset(e, 0, ECS_SIZEOF(ecs_entity_t) * to_add);
38273825
}
@@ -3834,6 +3832,8 @@ int32_t flecs_table_grow_data(
38343832
ecs_type_info_t *ti = type_info[i];
38353833
flecs_table_grow_column(world, column, ti, to_add, size, true);
38363834
ecs_assert(columns[i].size == size, ECS_INTERNAL_ERROR, NULL);
3835+
flecs_run_add_hooks(world, table, ti, column, e, table->type.array[i],
3836+
cur_count, to_add, false);
38373837
}
38383838

38393839
/* Add elements to each switch column */
@@ -4303,11 +4303,11 @@ int32_t flecs_table_appendn(
43034303
ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL);
43044304

43054305
flecs_table_check_sanity(table);
4306-
43074306
int32_t cur_count = flecs_table_data_count(data);
43084307
int32_t result = flecs_table_grow_data(
43094308
world, table, data, to_add, cur_count + to_add, ids);
43104309
flecs_table_check_sanity(table);
4310+
43114311
return result;
43124312
}
43134313

src/table.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,9 +1438,7 @@ int32_t flecs_table_grow_data(
14381438
/* Initialize entity ids and record ptrs */
14391439
int32_t i;
14401440
if (ids) {
1441-
for (i = 0; i < to_add; i ++) {
1442-
e[i] = ids[i];
1443-
}
1441+
ecs_os_memcpy_n(e, ids, ecs_entity_t, to_add);
14441442
} else {
14451443
ecs_os_memset(e, 0, ECS_SIZEOF(ecs_entity_t) * to_add);
14461444
}
@@ -1453,6 +1451,8 @@ int32_t flecs_table_grow_data(
14531451
ecs_type_info_t *ti = type_info[i];
14541452
flecs_table_grow_column(world, column, ti, to_add, size, true);
14551453
ecs_assert(columns[i].size == size, ECS_INTERNAL_ERROR, NULL);
1454+
flecs_run_add_hooks(world, table, ti, column, e, table->type.array[i],
1455+
cur_count, to_add, false);
14561456
}
14571457

14581458
/* Add elements to each switch column */
@@ -1922,11 +1922,11 @@ int32_t flecs_table_appendn(
19221922
ecs_assert(!table->lock, ECS_LOCKED_STORAGE, NULL);
19231923

19241924
flecs_table_check_sanity(table);
1925-
19261925
int32_t cur_count = flecs_table_data_count(data);
19271926
int32_t result = flecs_table_grow_data(
19281927
world, table, data, to_add, cur_count + to_add, ids);
19291928
flecs_table_check_sanity(table);
1929+
19301930
return result;
19311931
}
19321932

test/api/project.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@
815815
"ctor_on_move_pair",
816816
"move_on_realloc",
817817
"move_on_bulk_new",
818+
"on_add_on_bulk_new",
818819
"move_on_delete",
819820
"move_dtor_on_delete",
820821
"copy_on_override_pair",

0 commit comments

Comments
 (0)