Skip to content

How to create a gradually faded image via sdl2? #189

Open
@zhengqigao

Description

@zhengqigao

System : MacOS, ghc with sdl2 installed.

As stated in the title, how can I create a gradually faded image via sdl2? (Note that the figure is given by a .bmp file located somewhere in the PC.)

I have already written the code below. myFaded is actually the function I need. However, currently haskell will complain that there is no setSurfaceAlphamod.

{-# LANGUAGE OverloadedStrings #-}
module Main (main) where

import Control.Concurrent (threadDelay)
import Foreign.C.Types
import SDL.Vect
import SDL.Raw.Video
import qualified SDL

screenWidth, screenHeight :: CInt
(screenWidth, screenHeight) = (960, 720)

fadedtime, fadednum :: Int
(fadedtime, fadednum) = (2000000, 10)

getDataFileName :: FilePath -> IO FilePath
getDataFileName = return



myFaded :: Int -> Int -> SDL.Surface -> SDL.Surface -> SDL.Window -> IO ()
myFaded fadedtime fadednum surface screenSurface window
      | fadednum <= 0 = return ()
      | otherwise = do 
          SDL.surfaceBlit surface Nothing screenSurface Nothing
          SDL.updateWindowSurface window
          threadDelay holdtime
          newsurface <- SDL.setSurfaceAlphaMod surface alpha
          myFaded (fadedtime - holdtime) (fadednum - 1)  newsurface screenSurface window
          where alpha = 2
                holdtime = round $ fromIntegral $ fadedtime `div` fadednum




main :: IO ()
main = do
  SDL.initialize [SDL.InitVideo]
  window <- SDL.createWindow "our super mario" SDL.defaultWindow { SDL.windowInitialSize = V2 screenWidth screenHeight }
  SDL.showWindow window
  screenSurface <- SDL.getWindowSurface window

  helloWorld <- getDataFileName "figs/Entry.bmp" >>= SDL.loadBMP
  --SDL.surfaceBlit helloWorld Nothing screenSurface Nothing
  --SDL.updateWindowSurface window
  myFaded fadedtime fadednum helloWorld screenSurface window


  --SDL.updateWindowSurface window

  --threadDelay 2000000

  SDL.destroyWindow window
  SDL.freeSurface helloWorld
  SDL.quit

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions