~alcinnz/Typograffiti

ref: cb478a28d7241d8ee2992ebb44afae94d54ccd86 Typograffiti/app/Main.hs -rw-r--r-- 1.4 KiB
cb478a28 — Adrian Cochrane Integrate richtext APIs. 1 year, 10 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{-# LANGUAGE OverloadedStrings #-}
module Main where

import System.Environment (getArgs)
import Graphics.Text.Font.Render (makeDrawText', GlyphSize(..), TextTransform(..),
                                    AllocatedRendering(..), SpatialTransform(..))
import SDL hiding (rotate)
import Graphics.GL.Core32

import Data.Function (fix)
import Data.Text.Lazy (pack)
import Control.Monad (unless)

main :: IO ()
main = do
    SDL.initializeAll

    let openGL = defaultOpenGL { glProfile = Core Debug 3 3 }
        wcfg = defaultWindow {
            windowInitialSize = V2 640 480,
            windowGraphicsContext = OpenGLContext openGL,
            windowResizable = True
          }
    w <- createWindow "Typograffiti" wcfg
    _ <- glCreateContext w

    let ttfName = "assets/Lora-Regular.ttf"
    text <- pack <$> unwords <$> getArgs
    drawText <- makeDrawText' ttfName 0 (PixelSize 15 15) [] text
    drawText' <- drawText text

    fix $ \loop -> do
        events <- fmap eventPayload <$> pollEvents
        glClearColor 0 0 0 1
        glClear GL_COLOR_BUFFER_BIT

        sz@(V2 dw dh) <- glGetDrawableSize w
        glViewport 0 0 (fromIntegral dw) (fromIntegral dh)

        let offset = V2 0 $ fromIntegral dy
            V2 _ dy = arSize drawText'
        arDraw drawText' [
            TextTransformSpatial $ SpatialTransformTranslate offset
          ] sz

        glSwapWindow w
        unless (QuitEvent `elem` events) loop