74 lines
No EOL
2.3 KiB
Haskell
74 lines
No EOL
2.3 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)
|
|
import Graphics.Gloss.Data.Bitmap (BitmapData(..))
|
|
|
|
----------------------------- 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")
|
|
]
|
|
|
|
allEnvironment :: [(String, FilePath)]
|
|
allEnvironment = [
|
|
("void", "void.png"),
|
|
("tile", "tile.png"),
|
|
("wall", "wall.png"),
|
|
("entrance", "entrance.png"),
|
|
("exit", "exit.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 = map (\(f, s) -> (f, renderPNG (assetsFolder ++ "environment/" ++ s))) allEnvironment
|
|
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
|
|
|
|
-- 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 |