by flohofwoe · 2w ago · 0 comments
I've been using this style for almost 10 years now in the sokol headers, and it still feels like the right thing to do and actually more ergonomic than most other languages for initializing complex structs, e.g. see here for creating a 3D render pipeline object with dozens of creation options, but most of them keeping their default values:
state.pip = sg_make_pipeline(&(sg_pipeline_desc){
.layout = {
.attrs = {
[ATTR_texcube_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_texcube_color0].format = SG_VERTEXFORMAT_UBYTE4N,
[ATTR_texcube_texcoord0].format = SG_VERTEXFORMAT_SHORT2N
}
},
.shader = shd,
.index_type = SG_INDEXTYPE_UINT16,
.cull_mode = SG_CULLMODE_BACK,
.depth = {
.compare = SG_COMPAREFUNC_LESS_EQUAL,
.write_enabled = true
},
.label = "texcube-pipeline"
});
(or see here: https://github.com/floooh/sokol-samples/blob/11bbe1a6865e93c...)...this code snippet also makes use of C99 features that are not available in C++20 though (or most other languages), and it's really remarkable how well designed the C99 designated init feature as a whole is (even though it was an afterthought) and how badly its individual subfeatures were understood by most later languages (basically, only Odin got it "mostly right": https://github.com/floooh/sokol-odin/blob/7d47a31cad83a26ba1...)
I had never seen array designated initializer combined with the struct variant. Great foresight of whomever thought to make these features so flexible and composable:
.attrs = {
[ATTR_texcube_pos].format = SG_VERTEXFORMAT_FLOAT3,
[ATTR_texcube_color0].format = SG_VERTEXFORMAT_UBYTE4N,
[ATTR_texcube_texcoord0].format = SG_VERTEXFORMAT_SHORT2N
}