Skip to content

Instantly share code, notes, and snippets.

@Fusion86
Last active October 21, 2021 16:20
Show Gist options
  • Save Fusion86/22980ad6fd65d75aa71b2e325c9738e9 to your computer and use it in GitHub Desktop.
Save Fusion86/22980ad6fd65d75aa71b2e325c9738e9 to your computer and use it in GitHub Desktop.
import Graphics.Gloss
( Color,
Picture,
blank,
blue,
color,
white,
pictures,
rectangleSolid,
rgbaOfColor,
scale,
violet,
)
import Graphics.Gloss.SDL.Surface (CacheTexture (..), bitmapOfSurface, withSdlSurface)
import SDL.Font (initialize, load, Font, solid)
import SDL.Vect (V4 (..))
import System.FilePath ((</>))
import Data.Word (Word8)
-- Libraries die nodig zijn (evt meer)
-- sdl2-ttf gloss-sdl2-surface
-- Wellicht ook nog 'sdl2'
-- Bijv. zo ziet mijn package.yaml erui
-- ...
-- dependencies:
-- - base >= 4.7 && < 5
-- - directory
-- - containers
-- - filepath
-- - text
-- - bytestring
-- - pretty-simple
-- - sdl2
-- - sdl2-ttf
-- - gloss
-- - gloss-juicy
-- - gloss-sdl2-surface
-- - JuicyPixels
-- - JuicyPixels-extra
-- - xml
-- ...
-- Ook moet je dit in je stack.yaml gooien
-- extra-deps:
-- - git: https://gitlab.com/dpwiz/gloss-sdl2-surface.git
-- commit: f677f6b6c7cb0fb11b638c44613e29448f447312
-- - gloss-juicy-0.2.3@sha256:0c3bca95237cbf91f8b3b1936a0661f1e0457acd80502276d54d6c5210f88b25,1618
main :: IO ()
main = do
initialize
font <- load ("assets" </> "PressStart2P.ttf") 8
playIO _ _ 60 _ renderFunc _ _
-- | Render a string with given font and color. The origin is the middle of the string.
-- Does not cache, and maybe it leaks memory idk.
renderString :: Font -> Color -> String -> IO Picture
renderString f c str = do
surface <- solid f (colorCvt c) (pack str)
((dw, dh), bg) <- bitmapOfSurface NoCache surface
return bg
where
colorCvt :: Color -> V4 Word8
colorCvt c =
let (r, g, b, a) = rgbaOfColor c
in V4 (floor $ r * 255) (floor $ g * 255) (floor $ b * 255) (floor $ a * 255)
renderFunc :: WorldState -> Font -> IO Picture
renderfunc state font = do
selectLevelTxt <- renderString font white "Select a level"
return $ pictures [selectLevelTxt]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment