aboutsummaryrefslogtreecommitdiff
path: root/src/Chelleport/Context.hs
blob: b573975b718e6bf69d85ddbfef8c44f345f2d42f (plain) (blame)
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
module Chelleport.Context where

import Chelleport.Types
import Foreign.C (CFloat)
import qualified Graphics.X11 as X11
import SDL (($=))
import qualified SDL
import qualified SDL.Font as TTF

windowOpacity :: CFloat
windowOpacity = 0.6

fontSize :: Int
fontSize = 24

initializeContext :: IO DrawContext
initializeContext = do
  window <- initializeWindow
  renderer <- initializeRenderer window
  font <- loadFont

  display <- X11.openDisplay ""

  pure $
    DrawContext
      { ctxWindow = window,
        ctxRenderer = renderer,
        ctxFont = font,
        ctxX11Display = display
      }

loadFont :: IO TTF.Font
loadFont = do
  font <- TTF.load "Inter-Regular.ttf" fontSize
  TTF.setStyle font [TTF.Bold]
  pure font

initializeRenderer :: SDL.Window -> IO SDL.Renderer
initializeRenderer window = do
  renderer <- SDL.createRenderer window (-1) SDL.defaultRenderer
  SDL.windowOpacity window $= windowOpacity
  SDL.rendererDrawBlendMode renderer $= SDL.BlendAlphaBlend
  pure renderer

initializeWindow :: IO SDL.Window
initializeWindow = do
  let windowCfg =
        SDL.defaultWindow
          { SDL.windowMode = SDL.FullscreenDesktop,
            SDL.windowPosition = SDL.Absolute $ SDL.P $ SDL.V2 0 0,
            SDL.windowInitialSize = SDL.V2 0 0,
            SDL.windowBorder = False
          }
  window <- SDL.createWindow "Chelleport" windowCfg
  SDL.showWindow window
  pure window