Tiled wallpaper replicated all over.

This commit is contained in:
King_DuckZ 2014-03-20 22:07:47 +01:00
parent d0a052ddb2
commit 46ad5ea5fe
2 changed files with 66 additions and 3 deletions

View file

@ -20,12 +20,26 @@
#include "tiledwallpaper.hpp"
#include "texture.hpp"
#include "sdlmain.hpp"
#include "sizeratio.hpp"
#include <cassert>
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<float2>((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<ushort2>(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<float2>(parTileSize))
{
assert(parMain);
assert(ushort2(0) != parTileSize);
}
///--------------------------------------------------------------------------
///--------------------------------------------------------------------------
void TiledWallpaper::TileCountNotifiable::NotifyResChanged (const SizeRatio& parSize) {
BaseClass::NotifyResChanged(parSize);
m_tileCount = CountTilesInScreen(static_cast<ushort2>(parSize.Resolution()), static_cast<ushort2>(m_tileSize));
#if !defined(NDEBUG)
{
const ushort2 tileSize(static_cast<ushort2>(m_tileSize));
const ushort2 screenRes(static_cast<ushort2>(parSize.Resolution()));
const ushort2 tileCount(static_cast<ushort2>(m_tileCount));
assert(tileCount * tileSize < screenRes + tileSize);
}
#endif
}
} //namespace cloonel

View file

@ -38,12 +38,23 @@ namespace cloonel {
virtual void Draw ( void ) const;
private:
class TileCountNotifiable : public SizeNotifiable<regbehaviuors::DontRegister> {
class TileCountNotifiable : public SizeNotifiable<regbehaviuors::AutoRegister> {
typedef SizeNotifiable<regbehaviuors::AutoRegister> 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<Texture> m_tile;
size_t m_tileCountID;
};
} //namespace cloonel