add base support for entity animation and drawing

This commit is contained in:
2025-11-08 22:53:44 -05:00
parent a2b7d62971
commit 8cebec8684
3 changed files with 66 additions and 50 deletions
+37 -39
View File
@@ -7,52 +7,50 @@
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <cstddef> #include <cstddef>
#include <filesystem> #include <filesystem>
#include <iostream>
//mainly derived from https://www.sfml-dev.org/tutorials/3.0/graphics/vertex-array/#example-tile-map void Entity::setAnimationTiles(sf::Texture& textureSheet,
bool Entity::loadTexture(const std::filesystem::path& texturePath, sf::Vector2u graphicTileSize, int tiles) { sf::Vector2i pixelLocation,
if(!texture.loadFromFile(texturePath)) const std::string& animationName,
return false; 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 EntityAnimation entityAnimation;
int height = 1; entityAnimation.name = animationName;
int width = height * tiles;
int doubleTriVertex = 6;
//width * height * 2x triangle vertex for (int i = 0; i < animationTiles; i++) {
vertexArray.setPrimitiveType(sf::PrimitiveType::Triangles); sf::Vector2i tilePixelLocation(pixelLocation.x + (i * (pixelGap + tileSize.x)), pixelLocation.y);
vertexArray.resize(width * height * doubleTriVertex); sf::IntRect boundingRect(tilePixelLocation, tileSize);
sf::Sprite sprite(textureSheet, boundingRect);
for (size_t i = 0; i < width; ++i) { entityAnimation.sprites.push_back(sprite);
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);
}
} }
return true; animations.push_back(entityAnimation);
} };
void Entity::draw(sf::RenderTarget& target, sf::RenderStates states) const { void Entity::setActiveSprite(const std::string& animationName, int animationTile) {
states.transform *= getTransform(); for (auto& animation : animations) {
states.texture = &texture; if (animation.name == animationName) {
target.draw(vertexArray, states); activeSprite = &animation.sprites.at(animationTile);
return;
}
}
}; };
+21 -10
View File
@@ -1,16 +1,21 @@
#ifndef ENTITY_H #ifndef ENTITY_H
#define ENTITY_H #define ENTITY_H
#include "MazeMap.h"
#include <SFML/Graphics/Drawable.hpp> #include <SFML/Graphics/Drawable.hpp>
#include <SFML/Graphics/PrimitiveType.hpp> #include <SFML/Graphics/PrimitiveType.hpp>
#include <SFML/Graphics/Rect.hpp> #include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/RenderStates.hpp> #include <SFML/Graphics/RenderStates.hpp>
#include <SFML/Graphics/RenderTarget.hpp> #include <SFML/Graphics/RenderTarget.hpp>
#include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp> #include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/Transformable.hpp> #include <SFML/Graphics/Transformable.hpp>
#include <SFML/Graphics/VertexArray.hpp> #include <SFML/Graphics/VertexArray.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <filesystem> #include <filesystem>
#include <optional>
#include <string>
#include <vector>
enum class MovementDir { enum class MovementDir {
STATIC, STATIC,
@@ -20,23 +25,29 @@ enum class MovementDir {
RIGHT RIGHT
}; };
class Entity : public sf::Drawable, public sf::Transformable { struct EntityAnimation {
std::string name;
std::vector<sf::Sprite> sprites;
};
class Entity {
public: 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: protected:
//TileMap map; sf::Vector2i tileSize;
sf::VertexArray vertexArray; std::vector<EntityAnimation> animations;
sf::Texture texture;
int animationTiles; sf::Sprite* activeSprite;
int movementSpeed = 5;
void draw(sf::RenderTarget& target, sf::RenderStates states) const override; int movementSpeed;
}; };
#endif #endif
+8 -1
View File
@@ -1,15 +1,17 @@
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <iostream>
#include "Game.h" #include "Game.h"
#include "MazeMap.h" #include "MazeMap.h"
#include "ResourceManager.h" #include "ResourceManager.h"
#include "Entity.h"
void Game::run() { void Game::run() {
ResourceManager resources; ResourceManager resources;
MazeMap map; MazeMap map;
Entity pacman;
resources.loadTexture("all_textures", "assets/all_textures.png"); resources.loadTexture("all_textures", "assets/all_textures.png");
// resize all_textures to 32px // resize all_textures to 32px
resources.resizeTexture("all_textures", 32); resources.resizeTexture("all_textures", 32);
// Collision data: 1=wall, 0=path // Collision data: 1=wall, 0=path
@@ -27,6 +29,9 @@ void Game::run() {
{912, 0}, {912, 0},
{0, 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); auto window = sf::RenderWindow(sf::VideoMode(windowRes), windowName);
window.setFramerateLimit(maxFPS); window.setFramerateLimit(maxFPS);
@@ -44,6 +49,8 @@ void Game::run() {
window.draw(map); window.draw(map);
window.draw(*pacman.getActiveSprite());
//window.draw(); //window.draw();
window.display(); window.display();