#Any way to make c struct initialization less verbose?

1 messages · Page 1 of 1 (latest)

feral trench
#

Hi, I'm working with raylib, without the zig bindings and I've come across a pain point. Essentially I want to be able to use array initialization syntax for c structs but the compiler complains about it not being supported. i.e this:

const rl = @cImport(@cInclude("raylib.h"));

// NOT OK
var camera = rl.Camera{
  .position = .{4.0, 2.0, 0.0},
  .target = .{0.0, 2.0, 0.0},
  .up = .{0.0, 0.0, 0.0},
  .fovy = 60.0,
  .projection = rl.CAMERA_PERSPECTIVE,
};

// VERSUS WHAT IS OK
var camera = rl.Camera3D{
   .position = .{.x = 4.0, .y = 2.0, .z = 0.0},
   .target = .{.x = 0.0, .y = 2.0, .z = 0.0},
   .up = .{.x = 0.0, .y = 1.0, .z = 0.0},
   .fovy = 60.0,
   .projection = rl.CAMERA_PERSPECTIVE,
};

Any way to circumvent this, or make it so that I don't have to specify each struct member individually? Its not a huge issue, but I'd rather not have to type .x, .y, or .z every time I need to initialize a vector for raylib.

supple galleon
#

if the struct fields are named, you'll have to initialise them with their names.

how about making a utility function?

fn vec3(x: f64, y: f64, z: f64) Vec3 {
    return .{ .x = x, .y = y, .z = z };
}

(btw I don't know the types of these, so I used made-up names)