WP8_platformer / Platformer_WP8 / AnimationPlayer.cpp

#include "pch.h"
#include "AnimationPlayer.h"

#include "DirectXHelper.h"
#include "Rectangle.h"

using namespace DirectX;

void AnimationPlayer::PlayAnimation(std::shared_ptr<Animation> theAnimation)
{
	// the same animation as the current one
	if (theAnimation == animation)
	{
		return;
	}

	animation = theAnimation;
	frameIndex = 0;
	time = 0.0f;
}

void AnimationPlayer::Draw(GameTime gameTime, std::shared_ptr<SpriteBatch> spriteBatch, XMFLOAT2 position, SpriteEffects effects)
{
	if (animation == NULL)
	{
		throw new std::exception("No animation is currently playing.");
	}

	// Process passing time.
	time += gameTime.DeltaTime;

	while(time > animation->GetFrameTime())
	{
		time -= animation->GetFrameTime();
		
		// Advance the frame index; looping or clamping as appropriate.
		if (animation->IsLooping())
		{
			frameIndex = (frameIndex + 1) % animation->GetFrameCount();
		}
		else
		{
			frameIndex = min(frameIndex + 1, animation->GetFrameCount() - 1);
		}
	}

	int textureHeight = animation->GetTexture().Description().Height;
	Rectangle source(frameIndex * textureHeight, 0, textureHeight, textureHeight);
	XMFLOAT2 origin = GetOrigin();
	RECT rect = source.ToRect();

	spriteBatch->Draw(animation->GetTexture().ResourceView, position, &rect, DirectX::Colors::White, 0.0f, GetOrigin(), 1.0f, effects);
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.