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

Make GetKey(win, KEY_ESCAPE) == PRESS work #225

Merged
merged 2 commits into from
Mar 15, 2023

Conversation

cmcaine
Copy link
Contributor

@cmcaine cmcaine commented Feb 10, 2023

GetKey is defined to return a Bool

GetKey(window::Window, key) = Bool(ccall((:glfwGetKey, libglfw), Cint, (Window, Cint), window, key))

, but in the documentation it is defined as returning PRESS or RELEASE. In C that doesn't matter because true, false PRESS and RELEASE are all integers and RELEASE is equal to false. But in Julia these are different types and so e.g. GLFW.GetKey(window, GLFW.KEY_ESCAPE)) == GLFW.PRESS will always return false.

This breaks the example in the Readme of GLAbstraction.jl:

    if GLFW.GetKey(window, GLFW.KEY_ESCAPE) == GLFW.PRESS
        GLFW.SetWindowShouldClose(window, true)
    end

The method added in this PR tells Julia how to compare an Action and a Bool so that code calling GetKey as documented will work as expected, while also not breaking existing code that expects GetKey to return a Bool (e.g. the code in #163).

Julia will do slightly more work when comparing Bool and Action than when comparing two Bools because it will expand the Bool to 32 bits first. This could be avoided by defining the basetype of the Action enum as UInt8, but that would be a breaking change if arrays of Action are ever passed to C (I don't think they would be, but idk).

I tried to persuade Julia to omit the expansion to 32 bits, but I couldn't find anything that worked. Closest was b == unsafe_trunc(Bool, a), but that still emits more instructions than comparing two Bools.

`GetKey` is defined below to return a `Bool`, but in the documentation
it is defined as returning `PRESS` or `RELEASE`. In C that doesn't
matter, but in Julia these are different types and so e.g.
`GLFW.GetKey(window, GLFW.KEY_ESCAPE)) == GLFW.PRESS` will always return
false.

This method tells Julia how to compare an `Action` and a `Bool` so that
code calling `GetKey` as documented will work as expected.

Julia will do slightly more work when comparing `Bool` and `Action` than
when comparing two `Bool`s because it will expand the Bool to 32 bits
first. This could be avoided by defining the basetype of the `Action`
enum as `UInt8`, but that would be a breaking change if arrays of
`Action` are ever passed to C (I don't think they would be, but idk).

I tried to persuade Julia to omit the expansion to 32 bits, but I
couldn't find anything that worked. Closest was
`b == unsafe_trunc(Bool, a)`, but that still emits more instructions
than comparing two `Bool`s.
cmcaine added a commit to cmcaine/GLFW.jl that referenced this pull request Feb 10, 2023
@cmcaine
Copy link
Contributor Author

cmcaine commented Mar 15, 2023

Hi, this is a gentle reminder that this small PR exists. I appreciate that you're probably busy and I'll just bump this PR every once in a while to increase your chances of seeing it.

@SimonDanisch
Copy link
Member

Thank you!

@SimonDanisch SimonDanisch merged commit 6bdd564 into JuliaGL:master Mar 15, 2023
cmcaine added a commit to cmcaine/GLFW.jl that referenced this pull request Mar 15, 2023
I messed up somehow and committed a typo (I left out a colon).

This commit also ensures that ==(Action, Bool) gives the same result as
==(Bool, Action), because that just makes sense.

Sorry, Simon!
SimonDanisch added a commit that referenced this pull request Mar 16, 2023
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

Successfully merging this pull request may close these issues.

2 participants