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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - nischu

Pages: [1]
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

#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();
}
 

2
General / Why will some sprites not been drawed?
« on: April 18, 2018, 11:03:25 pm »
Here my code:
int main()
{
    sf::Texture texture;
    if (!texture.loadFromFile("/home/nico/Pictures/sheets/64x32_tictactoe.png"))
    {
        std::cerr << "Texture file couldn't loaded!\n";
        return EXIT_FAILURE;
    }
    sf::Sprite cross(texture,sf::Rect<int>(0,0,32,32));
    cross.setPosition(0.0f,0.0f);

    sf::Sprite ring(texture);
    ring.setTextureRect( sf::Rect<int>(32,0,32, 32));
    ring.setPosition(32.0f, 0.0f );

    std::array<sf::Sprite,9> sprite;
    for( auto sp : sprite)
    {
        sp = sf::Sprite(texture, sf::Rect<int>(0,0,32,32));
        sp.setPosition(100,100);
    }
    sf::RenderWindow window(sf::VideoMode(400, 400), "TicTacToe");

    while (window.isOpen())
    {
        window.clear();

        window.draw(cross);  // works
        window.draw(ring);  // works
        for (auto sp : sprite) window.draw(sp); // doesn't work

        window.display();

        sf::Event event;

        while (window.waitEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
        }
    }
    return EXIT_SUCCESS;
}
Any idea why the sprites of the std::array will not drawed? Thanks for help.

Pages: [1]