#include <SFML/Graphics.hpp>
#include <CheeseMap.hpp>
int main()
{
// load the texture
sf::Texture mapTexture;
if (!mapTexture.loadFromFile("resources/16colours(16x16_4x4each).png"))
return EXIT_FAILURE;
// some constants that define the format of the tiles inside the texture
const std::size_t numberOfTilesInTexture{ 16u };
const sf::Vector2<std::size_t> sizeOfTilesInTexture{ 4u, 4u };
const std::size_t numberOfTilesPerRowInTexture{ 4u };
// create the Cheese Map and set its texture
cm::Map map;
map.setTexture(mapTexture);
// set-up the texture atlas to contain all the tiles in the texture
map.textureAtlas.resize(numberOfTilesInTexture);
for (std::size_t i{ 0u }; i < map.textureAtlas.size(); ++i)
map.textureAtlas[i] = { sf::Vector2f(i % numberOfTilesPerRowInTexture * sizeOfTilesInTexture.x, i / numberOfTilesPerRowInTexture * sizeOfTilesInTexture.y), sf::Vector2f(sizeOfTilesInTexture) };
// add a grid map with random tile IDs
map.grids.resize(1u);
const sf::Vector2<std::size_t> sizeOfGridMap{ 50u, 10u };
map.grids[0u].rowWidth = sizeOfGridMap.x;
map.grids[0u].tileSize = { 50.f, 50.f };
map.grids[0u].tileIds.resize(sizeOfGridMap.x * sizeOfGridMap.y);
for (auto& tile : map.grids[0u].tileIds)
tile = rand() % numberOfTilesInTexture; // trivially random, just for an example
// basic window loop
sf::RenderWindow window(sf::VideoMode(960u, 540u), "Cheese Map - simple example 1");
sf::View mapView(window.getDefaultView());
sf::Clock clock;
while (window.isOpen())
{
sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); }
const float time{ clock.getElapsedTime().asSeconds() };
mapView.setCenter({ 500.f + std::cos(time * 2.f) * 200.f, 0.f + std::sin(time * 2.f) * 30.f }); // move the view around based on the time
map.update(mapView);
window.clear();
window.setView(mapView);
window.draw(map);
window.display();
}
}