/* Copyright 2014 Michele "King_DuckZ" Santullo This file is part of CloonelJump. CloonelJump is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. CloonelJump is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with CloonelJump. If not, see . */ #include "tiledwallpaper.hpp" #include "texture.hpp" #include "sdlmain.hpp" #include "sizeratio.hpp" #include "compatibility.h" #include #include namespace cloonel { namespace { float2 CountTilesInScreen ( const ushort2& parScreenSize, const ushort2& parTileSize ) a_pure; ///---------------------------------------------------------------------- ///---------------------------------------------------------------------- float2 CountTilesInScreen (const ushort2& parScreenSize, const ushort2& parTileSize) { assert(ushort2(0) != parTileSize); return vector_cast((parTileSize - 1U + 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)) #if defined(WITH_DEBUG_VISUALS) , m_midscreen(parMain, Colour(80, 250, 6), short2(0), short2(1) ) #endif { } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- TiledWallpaper::~TiledWallpaper() noexcept { } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void TiledWallpaper::Reload() { m_tile->Reload(); } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void TiledWallpaper::Destroy() noexcept { m_tile->Destroy(); } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void TiledWallpaper::Draw() const { const ushort2 grid(vector_cast(m_tileCount.tileCount())); const float2& sz = m_tileCount.tileSize(); //TODO: add code to tell the renderer if the current tile will need clipping or not 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), true); dest.x() += sz.x(); } dest.y() += sz.y(); } #if defined(WITH_DEBUG_VISUALS) //std::cout << "drawing line at " << WidthHeight() * float2(1.0f, 0.5f); //std::cout << " to " << m_tileCount.screenRes() << "\n"; m_midscreen.Render(WidthHeight() * float2(1.0f, 0.5f), m_tileCount.screenRes()); #endif } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- TiledWallpaper::TileCountNotifiable::TileCountNotifiable (SDLMain* parMain, const ushort2& parTileSize) : BaseClass(parMain, this), m_tileCount(CountTilesInScreen(parMain->WidthHeight(), parTileSize)), m_tileSize(vector_cast(parTileSize)) #if defined(WITH_DEBUG_VISUALS) , m_screenRes(1.0f) #endif { assert(parMain); assert(ushort2(0) != parTileSize); } ///-------------------------------------------------------------------------- ///-------------------------------------------------------------------------- void TiledWallpaper::TileCountNotifiable::OnResChanged (const SizeRatio& parSize) { std::cout << "Autoregistering" << std::endl; m_tileCount = CountTilesInScreen(vector_cast(parSize.Resolution()), vector_cast(m_tileSize)); #if defined(WITH_DEBUG_VISUALS) m_screenRes = parSize.Resolution(); #endif #if !defined(NDEBUG) { m_screenRes = parSize.Resolution(); const ushort2 tileSize(vector_cast(m_tileSize)); const ushort2 screenRes(vector_cast(parSize.Resolution())); const ushort2 tileCount(vector_cast(m_tileCount)); assert(tileCount * tileSize < screenRes + tileSize); } #endif } } //namespace cloonel