これをSDLサーフェスとHaskellバインディングで機能させるには、これを使用して、の型エイリアスであり、空のデータ宣言であるをsurfaceGetPixels
返します。これは、SDLサーフェスのピクセル形式とピクセルあたりのビット数がほぼすべてである可能性があるためです。したがって、基本的に32bpp形式の場合は、を使用して言うようにポインタをキャストします。Pixels
Ptr PixelData
PixelData
Ptr Word32
castPtr
ピクセルを取得/配置する例を次に示します。
getPixel32 :: MonadIO m => Surface -> Int -> Int -> m Pixel
getPixel32 s x y = liftIO $ do
ps <- surfaceGetPixels s
assert (x >= 0 && x < surfaceGetWidth s && y >= 0 && y < surfaceGetHeight s) $
Pixel `liftM` peekElemOff (castPtr ps :: Ptr Word32) offset
where offset = y * (fromIntegral $ surfaceGetPitch s `div` 4) + x
setPixel32 :: MonadIO m => Surface -> Int -> Int -> Pixel -> m ()
setPixel32 s x y (Pixel pixel) = liftIO $ do
ps <- surfaceGetPixels s
assert (x >= 0 && x < surfaceGetWidth s && y >= 0 && y < surfaceGetHeight s) $
pokeElemOff (castPtr ps :: Ptr Word32) offset pixel
where offset = y * (fromIntegral $ surfaceGetPitch s `div` 4) + x
同様に、ポインタを特定のポインタタイプにキャストし、それをglTexImage2Dに渡して、テクスチャをアップロードできます。