Skip to content

Commit

Permalink
Draw texture region directly with SpriteBatch
Browse files Browse the repository at this point in the history
  • Loading branch information
mogemimi committed Sep 5, 2018
1 parent bde1d53 commit c60450f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
19 changes: 19 additions & 0 deletions include/Pomdog/Experimental/Graphics/SpriteBatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Pomdog {

class AssetManager;
struct TextureRegion;

// NOTE: SpriteBatch uses the Cartesian coordinate system in which sprite is drawn.
// Also the `originPivot` represents a anchor point of sprite.
Expand Down Expand Up @@ -92,6 +93,24 @@ class POMDOG_EXPORT SpriteBatch final {
const Vector2& originPivot,
const Vector2& scale);

void Draw(
const std::shared_ptr<Texture2D>& texture,
const Vector2& position,
const TextureRegion& textureRegion,
const Color& color,
const Radian<float>& rotation,
const Vector2& originPivot,
float scale);

void Draw(
const std::shared_ptr<Texture2D>& texture,
const Vector2& position,
const TextureRegion& textureRegion,
const Color& color,
const Radian<float>& rotation,
const Vector2& originPivot,
const Vector2& scale);

void End();

int GetDrawCallCount() const noexcept;
Expand Down
56 changes: 56 additions & 0 deletions src/Experimental/Graphics/SpriteBatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "Pomdog/Content/AssetBuilders/PipelineStateBuilder.hpp"
#include "Pomdog/Content/AssetBuilders/ShaderBuilder.hpp"
#include "Pomdog/Content/AssetManager.hpp"
#include "Pomdog/Experimental/TexturePacker/TextureRegion.hpp"
#include "Pomdog/Graphics/BlendDescription.hpp"
#include "Pomdog/Graphics/BufferUsage.hpp"
#include "Pomdog/Graphics/ConstantBuffer.hpp"
Expand Down Expand Up @@ -49,6 +50,31 @@ namespace {
#include "Shaders/HLSL.Embedded/SpriteBatch_VS.inc.hpp"
#include "Shaders/Metal.Embedded/SpriteBatch.inc.hpp"

Vector2 ComputeSpriteOffset(const TextureRegion& region, const Vector2& originPivot) noexcept
{
if ((region.Subrect.Width <= 0) || (region.Subrect.Height <= 0)) {
return Vector2::Zero;
}

POMDOG_ASSERT(region.Subrect.Width > 0);
POMDOG_ASSERT(region.Subrect.Height > 0);

const auto regionSize = Vector2{
static_cast<float>(region.Width),
static_cast<float>(region.Height)};

const auto baseOffset = regionSize * originPivot;

const auto w = static_cast<float>(region.Subrect.Width);
const auto h = static_cast<float>(region.Subrect.Height);

auto offset = Vector2{
static_cast<float>(region.XOffset),
regionSize.Y - (static_cast<float>(region.YOffset) + h)};
offset = (baseOffset - offset) / Vector2{w, h};
return offset;
}

} // unnamed namespace

// MARK: - SpriteBatch::Impl
Expand Down Expand Up @@ -537,6 +563,36 @@ void SpriteBatch::Draw(
impl->Draw(texture, position, sourceRect, color, rotation, originPivot, scale, layerDepth);
}

void SpriteBatch::Draw(
const std::shared_ptr<Texture2D>& texture,
const Vector2& position,
const TextureRegion& textureRegion,
const Color& color,
const Radian<float>& rotation,
const Vector2& originPivot,
float scale)
{
POMDOG_ASSERT(impl);
auto offset = ComputeSpriteOffset(textureRegion, originPivot);
constexpr float layerDepth = 0.0f;
impl->Draw(texture, position, textureRegion.Subrect, color, rotation, offset, {scale, scale}, layerDepth);
}

void SpriteBatch::Draw(
const std::shared_ptr<Texture2D>& texture,
const Vector2& position,
const TextureRegion& textureRegion,
const Color& color,
const Radian<float>& rotation,
const Vector2& originPivot,
const Vector2& scale)
{
POMDOG_ASSERT(impl);
auto offset = ComputeSpriteOffset(textureRegion, originPivot);
constexpr float layerDepth = 0.0f;
impl->Draw(texture, position, textureRegion.Subrect, color, rotation, offset, scale, layerDepth);
}

int SpriteBatch::GetDrawCallCount() const noexcept
{
POMDOG_ASSERT(impl);
Expand Down

0 comments on commit c60450f

Please sign in to comment.