I’m trying to get a very quick and dirty animated display of some data produced using Haskell. The simplest thing to try seems to be ASCII art — in other words, something along the lines of:
type Frame = [[Char]] -- each frame is given as an array of characters
type Animation = [Frame]
display :: Animation -> IO ()
display = ??
How can I best do this?
The part I can’t figure out at all is how to ensure a minimal pause between frames; the rest is straightforward using putStrLn together with clearScreen from the ansi-terminal package, found via this answer.
Well, here’s a rough sketch of what I’d do:
Obviously I’m using SDL here purely for
getTicks, because it’s what I’ve used before. Feel free to replace it with any other function to get the current time.The first argument to
runFramesis–as the name suggests–the frame rate in hertz, i.e., frames per second. TherunFramesfunction first converts each frame into an action that draws it, then gives each to theaddDelayfunction, which checks the time before and after running the action, then sleeps until the frame time has passed.My own code would look a bit different than this, because I’d generally have a more complicated loop that would do other stuff, e.g., polling SDL for events, doing background processing, passing data to the next iteration, &c. But the basic idea is the same.
Obviously the nice thing about this approach is that, while still being fairly simple, you get a consistent frame rate when possible, with a clear means of specifying the target speed.