summaryrefslogtreecommitdiff
path: root/app/Scratchy.hs
diff options
context:
space:
mode:
authorMikkel Thestrup <mithe24@student.sdu.dk>2025-12-16 14:10:27 +0100
committerMikkel Thestrup <mithe24@student.sdu.dk>2025-12-25 15:16:53 +0100
commitaa9fc62a29c865452f3e5437c4166854583bba97 (patch)
treef99d64c5b6e31bac583a88d5fd4429bf9c8b248d /app/Scratchy.hs
downloadscratchy-aa9fc62a29c865452f3e5437c4166854583bba97.tar.gz
scratchy-aa9fc62a29c865452f3e5437c4166854583bba97.zip
Initial commit
Diffstat (limited to 'app/Scratchy.hs')
-rw-r--r--app/Scratchy.hs76
1 files changed, 76 insertions, 0 deletions
diff --git a/app/Scratchy.hs b/app/Scratchy.hs
new file mode 100644
index 0000000..cea910d
--- /dev/null
+++ b/app/Scratchy.hs
@@ -0,0 +1,76 @@
+module Scratchy where
+
+import Graphics.Gloss.Interface.Pure.Game
+
+import Scratchy.World
+import Scratchy.Syntax
+
+import Example.CircleThatMoves
+
+-- Main
+------------------------------------------------------------
+
+main :: IO ()
+main = play
+ (InWindow "Scratchy" (winW, winH) (100, 100))
+ white
+ 120
+ (World
+ white
+ []
+ []
+ circSprite
+ []
+ []
+ []
+ []
+ [])
+ drawWorld
+ (\e -> fst . runGame (handleEvent e))
+ (\tick ->
+ fst . runGame ( runProg
+ >> moveSprites tick
+ >> handleKeyEvents
+ >> tickTimers
+ >> runProg
+ ) )
+
+
+-- Rendering
+------------------------------------------------------------
+
+drawWorld :: World -> Picture
+drawWorld World{..} =
+ Pictures
+ ( backgroundFrom bg
+ : drawGrid
+ : map drawSprite sprites )
+
+backgroundFrom :: Color -> Picture
+backgroundFrom c =
+ let w' = fromIntegral gridW * cellSize
+ h' = fromIntegral gridH * cellSize
+ in Color c (rectangleSolid w' h')
+
+drawGrid :: Picture
+drawGrid =
+ let halfW = fromIntegral gridW * cellSize / 2
+ halfH = fromIntegral gridH * cellSize / 2
+ xs = [(-halfW), (-halfW + cellSize) .. halfW]
+ ys = [(-halfH), (-halfH + cellSize) .. halfH]
+ vlines = [ Line [ (x, -halfH), (x, halfH) ] | x <- xs ]
+ hlines = [ Line [ (-halfW, y), ( halfW, y) ] | y <- ys ]
+ in Color (greyN 0.85) (Pictures (vlines ++ hlines))
+
+drawSprite :: Sprite -> Picture
+drawSprite Sprite{..} =
+ let (x, y) = toScreen pos
+ in Translate x y $ pic
+
+toScreen :: (Float, Float) -> (Float, Float)
+toScreen (gx, gy) =
+ let fx = gx - (fromIntegral gridW - 1) / 2
+ fy = gy - (fromIntegral gridH - 1) / 2
+ in (fx * cellSize, fy * cellSize)
+
+