module RPGEngine.Render.Core ( Renderer , getRender , setRenderPos , overlay ) where import RPGEngine.Config import Data.Maybe import Graphics.Gloss import GHC.IO import Graphics.Gloss.Juicy ----------------------------- Constants ------------------------------ type Renderer a = a -> Picture unknownImage :: FilePath unknownImage = "unknown.png" allEntities :: [(String, FilePath)] allEntities = [ ("player", "player.png"), ("door", "door.png") ] allEnvironment :: [(String, FilePath)] allEnvironment = [ ("void", "void.png"), ("overlay", "overlay.png"), ("tile", "tile.png"), ("wall", "wall.png"), ("entrance", "entrance.png"), ("exit", "exit.png") ] allItems :: [(String, FilePath)] allItems = [ ("dagger", "dagger.png"), ("key", "key.png" ) ] allGui :: [(String, FilePath)] allGui = [ ("main", "main.png"), ("health", "health.png") ] -- Map of all renders library :: [(String, Picture)] library = unknown:entities ++ environment ++ gui ++ items where unknown = ("unknown", renderPNG (assetsFolder ++ unknownImage)) entities = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "entities/" ++ s))) allEntities environment = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "environment/" ++ s))) allEnvironment gui = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "gui/" ++ s))) allGui items = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "items/" ++ s))) allItems ------------------------------ Exported ------------------------------ -- Retrieve an image from the library. If the library does not contain -- the requested image, a default is returned. getRender :: String -> Picture getRender id = get filtered where filtered = filter ((== id) . fst) library get [] = snd $ head library get ((_, res):_) = res -- Move a picture by game coordinates setRenderPos :: Int -> Int -> Picture -> Picture setRenderPos x y = translate floatX floatY where floatX = fromIntegral x * zoom * resolution floatY = fromIntegral y * zoom * resolution overlay :: Picture overlay = setRenderPos offX offY $ pictures voids where voids = [setRenderPos x y void | x <- [0 .. width], y <- [0 .. height]] void = getRender "overlay" intZoom = round zoom :: Int height = round $ 4320 / resolution / zoom width = round $ 7680 / resolution / zoom offX = negate (width `div` 2) offY = negate (height `div` 2) ---------------------------------------------------------------------- -- Turn a path to a .png file into a Picture. renderPNG :: FilePath -> Picture renderPNG path = scale zoom zoom $ fromJust $ unsafePerformIO $ loadJuicyPNG path