Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
zhengqigao opened this issue Sep 30, 2018 · 0 comments
Open

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

zhengqigao opened this issue Sep 30, 2018 · 0 comments
Labels

Comments

@zhengqigao
Copy link

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

@dpwiz dpwiz added the question label Mar 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants