Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Crash with 2D array of RectangleShapes  (Read 4607 times)

0 Members and 1 Guest are viewing this topic.

Fondis

  • Newbie
  • *
  • Posts: 7
    • View Profile
Crash with 2D array of RectangleShapes
« on: October 18, 2014, 09:02:37 am »
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.
« Last Edit: October 18, 2014, 09:13:22 am by Fondis »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Crash with 2D array of RectangleShapes
« Reply #1 on: October 18, 2014, 10:20:16 am »
Run it in a debugger and see with the call stack where it crashes.

A better way would be to use a 1D vector of vertices and everything with one draw call.
Also don't use "using namespace"!
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Fondis

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crash with 2D array of RectangleShapes
« Reply #2 on: October 18, 2014, 11:21:44 am »
Thanks for your reply. This is what the stack looks like, but I'm not really sure how to interpret it.
I'll definitely look into the vertices method, I think I remember seeing a tutorial for it somewhere on the site.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Crash with 2D array of RectangleShapes
« Reply #3 on: October 18, 2014, 11:37:04 am »
Sounds like a graphics driver issue, try to update it.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Fondis

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crash with 2D array of RectangleShapes
« Reply #4 on: October 18, 2014, 12:34:30 pm »
So after updating my graphics drivers, the call stack looks like this:

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Crash with 2D array of RectangleShapes
« Reply #5 on: October 18, 2014, 02:31:39 pm »
sounds like you're trying to alocate more memory in a single stack than your computer can handle (or something like that, i'm not really into these low level things). try using vectors instead of arrays.
//remove this:
RectangleShape shapes[TILE_NUMBER_X][TILE_NUMBER_Y];

//and add this in the same line:
std::vector< std::vector<RectangleShape> > shapes;
shapes.resize(TILE_NUMBER_X);
for(int n=0; n<TILE_NUMBER_X; n++){
    shapes[n].resize(TILE_NUMBER_Y);
}

obs: you'll need
#include <vector>
.
« Last Edit: October 18, 2014, 02:34:30 pm by Stauricus »
Visit my game site (and hopefully help funding it? )
Website | IndieDB

Fondis

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crash with 2D array of RectangleShapes
« Reply #6 on: October 19, 2014, 02:39:11 am »
This seems to have fixed the problem. Thanks very much to both of you.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: Crash with 2D array of RectangleShapes
« Reply #7 on: October 19, 2014, 11:38:04 am »
I still advise you to use a vector of vertices or at least a 1D vector of RectangleShapes, the 2D to 1D mapping is rather easy. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Crash with 2D array of RectangleShapes
« Reply #8 on: October 19, 2014, 12:05:54 pm »
Yes. In general, a nested std::vector is rather inefficient and needs a lot of memory. If don't need the performance to resize very often along one dimension, I'd recommend using a 1D container and mapping indices. Iteration will be faster on a contigous block of memory, you'll have fewer reallocations and there will only be unused elements along one dimension.

See also: http://en.sfml-dev.org/forums/index.php?topic=12989.msg90955#msg90955
« Last Edit: October 19, 2014, 12:08:09 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything