mirror of
https://github.com/dylanbenzi/pacmen-sfml.git
synced 2026-11-27 08:26:59 +00:00
add entity class and texture loading
This commit is contained in:
+1
-1
@@ -20,6 +20,6 @@ FetchContent_Declare(json
|
||||
|
||||
FetchContent_MakeAvailable(SFML json)
|
||||
|
||||
add_executable(main src/main.cpp src/TileMap.cpp)
|
||||
add_executable(main src/main.cpp src/TileMap.cpp src/Entity.cpp)
|
||||
target_compile_features(main PRIVATE cxx_std_17)
|
||||
target_link_libraries(main PRIVATE SFML::Graphics nlohmann_json::nlohmann_json)
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
#include "Entity.h"
|
||||
#include <SFML/Graphics/PrimitiveType.hpp>
|
||||
#include <SFML/Graphics/RenderStates.hpp>
|
||||
#include <SFML/Graphics/RenderTarget.hpp>
|
||||
#include <SFML/Graphics/Vertex.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <cstddef>
|
||||
#include <filesystem>
|
||||
|
||||
Entity::Entity() {};
|
||||
|
||||
//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 tileSize, int tiles) {
|
||||
if(!texture.loadFromFile(texturePath))
|
||||
return false;
|
||||
|
||||
//assumes tiles are animation tiles of one row tileSize height and tiles width
|
||||
int height = 1;
|
||||
int width = height * tiles;
|
||||
int doubleTriVertex = 6;
|
||||
|
||||
//width * height * 2x triangle vertex
|
||||
vertexArray.setPrimitiveType(sf::PrimitiveType::Triangles);
|
||||
vertexArray.resize(width * height * doubleTriVertex);
|
||||
|
||||
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 / tileSize.x);
|
||||
const int tv = tileNumber / (texture.getSize().x / tileSize.x);
|
||||
|
||||
sf::Vertex* triangles = &vertexArray[(i + j * width) * doubleTriVertex];
|
||||
|
||||
triangles[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
|
||||
triangles[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
|
||||
triangles[2].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
|
||||
triangles[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);
|
||||
triangles[4].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
|
||||
triangles[5].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
|
||||
|
||||
triangles[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
|
||||
triangles[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
|
||||
triangles[2].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
|
||||
triangles[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
|
||||
triangles[4].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
|
||||
triangles[5].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void Entity::draw(sf::RenderTarget& target, sf::RenderStates states) const {
|
||||
states.transform *= getTransform();
|
||||
states.texture = &texture;
|
||||
target.draw(vertexArray, states);
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
#ifndef ENTITY_H
|
||||
#define ENTITY_H
|
||||
|
||||
#include <SFML/Graphics/Drawable.hpp>
|
||||
#include <SFML/Graphics/PrimitiveType.hpp>
|
||||
#include <SFML/Graphics/RenderStates.hpp>
|
||||
#include <SFML/Graphics/RenderTarget.hpp>
|
||||
#include <SFML/Graphics/Texture.hpp>
|
||||
#include <SFML/Graphics/Transformable.hpp>
|
||||
#include <SFML/Graphics/VertexArray.hpp>
|
||||
#include <SFML/System/Vector2.hpp>
|
||||
#include <filesystem>
|
||||
|
||||
enum class MovementDir {
|
||||
STATIC,
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT
|
||||
};
|
||||
|
||||
class Entity : public sf::Drawable, public sf::Transformable {
|
||||
public:
|
||||
Entity();
|
||||
|
||||
//assumes entity texture is one row of various animation texture states
|
||||
bool loadTexture(const std::filesystem::path& texturePath, sf::Vector2u tileSize, int tiles);
|
||||
protected:
|
||||
sf::VertexArray vertexArray;
|
||||
sf::Texture texture;
|
||||
|
||||
int movementSpeed = 5;
|
||||
|
||||
void draw(sf::RenderTarget& target, sf::RenderStates states) const override;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -12,6 +12,7 @@
|
||||
#include "TileMap.h"
|
||||
|
||||
enum class MovementDir {
|
||||
STATIC,
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
|
||||
+7
-3
@@ -10,8 +10,9 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "Entity.h"
|
||||
#include "TileMap.h"
|
||||
#include "MazeCharacter.h"
|
||||
//#include "MazeCharacter.h"
|
||||
|
||||
int main()
|
||||
{
|
||||
@@ -94,7 +95,10 @@ int main()
|
||||
|
||||
float pacmanSpeed = 5;
|
||||
|
||||
MazeCharacter pacman("assets/pink_pac_man_movement48.png", mazeMap);
|
||||
//MazeCharacter pacman("assets/pink_pac_man_movement48.png", mazeMap);
|
||||
|
||||
Entity pacman;
|
||||
pacman.loadTexture("assets/pink_pac_man_movement48.png", {48, 48}, 5);
|
||||
|
||||
while (window.isOpen())
|
||||
{
|
||||
@@ -123,7 +127,7 @@ int main()
|
||||
window.draw(movementDirText);
|
||||
window.draw(movementDir);
|
||||
|
||||
//window.draw(pacman);
|
||||
window.draw(pacman);
|
||||
|
||||
pacmanCenter = {pinkPacmanSpriteOne.getPosition().x + 16.0f, pinkPacmanSpriteOne.getPosition().y + 16.0f};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user