As eXpl0it3r said, your array is on the stack. It would make more sense to use a vector to store these rectangles as vectors store on the heap. It's a tad slower here but there are a
lot more tiles...
#include <SFML/Graphics.hpp>
#include <vector> // include this to be able to create the vector
const int tileW = 1;
const int tileH = 1;
const int tilesX = 800; // using the vector, we can use higher numbers here
const int tilesY = 600; // using the vector, we can use higher numbers here
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600, 32), "Tile Map");
int x, y;
std::vector<std::vector<sf::RectangleShape>> tiles(tilesX, std::vector<sf::RectangleShape>(tilesY)); // create 2d vector instead of 2d array
for (x = 0; x < tilesX; x++)
{
for (y = 0; y < tilesY; y++)
{
tiles[x][y].setSize(sf::Vector2f(tileH, tileW));
tiles[x][y].setFillColor(sf::Color(x * 255 / tilesX, y * 255 / tilesY, 0)); // removed brackets to allow usage of higher values for tilesX and tilesY
tiles[x][y].setPosition(sf::Vector2f(x * tileW, y * tileH));
}
}
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
for (x = 0; x < tilesX; x++)
for (y = 0; y < tilesY; y++)
window.draw(tiles[x][y]);
window.display();
}
}
I commented the only lines that were changed from eXpl0it3r's example.
First, it would be better to use a one-dimensional vector rather than a two-dimensional one and then access using modulus and division for the rows and columns.
Second, you're drawing each rectangle individually. In my version of this code, that's over 480 thousand draw calls! Multiple rectangles can be drawn using a single draw call if you use a vertex array. You may enjoy reading
this tutorial which explains vertex arrays and even gives you a
Tile Map example!
The code above did this:
It was slow. Give it a chance, yeah?