handle closing windows

This commit is contained in:
nek0 2019-01-31 16:09:01 +01:00
parent 756d63a2d6
commit 9f6c292406
2 changed files with 16 additions and 7 deletions

View file

@ -4,15 +4,24 @@ import qualified SDL
import Linear
import Control.Concurrent.MVar
-- internal impomrts
import Util
eventHandler :: SDL.Event -> IO ()
eventHandler (SDL.Event _ payload) =
handlePayload payload
eventHandler :: MVar Bool -> SDL.Event -> IO ()
eventHandler run (SDL.Event _ payload) =
handlePayload run payload
handlePayload :: SDL.EventPayload -> IO ()
handlePayload (SDL.WindowResizedEvent (SDL.WindowResizedEventData _ dim)) =
handlePayload :: MVar Bool -> SDL.EventPayload -> IO ()
handlePayload _ (SDL.WindowResizedEvent (SDL.WindowResizedEventData _ dim)) =
fitViewport (800 / 600) dim
handlePayload _ = return ()
handlePayload run (SDL.WindowClosedEvent _) = do
_ <- swapMVar run False
return ()
-- catch all other events
handlePayload _ _ = return ()

View file

@ -101,7 +101,7 @@ main = do
mapM_ (\(ident, context) -> do
let win = fromJust (lookup ident wins)
SDL.glMakeCurrent win context
mapM_ eventHandler =<< SDL.pollEvents
mapM_ (eventHandler run) =<< SDL.pollEvents
GL.clear [GL.ColorBuffer, GL.DepthBuffer, GL.StencilBuffer]
GL.flush
SDL.glSwapWindow win