1295.5. The Image Class
spriteData.flipHorizontal = flip;
}
// Flip image vertically
virtual void flipVertical(bool flip)
{
spriteData.flipVertical = flip;
}
Listing 5.24. e images ip functions.
e next three functions are explained in more detail in Section 5.7 later in this chapter.
Let’s go over them briey here just to complete the code in the
Image class.
• e update function receives the current frameTime as a parameter. e update
function is where all code that needs to be synchronized with elapsed time should
be placed. e update function sets which frame from the texture image should be
displayed, and it calls setRect to select the current frame from the texture image.
We will use the update function to draw animated sprites.
• e setCurrentFrame function sets the current frame in a multiple-frame im-
age. is function may be used to change an animation sequence or select a dier-
ent texture to apply to the image from a multiple-image texture.
• e setRect function sets the spriteData.rect structure used to select the
rectangular region of the texture to use for the current sprite.
5.5.2 Image get Functions
A number of get functions are included in image.h (Listing 5.25).
// Return reference to SpriteData structure
const virtual SpriteData& getSpriteInfo() {return spriteData;}
// Return visible parameter
virtual bool getVisible() {return visible;}
// Return X position
virtual float getX() {return spriteData.x;}
// Return Y position
virtual float getY() {return spriteData.y;}
// Return scale factor
virtual float getScale() {return spriteData.scale;}
// Return width
virtual int getWidth() {return spriteData.width;}
// Return height
virtual int getHeight() {return spriteData.height;}
// Return center X
virtual float getCenterX() {return spriteData.x + spriteData.
width/2*getScale();}
// Return center Y
virtual float getCenterY() {return spriteData.y + spriteData.
130 5. Sprites and Animation
width/2*getScale();}
// Return rotation angle in degrees
virtual float getDegrees() {return spriteData.angle*(180.0f/
(float)PI);}
// Return rotation angle in radians
virtual float getRadians() {return spriteData.angle;}
// Return delay between frames of animation
virtual float getFrameDelay() {return frameDelay;}
// Return number of starting frame
virtual int getStartFrame() {return startFrame;}
// Return number of ending frame
virtual int getEndFrame() {return endFrame;}
// Return number of current frame
virtual int getCurrentFrame() {return currentFrame;}
// Return RECT structure of Image
virtual RECT getSpriteDataRect() {return spriteData.rect;}
// Return state of animation complete
virtual bool getAnimationComplete() {return animComplete;}
// Return colorFilter
virtual COLOR_ARGB getColorFilter() {return colorFilter;}
Listing 5.25. e images get functions.
5.5.3 Image set Functions
A number of set functions are included in image.h (Listing 5.26).
// Set X location
virtual void setX(float newX) {spriteData.x = newX;}
// Set Y location
virtual void setY(float newY) {spriteData.y = newY;}
// Set scale
virtual void setScale(float s) {spriteData.scale = s;}
// Set rotation angle in degrees
// 0 degrees is up. Angles progress clockwise
virtual void setDegrees(float deg) {spriteData.angle = deg*((float)
PI/180.0f);}
// Set rotation angle in radians
// 0 radians is up. Angles progress clockwise
virtual void setRadians(float rad) {spriteData.angle = rad;}
// Set visible
virtual void setVisible(bool v) {visible = v;}
// Set delay between frames of animation
virtual void setFrameDelay(float d) {frameDelay = d;}
// Set starting and ending frames of animation
virtual void setFrames(int s, int e){startFrame = s; endFrame = e;}
// Set current frame of animation
virtual void setCurrentFrame(int c);
// Set spriteData.rect to draw currentFrame
virtual void setRect();
// Set spriteData.rect to r
virtual void setSpriteDataRect(RECT r) {spriteData.rect = r;}
// Set animation loop. lp = true to loop
virtual void setLoop(bool lp) {loop = lp;}
1315.6. Game Engine
// Set animation complete Boolean
virtual void setAnimationComplete(bool a) {animComplete = a;};
// Set color filter. (use WHITE for no change)
virtual void setColorFilter(COLOR_ARGB color) {colorFilter = color;}
Listing 5.26. e images set functions.
In addition to functions, the image.h le also contains the following properties:
• Graphics *graphics. A pointer to the games Graphics object will be saved
here.
• TextureManager *textureManager. A pointer to the textureManager ob-
ject will be saved here.
• SpriteData spriteData. A structure that contains the data required by the
Graphics::drawSprite function.
• COLOR_ARGB colorFilter. e color to apply as a lter when drawing the sprite
if FILTER is specied as the color parameter. e color constant WHITE is used for
no change.
• int cols. e number of columns (1 to n) in a multiframe sprite.
• int startFrame. e rst frame number of a multiframe animation sequence.
• int endFrame. e ending frame number of a multiframe animation sequence.
• int currentFrame. e current frame number of the animation sequence.
• double frameDelay. How long, in seconds, each frame of animation is displayed.
• double animTimer. e variable used to time the animation.
• HRESULT hr. Standard return type.
• bool loop. Set to true to make the animation repeat (the default). When false,
the animation will stay on the last frame and animComplete will be set to true.
• bool visible. true when the image is visible.
• bool initialized. true when the image has been successfully initialized.
• bool animComplete. true when a nonlooping animation sequence is complete.
5.6 Game Engine
Figure 5.7 contains the diagram of our game engine with the new additions.
132 5. Sprites and Animation
5.6.1 Drawing a Planet
With our new TextureManager and Image classes, it should be possible to draw a planet
with a transparent background.
We will need two TextureManager objects and two Image objects, one of each for the
background nebula and one of each for the planet. e objects are created in spacewar.h,
as highlighted in Listing 5.27.
// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// Chapter 5 spacewar.h v1.0
#ifndef _SPACEWAR_H // Prevent multiple definitions if this
#define _SPACEWAR_H // file is included in more than one place
#define WIN32_LEAN_AND_MEAN
#include "game.h"
#include "textureManager.h"
#include "image.h"
//========================================================================
// This class is the core of the game
//========================================================================
class Spacewar : public Game
{
private:
// Game items
TextureManager nebulaTexture; // Nebula texture
TextureManager planetTexture; // Planet texture
Input
Class
Graphics
Class
Game
Class
SpaceWar
Class
TextureManager
Class
Image
Class
SpriteDat a
Struct
input graphics
public
Game
nebulaTexture
planetTexture
textureManager
nebula
spriteData
planet
Figure 5.7. e game engine class diagram.
1335.6. Game Engine
Image planet; // Planet image
Image nebula; // Nebula image
public:
// Constructor
Spacewar();
// Destructor
virtual ~Spacewar();
// Initialize the game
void initialize(HWND hwnd);
void update(); // Must override pure virtual from Game
void ai(); // "
void collisions(); // "
void render(); // "
void releaseAll();
void resetAll();
};
#endif
Listing 5.27. Creating TextureManager and Image objects.
We add the necessary code to initialize the nebula and planet in the Spacewar::init
ialize
function (Listing 5.28).
Using values of 0 for width and height will apply the entire texture to the image when it is
drawn.
//========================================================================
// Initializes the game
// Throws GameError on error
//========================================================================
void Spacewar::initialize(HWND hwnd)
{
Game::initialize(hwnd); // Throws GameError
// Nebula texture
if (!nebulaTexture.initialize(graphics,NEBULA_IMAGE))
throw(GameError(gameErrorNS::FATAL_ERROR,
"Error initializing nebula texture"));
// Planet texture
if (!planetTexture.initialize(graphics,PLANET_IMAGE))
throw(GameError(gameErrorNS::FATAL_ERROR,
"Error initializing planet texture"));
// Nebula
if (!nebula.initialize(graphics,0,0,0,&nebulaTexture))
throw(GameError(gameErrorNS::FATAL_ERROR,
"Error initializing nebula"));
// Planet
if (!planet.initialize(graphics,0,0,0,&planetTexture))
throw(GameError(gameErrorNS::FATAL_ERROR,
"Error initializing planet"));
// Place planet in center of screen
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.96.247