summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.cpp2
-rw-r--r--src/renderer.cpp5
-rw-r--r--src/sprite.cpp45
-rw-r--r--src/sprite.h14
4 files changed, 55 insertions, 11 deletions
diff --git a/src/main.cpp b/src/main.cpp index f9db18d..65b322e 100644 --- a/src/main.cpp +++ b/src/main.cpp
@@ -12,6 +12,8 @@ void loop(Renderer& renderer) {
12 int lucasSprite = game.addSprite(Sprite("../res/lucas_anim.txt", renderer)); 12 int lucasSprite = game.addSprite(Sprite("../res/lucas_anim.txt", renderer));
13 int lucasSprite2 = game.addSprite(Sprite("../res/lucas_anim.txt", renderer)); 13 int lucasSprite2 = game.addSprite(Sprite("../res/lucas_anim.txt", renderer));
14 14
15 game.moveSprite(lucasSprite, {32,32});
16
15 Party party; 17 Party party;
16 party.addMember(game, lucasSprite); 18 party.addMember(game, lucasSprite);
17 party.addMember(game, lucasSprite2); 19 party.addMember(game, lucasSprite2);
diff --git a/src/renderer.cpp b/src/renderer.cpp index 18b8ed8..931e699 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -49,8 +49,9 @@ void Renderer::render(Game& game) {
49 SDL_RenderClear(ren_.get()); 49 SDL_RenderClear(ren_.get());
50 50
51 for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { 51 for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) {
52 SDL_Rect src { sprite.getFrame() * sprite.size().w(), 0, sprite.size().w(), sprite.size().h() }; 52 const SpriteFrame& frame = sprite.getFrame();
53 SDL_Rect dest { sprite.loc().x(), sprite.loc().y(), sprite.size().w(), sprite.size().h() }; 53 const SDL_Rect& src = frame.srcRect;
54 SDL_Rect dest { sprite.loc().x() - frame.center.x(), sprite.loc().y() - frame.center.y(), frame.size.w(), frame.size.h() };
54 SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), &src, &dest); 55 SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), &src, &dest);
55 } 56 }
56 57
diff --git a/src/sprite.cpp b/src/sprite.cpp index c52807a..b84f4ce 100644 --- a/src/sprite.cpp +++ b/src/sprite.cpp
@@ -12,14 +12,53 @@ Sprite::Sprite(std::string_view filename, Renderer& renderer) {
12 } 12 }
13 13
14 char ch; 14 char ch;
15 std::string line;
15 16
16 std::string imagename; 17 std::string imagename;
17 datafile >> imagename; 18 datafile >> imagename;
18 textureId_ = renderer.loadImageFromFile(imagename); 19 textureId_ = renderer.loadImageFromFile(imagename);
19 20
20 datafile >> size_.w(); 21 std::string framefilename;
21 datafile >> ch; //, 22 datafile >> framefilename;
22 datafile >> size_.h(); 23
24 std::ifstream framefile(framefilename);
25 if (!framefile.is_open()) {
26 throw std::invalid_argument("Could not find frame datafile: " + framefilename);
27 }
28
29 vec2i cellSize;
30 framefile >> cellSize.w();
31 framefile >> ch; //,
32 framefile >> cellSize.h();
33 std::getline(framefile, line); // cell size
34
35 int framesPerRow;
36 framefile >> framesPerRow;
37 std::getline(framefile, line); // frames per row
38
39 int numFrames;
40 framefile >> numFrames;
41 std::getline(framefile, line); // frames
42 std::getline(framefile, line); // blank
43
44 for (int i=0; i<numFrames; i++) {
45 SpriteFrame f;
46 framefile >> f.size.w();
47 framefile >> ch; //,
48 framefile >> f.size.h();
49 framefile >> ch; //,
50 framefile >> f.center.x();
51 framefile >> ch; //,
52 framefile >> f.center.y();
53 std::getline(framefile, line); // blank
54
55 f.srcRect.x = (i % framesPerRow) * cellSize.w();
56 f.srcRect.y = (i / framesPerRow) * cellSize.h();
57 f.srcRect.w = f.size.w();
58 f.srcRect.h = f.size.h();
59
60 frames_.push_back(std::move(f));
61 }
23 62
24 std::string animLine; 63 std::string animLine;
25 std::getline(datafile, animLine); // blank 64 std::getline(datafile, animLine); // blank
diff --git a/src/sprite.h b/src/sprite.h index 82ea90c..dc26fad 100644 --- a/src/sprite.h +++ b/src/sprite.h
@@ -8,6 +8,12 @@
8#include "renderer.h" 8#include "renderer.h"
9#include "vector.h" 9#include "vector.h"
10 10
11struct SpriteFrame {
12 SDL_Rect srcRect;
13 vec2i center;
14 vec2i size;
15};
16
11class Sprite { 17class Sprite {
12public: 18public:
13 19
@@ -21,11 +27,7 @@ public:
21 27
22 vec2i& loc() { return loc_; } 28 vec2i& loc() { return loc_; }
23 29
24 const vec2i& size() const { return size_; } 30 const SpriteFrame& getFrame() const { return frames_.at(animations_[curAnim_][curFrame_]); }
25
26 vec2i& size() { return size_; }
27
28 int getFrame() const { return animations_[curAnim_][curFrame_]; }
29 31
30 void setDirection(Direction dir); 32 void setDirection(Direction dir);
31 33
@@ -41,11 +43,11 @@ private:
41 43
42 int textureId_; 44 int textureId_;
43 vec2i loc_ { 0, 0 }; 45 vec2i loc_ { 0, 0 };
44 vec2i size_;
45 Direction curDir_ = Direction::down; 46 Direction curDir_ = Direction::down;
46 std::string state_; 47 std::string state_;
47 int curAnim_ = 0; 48 int curAnim_ = 0;
48 int curFrame_ = 0; 49 int curFrame_ = 0;
50 std::vector<SpriteFrame> frames_;
49 std::vector<std::vector<int>> animations_; 51 std::vector<std::vector<int>> animations_;
50 std::map<std::string, std::map<Direction, int>> stateDirToAnim_; 52 std::map<std::string, std::map<Direction, int>> stateDirToAnim_;
51}; 53};