diff --git a/SDL2pp/Surface.cc b/SDL2pp/Surface.cc index 0145803..43dccfb 100644 --- a/SDL2pp/Surface.cc +++ b/SDL2pp/Surface.cc @@ -51,6 +51,18 @@ 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"); +} + +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 Surface::Surface(RWops& rwops) { if ((surface_ = IMG_Load_RW(rwops.Get(), 0)) == nullptr) diff --git a/SDL2pp/Surface.hh b/SDL2pp/Surface.hh index 17f9be3..3f0413a 100644 --- a/SDL2pp/Surface.hh +++ b/SDL2pp/Surface.hh @@ -24,6 +24,7 @@ #include #include +#include #include #include @@ -210,6 +211,41 @@ 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); + + //////////////////////////////////////////////////////////// + /// \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 //////////////////////////////////////////////////////////// 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()