From 46ad5ea5fe4877f1393acfee4d79fc0a48c2a175 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 20 Mar 2014 22:07:47 +0100 Subject: [PATCH] Tiled wallpaper replicated all over. --- src/tiledwallpaper.cpp | 54 +++++++++++++++++++++++++++++++++++++++++- src/tiledwallpaper.hpp | 15 ++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/tiledwallpaper.cpp b/src/tiledwallpaper.cpp index c9ebea8..cd92b3b 100644 --- a/src/tiledwallpaper.cpp +++ b/src/tiledwallpaper.cpp @@ -20,12 +20,26 @@ #include "tiledwallpaper.hpp" #include "texture.hpp" #include "sdlmain.hpp" +#include "sizeratio.hpp" +#include namespace cloonel { + namespace { + float2 CountTilesInScreen ( const ushort2& parScreenSize, const ushort2& parTileSize ) __attribute__((pure)); + + ///---------------------------------------------------------------------- + ///---------------------------------------------------------------------- + float2 CountTilesInScreen (const ushort2& parScreenSize, const ushort2& parTileSize) { + assert(ushort2(0) != parTileSize); + return static_cast((parTileSize - 1 + parScreenSize) / parTileSize); + } + } //unnamed namespace + ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- TiledWallpaper::TiledWallpaper (const std::string&& parPath, SDLMain* parMain) : Drawable(128.0f, 128.0f), + m_tileCount(parMain, ushort2(128)), m_tile(new Texture(parPath, parMain, false)) { } @@ -50,6 +64,44 @@ namespace cloonel { ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void TiledWallpaper::Draw() const { - m_tile->Render(float2(0.0f), float2(1.0f)); + const ushort2 grid(static_cast(m_tileCount.tileCount())); + const float2& sz = m_tileCount.tileSize(); + + float2 dest; + dest.y() = 0.0f; + for (uint16_t y = 0; y < grid.y(); ++y) { + dest.x() = 0.0f; + for (uint16_t x = 0; x < grid.x(); ++x) { + m_tile->Render(dest, sz, float2(1.0f)); + dest.x() += sz.x(); + } + dest.y() += sz.y(); + } + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + TiledWallpaper::TileCountNotifiable::TileCountNotifiable (SDLMain* parMain, const ushort2& parTileSize) : + BaseClass(parMain), + m_tileCount(CountTilesInScreen(parMain->WidthHeight(), parTileSize)), + m_tileSize(static_cast(parTileSize)) + { + assert(parMain); + assert(ushort2(0) != parTileSize); + } + + ///-------------------------------------------------------------------------- + ///-------------------------------------------------------------------------- + void TiledWallpaper::TileCountNotifiable::NotifyResChanged (const SizeRatio& parSize) { + BaseClass::NotifyResChanged(parSize); + m_tileCount = CountTilesInScreen(static_cast(parSize.Resolution()), static_cast(m_tileSize)); +#if !defined(NDEBUG) + { + const ushort2 tileSize(static_cast(m_tileSize)); + const ushort2 screenRes(static_cast(parSize.Resolution())); + const ushort2 tileCount(static_cast(m_tileCount)); + assert(tileCount * tileSize < screenRes + tileSize); + } +#endif } } //namespace cloonel diff --git a/src/tiledwallpaper.hpp b/src/tiledwallpaper.hpp index a24638e..a61b8d0 100644 --- a/src/tiledwallpaper.hpp +++ b/src/tiledwallpaper.hpp @@ -38,12 +38,23 @@ namespace cloonel { virtual void Draw ( void ) const; private: - class TileCountNotifiable : public SizeNotifiable { + class TileCountNotifiable : public SizeNotifiable { + typedef SizeNotifiable BaseClass; + public: + TileCountNotifiable ( SDLMain* parMain, const ushort2& parTileSize ); + virtual ~TileCountNotifiable ( void ) noexcept = default; + + virtual void NotifyResChanged ( const SizeRatio& parSize ); + const float2& tileCount ( void ) const noexcept { return m_tileCount; } + const float2& tileSize ( void ) const noexcept { return m_tileSize; } + + private: + float2 m_tileCount; + const float2 m_tileSize; }; TileCountNotifiable m_tileCount; const std::unique_ptr m_tile; - size_t m_tileCountID; }; } //namespace cloonel