From 7b446854922b8a1b24a509ffc321306258337e01 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Thu, 27 Aug 2015 18:20:48 +0300 Subject: [PATCH 1/3] Additional 'see also' for Texture::GetFormat() --- SDL2pp/Texture.hh | 1 + 1 file changed, 1 insertion(+) diff --git a/SDL2pp/Texture.hh b/SDL2pp/Texture.hh index 290893c..6992394 100644 --- a/SDL2pp/Texture.hh +++ b/SDL2pp/Texture.hh @@ -402,6 +402,7 @@ public: /// /// \see http://wiki.libsdl.org/SDL_QueryTexture /// \see http://wiki.libsdl.org/SDL_QueryTexture#format + /// \see http://wiki.libsdl.org/SDL_PixelFormatEnum /// //////////////////////////////////////////////////////////// Uint32 GetFormat() const; From c54a021d8e2aa8682dd8ac33a63d34b1e1dff096 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Thu, 27 Aug 2015 18:21:31 +0300 Subject: [PATCH 2/3] Add Surface::GetFormat(), analogus to Texture::GetFormat() --- SDL2pp/Surface.cc | 4 ++++ SDL2pp/Surface.hh | 11 +++++++++++ 2 files changed, 15 insertions(+) diff --git a/SDL2pp/Surface.cc b/SDL2pp/Surface.cc index 3178ba0..7fc86fe 100644 --- a/SDL2pp/Surface.cc +++ b/SDL2pp/Surface.cc @@ -213,4 +213,8 @@ Point Surface::GetSize() const { return Point(surface_->w, surface_->h); } +Uint32 Surface::GetFormat() const { + return surface_->format->format; +} + } diff --git a/SDL2pp/Surface.hh b/SDL2pp/Surface.hh index 1703713..d932c2d 100644 --- a/SDL2pp/Surface.hh +++ b/SDL2pp/Surface.hh @@ -540,6 +540,17 @@ public: /// //////////////////////////////////////////////////////////// Point GetSize() const; + + //////////////////////////////////////////////////////////// + /// \brief Get texture format + /// + /// \return Surface raw format + /// + /// \see http://wiki.libsdl.org/SDL_Surface + /// \see http://wiki.libsdl.org/SDL_PixelFormatEnum + /// + //////////////////////////////////////////////////////////// + Uint32 GetFormat() const; }; } From 4e12008bcf7d123d9a9be166fe1fb8224519f4e5 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Thu, 27 Aug 2015 19:11:08 +0300 Subject: [PATCH 3/3] Implement Texture::Update() which takes pixel data from Surface --- SDL2pp/Texture.cc | 18 ++++++++++++++++++ SDL2pp/Texture.hh | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/SDL2pp/Texture.cc b/SDL2pp/Texture.cc index d17f245..e8ec191 100644 --- a/SDL2pp/Texture.cc +++ b/SDL2pp/Texture.cc @@ -94,6 +94,24 @@ Texture& Texture::Update(const Optional& rect, const void* pixels, int pit return *this; } +Texture& Texture::Update(const Optional& rect, Surface& surface) { + Rect real_rect = rect ? *rect : Rect(0, 0, GetWidth(), GetHeight()); + + real_rect.w = std::min(real_rect.w, surface.GetWidth()); + real_rect.h = std::min(real_rect.h, surface.GetHeight()); + + if (GetFormat() == surface.GetFormat()) { + Surface::LockHandle lock = surface.Lock(); + + return Update(real_rect, lock.GetPixels(), lock.GetPitch()); + } else { + Surface converted = surface.Convert(GetFormat()); + Surface::LockHandle lock = converted.Lock(); + + return Update(real_rect, lock.GetPixels(), lock.GetPitch()); + } +} + Texture& Texture::UpdateYUV(const Optional& rect, const Uint8* yplane, int ypitch, const Uint8* uplane, int upitch, const Uint8* vplane, int vpitch) { if (SDL_UpdateYUVTexture(texture_, rect ? &*rect : nullptr, yplane, ypitch, uplane, upitch, vplane, vpitch) != 0) throw Exception("SDL_UpdateYUVTexture"); diff --git a/SDL2pp/Texture.hh b/SDL2pp/Texture.hh index 6992394..9c5d7a6 100644 --- a/SDL2pp/Texture.hh +++ b/SDL2pp/Texture.hh @@ -313,6 +313,27 @@ public: //////////////////////////////////////////////////////////// Texture& Update(const Optional& rect, const void* pixels, int pitch); + //////////////////////////////////////////////////////////// + /// \brief Update the given texture rectangle with new pixel data taken from surface + /// + /// \param[in] rect Rect representing the area to update, or NullOpt to + /// update the entire texture + /// \param[in] surface Surface to take pixel data from + /// + /// \note No scaling is performed in this routine, so if rect and surface + /// sizes do not match, cropping is performed as appropriate + /// \note If surface and texture pixel formats do not match, surface is + /// automatically converted to texture format + /// + /// \returns Reference to self + /// + /// \throws SDL2pp::Exception + /// + /// \see http://wiki.libsdl.org/SDL_UpdateTexture + /// + //////////////////////////////////////////////////////////// + Texture& Update(const Optional& rect, Surface& surface); + //////////////////////////////////////////////////////////// /// \brief Update the given texture rectangle with new pixel data ///