This repository has been archived on 2023-06-24. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
2022FuncProg-project3-RPGEn.../lib/RPGEngine/Render/Core.hs

63 lines
No EOL
1.9 KiB
Haskell

module RPGEngine.Render.Core where
import Graphics.Gloss ( Picture, translate )
import GHC.IO (unsafePerformIO)
import Graphics.Gloss.Juicy (loadJuicyPNG)
import Data.Maybe (fromJust)
import Graphics.Gloss.Data.Picture (scale)
----------------------------- Constants ------------------------------
-- Default scale
zoom :: Float
zoom = 5.0
-- Resolution of the texture
resolution :: Float
resolution = 16
assetsFolder :: FilePath
assetsFolder = "assets/"
unknownImage :: FilePath
unknownImage = "unkown.png"
allEntities :: [(String, FilePath)]
allEntities = [
("player", "player.png"),
("door", "door.png")
]
allItems :: [(String, FilePath)]
allItems = [
("dagger", "dagger.png"),
("key", "key.png" )
]
-- Map of all renders
library :: [(String, Picture)]
library = unknown:entities ++ environment ++ gui ++ items
where unknown = ("unkown", renderPNG (assetsFolder ++ unknownImage))
entities = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "entities/" ++ s))) allEntities
environment = []
gui = []
items = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "items/" ++ s))) allItems
----------------------------------------------------------------------
-- Turn a path to a .png file into a Picture.
renderPNG :: FilePath -> Picture
renderPNG path = scale zoom zoom $ fromJust $ unsafePerformIO $ loadJuicyPNG path
-- 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
setRenderPos :: Int -> Int -> Picture -> Picture
setRenderPos x y = translate floatX floatY
where floatX = fromIntegral x * zoom * resolution
floatY = fromIntegral y * zoom * resolution