From 8cebec868496e0b678541a3784adcbb2fd5a4ebd Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Sat, 8 Nov 2025 22:53:44 -0500 Subject: [PATCH] add base support for entity animation and drawing --- src/Entity.cpp | 76 ++++++++++++++++++++++++-------------------------- src/Entity.h | 31 +++++++++++++------- src/Game.cpp | 9 +++++- 3 files changed, 66 insertions(+), 50 deletions(-) diff --git a/src/Entity.cpp b/src/Entity.cpp index f98a4e3..5252651 100644 --- a/src/Entity.cpp +++ b/src/Entity.cpp @@ -7,52 +7,50 @@ #include #include #include +#include -//mainly derived from https://www.sfml-dev.org/tutorials/3.0/graphics/vertex-array/#example-tile-map -bool Entity::loadTexture(const std::filesystem::path& texturePath, sf::Vector2u graphicTileSize, int tiles) { - if(!texture.loadFromFile(texturePath)) - return false; +void Entity::setAnimationTiles(sf::Texture& textureSheet, + sf::Vector2i pixelLocation, + const std::string& animationName, + sf::Vector2i tileSize, + unsigned int animationTiles, + unsigned int pixelGap) { + this->tileSize = tileSize; - //assumes tiles are animation tiles of one row graphicTileSize height and tiles width - int height = 1; - int width = height * tiles; - int doubleTriVertex = 6; + EntityAnimation entityAnimation; + entityAnimation.name = animationName; - //width * height * 2x triangle vertex - vertexArray.setPrimitiveType(sf::PrimitiveType::Triangles); - vertexArray.resize(width * height * doubleTriVertex); + for (int i = 0; i < animationTiles; i++) { + sf::Vector2i tilePixelLocation(pixelLocation.x + (i * (pixelGap + tileSize.x)), pixelLocation.y); + sf::IntRect boundingRect(tilePixelLocation, tileSize); + sf::Sprite sprite(textureSheet, boundingRect); - for (size_t i = 0; i < width; ++i) { - for (size_t j = 0; j < height; ++j) { - const int tileNumber = i; - const int tu = tileNumber % (texture.getSize().x / graphicTileSize.x); - const int tv = tileNumber / (texture.getSize().x / graphicTileSize.x); - - sf::Vertex* triangles = &vertexArray[(i + j * width) * doubleTriVertex]; - - triangles[0].position = sf::Vector2f(i * graphicTileSize.x, j * graphicTileSize.y); - triangles[1].position = sf::Vector2f((i + 1) * graphicTileSize.x, j * graphicTileSize.y); - triangles[2].position = sf::Vector2f(i * graphicTileSize.x, (j + 1) * graphicTileSize.y); - triangles[3].position = sf::Vector2f(i * graphicTileSize.x, (j + 1) * graphicTileSize.y); - triangles[4].position = sf::Vector2f((i + 1) * graphicTileSize.x, j * graphicTileSize.y); - triangles[5].position = sf::Vector2f((i + 1) * graphicTileSize.x, (j + 1) * graphicTileSize.y); - - triangles[0].texCoords = sf::Vector2f(tu * graphicTileSize.x, tv * graphicTileSize.y); - triangles[1].texCoords = sf::Vector2f((tu + 1) * graphicTileSize.x, tv * graphicTileSize.y); - triangles[2].texCoords = sf::Vector2f(tu * graphicTileSize.x, (tv + 1) * graphicTileSize.y); - triangles[3].texCoords = sf::Vector2f(tu * graphicTileSize.x, (tv + 1) * graphicTileSize.y); - triangles[4].texCoords = sf::Vector2f((tu + 1) * graphicTileSize.x, tv * graphicTileSize.y); - triangles[5].texCoords = sf::Vector2f((tu + 1) * graphicTileSize.x, (tv + 1) * graphicTileSize.y); - } + entityAnimation.sprites.push_back(sprite); } - return true; -} + animations.push_back(entityAnimation); +}; -void Entity::draw(sf::RenderTarget& target, sf::RenderStates states) const { - states.transform *= getTransform(); - states.texture = &texture; - target.draw(vertexArray, states); +void Entity::setActiveSprite(const std::string& animationName, int animationTile) { + for (auto& animation : animations) { + if (animation.name == animationName) { + activeSprite = &animation.sprites.at(animationTile); + return; + } + } }; + + + + + + + + + + + + + diff --git a/src/Entity.h b/src/Entity.h index fab8f3e..3bf497e 100644 --- a/src/Entity.h +++ b/src/Entity.h @@ -1,16 +1,21 @@ #ifndef ENTITY_H #define ENTITY_H +#include "MazeMap.h" #include #include #include #include #include +#include #include #include #include #include #include +#include +#include +#include enum class MovementDir { STATIC, @@ -20,23 +25,29 @@ enum class MovementDir { RIGHT }; -class Entity : public sf::Drawable, public sf::Transformable { +struct EntityAnimation { + std::string name; + std::vector sprites; +}; + +class Entity { public: - //Entity(TileMap& map) : map(map) {}; + Entity() : movementSpeed(5) {}; - bool loadTexture(const std::filesystem::path& texturePath, sf::Vector2u graphicTileSize, int tiles); + void setAnimationTiles(sf::Texture& textureSheet, sf::Vector2i pixelLocation, const std::string& animationName, sf::Vector2i tileSize, unsigned int animationTiles, unsigned int pixelGap); + + sf::Sprite* getActiveSprite() { return activeSprite; }; + + void setActiveSprite(const std::string& animationName, int animationTile); - void setTextureFrame(const sf::IntRect& rectangle); protected: - //TileMap map; + sf::Vector2i tileSize; - sf::VertexArray vertexArray; - sf::Texture texture; + std::vector animations; - int animationTiles; - int movementSpeed = 5; + sf::Sprite* activeSprite; - void draw(sf::RenderTarget& target, sf::RenderStates states) const override; + int movementSpeed; }; #endif diff --git a/src/Game.cpp b/src/Game.cpp index cfdf95b..aa0386b 100644 --- a/src/Game.cpp +++ b/src/Game.cpp @@ -1,15 +1,17 @@ #include +#include #include "Game.h" #include "MazeMap.h" #include "ResourceManager.h" +#include "Entity.h" void Game::run() { ResourceManager resources; MazeMap map; + Entity pacman; resources.loadTexture("all_textures", "assets/all_textures.png"); - // resize all_textures to 32px resources.resizeTexture("all_textures", 32); // Collision data: 1=wall, 0=path @@ -27,6 +29,9 @@ void Game::run() { {912, 0}, {0, 0}); + pacman.setAnimationTiles(resources.getTexture("all_textures"), {1820, 0}, "right_walking", {60, 60}, 2, 0); + pacman.setActiveSprite("right_walking", 0); + auto window = sf::RenderWindow(sf::VideoMode(windowRes), windowName); window.setFramerateLimit(maxFPS); @@ -44,6 +49,8 @@ void Game::run() { window.draw(map); + window.draw(*pacman.getActiveSprite()); + //window.draw(); window.display();