diff options
Diffstat (limited to 'src/sprite.cpp')
| -rw-r--r-- | src/sprite.cpp | 79 |
1 files changed, 76 insertions, 3 deletions
| diff --git a/src/sprite.cpp b/src/sprite.cpp index c8c4656..cc196ae 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp | |||
| @@ -1,7 +1,80 @@ | |||
| 1 | #include "sprite.h" | 1 | #include "sprite.h" |
| 2 | #include <SDL_image.h> | 2 | #include <SDL_image.h> |
| 3 | #include <fstream> | ||
| 4 | #include <list> | ||
| 5 | #include <regex> | ||
| 6 | #include "util.h" | ||
| 3 | 7 | ||
| 4 | Sprite::Sprite(std::string_view filename, Renderer& renderer, int width, int height) { | 8 | Sprite::Sprite(std::string_view filename, Renderer& renderer) { |
| 5 | textureId_ = renderer.loadImageFromFile(filename); | 9 | std::ifstream datafile(filename.data()); |
| 6 | size_ = { width*4, height*4 }; | 10 | if (!datafile.is_open()) { |
| 11 | throw std::invalid_argument(std::string("Could not find sprite datafile: ") + std::string(filename)); | ||
| 12 | } | ||
| 13 | |||
| 14 | char ch; | ||
| 15 | |||
| 16 | std::string imagename; | ||
| 17 | datafile >> imagename; | ||
| 18 | textureId_ = renderer.loadImageFromFile(imagename); | ||
| 19 | |||
| 20 | datafile >> size_.w(); | ||
| 21 | datafile >> ch; //, | ||
| 22 | datafile >> size_.h(); | ||
| 23 | |||
| 24 | std::string animLine; | ||
| 25 | std::getline(datafile, animLine); // blank | ||
| 26 | while (std::getline(datafile, animLine)) { | ||
| 27 | std::regex re(R"(([a-z]+)\[([a-z_]+)\]: ([0-9,]+))"); | ||
| 28 | std::smatch m; | ||
| 29 | std::regex_match(animLine, m, re); | ||
| 30 | |||
| 31 | std::vector<int> frames; | ||
| 32 | auto framestrs = splitStr<std::list<std::string>>(m[3], ","); | ||
| 33 | for (const std::string& f : framestrs) { | ||
| 34 | frames.push_back(std::stoi(f)); | ||
| 35 | } | ||
| 36 | |||
| 37 | int animId = animations_.size(); | ||
| 38 | animations_.push_back(std::move(frames)); | ||
| 39 | |||
| 40 | Direction dir = directionFromString(std::string(m[2])); | ||
| 41 | |||
| 42 | if (m[1] == "still") { | ||
| 43 | stillAnims_[dir] = animId; | ||
| 44 | } else { | ||
| 45 | walkingAnims_[dir] = animId; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | |||
| 49 | updateAnimation(); | ||
| 50 | } | ||
| 51 | |||
| 52 | void Sprite::setDirection(Direction dir) { | ||
| 53 | if (curDir_ != dir) { | ||
| 54 | curDir_ = dir; | ||
| 55 | updateAnimation(); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | |||
| 59 | void Sprite::setWalking(bool walking) { | ||
| 60 | if (isWalking_ != walking) { | ||
| 61 | isWalking_ = walking; | ||
| 62 | updateAnimation(); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | void Sprite::updateAnimation() { | ||
| 67 | if (isWalking_) { | ||
| 68 | curAnim_ = walkingAnims_[curDir_]; | ||
| 69 | } else { | ||
| 70 | curAnim_ = stillAnims_[curDir_]; | ||
| 71 | } | ||
| 72 | curFrame_ = 0; | ||
| 73 | } | ||
| 74 | |||
| 75 | void Sprite::tickAnim() { | ||
| 76 | curFrame_++; | ||
| 77 | if (curFrame_ >= animations_[curAnim_].size()) { | ||
| 78 | curFrame_ = 0; | ||
| 79 | } | ||
| 7 | } \ No newline at end of file | 80 | } \ No newline at end of file |
