From 908f792db6fdc04fde4b48e8873767edd5d59cea Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 31 Jan 2021 12:08:02 -0500 Subject: Made use of new sprite sheet data! --- res/lucas.png | Bin 10009 -> 90846 bytes res/lucas_anim.txt | 66 ++++----- res/lucas_frames.txt | 403 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 2 + src/renderer.cpp | 5 +- src/sprite.cpp | 45 +++++- src/sprite.h | 14 +- 7 files changed, 491 insertions(+), 44 deletions(-) create mode 100644 res/lucas_frames.txt diff --git a/res/lucas.png b/res/lucas.png index c059f96..dd053e5 100644 Binary files a/res/lucas.png and b/res/lucas.png differ diff --git a/res/lucas_anim.txt b/res/lucas_anim.txt index 19b1d67..daef503 100644 --- a/res/lucas_anim.txt +++ b/res/lucas_anim.txt @@ -1,34 +1,34 @@ ../res/lucas.png -17,26 -still[down]: 1 -still[left]: 4 -still[down_left]: 7 -still[down_right]: 10 -still[up]: 13 -still[right]: 16 -still[up_left]: 19 -still[up_right]: 22 -walk[down]: 0,1,2 -walk[left]: 3,4,5 -walk[down_left]: 6,7,8 -walk[down_right]: 9,10,11 -walk[up]: 12,13,14 -walk[right]: 15,16,17 -walk[up_left]: 18,19,20 -walk[up_right]: 21,22,23 -crouch[down]: 24 -crouch[down_left]: 25 -crouch[left]: 26 -crouch[up_left]: 27 -crouch[up]: 28 -crouch[up_right]: 29 -crouch[right]: 30 -crouch[down_right]: 31 -run[down]: 32,33,34,35 -run[right]: 36,37,38,39 -run[down_right]: 40,41,42,43 -run[up_right]: 44,45,46,47 -run[up]: 48,49,50,51 -run[left]: 52,53,54,55 -run[up_left]: 56,57,58,59 -run[down_left]: 60,61,62,63 \ No newline at end of file +../res/lucas_frames.txt +still[down]: 0 +still[down_left]: 1 +still[left]: 2 +still[up_left]: 3 +still[up]: 4 +still[up_right]: 5 +still[right]: 6 +still[down_right]: 7 +walk[down]: 40,0,48 +walk[down_left]: 41,1,49 +walk[left]: 42,2,50 +walk[up_left]: 43,3,51 +walk[up]: 44,4,52 +walk[up_right]: 45,5,53 +walk[right]: 46,6,54 +walk[down_right]: 47,7,55 +crouch[down]: 107 +crouch[down_left]: 108 +crouch[left]: 109 +crouch[up_left]: 110 +crouch[up]: 111 +crouch[up_right]: 112 +crouch[right]: 113 +crouch[down_right]: 114 +run[down]: 67,75,83,91 +run[down_left]: 68,76,84,92 +run[left]: 69,77,85,93 +run[up_left]: 70,78,86,94 +run[up]: 71,79,87,95 +run[up_right]: 72,80,88,96 +run[right]: 73,81,89,97 +run[down_right]: 74,82,90,98 \ No newline at end of file diff --git a/res/lucas_frames.txt b/res/lucas_frames.txt new file mode 100644 index 0000000..e64d1e1 --- /dev/null +++ b/res/lucas_frames.txt @@ -0,0 +1,403 @@ +32,64 cell size +10 frames per row +399 frames + +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,9,32 +16,32,9,32 +16,32,9,32 +16,32,9,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,9,32 +16,32,9,32 +16,32,9,32 +16,32,9,32 +16,24,8,24 +16,24,9,24 +17,32,10,32 +16,24,8,24 +16,24,8,24 +16,24,9,24 +17,32,7,32 +16,24,8,24 +16,24,8,24 +16,24,9,24 +17,24,10,24 +16,24,8,24 +16,24,9,24 +16,24,9,24 +17,24,7,24 +16,24,8,24 +17,24,9,24 +16,24,9,24 +17,24,10,24 +16,24,8,24 +17,24,9,24 +16,24,9,24 +17,24,7,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,9,24 +16,24,9,24 +16,24,9,24 +16,24,9,24 +17,24,9,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +17,24,9,24 +16,24,9,24 +16,24,9,24 +16,24,9,24 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +17,32,9,32 +16,32,9,32 +16,32,9,32 +16,32,9,32 +16,24,8,24 +16,24,8,24 +16,24,7,24 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +17,32,9,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +17,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +17,24,9,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +17,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +16,24,8,24 +32,32,16,32 +32,32,16,32 +32,32,16,32 +32,32,16,32 +32,24,16,24 +32,24,16,24 +16,24,8,24 +16,32,8,33 +16,32,8,32 +16,32,8,31 +16,32,8,33 +16,32,8,32 +16,32,8,31 +17,32,9,33 +17,32,9,32 +17,32,9,31 +17,32,12,14 +17,32,12,14 +16,32,9,15 +16,32,9,15 +16,32,5,15 +16,32,5,15 +16,32,5,15 +16,32,5,15 +16,32,3,16 +16,32,3,16 +16,32,4,16 +16,32,4,16 +16,32,6,18 +16,32,6,18 +16,32,8,18 +16,32,8,18 +16,32,8,20 +16,32,8,20 +16,32,10,16 +16,32,10,16 +32,24,16,6 +32,24,20,10 +32,16,26,8 +32,24,22,21 +16,32,8,25 +32,24,10,21 +32,16,6,10 +32,24,12,10 +16,32,9,32 +32,32,17,32 +32,24,17,24 +16,24,9,24 +32,32,17,32 +17,32,9,32 +32,32,17,32 +16,32,8,29 +16,32,8,29 +16,32,8,29 +16,32,8,29 +16,24,8,21 +16,24,8,21 +16,32,8,29 +16,32,8,29 +16,32,8,29 +16,32,8,29 +16,32,8,29 +16,32,8,29 +16,32,8,29 +16,32,8,29 +16,32,8,29 +32,32,16,32 +32,32,16,29 +32,32,16,29 +32,32,16,29 +32,32,16,29 +32,32,16,32 +32,32,16,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,9,32 +16,32,9,32 +16,32,9,35 +16,32,9,35 +16,32,6,36 +16,32,6,36 +16,32,2,36 +16,32,2,36 +16,32,2,36 +16,32,2,36 +16,32,0,37 +16,32,0,37 +16,32,1,37 +16,32,1,37 +16,32,3,39 +16,32,3,39 +16,32,5,39 +16,32,5,39 +16,32,5,41 +16,32,5,41 +16,32,7,37 +16,32,7,37 +16,32,4,14 +16,32,4,14 +16,32,7,15 +16,32,7,15 +16,32,11,15 +16,32,11,15 +16,32,11,15 +16,32,11,15 +16,32,13,16 +16,32,13,16 +16,32,12,16 +16,32,12,16 +16,32,10,18 +16,32,10,18 +16,32,8,18 +16,32,8,18 +16,32,8,20 +16,32,8,20 +16,32,6,16 +16,32,6,16 +16,32,9,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +32,16,16,16 +16,32,10,28 +16,32,7,32 +16,32,8,32 +32,32,16,32 +32,32,16,32 +16,32,9,32 +32,32,9,32 +32,32,17,32 +32,32,17,32 +32,32,17,32 +16,32,8,32 +16,32,8,32 +16,32,9,32 +16,32,9,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,31 +16,32,8,32 +17,39,9,40 +17,54,9,56 +17,53,9,55 +17,64,9,70 +18,63,6,69 +17,64,7,70 +18,64,7,70 +17,64,9,70 +16,64,8,70 +17,63,9,69 +18,64,11,70 +16,63,9,68 +17,64,11,70 +16,63,12,69 +17,39,9,40 +17,38,9,39 +17,37,9,37 +16,40,8,40 +16,37,8,37 +17,53,9,77 +17,45,9,83 +17,36,9,86 +16,32,9,87 +16,23,8,86 +32,24,16,89 +17,24,8,94 +32,32,16,101 +17,24,8,95 +32,32,16,100 +32,24,17,89 +32,24,17,85 +32,24,17,79 +32,18,19,65 +32,16,19,60 +32,16,18,60 +32,16,18,15 +32,24,14,22 +16,24,8,24 +16,24,9,23 +16,24,10,24 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,24,8,24 +16,24,8,24 +18,32,11,32 +17,24,10,24 +18,32,11,32 +17,24,10,24 +16,32,11,32 +17,24,11,24 +16,32,11,32 +17,24,11,24 +16,32,13,32 +16,32,15,32 +16,32,17,33 +16,32,19,33 +17,24,21,26 +17,24,23,26 +17,24,25,27 +17,24,27,28 +16,32,29,36 +16,32,31,37 +16,32,33,38 +16,32,35,39 +16,32,37,39 +16,32,39,38 +16,32,41,37 +16,32,43,36 +16,32,43,35 +16,32,45,34 +16,32,46,33 +16,32,48,32 +16,32,50,30 +16,32,52,28 +16,32,53,25 +16,32,54,22 +16,32,55,20 +16,32,56,17 +17,32,57,11 +17,32,57,6 +17,32,59,1 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,32,8,32 +16,24,8,24 +16,32,8,32 +32,32,16,32 +32,32,16,32 +32,32,16,32 +32,32,16,32 +32,32,16,32 +8,8,4,8 +8,8,4,8 +8,8,3,8 +16,32,8,32 +32,32,16,32 +32,32,16,32 +32,32,16,32 +16,24,8,18 +16,24,8,18 +16,24,8,18 +16,24,8,18 +16,24,8,18 +16,32,8,26 +16,32,8,26 +16,32,8,26 +16,32,8,26 +16,32,8,26 +16,32,8,26 +16,32,8,26 +16,32,8,26 +16,32,8,26 +32,32,16,26 +32,32,16,26 +32,32,16,26 +32,32,16,26 +32,32,16,26 +32,32,16,26 +32,32,16,26 +16,32,8,26 +32,16,16,12 +16,32,8,32 +16,32,9,32 +17,32,8,32 +16,24,10,26 +16,24,10,26 +16,24,11,26 +16,24,11,26 +16,31,10,33 +16,32,9,34 +16,32,8,34 +16,24,9,25 +16,24,9,24 +16,32,8,32 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) { int lucasSprite = game.addSprite(Sprite("../res/lucas_anim.txt", renderer)); int lucasSprite2 = game.addSprite(Sprite("../res/lucas_anim.txt", renderer)); + game.moveSprite(lucasSprite, {32,32}); + Party party; party.addMember(game, lucasSprite); 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) { SDL_RenderClear(ren_.get()); for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { - SDL_Rect src { sprite.getFrame() * sprite.size().w(), 0, sprite.size().w(), sprite.size().h() }; - SDL_Rect dest { sprite.loc().x(), sprite.loc().y(), sprite.size().w(), sprite.size().h() }; + const SpriteFrame& frame = sprite.getFrame(); + const SDL_Rect& src = frame.srcRect; + SDL_Rect dest { sprite.loc().x() - frame.center.x(), sprite.loc().y() - frame.center.y(), frame.size.w(), frame.size.h() }; SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), &src, &dest); } 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) { } char ch; + std::string line; std::string imagename; datafile >> imagename; textureId_ = renderer.loadImageFromFile(imagename); - datafile >> size_.w(); - datafile >> ch; //, - datafile >> size_.h(); + 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> 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 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 @@ #include "renderer.h" #include "vector.h" +struct SpriteFrame { + SDL_Rect srcRect; + vec2i center; + vec2i size; +}; + class Sprite { public: @@ -21,11 +27,7 @@ public: vec2i& loc() { return loc_; } - const vec2i& size() const { return size_; } - - vec2i& size() { return size_; } - - int getFrame() const { return animations_[curAnim_][curFrame_]; } + const SpriteFrame& getFrame() const { return frames_.at(animations_[curAnim_][curFrame_]); } void setDirection(Direction dir); @@ -41,11 +43,11 @@ private: int textureId_; vec2i loc_ { 0, 0 }; - vec2i size_; Direction curDir_ = Direction::down; std::string state_; int curAnim_ = 0; int curFrame_ = 0; + std::vector frames_; std::vector> animations_; std::map> stateDirToAnim_; }; -- cgit 1.4.1