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

Pages: [1]
1
Graphics / Re: Problem with creating a sf::Sprite in sf::Thread
« on: January 31, 2013, 04:35:35 am »
Ok so after a little more research i've found that the problem lie's within OpenGL, you need to disable the context in the main thread using "sf::Window::setActive()" then enable it in the thread you want to create new Sprites or textures in. Here's the post that helped me the most -> http://en.sfml-dev.org/forums/index.php?topic=2780.0

EDIT: This post can go ahead and be deleted if not found useful, it may be help to someone in the future though.

2
Graphics / Re: Issue with vector of sprites
« on: January 31, 2013, 03:52:19 am »
eigenbom is right, I've always done that via the use of pointers though(must have read something about it a while ago..) either way should work though. And no problem! Good luck with your game!

3
Graphics / Re: Issue with vector of sprites
« on: January 31, 2013, 02:54:22 am »
I see what the problem is. It's the two vectors your using to store the sprites and textures in your Sprite_Holder class. Your basically overwriting the last sprite/texture you made when calling Sprite_Holder::add_sprite. This is because your not storing them in vectors of pointers, therefore no new memory can be created! What you need to do is this

//make these vectors of pointers instead
vector<sf::Sprite*> sprites;
vector<sf::Texture*> textures;
 

Now when creating a new sprite or texture use the new operator. Like this

sprites.push_back(new sf::Sprite);
textures.push_back(new sf::Texture);
 

Then go ahead and set the last sprites texture to the last texture

sprites.back()->setTexture(*textures.back());
 

You should be able to easily implement this into your code and modify it to your needs. Btw make sure to delete that newly allocated memory when you don't need it anymore. Make sure when doing this to delete the sprite FIRST, THEN the texture or you'll have a null reference error when that sprite trys to access its texture. If you want to be safe and not have to worry about the mess of raw pointers, I would suggest using c++11's std::unique_ptr class to store those objects in. PM if you want help implementing this, I've done it several times and it was a pain at first!

4
Graphics / Problem with creating a sf::Sprite in sf::Thread
« on: January 31, 2013, 02:34:12 am »
I'm having trouble creating a sf::Sprite in a sf::Thread and then rendering it. Here's a small watered down example of what my code does. Ignore the stupid class names, I made this in about 10 minutes and was trying to make it as readable as possible -

#include <iostream>
#include <vector>
#include <memory>
#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>

//a random class for this example
class ObjectThing{
public:
        sf::Sprite sprite;
        sf::Texture texture;
        ObjectThing(float x, float y, float rotation){
                this->texture.loadFromFile("troll.png");
                this->sprite.setTexture(this->texture);
                this->sprite.setPosition(x, y);
                this->sprite.setRotation(rotation);
                std::cout << "Hello world!" << std::endl;
        }
};

//the class that holds and creates the objectThings
class ObjectThingHolder{
public:
        static std::vector<std::unique_ptr<ObjectThing> > objectThings;
        static void CreateObjectThing(float x, float y, float rotation){
                objectThings.push_back(std::unique_ptr<ObjectThing>(new ObjectThing(x, y, rotation)));
        }
};

//define the SpriteHolder's objectThings variable
std::vector<std::unique_ptr<ObjectThing> > ObjectThingHolder::objectThings;


//the function we'll use for our sf::Thread
void threadFunction(){
        //this object gets created but doesnt get rendered
        ObjectThingHolder::CreateObjectThing(0, 0, 0);
}

int main(){
        sf::RenderWindow window(sf::VideoMode(800, 600, 32), "SFML thread sprite test");

        //this guy also gets created but does render!
        ObjectThingHolder::CreateObjectThing(0, 0, 0);

        //create and launch our thread
        sf::Thread theThread(threadFunction);
        theThread.launch();

        //while windows open, poll events and render the world
        while(window.isOpen()){
                sf::Event e;
                while(window.pollEvent(e)){
                        if (e.type == sf::Event::Closed){
                                window.close();
                        }
                }

                //clear the screen
                window.clear(sf::Color(0, 0, 0, 255));

                //draw each of the ObjectThings sprites
                for(int i = 0; i < (int)ObjectThingHolder::objectThings.size(); i++){
                        window.draw(ObjectThingHolder::objectThings.at(i)->sprite);
                }

                //display the result
                window.display();
        }
}
 

As you can see after reading the code is that the ObjectThing created from within the main loop does get rendered while the ObjectThing created inside the sf::Thread doesn't. I'm completely oblivious to the reasoning for this. But I'm assuming it has something to do with the scope of the threads and what they can access. Help would be greatly appreciated!

Pages: [1]