Files
loop/src/Dodge/Base/Window.hs
T
2025-12-06 00:04:00 +00:00

62 lines
1.7 KiB
Haskell

-- | Getting the window size geometry.
module Dodge.Base.Window (
halfWidth,
halfHeight,
screenPolygon,
screenPolygonBord,
screenBox,
pointIsOnScreen,
) where
import Linear
import Control.Lens
import Dodge.Data.Camera
import Dodge.Data.Config
import Geometry
-- | A box covering the screen in world coordinates
screenPolygon :: Config -> Camera -> [Point2]
screenPolygon cfig w = map (scTran . scRot . scZoom) $ screenBox cfig
where
scRot = rotateV (w ^. camRot)
scZoom p
| (w ^. camZoom) /= 0 = (1 / (w ^. camZoom)) *^ p
| otherwise = p
scTran p = p + (w ^. camCenter)
-- | A box covering the screen in world coordinates, with a x and y border
screenPolygonBord ::
-- | X border
Float ->
-- | Y border
Float ->
Config ->
Camera ->
[Point2]
screenPolygonBord xbord ybord cfig w = [tr, tl, bl, br]
where
hw = halfWidth cfig - xbord
hh = halfHeight cfig - ybord
scZoom p
| (w ^. camZoom) /= 0 = (1 / (w ^. camZoom)) *^ p
| otherwise = p
theTransform = (+ (w ^. camCenter)) . rotateV (w ^. camRot) . scZoom
tr = theTransform (V2 hw hh)
tl = theTransform (V2 (- hw) hh)
br = theTransform (V2 hw (- hh))
bl = theTransform (V2 (- hw) (- hh))
halfWidth, halfHeight :: Config -> Float
halfWidth = (0.5 *) . windowXFloat
halfHeight = (0.5 *) . windowYFloat
-- | A box of the size of the screen in screen centered coordinates
screenBox :: Config -> [Point2]
screenBox w = rectNSWE hh (- hh) (- hw) hw
where
hw = halfWidth w
hh = halfHeight w
pointIsOnScreen :: Config -> Camera -> Point2 -> Bool
pointIsOnScreen cfig = flip pointInPoly . screenPolygon cfig