From a668a2dbd5638482886beaea6022fa9cf76ddc34 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Mon, 24 Jul 2017 00:18:29 -0700 Subject: [PATCH 1/2] Added Surface constructor based on SDL_CreateRGBSurfaceWithFormat (issue #80) --- SDL2pp/Surface.cc | 7 +++++++ SDL2pp/Surface.hh | 19 +++++++++++++++++++ tests/test_surface.cc | 11 +++++++++++ 3 files changed, 37 insertions(+) diff --git a/SDL2pp/Surface.cc b/SDL2pp/Surface.cc index 7babeaf..877efbd 100644 --- a/SDL2pp/Surface.cc +++ b/SDL2pp/Surface.cc @@ -51,6 +51,13 @@ Surface::Surface(void* pixels, int width, int height, int depth, int pitch, Uint throw Exception("SDL_CreateRGBSurfaceFrom"); } +#if SDL_VERSION_ATLEAST(2, 0, 5) +Surface::Surface(Uint32 flags, int width, int height, int depth, Uint32 format) { + if ((surface_ = SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format)) == nullptr) + throw Exception("SDL_CreateRGBSurfaceWithFormat"); +} +#endif + #ifdef SDL2PP_WITH_IMAGE Surface::Surface(RWops& rwops) { if ((surface_ = IMG_Load_RW(rwops.Get(), 0)) == nullptr) diff --git a/SDL2pp/Surface.hh b/SDL2pp/Surface.hh index c6ea34a..7ab9dc5 100644 --- a/SDL2pp/Surface.hh +++ b/SDL2pp/Surface.hh @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -210,6 +211,24 @@ public: /// //////////////////////////////////////////////////////////// Surface(void* pixels, int width, int height, int depth, int pitch, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask); + +#if SDL_VERSION_ATLEAST(2, 0, 5) + //////////////////////////////////////////////////////////// + /// \brief Create RGB surface with the given format + /// + /// \param[in] flags Flags are obsolete and should be set to 0 + /// \param[in] width Width of the surface + /// \param[in] height Height of the surface + /// \param[in] depth Depth of the surface + /// \param[in] format The pixel format of the surface + /// + /// \throws SDL2pp::Exception + /// + /// \see https://wiki.libsdl.org/SDL_CreateRGBSurfaceWithFormat + /// + //////////////////////////////////////////////////////////// + Surface(Uint32 flags, int width, int height, int depth, Uint32 format); +#endif #ifdef SDL2PP_WITH_IMAGE //////////////////////////////////////////////////////////// diff --git a/tests/test_surface.cc b/tests/test_surface.cc index a448240..e86066b 100644 --- a/tests/test_surface.cc +++ b/tests/test_surface.cc @@ -17,4 +17,15 @@ BEGIN_TEST(int, char*[]) EXPECT_EQUAL(crate.GetHeight(), 32); EXPECT_EQUAL(crate.GetSize(), Point(32, 32)); } + +#if SDL_VERSION_ATLEAST(2, 0, 5) + { + // Test create surface from pixel format + Surface surface(0, 600, 300, 24, SDL_PIXELFORMAT_RGBA32); + + EXPECT_EQUAL(surface.GetWidth(), 600); + EXPECT_EQUAL(surface.GetHeight(), 300); + EXPECT_EQUAL(surface.GetFormat(), SDL_PIXELFORMAT_RGBA32); + } +#endif END_TEST() From 5cf39322a6f84ddc919aca83562a08cea278f1f6 Mon Sep 17 00:00:00 2001 From: Vraiment Date: Mon, 24 Jul 2017 00:29:01 -0700 Subject: [PATCH 2/2] Added Surface constructor based on SDL_CreateRGBSurfaceWithFormatFrom (issue #80) --- SDL2pp/Surface.cc | 5 +++++ SDL2pp/Surface.hh | 17 +++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/SDL2pp/Surface.cc b/SDL2pp/Surface.cc index 877efbd..a87de0f 100644 --- a/SDL2pp/Surface.cc +++ b/SDL2pp/Surface.cc @@ -56,6 +56,11 @@ Surface::Surface(Uint32 flags, int width, int height, int depth, Uint32 format) if ((surface_ = SDL_CreateRGBSurfaceWithFormat(flags, width, height, depth, format)) == nullptr) throw Exception("SDL_CreateRGBSurfaceWithFormat"); } + +Surface::Surface(void* pixels, int width, int height, int depth, int pitch, Uint32 format) { + if ((surface_ = SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, depth, pitch, format)) == nullptr) + throw Exception("SDL_CreateRGBSurfaceWithFormatFrom"); +} #endif #ifdef SDL2PP_WITH_IMAGE diff --git a/SDL2pp/Surface.hh b/SDL2pp/Surface.hh index 7ab9dc5..ddcfdd2 100644 --- a/SDL2pp/Surface.hh +++ b/SDL2pp/Surface.hh @@ -228,6 +228,23 @@ public: /// //////////////////////////////////////////////////////////// Surface(Uint32 flags, int width, int height, int depth, Uint32 format); + + //////////////////////////////////////////////////////////// + /// \brief Create RGB surface with the given format from the given pixel data + /// + /// \param[in] pixels The pixel data to create the surface from + /// \param[in] width Width of the surface + /// \param[in] height Height of the surface + /// \param[in] depth Depth of the surface + /// \param[in] pitch The length of a row of pixels in bytes + /// \param[in] format The pixel format of the surface + /// + /// \throws SDL2pp::Exception + /// + /// \see https://wiki.libsdl.org/SDL_CreateRGBSurfaceWithFormat + /// + //////////////////////////////////////////////////////////// + Surface(void* pixels, int width, int height, int depth, int pitch, Uint32 format); #endif #ifdef SDL2PP_WITH_IMAGE