{-# LANGUAGE OverloadedStrings #-}
module Preload.Update
    ( pdataResizeUpdate
    ) where
import Data.Preload
import Framebuffer.Update
import Shader.Compile
import Shader.Data
import Data.Preload.Render

import qualified Data.ByteString as BS
import qualified Data.ByteString.Char8 as BSC
pdataResizeUpdate
    :: Int  -- ^ Scaled width
    -> Int  -- ^ Scaled height
    -> Int  -- ^ Full width
    -> Int  -- ^ Full height
    -> PreloadData
    -> IO PreloadData
pdataResizeUpdate xsize ysize xfull yfull pdata = do
    rd <- renderDataResizeUpdate xsize ysize xfull yfull (_renderData pdata)
    return (pdata {_renderData = rd})

renderDataResizeUpdate
    :: Int  -- ^ Scaled width
    -> Int  -- ^ Scaled height
    -> Int  -- ^ Full width
    -> Int  -- ^ Full height
    -> RenderData
    -> IO RenderData
renderDataResizeUpdate xsize ysize xfull yfull rdata = do
    rdata' <- sizeFBOs xsize ysize xfull yfull rdata
    bbVert <- BS.readFile "shader/texture/bloomBlur.vert"
    bbFrag <- BS.readFile "shader/texture/bloomBlur.frag"
    let (bh,bmid) = BS.breakSubstring "(" bbFrag
        (_,btt) = BS.breakSubstring ")" bmid
        bbFrag' = BS.append bh $ BS.append (BSC.pack $ '(' : show xsize ++ "," ++ show ysize) btt
    --BSC.putStrLn bbFrag'
    bbShad <- makeByteStringShaderUsingVAO "bloomBlur" [(vert,bbVert),(frag,bbFrag')] ETriangleStrip
                (_fullscreenShader rdata)
    return (rdata' {_bloomBlurShader = bbShad})
