0 | {- Tian Z (ecburx@burx.vip) -}
 1 |
 2 | ||| Animate a picture in a window.
 3 | module IdrisGL.Animate
 4 |
 5 | import IdrisGL.Picture
 6 | import IdrisGL.DataType
 7 | import IdrisGL.Color
 8 | import IdrisGL.SDL.SDL_event
 9 | import IdrisGL.SDL.SDL_video
10 | import IdrisGL.SDL.SDL_render
11 | import IdrisGL.SDL.SDL_surface
12 | import IdrisGL.SDL.SDL_timer
13 |
14 | ||| Open a new window and display the given animation.
15 | |||
16 | ||| @ window  Display mode.
17 | ||| @ bgColor Background color.
18 | ||| @ tpf     Time per frame. Providing frames per seconds control. (FPS) = 1/<value> (0: unlimited FPS)
19 | ||| @ picF    Function to produce the next frame of animation. It is passed the time in seconds since the program started.
20 | export
21 | animate : (window  : Display)
22 |        -> (bgColor : Color)
23 |        -> (tpf     : Double)
24 |        -> (picF    : (Double -> Picture))
25 |        -> IO ()
26 | animate window bgColor tps picF = do
27 |     win                      <- createWin window
28 |     ren                      <- createRenderer win
29 |     e                        <- newEve
30 |     loop                        ren win e 0
31 |     closeWin                    win
32 |     freeEve                     e
33 |     freeRender                  ren
34 |     where 
35 |       mutual
36 |         loop : Renderer -> Win -> Event -> Double -> IO ()
37 |         loop ren win e lastTime =
38 |             if   !getSecondsTicks - lastTime < tps
39 |             then loop'          ren win e lastTime
40 |             else do
41 |             setRenderDrawColor  ren bgColor
42 |             renderClear         ren
43 |             loadPicture         (picF !getSecondsTicks) ren win
44 |             renderPresent       ren
45 |             loop'               ren win e !getSecondsTicks
46 |
47 |         loop' : Renderer -> Win -> Event -> Double -> IO ()
48 |         loop'     ren win e lastTime with (eveType e)
49 |             loop' _   _   _ _        | E_QUIT = pure ()
50 |             loop' ren win e lastTime | _      = loop ren win e lastTime
51 |