Skip to content

Commit

Permalink
dunai-examples: Upgrade example to use SDL2. Refs ivanperez-keera#446
Browse files Browse the repository at this point in the history
SDL 1 is deprecated, and no longer maintained. To keep the example
working with more modern versions of packages in the Haskell ecosystem,
this commit ports bouncingball to use SDL2.
  • Loading branch information
mchav committed Nov 4, 2024
1 parent a7ed00e commit 0aaa548
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 19 deletions.
49 changes: 34 additions & 15 deletions dunai-examples/bouncingball/BouncingBall.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{-# LANGUAGE Arrows #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE Arrows #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
-- |
-- Copyright : (c) Ivan Perez and Manuel Baerenz, 2016
-- License : BSD3
Expand All @@ -15,20 +16,29 @@ import FRP.Yampa as Yampa
#endif

import Control.Concurrent
import Graphics.UI.SDL as SDL
import Graphics.UI.SDL.Primitives as SDL
import Foreign.C.Types
import qualified SDL
import qualified SDL.Primitive as SDL

screenWidth, screenHeight :: CInt
(screenWidth, screenHeight) = (800, 600)

main :: IO ()
main = do
SDL.init [InitEverything]
SDL.setVideoMode 800 600 32 [SWSurface]
SDL.initialize [SDL.InitVideo]
window <- SDL.createWindow
"Bouncing ball"
SDL.defaultWindow { SDL.windowInitialSize = SDL.V2 screenWidth screenHeight }
SDL.showWindow window
renderer <- SDL.createRenderer window (-1) SDL.defaultRenderer

reactimate (return ())
(\_ -> return (0.01, Just ()))
(\_ e -> render e >> return False)
(\_ -> return (0.005, Just ()))
(\_ e -> render e renderer)
sf

sf = bouncingBall (100.0 :: Float) 0.0


bouncingBall p0 v0 =
switch (proc (_) -> do
(p,v) <- fallingBall p0 v0 -< ()
Expand All @@ -55,18 +65,27 @@ fallingBall p0 v0 = proc () -> do
-- whenS :: (a -> Bool) -> SF a (Yampa.Event a)
-- whenS p = (((arr p >>> edge) &&& arr id) >>> (arr (uncurry tag)))

render (p,_) = do
screen <- SDL.getVideoSurface
render :: RealFrac a => (a, b) -> SDL.Renderer -> IO Bool
render (p,_) renderer = do
events <- SDL.pollEvents
let quit = elem SDL.QuitEvent $ map SDL.eventPayload events

white <- SDL.mapRGB (SDL.surfaceGetPixelFormat screen) 0xFF 0xFF 0xFF
SDL.fillRect screen Nothing white
-- Set background.
let white = SDL.V4 maxBound maxBound maxBound maxBound
SDL.rendererDrawColor renderer SDL.$= white
SDL.clear renderer

SDL.filledCircle screen 100 (600 - 100 - round p) 30 (Pixel 0xFF0000FF)
let red = SDL.V4 maxBound 0 0 maxBound
SDL.rendererDrawColor renderer SDL.$= red

SDL.flip screen
SDL.fillCircle renderer (SDL.V2 100 (600 - 100 - round p)) 30 red

SDL.present renderer

threadDelay 1000

return quit

-- * Auxiliary functions

-- | Integrate a linearly changing input.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ executable dunai-examples-bearriver-bouncingball
ghc-options: -Wall -fno-warn-unused-do-bind
cpp-options: -DBEARRIVER
build-depends: base >=4.6 && < 5,
SDL,
SDL-gfx,
sdl2,
sdl2-gfx,
bearriver

-- transformers, mtl,
Expand All @@ -29,8 +29,8 @@ executable dunai-examples-bearriver-bouncingball-yampa
default-language: Haskell2010
ghc-options: -Wall -fno-warn-unused-do-bind
build-depends: base >=4.6 && < 5,
SDL,
SDL-gfx,
sdl2,
sdl2-gfx,
Yampa

source-repository head
Expand Down

0 comments on commit 0aaa548

Please sign in to comment.