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.


Topics - Tarbal

Pages: [1]
1
Graphics / Segmentation Fault with vector<Sprite>
« on: October 12, 2016, 11:14:24 pm »
I'm not a c++ wizard, but I'm pretty sure this is an issue with SFML itself. I kept it really basic, but I'm having trouble with vector<Sprite>. Do I really have to list them all explicitly? A loop seems easiest, so that's how I tried, but I don't think push_back is working, according to my gdb debugger. As the subject says, I get a segmentation fault upon compilation of the following code:

#ifndef events_H
#define events_H

#include <SFML/Graphics.hpp>
#include <string>

class LiveEvents
{
public:
        void Switchboard();
private:
        union switches;
};

#endif

 


#include <events.hpp>

void LiveEvents::Switchboard()
{
        sf::Event event;
        switch (event.type)
        {
                case sf::Event::KeyPressed:
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
                                // do stuff                    
                break;

        }
        switch (event.type)
        {
                case sf::Event::KeyPressed:
                        if(event.key.code == sf::Keyboard::C)
                                //do stuff
                break;

        }

}

 

And the main loop:

#include <events.hpp>
#include <vector>
int main()
{
        sf::RenderWindow screen(sf::VideoMode(800,600), "Window");
        screen.setVerticalSyncEnabled(true);
        screen.setFramerateLimit(60);
        while (screen.isOpen())
        {
                sf::Event screen_event;
                while(screen.pollEvent(screen_event))
                {
                        switch(screen_event.type)
                        {
                                case sf::Event::Closed:
                                        screen.close();
                                default:
                                break;
                        }
                        LiveEvents switches;
                        switches.Switchboard();
                }
                screen.clear(sf::Color::Black);

                sf::Texture background;
                if(!background.loadFromFile("../textures/background_rect.png"))
                {
                        return -1;
                }
                sf::Sprite sprite;
                sprite.setTexture(background);
                int width = sprite.getGlobalBounds().width;
                int height = sprite.getGlobalBounds().height;
                std::vector<sf::Sprite> sprite_vector;
                for(int i=0; i<3; i++)
                {
                        sprite_vector[i].push_back(sprite);
                }
                for(int i=0; i<3; i++)
                {
                        for(int j=0; j<3; j++)
                        {
                                sprite_vector[i].setPosition(sf::Vector2f(i*width,j*height));
                        }
                }
                for(int i=0; i<3; i++)
                {
                        screen.draw(sprite_vector[i]);
                }

                screen.display();
        }
}

 

Is there a better way to handle containers than vector<Sprite>? As it looks right now, I'll have to list all of them explicitly, and no way am I doing that. I've seen a few forum posts that say do this or that, but no one shows a simple example of how to display multiple sprites. Can someone please show me and/or point out what's wrong with my code?

edit: left out the push_back loop on accident. I must have changed something, but now I get

error: ‘__gnu_cxx::__alloc_traits<std::allocator<sf::Sprite> >::value_type’ has no member named ‘push_back’
    sprite_vector[i].push_back(sprite);
 

Pages: [1]