From 8fb21cb43b610c5a04268637155d3efb07217040 Mon Sep 17 00:00:00 2001 From: Akshay Nair Date: Fri, 13 Dec 2024 20:25:35 +0530 Subject: Refactor window drawing and app shell --- src/Chelleport/Draw.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/Chelleport/Draw.hs (limited to 'src/Chelleport/Draw.hs') diff --git a/src/Chelleport/Draw.hs b/src/Chelleport/Draw.hs new file mode 100644 index 0000000..c042e32 --- /dev/null +++ b/src/Chelleport/Draw.hs @@ -0,0 +1,25 @@ +module Chelleport.Draw where + +import Chelleport.AppShell (DrawContext (ctxFont, ctxRenderer)) +import Data.Text (Text) +import qualified SDL +import qualified SDL.Font as TTF + +-- Render text to the screen +renderText :: DrawContext -> Text -> IO () +renderText ctx text = do + -- Render text in white + surface <- TTF.blended (ctxFont ctx) (SDL.V4 255 255 255 255) text + texture <- SDL.createTextureFromSurface (ctxRenderer ctx) surface + SDL.freeSurface surface + + -- Get text dimensions + textureInfo <- SDL.queryTexture texture + let textWidth = SDL.textureWidth textureInfo + textHeight = SDL.textureHeight textureInfo + position = + SDL.P (SDL.V2 ((640 - textWidth) `div` 2) ((480 - textHeight) `div` 2)) + + -- Render the texture + SDL.copy (ctxRenderer ctx) texture Nothing $ Just (SDL.Rectangle position (SDL.V2 textWidth textHeight)) + SDL.destroyTexture texture -- cgit v1.3.1