简体   繁体   中英

Haskell IdleCallback too slow

I just started designing some graphics in haskell. I want to create an animated picture with a rotating sphere, so I created an IdleCallback function to constantly update the angle value:

idle :: IORef GLfloat -> IdleCallback
idle angle = do 
  a <- get angle
  angle $= a+1
  postRedisplay Nothing

I'm adding 1 each time to the angle because I want to make my sphere smoothly rotate, rather than just jump from here to there. The problem is that now it rotates TOO slow. Is there a way to keep the rotation smooth and make it faster??

Thanks a lot!

There's not a lot to go on here. I don't see an explicit delay anywhere, so I'm guessing it's slow just because of how long it takes to update?

It also doesn't look explicitly recursive, so it seems like the problem is outside the scope of this snippet.

Also I don't know which libraries you may be using.

In general, though, that IORef makes me feel unhappy. While it may be common in other languages to have global variables, IORef s in Haskell have their place, but are often a bad sign.

Even in another language, I don't think I'd do this with a global variable.

If you want to do time-updating things in Haskell, one "common" approach is to use a Functional Reactive Programming library.

They are built to have chains of functions that trigger off of a signal coming from outside, modifying the state of something, which eventually renders an output.

I've used them in the past for (simple) games, and in your case you could construct a system that is fed a clock signal 24 times per second, or whatever, and uses that to update the counter and yield a new image to blit.

My answer is kind of vague, but the question is a little vague too, so hopefully I've at least given you something to look into.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM