/*
 * Copyright (C) 2020-2021 Aloysius Indrayanto
 *
 * This file is part of the eCxx library, see LICENSE file for the license details.
 */


#ifndef INCLUDE_ECXX_CTL_GFX_SPRITE_SHEET_2D_TPP
#error "this file cannot be included directly"
#endif
#undef INCLUDE_ECXX_CTL_GFX_SPRITE_SHEET_2D_TPP


#ifndef ECXX_CTL_GFX_SPRITE_SHEET_2D_TPP
#define ECXX_CTL_GFX_SPRITE_SHEET_2D_TPP


namespace eCxx {


//
// A simple 2D sprite sheet
//
template <typename ColorT, CBP::SUInt SpriteXCountN, CBP::SUInt SpriteYCountN, CBP::SUInt SpriteXSizeN, CBP::SUInt SpriteYSizeN, typename PGMPointerT>
inline SpriteSheet2D<ColorT, SpriteXCountN, SpriteYCountN, SpriteXSizeN, SpriteYSizeN, PGMPointerT>
::SpriteSheet2D(PGMPointerType ssData)
: _ssData(ssData           )
, _ssIt  (PGMPointerT::null)
{}


template <typename ColorT, CBP::SUInt SpriteXCountN, CBP::SUInt SpriteYCountN, CBP::SUInt SpriteXSizeN, CBP::SUInt SpriteYSizeN, typename PGMPointerT>
inline void SpriteSheet2D<ColorT, SpriteXCountN, SpriteYCountN, SpriteXSizeN, SpriteYSizeN, PGMPointerT>
::obtainSCDBegin(CBP::SUInt xIndex, CBP::SUInt yIndex) const
{
    const CBP::DUInt spriteIndex = static_cast<CBP::DUInt>(yIndex) * static_cast<CBP::DUInt>(XCount)
                                 + static_cast<CBP::DUInt>(xIndex);

    _ssIt.p = _ssData.p + spriteIndex * SpriteByteSize;
}


template <typename ColorT, CBP::SUInt SpriteXCountN, CBP::SUInt SpriteYCountN, CBP::SUInt SpriteXSizeN, CBP::SUInt SpriteYSizeN, typename PGMPointerT>
inline const typename SpriteSheet2D<ColorT, SpriteXCountN, SpriteYCountN, SpriteXSizeN, SpriteYSizeN, PGMPointerT>
::ColorType SpriteSheet2D<ColorT, SpriteXCountN, SpriteYCountN, SpriteXSizeN, SpriteYSizeN, PGMPointerT>::obtainSCDGet() const
{
    const ColorType& col = ColorType::fromPGMCharPointerRaw(_ssIt.p);

    _ssIt.p += ColorByteSize;

    return col;
}


} // namespace eCxx


#endif
