-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprite.cpp
36 lines (34 loc) · 1 KB
/
sprite.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
#include "sprite.h"
#include <fstream>
#include <iostream>
#include <ncurses.h>
#include <sstream>
#include <string>
using namespace std;
const std::string SEPARATOR = "\n;\n";
const int MAX_FRAMES = 100;
Sprite::Sprite(const std::string filePath) {
spritePath = filePath;
index = 0;
ifstream spriteFile(spritePath, std::ios_base::binary);
cout << "Sprite file " << spritePath << " loaded.\n";
stringstream buffer;
buffer << spriteFile.rdbuf();
std::string allFrames = buffer.str();
int frameLength = allFrames.find(SEPARATOR);
while (frameLength != -1 && frameLength != 0) {
framesCache.push_back(allFrames.substr(0, frameLength));
allFrames = allFrames.substr(frameLength + SEPARATOR.length());
frameLength = allFrames.find(SEPARATOR);
}
framesCache.push_back(allFrames);
}
std::string Sprite::getNextFrame() {
if (index < framesCache.size()) {
std::string frame = framesCache[index];
index++;
return frame;
} else {
return framesCache[framesCache.size() - 1];
}
}