Skip to content

Commit 794a5bf

Browse files
committed
Merge branch 'BartAdv-updateTexture' into master-next
2 parents b9683c5 + 3d8a8d9 commit 794a5bf

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/SDL/Video/Renderer.hs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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
124125
import Data.Word
125126
import Foreign.C.String
126127
import Foreign.C.Types
128+
import Foreign.ForeignPtr
127129
import Foreign.Marshal.Alloc
128130
import Foreign.Marshal.Utils
129131
import Foreign.Ptr
@@ -136,6 +138,7 @@ import SDL.Exception
136138
import SDL.Internal.Numbered
137139
import SDL.Internal.Types
138140
import qualified Data.ByteString as BS
141+
import qualified Data.ByteString.Internal as BSI
139142
import qualified Data.Text.Encoding as Text
140143
import qualified Data.Vector.Storable as SV
141144
import 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

Comments
 (0)