-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Manuel Bärenz
committed
Jun 7, 2024
1 parent
b0ce320
commit 5197f79
Showing
32 changed files
with
2,126 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
< constMCl (paintAllIO _) -- paintAllIO clears the drawing canvas and draws the given image | ||
> constMCl (paintAllIO (circleSolid 10)) -- paintAllIO clears the drawing canvas and draws the given image |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
< rhine = sinceInitS >-> arrMCl (\t -> translate 0 (10 * t) $ paintAllIO $ circleSolid 10) @@ GlossSimClockIO | ||
> rhine = sinceInitS >-> arrMCl (\t -> paintAllIO $ translate 0 (10 * t) $ circleSolid 10) @@ GlossSimClockIO |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
> -- base | ||
> import GHC.Float (double2Float) | ||
> | ||
< movingCircle = sinceInitS >-> arr (\t -> translate 0 (10 * t) $ circleSolid 10) -- realToFrac works as well! | ||
> movingCircle = sinceInitS >-> arr (\t -> translate 0 (10 * double2Float t) $ circleSolid 10) -- realToFrac works as well! | ||
< _ (Millisecond 500) | ||
> GlossConcTClock IO (Millisecond 500) | ||
< gameClock = _ waitClock | ||
> gameClock = glossConcTClock waitClock | ||
< _ _ GlossSimClockIO | ||
> GlossClockUTC IO GlossSimClockIO | ||
< visualizationClock = _ GlossSimClockIO | ||
> visualizationClock = glossClockUTC GlossSimClockIO | ||
< rhine = movingCircle @@ gameClock >-- _ blank --> visualize @@ visualizationClock | ||
> rhine = movingCircle @@ gameClock >-- keepLast blank --> visualize @@ visualizationClock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
< _ -> _ | ||
> (EventKey (SpecialKey KeyRight) Down _ _) -> Just TurnRight | ||
> (EventKey (SpecialKey KeyLeft) Down _ _) -> Just TurnLeft | ||
> _ -> Nothing | ||
< let newDirection = _ | ||
< newPosition = _ | ||
< in Result _ _ | ||
> let newDirection = maybe direction (`changeDirection` direction) turnMaybe | ||
> newPosition = stepPosition newDirection position | ||
> in Result (newPosition, newDirection) newPosition |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
< arr (Just <<< Apple) <<< _ -< (Position (-boardSize) (-boardSize), Position boardSize boardSize) | ||
> arr (Just <<< Apple) <<< getRandomRS -< (Position (-boardSize) (-boardSize), Position boardSize boardSize) | ||
< addedApple <- _ -< () | ||
> addedApple <- evalRandIOS' newApple -< () | ||
< game = _ | ||
> game = feedback DontEat $ proc (turn, eat) -> do | ||
> snake <- snakeSF -< (turn, eat) | ||
> (apples, eatNext) <- applesSF -< head $ body snake | ||
> returnA -< ((snake, apples), eatNext) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
< _ | ||
> try $ liftClSF snakeAndApples >>> throwOnCond (fst >>> illegal) () >>> arr Just | ||
> safe $ pure Nothing |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
module TestGloss where | ||
|
||
-- base | ||
import Control.Concurrent | ||
import Control.Monad | ||
import Data.IORef | ||
import System.Exit | ||
|
||
-- rhine-gloss | ||
import FRP.Rhine.Gloss | ||
|
||
expectPic :: Picture -> [Picture] -> IO () | ||
expectPic received expected = expectPics [received] [expected] | ||
|
||
expectPics :: [Picture] -> [[Picture]] -> IO () | ||
expectPics receiveds expecteds = do | ||
forM_ (zip receiveds expecteds) $ \(received, expected) -> do | ||
let flattened = flattenPictures received | ||
when (flattened /= expected) $ do | ||
putStrLn $ "Expected: " ++ show expected | ||
putStrLn $ "Received: " ++ show flattened | ||
exitFailure | ||
putStrLn "Well done!" | ||
|
||
flattenPictures :: Picture -> [Picture] | ||
flattenPictures (Pictures ps) = ps >>= flattenPictures | ||
flattenPictures Blank = [] | ||
flattenPictures picture = [picture] | ||
|
||
stepGlossRhine :: (Clock GlossConc cl, Time cl ~ Time (Out cl), Time cl ~ Time (In cl), GetClockProxy cl) => Rhine GlossConc cl () () -> [Float] -> IO [Picture] | ||
stepGlossRhine rhine timestamps = stepGlossRhineWithInput rhine timestamps [] | ||
|
||
stepGlossRhineWithInput :: (Clock GlossConc cl, Time cl ~ Time (Out cl), Time cl ~ Time (In cl), GetClockProxy cl) => Rhine GlossConc cl () () -> [Float] -> [Event] -> IO [Picture] | ||
stepGlossRhineWithInput rhine timestamps events = do | ||
vars <- makeGlossEnv | ||
void $ forkIO $ forM_ events $ putMVar $ eventVar vars | ||
void $ forkIO $ runGlossConcT (flow rhine) vars | ||
forM timestamps $ \timestamp -> do | ||
putMVar (timeVar vars) timestamp | ||
threadDelay 33333 | ||
readIORef (picRef vars) | ||
|
||
specialKey :: SpecialKey -> Event | ||
specialKey key = EventKey (SpecialKey key) Down (Modifiers Down Down Down) (0, 0) | ||
|
||
keyRight :: Event | ||
keyRight = specialKey KeyRight | ||
|
||
keyLeft :: Event | ||
keyLeft = specialKey KeyLeft |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{- | Circle. | ||
Let's draw something! | ||
Rhine connects to the famous gloss library for 2d graphics. | ||
Have a look at https://hackage.haskell.org/package/gloss to learn more about it! | ||
The connection between Rhine and gloss is provided by the library https://hackage.haskell.org/package/rhine-gloss, | ||
which encapsulates the effects of drawing pictures in gloss in a monad, 'GlossConcT', | ||
and provides several clocks to interact with the gloss system. | ||
To warm up, let's just draw a circle. | ||
-} | ||
module Koan where | ||
|
||
-- rhine | ||
import FRP.Rhine | ||
|
||
-- rhine-gloss | ||
import FRP.Rhine.Gloss | ||
|
||
{- | The main 'Rhine' of this program. | ||
/--- We use effects in 'GlossConc' to draw images. | ||
| | ||
| /--- This clock ticks whenever an image is drawn on the screen by the gloss backend. | ||
| | | ||
v v | ||
-} | ||
rhine :: Rhine GlossConc GlossSimClockIO () () | ||
-- Can you create a solid circle of radius 10 here? | ||
-- Have a look at https://hackage.haskell.org/package/gloss/docs/Graphics-Gloss-Data-Picture.html for inspiration. | ||
rhine = | ||
constMCl (paintAllIO _) -- paintAllIO clears the drawing canvas and draws the given image | ||
@@ GlossSimClockIO -- The singleton value of GlossSimClockIO. | ||
|
||
main :: IO () | ||
-- Make sure to keep this definition here as it is: The tests depend on it. | ||
main = | ||
flowGlossIO -- This function can replace 'flow' when you're using the gloss backend. | ||
defaultSettings -- Settings for the gloss window context such as size, title, and background colour. | ||
rhine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{- | Circle. | ||
Let's draw something! | ||
Rhine connects to the famous gloss library for 2d graphics. | ||
Have a look at https://hackage.haskell.org/package/gloss to learn more about it! | ||
The connection between Rhine and gloss is provided by the library https://hackage.haskell.org/package/rhine-gloss, | ||
which encapsulates the effects of drawing pictures in gloss in a monad, 'GlossConcT', | ||
and provides several clocks to interact with the gloss system. | ||
To warm up, let's just draw a circle. | ||
-} | ||
module Koan where | ||
|
||
-- rhine | ||
import FRP.Rhine | ||
|
||
-- rhine-gloss | ||
import FRP.Rhine.Gloss | ||
|
||
{- | The main 'Rhine' of this program. | ||
/--- We use effects in 'GlossConc' to draw images. | ||
| | ||
| /--- This clock ticks whenever an image is drawn on the screen by the gloss backend. | ||
| | | ||
v v | ||
-} | ||
rhine :: Rhine GlossConc GlossSimClockIO () () | ||
-- Can you create a solid circle of radius 10 here? | ||
-- Have a look at https://hackage.haskell.org/package/gloss/docs/Graphics-Gloss-Data-Picture.html for inspiration. | ||
rhine = | ||
constMCl (paintAllIO (circleSolid 10)) -- paintAllIO clears the drawing canvas and draws the given image | ||
@@ GlossSimClockIO -- The singleton value of GlossSimClockIO. | ||
|
||
main :: IO () | ||
-- Make sure to keep this definition here as it is: The tests depend on it. | ||
main = | ||
flowGlossIO -- This function can replace 'flow' when you're using the gloss backend. | ||
defaultSettings -- Settings for the gloss window context such as size, title, and background colour. | ||
rhine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Main where | ||
|
||
-- rhine-gloss | ||
import FRP.Rhine.Gloss | ||
|
||
-- test-gloss | ||
import TestGloss | ||
|
||
-- koan | ||
import Koan (rhine) | ||
|
||
main :: IO () | ||
main = do | ||
[pic] <- stepGlossRhine rhine [1] | ||
expectPic pic [circleSolid 10] |
Oops, something went wrong.