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! --- src/main.cpp | 2 ++ src/renderer.cpp | 5 +++-- src/sprite.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++--- src/sprite.h | 14 ++++++++------ 4 files changed, 55 insertions(+), 11 deletions(-) (limited to 'src') 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