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 - Tarbal

Pages: [1]
1
Graphics / Re: Segmentation Fault with vector<Sprite>
« on: October 12, 2016, 11:51:06 pm »
OK, it's working now that I took out the [i]. Sorry if I wasted your time.

2
Graphics / Re: Segmentation Fault with vector<Sprite>
« on: October 12, 2016, 11:47:42 pm »
I was changing stuff at the last second before I posted and went from all 5's to 3's, but missed one. Was trying to edit in here, but didn't work out so well. In any case, it's the

sprite_vector[i].push_back(sprite)
 

that I don't understand why it doesn't work.

Any help would be greatly appreciated.

3
Graphics / Re: Segmentation Fault with vector<Sprite>
« on: October 12, 2016, 11:33:45 pm »
Yeah, I just edited. I must have changed something. I didn't mean SFML was wrong, I meant I don't understand containers in SFML

4
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]