I'm using reactive-banana and sdl2 (using this glue library) for a game-like project. A Behavior is created for the "absolute mouse location", as well as a Behavior for the "relative mouse location" (a.k.a. the mouse movement). When not using FRP this works well, but with FRP the "relative mouse location" becomes a problem: it seems only a small amount of the data comes through. I suspect this happens because the underlying "SDL events" (that we represent with a Behavior) do not line up nicely with the tick Events.
So I want to calculate my own mouse movement, by simply comparing the mouse location at the current tick with the location at the previous tick. I'm not sure if this will solve my problems, but I have good hope :)
First of all I'm lost on how to approach it: the State monad, or an IORef, or does reactive-banana provide another means?
I'll give a small excerpt of the code I currently have:
makeNetwork :: GraphicsData -> SDLEventSource -> MomentIO ()
makeNetwork gd sdlEventSource = mdo
tickE <- tickEvent sdlEventSource
mouseMovementB <- fromPoll SDL.getRelativeMouseLocation
mousePositionB <- fromPoll SDL.getAbsoluteMouseLocation
let mousePositionE = mousePositionB <@ tickE
mouseMovementE = mouseMovementB <@ tickE -- this yields flaky data
-- ... the rest of the network description left out ...
As explained above I'd like to express mouseMovementE in terms of the mousePositionB at current tickE (known as mousePositionE) and the mousePositionE value at the previous tickE.
Any help is greatly appreciated!
You are looking for
accumEwhich builds events from streams of events. I highly recommend reading the recursion section of the documentation which describes how it's implemented in terms ofstepperandapply.To compute the difference between two points with
accumEwe'll need to keep track of the previous point. We'll also keep track of the current point. This will keep a little two-item history of the most recent events.To get the difference we'll subtract the previous one from the current one. This will give a complete network like
zeroand^-^come fromLinear.Vector