这是我的代码:
t_game_elements *initializeEnvironnement(sf::RenderWindow *window)
{
t_game_elements *gameElements;
playerBat playerBatOne(0, 200);
playerBat playerBatTwo(790, 200);
gameElements = (t_game_elements *)malloc(1000);
gameElements->playerOne = playerBatOne;
gameElements->playerTwo = playerBatTwo;
*gameElements->playerOne.getShape();
window->draw(*playerBatOne.getShape()); // this line don't segfault
window->draw(*gameElements->playerOne.getShape()); // this line segfault
return (gameElements);
}
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 500), "Pong", sf::Style::Default);
t_game_elements *elements;
elements = initializeEnvironnement(&window);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
{
window.close();
}
}
if (event.type == sf::Event::Closed)
window.close();
}
// window.clear();
// window.draw(*elements->playerOne.getShape());
// window.draw(*elements.playerTwo->getShape());
window.display();
}
return 0;
}
class playerBat
{
public:
playerBat(int x, int y);
sf::RectangleShape *getShape();
void setShape(sf::RectangleShape *Shape);
int test = 10;
private:
sf::RectangleShape shapeBat;
};
typedef struct s_game_elements
{
playerBat playerOne;
playerBat playerTwo;
} t_game_elements;
playerBat::playerBat(int x, int y)
{
sf::RectangleShape rectangle(sf::Vector2f(120, 50));
rectangle.setSize(sf::Vector2f(10, 100));
rectangle.setPosition(x, y);
rectangle.setFillColor(sf::Color::Green);
shapeBat = rectangle;
}
void playerBat::setShape(sf::RectangleShape *Shape)
{
shapeBat = *Shape;
}
sf::RectangleShape *playerBat::getShape()
{
return &shapeBat;
}
我分配1000个字节只是为了确保我分配足够的内存,我将在我首先修复segfautlt时更改它。
我试图显示我班级的变量'test',但效果很好。
知道为什么我会出现段错误吗?
由于