如何通过 sdl2 创建逐渐褪色的图像?
How to create a gradually faded image via sdl2?
系统:MacOS,安装了 sdl2 的 ghc。
如题所述,如何通过sdl2制作渐变图像? (请注意,该图是由位于 PC 某处的 .bmp 文件给出的。)
下面的代码我已经写好了。 myFaded
其实就是我需要的功能。不过目前haskell会抱怨没有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
我建议使用 Renderer
和 Texture
s 而不是 Surface
(reason)。使用 Texture
,您可以这样设置 alpha 模式:
textureBlendMode texture $= BlendAlphaBlend
textureAlphaMod texture $= 255 - fadednum
请注意,这些是全局变量,因此您可能希望在 copy
之后将 textureAlphaMod
设置回 255。
您编写的代码的简化版本转换为使用 Renderer
和 Texture
将如下所示:
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Control.Concurrent (threadDelay)
import Foreign.C.Types
import Data.Word
import SDL.Vect
import qualified SDL
screenWidth, screenHeight :: CInt
(screenWidth, screenHeight) = (960, 720)
fadednum :: Word8
fadednum = 0
getDataFileName :: FilePath -> IO FilePath
getDataFileName = return
main :: IO ()
main = do
SDL.initialize [SDL.InitVideo]
window <- SDL.createWindow "our super mario" SDL.defaultWindow { SDL.windowInitialSize = V2 screenWidth screenHeight }
SDL.showWindow window
renderer <- SDL.createRenderer window (-1) SDL.defaultRenderer
bmp <- getDataFileName "Entry.bmp" >>= SDL.loadBMP
helloWorld <- SDL.createTextureFromSurface renderer bmp
SDL.freeSurface bmp
myFaded fadednum helloWorld renderer window
SDL.destroyWindow window
SDL.destroyTexture helloWorld
SDL.quit
myFaded :: Word8 -> SDL.Texture -> SDL.Renderer -> SDL.Window -> IO ()
myFaded fadednum texture renderer window
| fadednum == 255 = return ()
| otherwise = do
SDL.clear renderer
SDL.textureBlendMode texture SDL.$= SDL.BlendAlphaBlend
SDL.textureAlphaMod texture SDL.$= 255 - fadednum
SDL.copy renderer texture Nothing Nothing
SDL.textureAlphaMod texture SDL.$= 255
SDL.present renderer
threadDelay 10000
myFaded (fadednum + 1) texture renderer window
系统:MacOS,安装了 sdl2 的 ghc。
如题所述,如何通过sdl2制作渐变图像? (请注意,该图是由位于 PC 某处的 .bmp 文件给出的。)
下面的代码我已经写好了。 myFaded
其实就是我需要的功能。不过目前haskell会抱怨没有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
我建议使用 Renderer
和 Texture
s 而不是 Surface
(reason)。使用 Texture
,您可以这样设置 alpha 模式:
textureBlendMode texture $= BlendAlphaBlend
textureAlphaMod texture $= 255 - fadednum
请注意,这些是全局变量,因此您可能希望在 copy
之后将 textureAlphaMod
设置回 255。
您编写的代码的简化版本转换为使用 Renderer
和 Texture
将如下所示:
{-# LANGUAGE OverloadedStrings #-}
module Main (main) where
import Control.Concurrent (threadDelay)
import Foreign.C.Types
import Data.Word
import SDL.Vect
import qualified SDL
screenWidth, screenHeight :: CInt
(screenWidth, screenHeight) = (960, 720)
fadednum :: Word8
fadednum = 0
getDataFileName :: FilePath -> IO FilePath
getDataFileName = return
main :: IO ()
main = do
SDL.initialize [SDL.InitVideo]
window <- SDL.createWindow "our super mario" SDL.defaultWindow { SDL.windowInitialSize = V2 screenWidth screenHeight }
SDL.showWindow window
renderer <- SDL.createRenderer window (-1) SDL.defaultRenderer
bmp <- getDataFileName "Entry.bmp" >>= SDL.loadBMP
helloWorld <- SDL.createTextureFromSurface renderer bmp
SDL.freeSurface bmp
myFaded fadednum helloWorld renderer window
SDL.destroyWindow window
SDL.destroyTexture helloWorld
SDL.quit
myFaded :: Word8 -> SDL.Texture -> SDL.Renderer -> SDL.Window -> IO ()
myFaded fadednum texture renderer window
| fadednum == 255 = return ()
| otherwise = do
SDL.clear renderer
SDL.textureBlendMode texture SDL.$= SDL.BlendAlphaBlend
SDL.textureAlphaMod texture SDL.$= 255 - fadednum
SDL.copy renderer texture Nothing Nothing
SDL.textureAlphaMod texture SDL.$= 255
SDL.present renderer
threadDelay 10000
myFaded (fadednum + 1) texture renderer window