Why does my program exit immediately after playing a glossy animation?

I am learning how to use the Gloss library to create animations in Haskell.

Consider the code below that animates a circle that contracts and expands its radius over time.

import Graphics.Gloss
import Graphics.Gloss.Interface.Pure.Game

type Radius = Float
type RealTime   = Float

data World = World Radius RealTime

main :: IO ()
main
 = do   let initWorld = World 100.0 0
        let windowSize = (800,800)
        let windowPosition = (200,200)
        putStrLn "Before play"
        play (InWindow "Wobbling Circle" windowSize windowPosition)
              white 40 initWorld renderWorld handleEvent stepWorld
        putStrLn "After play"

renderWorld :: World -> Picture
renderWorld (World rad rtime ) = Pictures [ Circle rad ]

handleEvent :: Event -> World -> World
handleEvent _ = id

stepWorld ::  Float -> World -> World -- wobbling circle
stepWorld  _ (World _ rtime)  = World (100 * sin rtime) (rtime+0.1) 

I compiled this code using ghc --make -O2 -threaded Main.hson a Ubuntu 14.04 machine.

When I run the code, the "Before the game" instruction is displayed, and the animation starts as expected. However, when I close the animation window, the code immediately exits without printing the "After the game" instruction. Why is this?

+4
source share
1 answer

, GLUT ( ). gloss ( , ):

instance Backend GLUTState where
        initBackendState           = glutStateInit
        initializeBackend          = initializeGLUT

        -- non-freeglut doesn't like this: (\_ -> GLUT.leaveMainLoop)
        exitBackend                = (\_ -> System.exitWith System.ExitSuccess)

....

, gloss exitBackend, . GLUT System.exitWith, , , . leaveMainLoop, , , glut, freeglut, (? . ).

freeglut gloss, exitBackend; GLFW, .

+2

Source: https://habr.com/ru/post/1655268/


All Articles