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

Can't switch on Color type #176

Open
Bleb1k opened this issue Nov 28, 2024 · 1 comment
Open

Can't switch on Color type #176

Bleb1k opened this issue Nov 28, 2024 · 1 comment

Comments

@Bleb1k
Copy link

Bleb1k commented Nov 28, 2024

I'm struggling to make a working switch statement for Color type, I've tried this:

game_state.color = switch (game_state.color.toInt()) {
  rl.Color.lime.toInt() => rl.Color.purple,
  rl.Color.purple.toInt() => rl.Color.magenta,
  rl.Color.magenta.toInt() => rl.Color.black,
  rl.Color.black.toInt() => rl.Color.lime,
  else => unreachable,
};

It fails because .toInt() tries to call external C function, and can't do so at compile time.
I also tried like this:

game_state.color = switch (game_state.color) {
  rl.Color.lime => rl.Color.purple,
  rl.Color.purple => rl.Color.magenta,
  rl.Color.magenta => rl.Color.black,
  rl.Color.black => rl.Color.lime,
  else => unreachable,
};

But zig can't switch on extern types, here's error for that one:

src/game.zig:99:46: error: switch on type 'raylib.Color'
        game_state.color = switch (game_state.color) {
                                   ~~~~~~~~~~^~~~~~
/home/bleb1k/.cache/zig/p/122058d3ea6318efb819d0bffba630afd1a459fa3a99b4bfe4b680a937d5de04d2fc/lib/raylib.zig:719:26: note: struct declared here
pub const Color = extern struct {
                  ~~~~~~~^~~~~~
@Bleb1k
Copy link
Author

Bleb1k commented Nov 28, 2024

fixed by changing fn toInt to

pub fn toInt(self: Color) i32 {
  return if (@inComptime())
    (@as(i32, self.r) << 24) | (@as(i32, self.g) << 16) | (@as(i32, self.b) << 8) | @as(i32, self.a)
  else
    rl.colorToInt(self);
}

This change makes this compile:

game_state.color = switch (game_state.color.toInt()) {
  rl.Color.lime.toInt() => rl.Color.purple,
  rl.Color.purple.toInt() => rl.Color.magenta,
  rl.Color.magenta.toInt() => rl.Color.black,
  rl.Color.black.toInt() => rl.Color.lime,
  else => unreachable,
};

If this change is acceptable, may I fork this repo and make a PR?

@Bleb1k Bleb1k changed the title Cant switch on Color type Can't switch on Color type Nov 29, 2024
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

1 participant