[Haskell-cafe] Data types, opengl display loop and readIORef/writeIORef

bbrown bbrown at botspiritcompany.com
Mon Oct 8 10:46:19 EDT 2007

This is more an aesthetics question, I have a simple opengl application that 
has a singleton like object I need to pass around to the display loop and 
possibly to the keyboard action handler. (pseudo code)

data SimpleMech = SimpleMech {
      mechPos         :: !MVector,
      mechRot         :: !MVector
    } deriving (Read)

mainMech = initMech (0, 0, 0) (0, 0, 0)
mainMechRef <- newIORef(mainMech)

displayCallback $= displayGameLoop mainMechRef

rotateMech :: (Double, SimpleMech) -> SimpleMech
rotateMech (angle, mech) =
  -- Calculate a new rotation
  let (xrot,yrot,zrot) = (mechRot mech)
      newyrot = newyrot + angle
  in SimpleMech { mechRot = (xrot, newyrot, zrot),
                  mechPos = (mechPos mech) }

displayGameLoop mechRef = do
mech <- get mechRef
mechRef $= (rotateMech (0.1, mech))

For some reason in that code above, the "mechRef" doesnt update the value I 
want to change.  It is like I am getting an instance of the initial valyue 
back.  Also, when I use writeIORef, eg writeIORef mechRef  (rotateMech (0.1, 

I get stack overflow exceptions.

The displayGameLoop will get called an infinite amount of times, so I think I 
have to be careful there.  But I wonder why the reference isn't getting 
updated?  Is this an approach you would take, eg passing "mechRef" around, a 
singleton game object.

Berlin Brown
[berlin dot brown at gmail dot com]

More information about the Haskell-Cafe mailing list