From ed3fbb89531b62324924a9302223a2d8e3f268b7 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 25 Jan 2016 17:40:00 +0300 Subject: [PATCH 1/8] Optimize examples CMakeLists --- examples/CMakeLists.txt | 61 ++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 34 deletions(-) diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 824e2de..33fe8d3 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,43 +1,36 @@ ADD_DEFINITIONS(-DTESTDATA_DIR="${PROJECT_SOURCE_DIR}/testdata") -ADD_EXECUTABLE(sprites sprites.cc) -TARGET_LINK_LIBRARIES(sprites ${SDL2PP_LIBRARIES}) - -ADD_EXECUTABLE(lines lines.cc) -TARGET_LINK_LIBRARIES(lines ${SDL2PP_LIBRARIES}) - -ADD_EXECUTABLE(rendertarget rendertarget.cc) -TARGET_LINK_LIBRARIES(rendertarget ${SDL2PP_LIBRARIES}) - -ADD_EXECUTABLE(audio_sine audio_sine.cc) -TARGET_LINK_LIBRARIES(audio_sine ${SDL2PP_LIBRARIES}) - -ADD_EXECUTABLE(audio_wav audio_wav.cc) -TARGET_LINK_LIBRARIES(audio_wav ${SDL2PP_LIBRARIES}) +SET(EXAMPLES + audio_sine + audio_wav + lines + rendertarget + sprites +) IF(SDL2PP_WITH_IMAGE) - ADD_EXECUTABLE(image image.cc) - TARGET_LINK_LIBRARIES(image ${SDL2PP_LIBRARIES}) - - ADD_EXECUTABLE(fill fill.cc) - TARGET_LINK_LIBRARIES(fill ${SDL2PP_LIBRARIES}) + SET(EXAMPLES ${EXAMPLES} + image + fill + ) ENDIF(SDL2PP_WITH_IMAGE) +IF(SDL2PP_WITH_MIXER) + SET(EXAMPLES ${EXAMPLES} + mixer + mixer_music + mixer_music_sine + mixer_effects + ) +ENDIF(SDL2PP_WITH_MIXER) + IF(SDL2PP_WITH_TTF) - ADD_EXECUTABLE(ttf ttf.cc) - TARGET_LINK_LIBRARIES(ttf ${SDL2PP_LIBRARIES}) + SET(EXAMPLES ${EXAMPLES} + ttf + ) ENDIF(SDL2PP_WITH_TTF) -IF(SDL2PP_WITH_MIXER) - ADD_EXECUTABLE(mixer mixer.cc) - TARGET_LINK_LIBRARIES(mixer ${SDL2PP_LIBRARIES}) - - ADD_EXECUTABLE(mixer_music mixer_music.cc) - TARGET_LINK_LIBRARIES(mixer_music ${SDL2PP_LIBRARIES}) - - ADD_EXECUTABLE(mixer_music_sine mixer_music_sine.cc) - TARGET_LINK_LIBRARIES(mixer_music_sine ${SDL2PP_LIBRARIES}) - - ADD_EXECUTABLE(mixer_effects mixer_effects.cc) - TARGET_LINK_LIBRARIES(mixer_effects ${SDL2PP_LIBRARIES}) -ENDIF(SDL2PP_WITH_MIXER) +FOREACH(EXAMPLE ${EXAMPLES}) + ADD_EXECUTABLE(${EXAMPLE} ${EXAMPLE}.cc) + TARGET_LINK_LIBRARIES(${EXAMPLE} ${SDL2PP_LIBRARIES}) +ENDFOREACH(EXAMPLE) From b0ea9e7216dc405854f30c992100fe50f6154132 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 25 Jan 2016 19:27:18 +0300 Subject: [PATCH 2/8] Add stub surface test --- tests/CMakeLists.txt | 10 +++++++++- tests/test_surface.cc | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 tests/test_surface.cc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index afe1a55..1d9d76a 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -15,9 +15,17 @@ SET(LIVE_TESTS ) IF(SDL2PP_WITH_MIXER) - SET(LIVE_TESTS ${LIVE_TESTS} live_mixer) + SET(LIVE_TESTS ${LIVE_TESTS} + live_mixer + ) ENDIF(SDL2PP_WITH_MIXER) +IF(SDL2PP_WITH_IMAGE) + SET(CLI_TESTS ${CLI_TESTS} + test_surface + ) +ENDIF(SDL2PP_WITH_IMAGE) + # disable self-move warning: it's deliberately used in tests INCLUDE(AppendCXXFlagIfSupported) APPEND_CXX_FLAG_IF_SUPPORTED(-Wno-self-move CMAKE_CXX_FLAGS) diff --git a/tests/test_surface.cc b/tests/test_surface.cc new file mode 100644 index 0000000..076445a --- /dev/null +++ b/tests/test_surface.cc @@ -0,0 +1,17 @@ +#include + +#include + +#include "testing.h" + +using namespace SDL2pp; + +BEGIN_TEST(int, char*[]) + Surface crate(TESTDATA_DIR "/crate.png"); + + { + EXPECT_EQUAL(crate.GetWidth(), 32); + EXPECT_EQUAL(crate.GetHeight(), 32); + EXPECT_EQUAL(crate.GetSize(), Point(32, 32)); + } +END_TEST() From bc6ff5f45041d6c751aa47981b1e259a21fb78ce Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 25 Jan 2016 19:46:18 +0300 Subject: [PATCH 3/8] Add basic font test --- tests/CMakeLists.txt | 6 +++ tests/test_font.cc | 102 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 tests/test_font.cc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1d9d76a..5f5a820 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -26,6 +26,12 @@ IF(SDL2PP_WITH_IMAGE) ) ENDIF(SDL2PP_WITH_IMAGE) +IF(SDL2PP_WITH_TTF) + SET(CLI_TESTS ${CLI_TESTS} + test_font + ) +ENDIF(SDL2PP_WITH_TTF) + # disable self-move warning: it's deliberately used in tests INCLUDE(AppendCXXFlagIfSupported) APPEND_CXX_FLAG_IF_SUPPORTED(-Wno-self-move CMAKE_CXX_FLAGS) diff --git a/tests/test_font.cc b/tests/test_font.cc new file mode 100644 index 0000000..9b5a058 --- /dev/null +++ b/tests/test_font.cc @@ -0,0 +1,102 @@ +#include + +#include +#include + +#include "testing.h" + +using namespace SDL2pp; + +BEGIN_TEST(int, char*[]) + SDLTTF ttf; + Font font(TESTDATA_DIR "/Vera.ttf", 30); + + { + // Move tests + TTF_Font* ptr = font.Get(); + + EXPECT_TRUE(ptr != nullptr); + + Font font1(std::move(font)); + EXPECT_TRUE(font1.Get() == ptr); + EXPECT_TRUE(font.Get() == nullptr); + + std::swap(font, font1); + EXPECT_TRUE(font.Get() == ptr); + EXPECT_TRUE(font1.Get() == nullptr); + + font = std::move(font); // self-move + EXPECT_TRUE(font.Get() == ptr); + } + + { + // Font style + EXPECT_EQUAL(font.GetStyle(), TTF_STYLE_NORMAL); + + font.SetStyle(TTF_STYLE_BOLD | TTF_STYLE_ITALIC | TTF_STYLE_UNDERLINE | TTF_STYLE_STRIKETHROUGH); + EXPECT_EQUAL(font.GetStyle(), TTF_STYLE_BOLD | TTF_STYLE_ITALIC | TTF_STYLE_UNDERLINE | TTF_STYLE_STRIKETHROUGH); + + font.SetStyle(); + EXPECT_EQUAL(font.GetStyle(), TTF_STYLE_NORMAL); + } + + { + // Outline + EXPECT_EQUAL(font.GetOutline(), 0); + + font.SetOutline(2); + EXPECT_EQUAL(font.GetOutline(), 2); + + font.SetOutline(); + EXPECT_EQUAL(font.GetOutline(), 0); + } + + { + // Hinting + EXPECT_EQUAL(font.GetHinting(), TTF_HINTING_NORMAL); + + font.SetHinting(TTF_HINTING_LIGHT); + EXPECT_EQUAL(font.GetHinting(), TTF_HINTING_LIGHT); + + font.SetHinting(); + EXPECT_EQUAL(font.GetHinting(), TTF_HINTING_NORMAL); + } + + { + // Kerning + EXPECT_EQUAL(font.GetKerning(), true); + + font.SetKerning(false); + EXPECT_EQUAL(font.GetKerning(), false); + + font.SetKerning(); + EXPECT_EQUAL(font.GetKerning(), true); + } + + { + // Metrics + EXPECT_EQUAL(font.GetHeight(), 36); + EXPECT_EQUAL(font.GetAscent(), 28); + EXPECT_EQUAL(font.GetDescent(), -7); + EXPECT_EQUAL(font.GetLineSkip(), 35); + } + + { + // Faces + EXPECT_EQUAL(font.GetNumFaces(), 1); + } + + { + // Fixed width + EXPECT_EQUAL(font.IsFixedWidth(), false); + } + + { + // Names + auto family = font.GetFamilyName(); + auto style = font.GetStyleName(); + + EXPECT_TRUE(family && *family == "Bitstream Vera Sans"); + EXPECT_TRUE(style && *style == "Roman"); + } +END_TEST() From 15e801dc7bf654ce5591f4f4f2f19d1d3bfdb65c Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 25 Jan 2016 20:19:23 +0300 Subject: [PATCH 4/8] More Font tests --- tests/test_font.cc | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/tests/test_font.cc b/tests/test_font.cc index 9b5a058..ccc3fa0 100644 --- a/tests/test_font.cc +++ b/tests/test_font.cc @@ -2,6 +2,7 @@ #include #include +#include #include "testing.h" @@ -99,4 +100,36 @@ BEGIN_TEST(int, char*[]) EXPECT_TRUE(family && *family == "Bitstream Vera Sans"); EXPECT_TRUE(style && *style == "Roman"); } + + { + // Glyphs provided + EXPECT_TRUE(font.IsGlyphProvided(u'A')); + EXPECT_TRUE(font.IsGlyphProvided(u'¼')); + EXPECT_TRUE(!font.IsGlyphProvided(u'л')); + EXPECT_TRUE(!font.IsGlyphProvided(u'Ы')); + } + + { + // Glyph metrics + int minx, maxx, miny, maxy, advance; + + // Why doesn't TTF_GlyphMetrics on non-existing glyph not return -1?! + //EXPECT_EXCEPTION(font.GetGlyphMetrics(u'л', minx, maxx, miny, maxy, advance), Exception); + + EXPECT_NO_EXCEPTION(font.GetGlyphMetrics(u'A', minx, maxx, miny, maxy, advance)); + + EXPECT_EQUAL(minx, 0); + EXPECT_EQUAL(maxx, 20); + EXPECT_EQUAL(miny, 0); + EXPECT_EQUAL(maxy, 22); + EXPECT_EQUAL(advance, 21); + + EXPECT_EQUAL(font.GetGlyphRect(u'A'), Rect(0, 0, 20, 22)); + EXPECT_EQUAL(font.GetGlyphAdvance(u'A'), 21); + + // Text size + EXPECT_EQUAL(font.GetSizeText("AA"), Point(43, 36)); + EXPECT_EQUAL(font.GetSizeUTF8(u8"AA"), Point(43, 36)); + EXPECT_EQUAL(font.GetSizeUNICODE(u"AA"), Point(43, 36)); + } END_TEST() From d75d68a2a0cd867543977a6101ff886f38310089 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 25 Jan 2016 20:33:02 +0300 Subject: [PATCH 5/8] Add font rendering tests --- tests/test_font.cc | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/test_font.cc b/tests/test_font.cc index ccc3fa0..d3dd6ba 100644 --- a/tests/test_font.cc +++ b/tests/test_font.cc @@ -132,4 +132,20 @@ BEGIN_TEST(int, char*[]) EXPECT_EQUAL(font.GetSizeUTF8(u8"AA"), Point(43, 36)); EXPECT_EQUAL(font.GetSizeUNICODE(u"AA"), Point(43, 36)); } + + { + // Rendering + // XXX: add real pixel color tests + EXPECT_EQUAL(font.RenderText_Solid("AA", SDL_Color{255, 255, 255, 255}).GetSize(), Point(43, 36)); + EXPECT_EQUAL(font.RenderUTF8_Solid(u8"AA", SDL_Color{255, 255, 255, 255}).GetSize(), Point(43, 36)); + EXPECT_EQUAL(font.RenderUNICODE_Solid(u"AA", SDL_Color{255, 255, 255, 255}).GetSize(), Point(43, 36)); + + EXPECT_EQUAL(font.RenderText_Shaded("AA", SDL_Color{255, 255, 255, 255}, SDL_Color{0, 0, 0, 255}).GetSize(), Point(43, 36)); + EXPECT_EQUAL(font.RenderUTF8_Shaded(u8"AA", SDL_Color{255, 255, 255, 255}, SDL_Color{0, 0, 0, 255}).GetSize(), Point(43, 36)); + EXPECT_EQUAL(font.RenderUNICODE_Shaded(u"AA", SDL_Color{255, 255, 255, 255}, SDL_Color{0, 0, 0, 255}).GetSize(), Point(43, 36)); + + EXPECT_EQUAL(font.RenderText_Blended("AA", SDL_Color{255, 255, 255, 255}).GetSize(), Point(43, 36)); + EXPECT_EQUAL(font.RenderUTF8_Blended(u8"AA", SDL_Color{255, 255, 255, 255}).GetSize(), Point(43, 36)); + EXPECT_EQUAL(font.RenderUNICODE_Blended(u"AA", SDL_Color{255, 255, 255, 255}).GetSize(), Point(43, 36)); + } END_TEST() From e49423f3f12d9385047d819c8f84492ccae2cced Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 25 Jan 2016 21:46:53 +0300 Subject: [PATCH 6/8] Remove obsolete tests --- tests/header_containerrwops.cc | 5 ----- tests/header_streamrwops.cc | 5 ----- 2 files changed, 10 deletions(-) delete mode 100644 tests/header_containerrwops.cc delete mode 100644 tests/header_streamrwops.cc diff --git a/tests/header_containerrwops.cc b/tests/header_containerrwops.cc deleted file mode 100644 index 67a124e..0000000 --- a/tests/header_containerrwops.cc +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int main(int, char*[]) { - return 0; -} diff --git a/tests/header_streamrwops.cc b/tests/header_streamrwops.cc deleted file mode 100644 index f4f3ee4..0000000 --- a/tests/header_streamrwops.cc +++ /dev/null @@ -1,5 +0,0 @@ -#include - -int main(int, char*[]) { - return 0; -} From c001bf7cad03980430b74231fdbaa409accfc307 Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 25 Jan 2016 22:56:11 +0300 Subject: [PATCH 7/8] Add default value for Texture::SetBlendMode --- SDL2pp/Texture.hh | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SDL2pp/Texture.hh b/SDL2pp/Texture.hh index 9c5d7a6..b481a22 100644 --- a/SDL2pp/Texture.hh +++ b/SDL2pp/Texture.hh @@ -1,6 +1,6 @@ /* libSDL2pp - C++11 bindings/wrapper for SDL2 - Copyright (C) 2013-2015 Dmitry Marakasov + Copyright (C) 2013-2016 Dmitry Marakasov This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages @@ -367,7 +367,7 @@ public: /// \see http://wiki.libsdl.org/SDL_SetTextureBlendMode /// //////////////////////////////////////////////////////////// - Texture& SetBlendMode(SDL_BlendMode blendMode); + Texture& SetBlendMode(SDL_BlendMode blendMode = SDL_BLENDMODE_NONE); //////////////////////////////////////////////////////////// /// \brief Set an additional alpha value multiplied into render copy operations From c98e3a653fb7278ee5e23f05d644c96011bea72d Mon Sep 17 00:00:00 2001 From: Dmitry Marakasov Date: Mon, 25 Jan 2016 22:57:16 +0300 Subject: [PATCH 8/8] Extend renderer test --- tests/live_rendering.cc | 100 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 94 insertions(+), 6 deletions(-) diff --git a/tests/live_rendering.cc b/tests/live_rendering.cc index 4103e25..f6bb032 100644 --- a/tests/live_rendering.cc +++ b/tests/live_rendering.cc @@ -57,7 +57,7 @@ BEGIN_TEST(int, char*[]) PixelInspector pixels(320, 240, 4); { - // clear + // Clear renderer.SetDrawColor(1, 2, 3); renderer.Clear(); pixels.Retrieve(renderer); @@ -69,7 +69,7 @@ BEGIN_TEST(int, char*[]) } { - // draw points + // Draw points renderer.SetDrawColor(0,0,0); renderer.Clear(); @@ -93,7 +93,7 @@ BEGIN_TEST(int, char*[]) } { - // draw lines + // Draw lines renderer.SetDrawColor(0, 0, 0); renderer.Clear(); @@ -118,7 +118,7 @@ BEGIN_TEST(int, char*[]) } { - // draw rects + // Draw rects renderer.SetDrawColor(0, 0, 0); renderer.Clear(); @@ -162,7 +162,7 @@ BEGIN_TEST(int, char*[]) } { - // fill rects + // Fill rects renderer.SetDrawColor(0, 0, 0); renderer.Clear(); @@ -206,7 +206,7 @@ BEGIN_TEST(int, char*[]) } { - // blend + // Blend renderer.SetDrawColor(0, 0, 0); renderer.Clear(); @@ -224,4 +224,92 @@ BEGIN_TEST(int, char*[]) renderer.Present(); SDL_Delay(1000); } + +#ifdef SDL2PP_WITH_IMAGE + { + // Texture + renderer.SetDrawColor(0, 0, 0); + renderer.Clear(); + + Texture texture(renderer, TESTDATA_DIR "/crate.png"); + + EXPECT_EQUAL(texture.GetWidth(), 32); + EXPECT_EQUAL(texture.GetHeight(), 32); + EXPECT_EQUAL(texture.GetSize(), Point(32, 32)); + + renderer.Copy(texture, NullOpt, Point(0, 0)); + + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test3x3(1, 1, 0x032, 238, 199, 0)); + + renderer.Present(); + SDL_Delay(1000); + + // Texture: fill copy + renderer.SetDrawColor(0, 0, 0); + renderer.Clear(); + + renderer.FillCopy(texture, NullOpt, Rect(0, 0, 48, 48), Point(16, 16), 0); + + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test3x3(1+16, 1+16, 0x032, 238, 199, 0)); + + renderer.Present(); + SDL_Delay(1000); + + // Texture: alpha blending/modulation + renderer.SetDrawColor(0, 0, 0); + renderer.Clear(); + + EXPECT_EQUAL(texture.GetBlendMode(), SDL_BLENDMODE_NONE); + texture.SetBlendMode(SDL_BLENDMODE_BLEND); + EXPECT_EQUAL(texture.GetBlendMode(), SDL_BLENDMODE_BLEND); + + EXPECT_EQUAL((int)texture.GetAlphaMod(), 255); + texture.SetAlphaMod(127); + EXPECT_EQUAL((int)texture.GetAlphaMod(), 127); + + renderer.Copy(texture, NullOpt, Point(0, 0)); + + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test3x3(1, 1, 0x032, 119, 99, 0)); + + renderer.Present(); + SDL_Delay(1000); + + texture.SetBlendMode(); + EXPECT_EQUAL(texture.GetBlendMode(), SDL_BLENDMODE_NONE); + texture.SetAlphaMod(); + EXPECT_EQUAL((int)texture.GetAlphaMod(), 255); + + // Texture: color modulation + renderer.SetDrawColor(0, 0, 0); + renderer.Clear(); + + Uint8 r, g, b; + texture.GetColorMod(r, g, b); + EXPECT_EQUAL((int)r, 255); + EXPECT_EQUAL((int)g, 255); + EXPECT_EQUAL((int)b, 255); + texture.SetColorMod(89, 241, 50); + + renderer.Copy(texture, NullOpt, Point(0, 0)); + + pixels.Retrieve(renderer); + + EXPECT_TRUE(pixels.Test3x3(1, 1, 0x032, 83, 188, 0)); + + renderer.Present(); + SDL_Delay(1000); + + texture.SetColorMod(); + texture.GetColorMod(r, g, b); + EXPECT_EQUAL((int)r, 255); + EXPECT_EQUAL((int)g, 255); + EXPECT_EQUAL((int)b, 255); + } +#endif // SDL2PP_WITH_IMAGE END_TEST()