Follow

I'm trying to implement a little ECS in C with raylib, but I keep running into problems with how to organize everything. C doesn't have classes or the ability to link functions and stuctures, so I'm not sure how to do basic things like set up an entity manager. If there are some C patterns that I just don't know about and should be using, or if anyone has suggestions on places to look for help, it would be very much appreciated!

· · Web · 1 · 0 · 0

@flbr yeah the basic pattern is that the first parameter of all your functions is a pointer to a structure named *ctx whose type is whatever “class” the function operates on. one “class” per file.

@flbr so you’d define the struct at the top of each file and then functions that act upon it all taking StructName *ctx as first parameter

@flbr you’d also of course have create and destroy functions for allocating, initialising and freeing ctx objects and returning pointers

@flbr adopt a naming pattern like StructName_method for all the functions

@flbr this is of course not the only possible way to do it, but it’s what i’ve seen done with a bunch of the libs i’ve used. spidermonkey has javascript contexts. cairo has canvas contexts, opengl has gl contsxts. it’s not explicitly OOP but it has the essential features.

@zens ah, thank you, that's way better than what I was trying!

@flbr one neat little consequence of this pattern is that “static”variables in c end up acting a lot like static properties in c++; variables scoped to the “class”

Sign in to participate in the conversation
Merveilles

Revel in the marvels of the universe. We are a collective of forward-thinking individuals who strive to better ourselves and our surroundings through constant creation. We express ourselves through music, art, games, and writing. We also put great value in play. A warm welcome to any like-minded people who feel these ideals resonate with them.