Giter VIP home page Giter VIP logo

2jz's Introduction

2JZ

Hey!

Thanks for coming through and taking a look at this repo. As you might be able to tell, it's a good bit out of date and I don't really have any plans to maintain or update it at any point, so this is likely the last note I'll leave here. For those wanting to potentially make use of this in the future for whatever reason (likely me, wanting to demo to someone) the 0.7.0 release from https://ziglang.org/download/ seems to work for the Pong demo.

An archetype-based entity-component-system(ECS) library in Zig, inspired by hecs and the many other delightful ECS libraries of the world. I'd highly recommend reading the README there for more background on ECS in general.

Example

const allocator = std.testing.allocator;
const expect = std.testing.expect;

const Position = struct { x: u32, y: u32 };
const HP = struct { points: u8, alive: bool };

// Create a world from your component types
var world = try World.init(allocator, .{ Position, HP });
defer world.deinit();

// Create entries with any combination of types
var entity = try world.spawn(.{Position{ .x = 5, .y = 7 }});
var entity2 = try world.spawn(.{ Position{ .x = 1, .y = 2 }, HP{ .points = 100, .alive = true } });

// Query for all entries containing a Position
var query = try world.query(.{Position});

while (query.next()) {
    var position = query.dataMut(Position);
    position.x *= 2;

    const ent = query.entity();

    // Prints both entities' Position information
    if (world.remove(ent)) {
        std.debug.print("removed entity: {}, with position: {}\n", .{ ent, position });
    }
}

ECS in a Breath

ECS architecture is designed to make it easy to maintain loosely-coupled state and behavior for complex systems, while ideally not sacrificing in performance or developer ergonomics. Basically, entities are things, components are descriptions of those things, and systems are ways to use and modify those descriptions.

What's this archetype bit about?

The naive approach to this architecture might have you arrange your components in separate arrays of some kind, as seen in this easy to follow jumping off point. However, this approach effectively kills your chances of pulling in all the information you need for a given system's operation in a minimal number of cache lines. The archetype helps to group your heterogeneous data together as densely as possible, allowing for efficient memory access and minimal need for the cross-referencing data structures you get otherwise.

Examples

Check out the examples directory to get a feel for what usage might look like in practice. At the time of writing, this includes a Pong game written with SDL, and is a great example of Zig's fantastic C integration story.

Acknowledgements

I'd love to be able to take a moment to give a huge thank you to the Zig community for being such an awesome bunch of helpful hackers and tinkerers. Tuning into the many showcases and demos from that scene is what convinced me to keep pushing with this project, and the Discord is an amazing place to pick up on all sorts of systems programming tidbits and adventures.

Of course, I also owe a good amount to the other open source ECS libraries of the world, most notably hecs above, for providing helpful design pillars for me to see, ignore, and then realize later was in fact a much better way of doing things.

2jz's People

Stargazers

Denis274 avatar caleb dehaan avatar  avatar  avatar Raul Leal avatar Louis Pearson avatar Everett Wilson avatar Greg Engle avatar Ethan Jackwitz avatar  avatar Guillaume D. avatar Mark Malinowski avatar Boo avatar Martin Wickham avatar Loris Cro avatar

Watchers

Everett Wilson avatar

Forkers

gengle

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.