Space Fighter
A "shmup" game for Computer Programming C++
Loading...
Searching...
No Matches
Animation.cpp
Go to the documentation of this file.
1
2/*
3 ██╗ ██╗ █████╗ ████████╗ █████╗ ███╗ ██╗ █████╗
4 ██║ ██╔╝ ██╔══██╗ ╚══██╔══╝ ██╔══██╗ ████╗ ██║ ██╔══██╗
5 █████╔╝ ███████║ ██║ ███████║ ██╔██╗ ██║ ███████║
6 ██╔═██╗ ██╔══██║ ██║ ██╔══██║ ██║╚██╗██║ ██╔══██║
7 ██║ ██╗ ██║ ██║ ██║ ██║ ██║ ██║ ╚████║ ██║ ██║
8 ╚═╝ ╚═╝ ╚═╝ ╚═╝/\ ╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═══╝ ╚═╝ ╚═╝
9 /vvvvvvvvvvvvvvvvvvv \=========================================,
10 `^^^^^^^^^^^^^^^^^^^ /---------------------------------------"
11 Katana Engine \/ © 2012 - Shuriken Studios LLC
12 http://www.shurikenstudios.com
13*/
14
15#include "KatanaEngine.h"
16
17namespace KatanaEngine
18{
20 {
21 m_secondsPerFrame = 0.6f;
22 m_currentFrameTime = m_secondsPerFrame;
23 m_currentIndex = 0;
24 m_loopCounter = -1;
25 m_isPlaying = true;
26 }
27
28
30 {
31 std::vector<Region *>::iterator it;
32 for (it = m_frames.begin(); it != m_frames.end(); ++it)
33 {
34 delete *it;
35 }
36 m_frames.clear();
37 }
38
39
40 void Animation::Update(const GameTime& gameTime)
41 {
42 if (m_isPlaying)
43 {
44 m_currentFrameTime -= gameTime.GetElapsedTime();
45
46 if (m_currentFrameTime <= 0)
47 {
48 m_currentIndex++;
49 m_currentFrameTime = m_secondsPerFrame;
50
51 if (m_currentIndex == m_frames.size())
52 {
53 if (m_loopCounter > 0) m_loopCounter--;
54 else if (m_loopCounter == 0) Stop();
55 else m_currentIndex = 0;
56 }
57 }
58 }
59 }
60
61
62 bool Animation::Load(const std::string &path, ResourceManager *pManager)
63 {
64 std::ifstream fileIn(path.c_str(), std::ifstream::in);
65
66 if (fileIn.is_open() && fileIn.good())
67 {
68 std::string line;
69 std::vector<std::string> splitElements;
70
71 bool loadingSpriteSheet = true;
72 bool loadingFrameTime = true;
73
74 while (getline(fileIn, line))
75 {
76 ParseComments(line);
77 if (line.empty()) continue;
78
79 if (loadingSpriteSheet)
80 {
81 m_pTexture = pManager->Load<Texture>(line);
82 loadingSpriteSheet = false;
83 }
84 else if (loadingFrameTime)
85 {
86 m_secondsPerFrame = atof(line.c_str());
87 loadingFrameTime = false;
88 }
89 else // loading frames
90 {
91 Split(line, ',', splitElements);
92
93 Region *frame = new Region;
94 frame->X = atoi(splitElements[0].c_str());
95 frame->Y = atoi(splitElements[1].c_str());
96 frame->Width = atoi(splitElements[2].c_str());
97 frame->Height = atoi(splitElements[3].c_str());
98 m_frames.push_back(frame);
99 }
100 }
101
102 fileIn.close();
103 }
104 else
105 {
106 return false;
107 }
108
109 return true;
110 }
111
112
113 void Animation::SetCurrentFrame(const unsigned int index)
114 {
115 if (index < m_frames.size())
116 {
117 m_currentIndex = index;
118 m_currentFrameTime = m_secondsPerFrame;
119 }
120 }
121
122
124 {
125 Pause();
127 }
128
130 {
131 Animation *clone = new Animation;
132
133 clone->m_pTexture = m_pTexture;
134 clone->m_frames = m_frames;
135 clone->m_secondsPerFrame = m_secondsPerFrame;
136 clone->m_isPlaying = m_isPlaying;
137 clone->m_currentFrameTime = m_currentFrameTime;
138 clone->m_currentIndex = m_currentIndex;
139
140 return clone;
141 }
142}
Represents timing and framing values for texture animations.
Definition Animation.h:21
virtual void SetCurrentFrame(const unsigned int index)
Sets the current frame of the animation.
virtual void Pause()
Pauses the animation.
Definition Animation.h:82
virtual void Update(const GameTime &gameTime)
Updates the animation.
Definition Animation.cpp:40
virtual Resource * Clone()
Used to create a clone of the animation.
virtual bool Load(const std::string &path, ResourceManager *pManager)
Load the desired animation into memory.
Definition Animation.cpp:62
virtual void Stop()
Stops the animation.
Contains timing values for game updates and rendering.
Definition GameTime.h:21
double GetElapsedTime() const
Gets the time in seconds since last frame.
Definition GameTime.h:30
Defines a rectangular region defined by a point, height, width.
Definition Region.h:21
int X
The left side of the region.
Definition Region.h:132
int Height
The height of the region.
Definition Region.h:135
int Y
The top of the region.
Definition Region.h:133
int Width
The width of the region.
Definition Region.h:134
Base class for all resource types to be managed by the ResourceManager class.
Definition Resource.h:23
void Split(const std::string &line, const char delimeter, std::vector< std::string > &elements)
Splits a string into a vector of strings.
Definition Resource.cpp:19
void ParseComments(std::string &line)
Removes c-style, single-line comments from a line of text.
Definition Resource.cpp:34
Loads and manages the lifespan of objects from external files.
T * Load(const std::string &path, const bool cache=true, const bool appendContentPath=true)
Load and manage a resource.
Represents a 2D grid of texels.
Definition Texture.h:21
Katana Engine is a library of classes, interfaces, and value types that provides a foundation for dev...
Definition Animation.cpp:18