From 59dfd3da4d3e5d4a368a664c51c22bbdc8ff2ca4 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 13 Feb 2021 10:11:23 -0500 Subject: Added sprite shadows --- src/game.cpp | 5 +++++ src/map.cpp | 2 ++ src/map.h | 1 + src/renderer.cpp | 6 ++++++ src/sprite.h | 1 + 5 files changed, 15 insertions(+) (limited to 'src') diff --git a/src/game.cpp b/src/game.cpp index fcd5f44..d1e5b8e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -34,6 +34,7 @@ void Game::loadMap(std::string filename, std::string warpPoint, Direction dir) { getSystem().setUpCollision(lucasSprite, {-8, -8}, {12, 8}, true); getSystem().initSprite(lucasSprite, "../res/sprites/lucas_anim.txt"); getSystem().setSpriteDirection(lucasSprite, dir); + getSprite(lucasSprite).hasShadow = true; getSprite(lucasSprite).player = true; getSystem().initSprite(lucasSprite); @@ -41,18 +42,21 @@ void Game::loadMap(std::string filename, std::string warpPoint, Direction dir) { getSystem().initSprite(kumaSprite, warpLoc); getSystem().initSprite(kumaSprite, "../res/sprites/kuma_anim.txt"); getSystem().setSpriteDirection(kumaSprite, dir); + getSprite(kumaSprite).hasShadow = true; getSystem().addSpriteToParty(lucasSprite, kumaSprite); int dusterSprite = emplaceSprite("duster"); getSystem().initSprite(dusterSprite, warpLoc); getSystem().initSprite(dusterSprite, "../res/sprites/duster_anim.txt"); getSystem().setSpriteDirection(dusterSprite, dir); + getSprite(dusterSprite).hasShadow = true; getSystem().addSpriteToParty(lucasSprite, dusterSprite); int boneySprite = emplaceSprite("boney"); getSystem().initSprite(boneySprite, warpLoc); getSystem().initSprite(boneySprite, "../res/sprites/boney_anim.txt"); getSystem().setSpriteDirection(boneySprite, dir); + getSprite(boneySprite).hasShadow = true; getSystem().addSpriteToParty(lucasSprite, boneySprite); for (const Prototype& p : map_->getPrototypes()) { @@ -61,6 +65,7 @@ void Game::loadMap(std::string filename, std::string warpPoint, Direction dir) { getSystem().setUpCollision(spriteId, p.collisionOffset, p.collisionSize, true); if (!p.animationFilename.empty()) { getSystem().initSprite(spriteId, p.animationFilename); + getSprite(spriteId).hasShadow = p.shadow; } getSprite(spriteId).interactionScript = p.interactionScript; } diff --git a/src/map.cpp b/src/map.cpp index da382fd..3686bbd 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -74,6 +74,8 @@ Map::Map(std::string_view name) : name_(name) { p.animationFilename = property.getStringValue(); } else if (property.getName() == "interactionScript") { p.interactionScript = property.getStringValue(); + } else if (property.getName() == "shadow") { + p.shadow = property.getBoolValue(); } } diff --git a/src/map.h b/src/map.h index 2d448e4..7dbe613 100644 --- a/src/map.h +++ b/src/map.h @@ -23,6 +23,7 @@ struct Prototype { vec2i collisionSize; std::string animationFilename; std::string interactionScript; + bool shadow = false; }; struct Trigger { diff --git a/src/renderer.cpp b/src/renderer.cpp index 246b7fb..f07931b 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -122,8 +122,14 @@ void Renderer::render(Game& game) { SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); + int shadowTexId = loadImageFromFile("../res/shadow.png"); for (const Sprite& sprite : game.getSystem().getSpritesByY() | game.spriteView()) { if (sprite.isAnimated) { + if (sprite.hasShadow) { + const SDL_Rect shadowDest { sprite.loc.x() - 8, sprite.loc.y() - 8, 16, 8 }; + SDL_RenderCopy(ren_.get(), textures_.at(shadowTexId).get(), nullptr, &shadowDest); + } + const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).at(sprite.animationFrame)); 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() }; diff --git a/src/sprite.h b/src/sprite.h index 6ef2f57..84a7b03 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -50,6 +50,7 @@ public: std::vector frames; std::vector> animations; std::map> nameDirToAnim; + bool hasShadow = false; // Character bool orientable = false; -- cgit 1.4.1