ebitengine / purego

Apache License 2.0
1.95k stars 63 forks source link

Float return Values dont work #176

Closed JupiterRider closed 7 months ago

JupiterRider commented 7 months ago

This one always returns zero:

var getFrameTime func() float32
purego.RegisterLibFunc(&getFrameTime, raylibDll, "GetFrameTime")
delta := getFrameTime()
// delta is 0

When using the syscall package like this, it works:

getFrameTime, _ := syscall.GetProcAddress(handle, "GetFrameTime")
_, ret, _ := syscall.SyscallN(getFrameTime)
delta := *(*float32)(unsafe.Pointer(&ret))
// delta is 0.0166667
TotallyGamerJet commented 7 months ago

Yeah I noticed & fixed this when working on struct support. The fix is probably worth merging now instead of waiting for my struct PR to be complete.

JupiterRider commented 7 months ago

Thank you very much! :)

TotallyGamerJet commented 7 months ago

Also, fyi there is a safer way to convert the bit representation to a float using math.Float32frombits and it's 64bit equivalent.

JupiterRider commented 7 months ago

Also, fyi there is a safer way to convert the bit representation to a float using math.Float32frombits and it's 64bit equivalent.

Ah ok. So you mean, when converting from uintptr to float?

TotallyGamerJet commented 7 months ago

Ah ok. So you mean, when converting from uintptr to float?

Yes. In your second example instead of using unsafe you could've just done:

math.Float32frombits(ret)
hajimehoshi commented 7 months ago

The test fails on 0.5. I'll revert the cherry-pick on 0.5 once.

https://github.com/ebitengine/purego/actions/runs/6915374716/job/18814228166

TotallyGamerJet commented 7 months ago

It seems to be because 0.5 doesn't have #63 which added callbacks on Linux which the float tests need.

You can tell because the other callback tests are skipped.

hajimehoshi commented 7 months ago

You can tell because the other callback tests are skipped.

Sure!

hajimehoshi commented 7 months ago

Done: 930b6648e1294aae39e60c46b48e971ca4e4bf1a