-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.cpp
189 lines (174 loc) · 7.09 KB
/
image.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
Group #: 3
Members: Chris Prosser, Jacob Gearhart, Kory Kappel,
Course: COMP 322, Advanced Programming
Date: 28 October 2013
Description: This file is inherited from the skeleton
provided by Charles Kelly.
*/
// Programming 2D Games
// Copyright (c) 2011 by:
// Charles Kelly
// Image.cpp v1.0
#include "image.h"
//=============================================================================
// default constructor
//=============================================================================
Image::Image()
{
initialized = false; // set true when successfully initialized
spriteData.width = 2;
spriteData.height = 2;
spriteData.x = 0.0;
spriteData.y = 0.0;
spriteData.scale = 1.0;
spriteData.angle = 0.0;
spriteData.rect.left = 0; // used to select one frame from multi-frame image
spriteData.rect.top = 0;
spriteData.rect.right = spriteData.width;
spriteData.rect.bottom = spriteData.height;
spriteData.texture = NULL; // the sprite texture (picture)
spriteData.flipHorizontal = false;
spriteData.flipVertical = false;
cols = 1;
textureManager = NULL;
startFrame = 0;
endFrame = 0;
currentFrame = 0;
frameDelay = 1.0; // default to 1 second per frame of animation
animTimer = 0.0;
visible = true; // the image is visible
loop = true; // loop frames
animComplete = false;
graphics = NULL; // link to graphics system
colorFilter = graphicsNS::WHITE; // WHITE for no change
}
//=============================================================================
// destructor
//=============================================================================
Image::~Image()
{}
//=============================================================================
// Initialize the Image.
// Post: returns true if successful, false if failed
// pointer to Graphics
// width of Image in pixels (0 = use full texture width)
// height of Image in pixels (0 = use full texture height)
// number of columns in texture (1 to n) (0 same as 1)
// pointer to TextureManager
//=============================================================================
bool Image::initialize(Graphics *g, int width, int height, int ncols,
TextureManager *textureM)
{
try{
graphics = g; // the graphics object
textureManager = textureM; // pointer to texture object
spriteData.texture = textureManager->getTexture();
if(width == 0)
width = textureManager->getWidth(); // use full width of texture
spriteData.width = width;
if(height == 0)
height = textureManager->getHeight(); // use full height of texture
spriteData.height = height;
cols = ncols;
if (cols == 0)
cols = 1; // if 0 cols use 1
// configure spriteData.rect to draw currentFrame
spriteData.rect.left = (currentFrame % cols) * spriteData.width;
// right edge + 1
spriteData.rect.right = spriteData.rect.left + spriteData.width;
spriteData.rect.top = (currentFrame / cols) * spriteData.height;
// bottom edge + 1
spriteData.rect.bottom = spriteData.rect.top + spriteData.height;
}
catch(...) {return false;}
initialized = true; // successfully initialized
return true;
}
//=============================================================================
// Draw the image using color as filter
// The color parameter is optional, WHITE is assigned as default in image.h
// Pre : spriteBegin() is called
// Post: spriteEnd() is called
//=============================================================================
void Image::draw(COLOR_ARGB color)
{
if (!visible || graphics == NULL)
return;
// get fresh texture incase onReset() was called
spriteData.texture = textureManager->getTexture();
if(color == graphicsNS::FILTER) // if draw with filter
graphics->drawSprite(spriteData, colorFilter); // use colorFilter
else
graphics->drawSprite(spriteData, color); // use color as filter
}
//=============================================================================
// Draw this image using the specified SpriteData.
// The current SpriteData.rect is used to select the texture.
// Pre : spriteBegin() is called
// Post: spriteEnd() is called
//=============================================================================
void Image::draw(SpriteData sd, COLOR_ARGB color)
{
if (!visible || graphics == NULL)
return;
sd.rect = spriteData.rect; // use this Images rect to select texture
sd.texture = textureManager->getTexture(); // get fresh texture incase onReset() was called
if(color == graphicsNS::FILTER) // if draw with filter
graphics->drawSprite(sd, colorFilter); // use colorFilter
else
graphics->drawSprite(sd, color); // use color as filter
}
//=============================================================================
// update
// typically called once per frame
// frameTime is used to regulate the speed of movement and animation
//=============================================================================
void Image::update(float frameTime)
{
if (endFrame - startFrame > 0) // if animated sprite
{
animTimer += frameTime; // total elapsed time
if (animTimer > frameDelay)
{
animTimer -= frameDelay;
currentFrame++;
if (currentFrame < startFrame || currentFrame > endFrame)
{
if(loop == true) // if looping animation
currentFrame = startFrame;
else // not looping animation
{
currentFrame = endFrame;
animComplete = true; // animation complete
}
}
setRect(); // set spriteData.rect
}
}
}
//=============================================================================
// Set the current frame of the image
//=============================================================================
void Image::setCurrentFrame(int c)
{
if(c >= 0)
{
currentFrame = c;
animComplete = false;
setRect(); // set spriteData.rect
}
}
//=============================================================================
// Set spriteData.rect to draw currentFrame
//=============================================================================
inline void Image::setRect()
{
// configure spriteData.rect to draw currentFrame
spriteData.rect.left = (currentFrame % cols) * spriteData.width;
// right edge + 1
spriteData.rect.right = spriteData.rect.left + spriteData.width;
spriteData.rect.top = (currentFrame / cols) * spriteData.height;
// bottom edge + 1
spriteData.rect.bottom = spriteData.rect.top + spriteData.height;
}