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
|
#include "sprite.h"
#include <SDL_image.h>
#include <fstream>
#include <list>
#include <regex>
#include "util.h"
Sprite::Sprite(std::string_view filename, Renderer& renderer) {
std::ifstream datafile(filename.data());
if (!datafile.is_open()) {
throw std::invalid_argument(std::string("Could not find sprite datafile: ") + std::string(filename));
}
char ch;
std::string line;
std::string imagename;
datafile >> imagename;
textureId_ = renderer.loadImageFromFile(imagename);
std::string framefilename;
datafile >> framefilename;
std::ifstream framefile(framefilename);
if (!framefile.is_open()) {
throw std::invalid_argument("Could not find frame datafile: " + framefilename);
}
vec2i cellSize;
framefile >> cellSize.w();
framefile >> ch; //,
framefile >> cellSize.h();
std::getline(framefile, line); // cell size
int framesPerRow;
framefile >> framesPerRow;
std::getline(framefile, line); // frames per row
int numFrames;
framefile >> numFrames;
std::getline(framefile, line); // frames
std::getline(framefile, line); // blank
for (int i=0; i<numFrames; i++) {
SpriteFrame f;
framefile >> f.size.w();
framefile >> ch; //,
framefile >> f.size.h();
framefile >> ch; //,
framefile >> f.center.x();
framefile >> ch; //,
framefile >> f.center.y();
std::getline(framefile, line); // blank
f.srcRect.x = (i % framesPerRow) * cellSize.w();
f.srcRect.y = (i / framesPerRow) * cellSize.h();
f.srcRect.w = f.size.w();
f.srcRect.h = f.size.h();
frames_.push_back(std::move(f));
}
std::string animLine;
std::getline(datafile, animLine); // blank
while (std::getline(datafile, animLine)) {
std::regex re(R"(([a-z]+)\[([a-z_]+)\]: ([0-9,]+))");
std::smatch m;
std::regex_match(animLine, m, re);
std::vector<int> frames;
auto framestrs = splitStr<std::list<std::string>>(m[3], ",");
for (const std::string& f : framestrs) {
frames.push_back(std::stoi(f));
}
int animId = animations_.size();
animations_.push_back(std::move(frames));
Direction dir = directionFromString(std::string(m[2]));
stateDirToAnim_[m[1]][dir] = animId;
}
updateAnimation();
}
void Sprite::setDirection(Direction dir) {
if (curDir_ != dir) {
curDir_ = dir;
updateAnimation();
}
}
void Sprite::setState(std::string state) {
if (state_ != state) {
state_ = state;
updateAnimation();
}
}
void Sprite::updateAnimation() {
curAnim_ = stateDirToAnim_[state_][curDir_];
curFrame_ = 0;
}
void Sprite::tickAnim() {
curFrame_++;
if (curFrame_ >= animations_[curAnim_].size()) {
curFrame_ = 0;
}
}
|