52700.fb2 Учебник по Haskell - читать онлайн бесплатно полную версию книги . Страница 330

Учебник по Haskell - читать онлайн бесплатно полную версию книги . Страница 330

-------------------------------

-- graphics

display state = do

drawState =<< get state

simTime <- simulate =<< get state

sleep (max 0 $ frameTime - simTime)

drawState :: State -> IO ()

drawState st = do

pos <- get $ ballPos st

G. clear [G.ColorBuffer]

drawWalls

drawBall pos

G. swapBuffers

drawBall :: H.Position -> IO ()

298 | Глава 20: Императивное программирование

drawBall pos = do

G. color red

circle x y $ d2gl ballRadius

where (x, y) = vec2gl pos

drawWalls :: IO ()

drawWalls = do

G. color black

line (-dow2) (-doh2) (-dow2) doh2

line (-dow2) doh2

dow2

doh2

line dow2

doh2

dow2

(-doh2)

line dow2

(-doh2)

(-dow2) (-doh2)

where dow2 = d2gl ow2

doh2 = d2gl oh2

onMouse state = do

mb <- G. getMouseButton ButtonLeft

when (mb == Press) (get G. mousePos >>= updateVel state)

updateVel state pos = do

size <- get G. windowSize

st <- get state

p0 <- get $ ballPos st

v0 <- get $ ballVel st

let p1 = mouse2canvas size pos

ballVel st $=

H. scale (H. normalize $ p1 - p0) (max minVel $ H. len v0)

mouse2canvas :: G.Size -> G.Position -> H.Vector

mouse2canvas (G.Size sx sy) (G.Position mx my) = H.Vector x y

where d a b

= fromIntegral a / fromIntegral b