~alcinnz/Typograffiti

ref: c8e0b8e470f8fc4f994f2538f5f3c1bb66106a42 Typograffiti/typograffiti-sdl/app/Main.hs -rw-r--r-- 3.1 KiB
c8e0b8e4 — Schell Carl Scivally More abstract (#6) 5 years 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
{-# LANGUAGE FlexibleContexts  #-}
{-# LANGUAGE OverloadedStrings #-}
module Main where

import           Control.Concurrent.STM (atomically, putTMVar, takeTMVar)
import           Control.Monad          (unless, foldM_)
import           Control.Monad.Except   (MonadError, runExceptT)
import           Control.Monad.IO.Class (MonadIO (..))
import           Data.Function          (fix)
import qualified Data.Map               as M
import           Linear                 (V2 (..), V4 (..))
import           SDL                    (Renderer, ($=))
import qualified SDL
import           System.FilePath        ((</>))

import           Typograffiti.SDL
import           Typograffiti.Store


myTextStuff
  :: ( MonadIO m
     , MonadError String m
     )
  => Renderer
  -> m ()
myTextStuff r = do
  let ttfName = "assets" </> "Lora-Regular.ttf"
      glyphSz = GlyphSizeInPixels 16 16
  store <- newDefaultFontStore r
  RenderedGlyphs draw size <-
    getTextRendering
      r
      store
      ttfName
      glyphSz
      $ unlines
          [ "Hey there!"
          , "This is a test of the emergency word system."
          , "Quit at any time."
          ]

  liftIO $ print ("text size" :: String, size)

  fix $ \loop -> do
    events <- fmap SDL.eventPayload
      <$> SDL.pollEvents

    SDL.rendererDrawColor r $= V4 175 175 175 255
    SDL.clear r

    --s@(GlyphRenderingData _ dict _) <-
    --  liftIO
    --    $ atomically
    --    $ takeTMVar
    --    $ unStore store

    --case M.lookup (ttfName, glyphSz) dict of
    --  Nothing -> return ()
    --  Just (Dictionary atlas cache) -> do
    --    SDL.copy
    --      r
    --      (atlasTexture atlas)
    --      Nothing
    --      $ Just
    --      $ SDL.Rectangle
    --         0
    --         $ fromIntegral
    --           <$> atlasTextureSize atlas

    --    let V2 _ startingY = atlasTextureSize atlas
    --        renderTex y ar = do
    --          case (arTextures ar, arSizes ar) of
    --            (tex:_, sz@(V2 _ szy):_) -> do
    --              SDL.copy
    --                r
    --                tex
    --                Nothing
    --                $ Just
    --                $ SDL.Rectangle
    --                    (SDL.P $ fromIntegral <$> V2 0 y)
    --                    $ fromIntegral
    --                      <$> sz
    --              return $ y + szy
    --            (_, _) -> return y
    --    foldM_
    --      renderTex
    --      startingY
    --      $ M.elems
    --      $ unWordCache cache

    --liftIO
    --  $ atomically
    --  $ putTMVar
    --      (unStore store)
    --      s

    draw [move 100 100, color 1.0 0 0 1.0]
    draw [move 100 120, color 1.0 1.0 1.0 1.0]

    SDL.present r
    unless (SDL.QuitEvent `elem` events) loop


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

  let wcfg = SDL.defaultWindow
        { SDL.windowInitialSize = V2 640 480 }
      rcfg = SDL.defaultRenderer
        { SDL.rendererType = SDL.AcceleratedVSyncRenderer }

  w <- SDL.createWindow "Typograffiti SDL" wcfg
  r <- SDL.createRenderer w (-1) rcfg

  SDL.rendererDrawBlendMode r $= SDL.BlendAlphaBlend

  runExceptT (myTextStuff r)
    >>= either (fail . show) return