1
Window / Getting segfault at RenderWindow method callings
« on: September 20, 2019, 08:41:46 pm »
Each time I try to invoke any method of my RenderWindow object, I get a segfault:
At this example at 1st at line 117
At this example at 1st at line 117
#include <SFML/Graphics.hpp>
#include <array>
#include <iostream>
#include <cstdlib>
#include <forward_list>
const int ROWS = 10;
const int COLS = 20;
const int CELL_SIZE = 16;
namespace LifeName {
enum Life { Grass = 1, Cow };
}
class Life : public sf::Drawable
{
public:
int age;
sf::Sprite sprite;
sf::Vector2<int> pos;
LifeName::Life lifeName;
bool is_spawn;
public:
Life() : age(0), is_spawn(false)
{ sprite.setColor(sf::Color(0xff,0xff,0xff,0x00));}
virtual void draw( sf::RenderTarget & rt, sf::RenderStates s) const override
{
rt.draw( sprite );
}
virtual void live() = 0;
virtual void spawn( std::array<std::array<int,COLS>,ROWS> & world) const = 0;
};
class Grass : public Life
{
public:
void draw( sf::RenderTarget & rt, sf::RenderStates s) const override
{
rt.draw(sprite);
}
void live() override
{
if( is_spawn == true) is_spawn = false;
if( age ++ > 10)
{
is_spawn = true;
}
}
void spawn(std::array<std::array<int,COLS>,ROWS> & world) const override {}
};
class Animal : public Life
{
};
class Game : public sf::NonCopyable
{
std::array<std::array<Life*,COLS>,ROWS> m_grid;
std::forward_list<Life*> m_lifeList;
sf::Vector2<int> m_cellSize;
sf::RenderWindow & m_window;
sf::Texture m_grassTexture;
public:
Game() : m_cellSize(CELL_SIZE,CELL_SIZE),
m_window( *new sf::RenderWindow(sf::VideoMode(COLS*CELL_SIZE,
ROWS*CELL_SIZE),"Test") )
{
m_grassTexture.loadFromFile("/home/nico/develop/c++/sfml/gol/media/64x16_grass.png");
for ( int row = 0; row < m_grid.size(); ++row ) {
for( int col = 0; col < m_grid[row].size(); ++col)
{
if( std::rand()%10 == 0) {
Life * grassCell = new Grass;
m_grid[col][row] = grassCell;
grassCell->pos = std::move(sf::Vector2<int>(col, row));
m_lifeList.push_front(grassCell);
grassCell->sprite.setPosition( col*CELL_SIZE, row*CELL_SIZE );
grassCell->sprite.setTexture( m_grassTexture );
grassCell->sprite.setTextureRect(
sf::Rect<int>(0 ,0, m_cellSize.x, m_cellSize.y )
);
}
else {
m_grid[col][row] = nullptr;
}
}
}
}
~Game()
{
delete &m_window;
for( Life * life : m_lifeList) delete life;
}
private:
void draw()
{
for( auto const life : m_lifeList)
{
m_window.draw( * life );
}
}
void update()
{
for( Life * life : m_lifeList)
life->live();
}
public:
void run()
{
std::cerr << "Mark0\n"; //DEBUG
while( m_window.isOpen() ) // <- Here I get the segfault
{
sf::Event event;
std::cerr << "Mark1\n"; //DEBUG
while( m_window.pollEvent(event))
{
if( event.type == sf::Event::Closed)
{
m_window.close();
}
}
std::cerr << "Mark2\n"; //DEBUG
m_window.clear( sf::Color::Black );
draw();
std::cerr << "Mark3\n"; //DEBUG
m_window.display();
update();
sf::sleep(sf::seconds(1));
}
}
};
int main()
{
Game game;
game.run();
}
#include <array>
#include <iostream>
#include <cstdlib>
#include <forward_list>
const int ROWS = 10;
const int COLS = 20;
const int CELL_SIZE = 16;
namespace LifeName {
enum Life { Grass = 1, Cow };
}
class Life : public sf::Drawable
{
public:
int age;
sf::Sprite sprite;
sf::Vector2<int> pos;
LifeName::Life lifeName;
bool is_spawn;
public:
Life() : age(0), is_spawn(false)
{ sprite.setColor(sf::Color(0xff,0xff,0xff,0x00));}
virtual void draw( sf::RenderTarget & rt, sf::RenderStates s) const override
{
rt.draw( sprite );
}
virtual void live() = 0;
virtual void spawn( std::array<std::array<int,COLS>,ROWS> & world) const = 0;
};
class Grass : public Life
{
public:
void draw( sf::RenderTarget & rt, sf::RenderStates s) const override
{
rt.draw(sprite);
}
void live() override
{
if( is_spawn == true) is_spawn = false;
if( age ++ > 10)
{
is_spawn = true;
}
}
void spawn(std::array<std::array<int,COLS>,ROWS> & world) const override {}
};
class Animal : public Life
{
};
class Game : public sf::NonCopyable
{
std::array<std::array<Life*,COLS>,ROWS> m_grid;
std::forward_list<Life*> m_lifeList;
sf::Vector2<int> m_cellSize;
sf::RenderWindow & m_window;
sf::Texture m_grassTexture;
public:
Game() : m_cellSize(CELL_SIZE,CELL_SIZE),
m_window( *new sf::RenderWindow(sf::VideoMode(COLS*CELL_SIZE,
ROWS*CELL_SIZE),"Test") )
{
m_grassTexture.loadFromFile("/home/nico/develop/c++/sfml/gol/media/64x16_grass.png");
for ( int row = 0; row < m_grid.size(); ++row ) {
for( int col = 0; col < m_grid[row].size(); ++col)
{
if( std::rand()%10 == 0) {
Life * grassCell = new Grass;
m_grid[col][row] = grassCell;
grassCell->pos = std::move(sf::Vector2<int>(col, row));
m_lifeList.push_front(grassCell);
grassCell->sprite.setPosition( col*CELL_SIZE, row*CELL_SIZE );
grassCell->sprite.setTexture( m_grassTexture );
grassCell->sprite.setTextureRect(
sf::Rect<int>(0 ,0, m_cellSize.x, m_cellSize.y )
);
}
else {
m_grid[col][row] = nullptr;
}
}
}
}
~Game()
{
delete &m_window;
for( Life * life : m_lifeList) delete life;
}
private:
void draw()
{
for( auto const life : m_lifeList)
{
m_window.draw( * life );
}
}
void update()
{
for( Life * life : m_lifeList)
life->live();
}
public:
void run()
{
std::cerr << "Mark0\n"; //DEBUG
while( m_window.isOpen() ) // <- Here I get the segfault
{
sf::Event event;
std::cerr << "Mark1\n"; //DEBUG
while( m_window.pollEvent(event))
{
if( event.type == sf::Event::Closed)
{
m_window.close();
}
}
std::cerr << "Mark2\n"; //DEBUG
m_window.clear( sf::Color::Black );
draw();
std::cerr << "Mark3\n"; //DEBUG
m_window.display();
update();
sf::sleep(sf::seconds(1));
}
}
};
int main()
{
Game game;
game.run();
}