|
1 | 1 | # Flecs-Java |
2 | 2 |
|
3 | | -Java wrapper for the ECS library [Flecs](https://github.com/SanderMertens/flecs) using the Java 25 Foreign Function & Memory API (FFM). |
| 3 | + |
| 4 | + |
| 5 | +Java bindings for [Flecs](https://github.com/SanderMertens/flecs) - A fast and flexible Entity Component System (ECS) using Java 25's Foreign Function & Memory API (FFM). |
| 6 | + |
| 7 | +## What is Flecs? |
| 8 | + |
| 9 | +Flecs is a powerful ECS framework written in C that provides high-performance data-oriented programming. This wrapper brings Flecs capabilities to Java while using Project Panama's FFM API. |
| 10 | + |
| 11 | +- **Multi-Platform**: Support for Linux, Windows, and macOS |
| 12 | + |
| 13 | +## Requirements |
| 14 | + |
| 15 | +### Runtime |
| 16 | +- **Java 25+** |
| 17 | +- **Gradle 9+** |
| 18 | + |
| 19 | +### Build from Source |
| 20 | +- **GCC or compatible C compiler** (for compiling the native Flecs library) |
| 21 | +- **jextract-25** (for generating Java FFM bindings when updating Flecs version) |
| 22 | +- **Supported Architectures**: |
| 23 | + - Linux: x86_64, aarch64 |
| 24 | + - Windows: x86_64, aarch64 |
| 25 | + - macOS: x86_64, aarch64 |
| 26 | + |
| 27 | +## Installation |
| 28 | + |
| 29 | +### Gradle |
| 30 | + |
| 31 | +```gradle |
| 32 | +dependencies { |
| 33 | + implementation 'io.github.elebras1:flecs-java:0.2.0' |
| 34 | + annotationProcessor 'io.github.elebras1:flecs-java:0.2.0' |
| 35 | +
|
| 36 | +} |
| 37 | +``` |
| 38 | + |
| 39 | +### Build from Source |
| 40 | + |
| 41 | +```bash |
| 42 | +# Clone repository |
| 43 | +git clone https://github.com/elebras1/flecs-java.git |
| 44 | +cd flecs-java |
| 45 | + |
| 46 | +# Build (downloads Flecs, compiles natives) |
| 47 | +./gradlew build |
| 48 | + |
| 49 | +# Run examples |
| 50 | +./gradlew :examples:run |
| 51 | +``` |
| 52 | + |
| 53 | +## Example |
| 54 | + |
| 55 | +```java |
| 56 | +import com.github.elebras1.flecs.*; |
| 57 | +import com.github.elebras1.flecs.annotation.FlecsComponent; |
| 58 | + |
| 59 | +// Define components as records |
| 60 | +@FlecsComponent |
| 61 | +record Position(float x, float y) {} |
| 62 | + |
| 63 | +@FlecsComponent |
| 64 | +record Velocity(float dx, float dy) {} |
| 65 | + |
| 66 | +public class Example { |
| 67 | + public static void main(String[] args) { |
| 68 | + try (Flecs world = new Flecs()) { |
| 69 | + // Register components |
| 70 | + world.component(Position.class); |
| 71 | + world.component(Velocity.class); |
| 72 | + |
| 73 | + // Create entities |
| 74 | + Entity player = world.obtainEntity(world.entity("Player")); |
| 75 | + player.set(new Position(0, 0)) |
| 76 | + .set(new Velocity(1, 0)); |
| 77 | + |
| 78 | + Entity enemy = world.obtainEntity(world.entity("Enemy")); |
| 79 | + enemy.set(new Position(10, 5)) |
| 80 | + .set(new Velocity(-0.5f, 0)); |
| 81 | + |
| 82 | + // Create a movement system |
| 83 | + world.system("MoveSystem") |
| 84 | + .with(Position.class) |
| 85 | + .with(Velocity.class) |
| 86 | + .kind(FlecsConstants.EcsOnUpdate) |
| 87 | + .iter(it -> { |
| 88 | + Field<Position> positions = it.field(Position.class, 0); |
| 89 | + Field<Velocity> velocities = it.field(Velocity.class, 1); |
| 90 | + |
| 91 | + for (int i = 0; i < it.count(); i++) { |
| 92 | + Position pos = positions.get(i); |
| 93 | + Velocity vel = velocities.get(i); |
| 94 | + |
| 95 | + // Update position |
| 96 | + positions.set(i, new Position( |
| 97 | + pos.x() + vel.dx() * it.deltaTime(), |
| 98 | + pos.y() + vel.dy() * it.deltaTime() |
| 99 | + )); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + // Run simulation |
| 104 | + for (int i = 0; i < 10; i++) { |
| 105 | + world.progress(0.016f); // 60 FPS |
| 106 | + } |
| 107 | + |
| 108 | + // Query entities |
| 109 | + try (Query query = world.query() |
| 110 | + .with(Position.class) |
| 111 | + .build()) { |
| 112 | + query.each(entityId -> { |
| 113 | + Entity e = world.obtainEntity(entityId); |
| 114 | + Position pos = e.get(Position.class); |
| 115 | + System.out.printf("%s: (%.2f, %.2f)%n", |
| 116 | + e.getName(), pos.x(), pos.y()); |
| 117 | + }); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +## Documentation |
| 125 | + |
| 126 | +- **[Flecs Manual](https://www.flecs.dev/flecs/)** - Official Flecs documentation |
| 127 | +- **[Examples](examples/src/main/java/com/github/elebras1/flecs/examples/)** - Code examples covering various features |
| 128 | + |
| 129 | +## Architecture |
| 130 | + |
| 131 | +### FFM API Integration |
| 132 | + |
| 133 | +Flecs-Java uses Java 25's Foreign Function & Memory API for direct C interop: |
| 134 | +- **Zero JNI overhead**: Direct native calls without marshalling |
| 135 | +- **Memory safety**: Arena-based memory management |
| 136 | +- **Type safety**: Strong typing with `MemorySegment` and layouts |
| 137 | + |
| 138 | +### Component System |
| 139 | + |
| 140 | +Components are defined as Java records with the `@FlecsComponent` annotation. An annotation processor generates the necessary memory layouts and accessor code at compile time. |
| 141 | + |
| 142 | +## Building |
| 143 | + |
| 144 | +### Build Process Overview |
| 145 | + |
| 146 | +The build process automatically handles the following steps: |
| 147 | + |
| 148 | +1. **Download Flecs C Source** (`downloadFlecs`) |
| 149 | +2. **Compile Native Library** (`compileFlecsNative`) |
| 150 | +3. **Compile Annotation Processor** (`compileProcessor`) |
| 151 | +4. **Generate Java Source** (Annotation Processing Phase) |
| 152 | +5. **Package JAR** |
| 153 | +6. **Runtime Native Loading** |
| 154 | + |
| 155 | +### Updating FFM Bindings |
| 156 | + |
| 157 | +When updating the Flecs version, maintainers must regenerate the FFM bindings: |
| 158 | + |
| 159 | +```bash |
| 160 | +# (requires jextract-25 installed) |
| 161 | +./gradlew generateFlecsBindings |
| 162 | +``` |
| 163 | + |
| 164 | +This generates the Java FFM interface bindings from `flecs.h` and stores them in `src/main/generated/`. Regular users don't need to run this task. |
| 165 | + |
| 166 | +## Contributing |
| 167 | + |
| 168 | +This wrapper currently implements core ECS functionality but does not yet support all Flecs features. |
| 169 | +Feel free to open an issue or pull request. All contributions are welcome! |
| 170 | + |
| 171 | +1. Fork the repository |
| 172 | +2. Create a feature branch |
| 173 | +3. Make your changes |
| 174 | +4. Submit a pull request |
| 175 | + |
| 176 | +Or just report issues you encounter! |
| 177 | + |
| 178 | +## Support |
| 179 | + |
| 180 | +- **Issues**: [GitHub Issues](https://github.com/elebras1/flecs-java/issues) |
| 181 | +- **Flecs Discord**: [Join the community](https://discord.gg/flecs) |
| 182 | + |
4 | 183 | ## License |
5 | 184 |
|
6 | | -This project is licensed under [LICENSE](LICENSE). The Flecs library is licensed under the MIT license. |
| 185 | +Flecs-Java is licensed under the [MIT License](LICENSE). |
| 186 | + |
| 187 | +Flecs (the underlying C library) is also licensed under the MIT License. See the [Flecs repository](https://github.com/SanderMertens/flecs) for details. |
0 commit comments