Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
alexmozaidze opened this issue May 17, 2024 · 1 comment
Closed

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

alexmozaidze opened this issue May 17, 2024 · 1 comment

Comments

@alexmozaidze
Copy link

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()});
}
@alexmozaidze alexmozaidze changed the title [Feature Request] Use packed structs instead of enums for options [Feature Request] Use packed structs for config enums May 17, 2024
@Not-Nik
Copy link
Owner

Not-Nik commented May 20, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants