@@ -88,6 +88,7 @@ module SDL.Video.Renderer
8888 , createTexture
8989 , TextureAccess (.. )
9090 , createTextureFromSurface
91+ , updateTexture
9192 , destroyTexture
9293 , glBindTexture
9394 , glUnbindTexture
@@ -124,6 +125,7 @@ import Data.Typeable
124125import Data.Word
125126import Foreign.C.String
126127import Foreign.C.Types
128+ import Foreign.ForeignPtr
127129import Foreign.Marshal.Alloc
128130import Foreign.Marshal.Utils
129131import Foreign.Ptr
@@ -136,6 +138,7 @@ import SDL.Exception
136138import SDL.Internal.Numbered
137139import SDL.Internal.Types
138140import qualified Data.ByteString as BS
141+ import qualified Data.ByteString.Internal as BSI
139142import qualified Data.Text.Encoding as Text
140143import qualified Data.Vector.Storable as SV
141144import qualified Data.Vector.Storable.Mutable as MSV
@@ -207,6 +210,23 @@ glUnbindTexture (Texture t) =
207210 throwIfNeg_ " SDL.Video.Renderer.glUnindTexture" " SDL_GL_UnbindTexture" $
208211 Raw. glUnbindTexture t
209212
213+ -- | Updates texture rectangle with new pixel data.
214+ --
215+ -- See @<https://wiki.libsdl.org/SDL_UpdateTexture SDL_UpdateTexture>@ for C documentation.
216+ updateTexture :: (Functor m , MonadIO m )
217+ => Texture -- ^ The 'Texture' to be updated
218+ -> Maybe (Rectangle CInt ) -- ^ The area to update, Nothing for entire texture
219+ -> BS. ByteString -- ^ The raw pixel data
220+ -> CInt -- ^ The number of bytes in a row of pixel data, including padding between lines
221+ -> m Texture
222+ updateTexture tex@ (Texture t) rect pixels pitch = do
223+ liftIO $ throwIfNeg_ " SDL.Video.updateTexture" " SDL_UpdateTexture" $
224+ maybeWith with rect $ \ rectPtr ->
225+ let (pixelForeign, _, _) = BSI. toForeignPtr pixels
226+ in withForeignPtr pixelForeign $ \ pixelsPtr ->
227+ Raw. updateTexture t (castPtr rectPtr) (castPtr pixelsPtr) pitch
228+ return tex
229+
210230-- | Destroy the specified texture.
211231--
212232-- See @<https://wiki.libsdl.org/SDL_DestroyTexture SDL_DestroyTexture>@ for the C documentation.
0 commit comments