Not-Nik / raylib-zig

Manually tweaked, auto-generated raylib bindings for zig. https://github.com/raysan5/raylib
MIT License
459 stars 101 forks source link

[Feature Request] Use packed structs for config enums #93

Closed alexmozaidze closed 1 month ago

alexmozaidze commented 1 month ago

Packed structs with only booleans as fields can be interfaced like a struct, and then easily converted to an integer. Such approach is better than plain enum, since one does not have to convert enum to an int and then back every time to change the config.

Example:

const std = @import("std");

const ConfigFlags = packed struct {
    fullscreen_mode: bool = false,
    window_resizable: bool = false,
    window_undecorated: bool = false,
    window_transparent: bool = false,
    msaa_4x_hint: bool = false,
    vsync_hint: bool = false,
    window_hidden: bool = false,
    window_always_run: bool = false,
    window_minimized: bool = false,
    window_maximized: bool = false,
    window_unfocused: bool = false,
    window_topmost: bool = false,
    window_highdpi: bool = false,
    window_mouse_passthrough: bool = false,
    borderless_windowed_mode: bool = false,
    interlaced_hint: bool = false,

    const BackingInteger = @typeInfo(@This()).Struct.backing_integer.?;

    pub fn asCInt(self: ConfigFlags) c_int {
        const bitcasted_self: BackingInteger = @bitCast(self);

        return @intCast(bitcasted_self);
    }
};

pub fn main() void {
    const opts = ConfigFlags {
        .fullscreen_mode = true,
        .vsync_hint = true,
        .window_undecorated = true,
    };

    std.debug.print("opts: {}\n\nopts as c_int: {b}\n", .{opts, opts.asCInt()});
}
Not-Nik commented 1 month ago

Good idea, but I'd like to but the casting into the proxy functions we have anyways, so we'll have to integrate it into generate_functions.py