Hi folks,
I'm having an issue using a 2D array of RectangleShapes. This is based off some of the code in a tutorial, it's meant to create a bunch of RectangleShapes, give them textures and then draw them. For some reason, when the number of tiles (and so the number of rectangleShapes) gets to a certain number, it starts crashing. The debugger says 'Segmentation Fault'. I've been going over my textbooks and a bunch of forum posts but I can't figure it out. Hopefully it's just something silly I forgot.
Has anyone else had experience with this sort of thing? And more generally, is this the 'correct' way to be doing tiled maps? Is there some other way that's more efficient or easier?
Thanks for your time.
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
const int TILE_NUMBER_X = 83; //The code works if these two are set to 82.
const int TILE_NUMBER_Y = 83;
const int TILESIZE = 32;
int main()
{
Texture tileset;
tileset.loadFromFile("C:/tileset.png");
RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
RectangleShape shapes[TILE_NUMBER_X][TILE_NUMBER_Y];
for (int y = 0; y < (TILE_NUMBER_X); y++)
{
for (int x = 0; x < (TILE_NUMBER_Y); x++)
{
shapes[x][y].setSize(Vector2f(TILESIZE, TILESIZE));
shapes[x][y].setPosition(x * TILESIZE, y* TILESIZE);
shapes[x][y].setTexture(&tileset);
shapes[x][y].setTextureRect(IntRect(32,32, 32, 32)); //This normally reads from an .tmx file.
}
}
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed)
window.close();
}
window.clear();
(for (int x = 0; x < (TILE_NUMBER_X); x++)
{
for (int y = 0; y < (TILE_NUMBER_Y); y++)
{
window.draw(shapes[x][y]);
}
}
window.display();
}
return 0;
}
Oh, I meant to post this in graphics. Sorry about that.