Click here to Skip to main content
15,892,199 members
Articles / Desktop Programming / Win32

TetroGL: An OpenGL Game Tutorial in C++ for Win32 Platforms - Part 2

Rate me:
Please Sign up or sign in to vote.
4.95/5 (36 votes)
29 Mar 2009CPOL28 min read 202.9K   5.6K   95  
Learn how to load images, display them on the screen and manage them efficiently and how to display animations.
#ifndef _ANIMATEDSPRITE_H_
#define _ANIMATEDSPRITE_H_

#include "Image.h"
#include "ImageList.h"
#include <string>
#include <map>
#include <list>

// This class represent an animated sprite: it is able to play
// different animations that were previously loaded.
class CAnimatedSprite
{
public:
	// Default constructor and destructor.
	CAnimatedSprite();
	~CAnimatedSprite();

	// Adds a new animation for the sprite. The strAnimName 
	// is a string that identifies the animation and should 
	// be unique for this sprite.
	void AddAnimation(const std::string& strAnimName, 
					  const CImageList& lstAnimation);
	// Plays a previously loaded animation. The strAnimName
	// is the name that was passed when calling AddAnimation.
	void PlayAnimation(const std::string& strAnimName);

	// Draw the current frame of the animation at the sprite
	// current position.
	void DrawSprite();
	// Go to the next animation frame.
	void NextFrame();

	// Set the position of the sprite
	void SetPosition(int XPos, int YPos) 
	{
		m_iXPos = XPos;
		m_iYPos = YPos;
	}
	// Move the sprite from its current position
	void OffsetPosition(int XOffset, int YOffset) 
	{
		m_iXPos += XOffset;
		m_iYPos += YOffset;
	}

private:
	typedef std::map<std::string, CImageList> TAnimMap;
	typedef TAnimMap::iterator TAnimMapIter;

	// Map containing all the animations that can be 
	// played.
	TAnimMap m_mapAnimations;
	// Iterator to the current animation being played
	TAnimMapIter  m_iterCurrentAnim;

	// Position of the sprite
	int m_iXPos;
	int m_iYPos;
};

#endif  // _ANIMATEDSPRITE_H_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Belgium Belgium
I am a 29 years old guy and I live with my girlfriend in Hoegaarden, little city from Belgium well known for its white beer Smile | :) .
I studied as an industrial engineer in electronics but I oriented myself more towards software development when I started to work.
Currently I am working in a research centre in mechatronica. I mainly develop in C++ but I also do a bit of Java.
When I have so spare time, I like to read (mainly fantasy) and play electric guitar.

Comments and Discussions