add basic tilemap

This commit is contained in:
2025-11-02 21:15:51 -05:00
parent 4a098a128a
commit 6893f5f247
2 changed files with 84 additions and 0 deletions
+84
View File
@@ -1,9 +1,91 @@
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include <SFML/Graphics/CircleShape.hpp> #include <SFML/Graphics/CircleShape.hpp>
#include <SFML/System/Vector2.hpp>
#include <SFML/Window/Keyboard.hpp> #include <SFML/Window/Keyboard.hpp>
class TileMap : public sf::Drawable, public sf::Transformable
{
public:
bool load(const std::filesystem::path& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{
// load the tileset texture
if (!m_tileset.loadFromFile(tileset))
return false;
// resize the vertex array to fit the level size
m_vertices.setPrimitiveType(sf::PrimitiveType::Triangles);
m_vertices.resize(width * height * 6);
// populate the vertex array, with two triangles per tile
for (unsigned int i = 0; i < width; ++i)
{
for (unsigned int j = 0; j < height; ++j)
{
// get the current tile number
const int tileNumber = tiles[i + j * width];
// find its position in the tileset texture
const int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
const int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
// get a pointer to the triangles' vertices of the current tile
sf::Vertex* triangles = &m_vertices[(i + j * width) * 6];
// define the 6 corners of the two triangles
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);
// define the 6 matching texture coordinates
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;
}
private:
void draw(sf::RenderTarget& target, sf::RenderStates states) const override
{
// apply the transform
states.transform *= getTransform();
// apply the tileset texture
states.texture = &m_tileset;
// draw the vertex array
target.draw(m_vertices, states);
}
sf::VertexArray m_vertices;
sf::Texture m_tileset;
};
int main() int main()
{ {
constexpr std::array level = {
0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
};
TileMap map;
if (!map.load("src/tileset.png", {32, 32}, level.data(), 16, 8))
return -1;
auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "Pacmen"); auto window = sf::RenderWindow(sf::VideoMode({1920u, 1080u}), "Pacmen");
window.setFramerateLimit(144); window.setFramerateLimit(144);
@@ -22,6 +104,8 @@ int main()
window.clear(); window.clear();
window.draw(map);
window.draw(pacman); window.draw(pacman);
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) { if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::W)) {
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB