0
\$\begingroup\$

I am encountering a perplexing issue in my SFML game development where my program crashes instantly during runtime when I add a CircleShape as an instance variable in my Tile.h file. The crash occurs even before my game window is loaded.

Here is the relevant section of my Tile.h file:

#pragma once
#include <SFML/Graphics.hpp>

class Tile{
private:
    sf::IntRect rect;
    sf::Sprite sprite;
    int width;
    int x;
    int y;
    sf::CircleShape hexagon; // The problematic line causing the crash
public:
    Tile(int x, int y, int width, sf::Texture& texture);
    Tile();
    int getX();
    int getY();
    int getWidth();
    void setX(int x);
    void setY(int y);
    void setWidth(int w);
    void draw(sf::RenderWindow& window);
};

The crash persists whether or not I initialize the hexagon instance in the constructors. Strangely, the program runs successfully if I remove the instance variable declaration in Tile.h and instead initialize it within the constructors like this:

Tile::Tile(int x, int y, int width, sf::Texture& texture) : x(x), y(y), width(width), rect(x, y, width, width), sprite(texture){
    sf::CircleShape hexagon = sf::CircleShape(20, 6);
    sprite.setScale((double)width / (double)texture.getSize().x, (double)width / (double)texture.getSize().y);
    sprite.setPosition(x, y);
}

Tile::Tile() : x(0), y(0), width(1) {
    sf::CircleShape hexagon = sf::CircleShape(20, 6);
}

This leads me to believe that the inclusion of the instance variable in Tile.h is the root cause of the error. I plan to use the hexagon for collision detection and have omitted its drawing for now.

I would appreciate any insights or suggestions on how to resolve this issue and understand why the CircleShape instance is causing the program to crash. Thank you!

\$\endgroup\$
2
  • \$\begingroup\$ Do you get any particular error message from the crash, or when hooking a debugger and stepping through the start-up code? \$\endgroup\$ Commented Feb 21, 2024 at 17:09
  • 4
    \$\begingroup\$ The code you've posted has a local sf::CircleShape variable within the constructor, this obviously does nothing so it's unclear what your code is supposed to do. Can you post the actual code that crashes? Or, better, a minimum viable example showing the crash. \$\endgroup\$ Commented Feb 21, 2024 at 21:26

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.