Skip to content

Commit 395eea4

Browse files
committed
update set adn get field string access in iterator + new exemple
1 parent ccb8c70 commit 395eea4

2 files changed

Lines changed: 62 additions & 8 deletions

File tree

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.github.elebras1.flecs.examples;
2+
3+
import com.github.elebras1.flecs.*;
4+
import com.github.elebras1.flecs.examples.components.Minister;
5+
import com.github.elebras1.flecs.util.FlecsConstants;
6+
7+
import java.util.Random;
8+
9+
public class MultiThreadedSystemExemple {
10+
public static void main(String[] args) {
11+
try (World world = new World()) {
12+
System.out.println("=== Test Multithreaded system + native access ===");
13+
world.component(Minister.class);
14+
world.setThreads(4);
15+
16+
Random rnd = new Random();
17+
for (int i = 0; i < 1000; i++) {
18+
world.obtainEntity(world.entity("Min_" + i)).set(new Minister("M-" + i, "default.png", rnd.nextFloat() * 50, 2020, 0));
19+
}
20+
21+
world.system("LoyaltySystem")
22+
.with(Minister.class)
23+
.kind(FlecsConstants.EcsOnUpdate)
24+
.multiThreaded(true)
25+
.iter(it -> {
26+
int count = it.count();
27+
for (int i = 0; i < count; i++) {
28+
float loyalty = it.fieldFloat(Minister.class, 0, "loyalty", i);
29+
30+
float newLoyalty = Math.min(loyalty + 10.0f, 100.0f);
31+
String newImg = newLoyalty > 50 ? "happy.png" : "angry.png";
32+
33+
it.setFieldFloat(Minister.class, 0, "loyalty", i, newLoyalty);
34+
it.setFieldString(Minister.class, 0, "imageFileName", i, newImg);
35+
}
36+
});
37+
38+
for (int f = 0; f < 5; f++) {
39+
world.progress(0.016f);
40+
}
41+
42+
Minister m = world.obtainEntity(world.lookup("Min_42")).get(Minister.class);
43+
System.out.printf("Check Min_42 -> Loyalty: %.1f | Img: %s%n", m.loyalty(), m.imageFileName());
44+
}
45+
}
46+
}

src/main/java/com/github/elebras1/flecs/Iter.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.github.elebras1.flecs;
22

3+
import java.lang.foreign.MemoryLayout;
34
import java.lang.foreign.MemorySegment;
45
import java.lang.foreign.ValueLayout;
56
import java.util.Arrays;
@@ -174,13 +175,9 @@ public <T> byte fieldByte(Class<T> componentClass, int index, String fieldName,
174175

175176
public <T> String fieldString(Class<T> componentClass, int index, String fieldName, int i) {
176177
long offset = fieldPtr(componentClass, index, fieldName, i);
177-
MemorySegment stringPointer = this.cachedColumns[index].get(ValueLayout.ADDRESS.withByteAlignment(1), offset);
178-
179-
if (stringPointer.address() == 0) {
180-
return null;
181-
}
182-
183-
return stringPointer.reinterpret(Short.MAX_VALUE).getString(0);
178+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
179+
long capacity = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
180+
return this.cachedColumns[index].asSlice(offset, capacity).getString(0);
184181
}
185182

186183
public <T> void setFieldInt(Class<T> componentClass, int index, String fieldName, int i, int value) {
@@ -219,7 +216,18 @@ public <T> void setFieldByte(Class<T> componentClass, int index, String fieldNam
219216
}
220217

221218
public <T> void setFieldString(Class<T> componentClass, int index, String fieldName, int i, String value) {
222-
throw new UnsupportedOperationException("setFieldString not yet implemented");
219+
long offset = fieldPtr(componentClass, index, fieldName, i);
220+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
221+
long capacity = component.layout().select(MemoryLayout.PathElement.groupElement(fieldName)).byteSize();
222+
MemorySegment slice = this.cachedColumns[index].asSlice(offset, capacity);
223+
if (value == null || value.isEmpty()) {
224+
slice.set(ValueLayout.JAVA_BYTE, 0, (byte) 0);
225+
return;
226+
}
227+
byte[] bytes = value.getBytes(java.nio.charset.StandardCharsets.UTF_8);
228+
int len = Math.min(bytes.length, (int) capacity - 1);
229+
MemorySegment.copy(bytes, 0, slice, ValueLayout.JAVA_BYTE, 0, len);
230+
slice.set(ValueLayout.JAVA_BYTE, len, (byte) 0);
223231
}
224232

225233
public long event() {

0 commit comments

Comments
 (0)