diff options
author | Star Rauchenberger <fefferburbia@gmail.com> | 2022-03-12 12:09:58 -0500 |
---|---|---|
committer | Star Rauchenberger <fefferburbia@gmail.com> | 2022-03-12 12:09:58 -0500 |
commit | be9ccb73bc20b03f62c77f5d529602a10ef4eda9 (patch) | |
tree | 6f88a9b780a75e52b2f04b6f61167f4ef46590a7 /src/animation.cpp | |
parent | c1a88a064a0cddbc1df2e716ef2102d22bbf681b (diff) | |
download | ether-be9ccb73bc20b03f62c77f5d529602a10ef4eda9.tar.gz ether-be9ccb73bc20b03f62c77f5d529602a10ef4eda9.tar.bz2 ether-be9ccb73bc20b03f62c77f5d529602a10ef4eda9.zip |
player has a sprite now thanks to world of solaria
Diffstat (limited to 'src/animation.cpp')
-rw-r--r-- | src/animation.cpp | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/animation.cpp b/src/animation.cpp new file mode 100644 index 0000000..fbf7ccf --- /dev/null +++ b/src/animation.cpp | |||
@@ -0,0 +1,93 @@ | |||
1 | #include "animation.h" | ||
2 | #include <string_view> | ||
3 | #include <fstream> | ||
4 | #include <stdexcept> | ||
5 | #include <string> | ||
6 | #include <regex> | ||
7 | #include <list> | ||
8 | #include "direction.h" | ||
9 | #include "util.h" | ||
10 | |||
11 | Animation::Animation(std::string_view path) { | ||
12 | std::ifstream datafile(path.data()); | ||
13 | if (!datafile.is_open()) { | ||
14 | throw std::invalid_argument(std::string("Could not find sprite datafile: ") + path.data()); | ||
15 | } | ||
16 | |||
17 | std::string animLine; | ||
18 | char ch; | ||
19 | int cellWidth; | ||
20 | int cellHeight; | ||
21 | datafile >> cellWidth; | ||
22 | datafile >> ch; //, | ||
23 | datafile >> cellHeight; | ||
24 | std::getline(datafile, animLine); // cell size | ||
25 | |||
26 | int framesPerRow; | ||
27 | datafile >> framesPerRow; | ||
28 | std::getline(datafile, animLine); // frames per row | ||
29 | |||
30 | int numFrames; | ||
31 | datafile >> numFrames; | ||
32 | std::getline(datafile, animLine); // frames | ||
33 | std::getline(datafile, animLine); // blank | ||
34 | |||
35 | for (int i=0; i<numFrames; i++) { | ||
36 | SDL_Rect srcRect; | ||
37 | srcRect.x = (i % framesPerRow) * cellWidth; | ||
38 | srcRect.y = (i / framesPerRow) * cellHeight; | ||
39 | srcRect.w = cellWidth; | ||
40 | srcRect.h = cellHeight; | ||
41 | frames_.push_back(srcRect); | ||
42 | } | ||
43 | |||
44 | while (std::getline(datafile, animLine)) { | ||
45 | std::regex re(R"(([a-z!._]+)\[([a-z_]+)\]: ([0-9,]+))"); | ||
46 | std::smatch m; | ||
47 | std::regex_match(animLine, m, re); | ||
48 | |||
49 | std::string animName = m[1]; | ||
50 | std::vector<int> anim; | ||
51 | auto framestrs = splitStr<std::list<std::string>>(m[3], ","); | ||
52 | for (const std::string& f : framestrs) { | ||
53 | anim.push_back(std::stoi(f)); | ||
54 | } | ||
55 | |||
56 | int animId = animations_.size(); | ||
57 | animations_.push_back(std::move(anim)); | ||
58 | |||
59 | Direction dir = directionFromString(std::string(m[2])); | ||
60 | nameDirToAnim_[animName][dir] = animId; | ||
61 | } | ||
62 | |||
63 | updateAnim(); | ||
64 | } | ||
65 | |||
66 | void Animation::setDirection(Direction dir) { | ||
67 | dir_ = dir; | ||
68 | updateAnim(); | ||
69 | } | ||
70 | |||
71 | void Animation::setAnimation(std::string_view anim) { | ||
72 | animationName_ = anim; | ||
73 | updateAnim(); | ||
74 | } | ||
75 | |||
76 | void Animation::update(int dt) { | ||
77 | animTimer_.accumulate(dt); | ||
78 | |||
79 | while (animTimer_.step()) { | ||
80 | animationFrame_++; | ||
81 | |||
82 | if (animationFrame_ >= animations_.at(animationId_).size()) { | ||
83 | animationFrame_ = 0; | ||
84 | } | ||
85 | } | ||
86 | } | ||
87 | |||
88 | void Animation::updateAnim() { | ||
89 | if (nameDirToAnim_.at(animationName_).at(dir_) != animationId_) { | ||
90 | animationId_ = nameDirToAnim_.at(animationName_).at(dir_); | ||
91 | animationFrame_ = 0; | ||
92 | } | ||
93 | } | ||