Added Surface constructor based on SDL_CreateRGBSurfaceWithFormat (issue #80)

This commit is contained in:
Vraiment 2017-07-24 00:18:29 -07:00
parent 7cfc30970b
commit a668a2dbd5
3 changed files with 37 additions and 0 deletions

View File

@ -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)

View File

@ -24,6 +24,7 @@
#include <SDL_stdinc.h>
#include <SDL_blendmode.h>
#include <SDL_version.h>
#include <SDL2pp/Config.hh>
#include <SDL2pp/Optional.hh>
@ -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
////////////////////////////////////////////////////////////

View File

@ -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()