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

Pages: [1]
1
General / Re: List iterator not incrementable for published event
« on: September 09, 2014, 10:02:58 pm »
Awesome, thanks for the helpful tip!

2
General / Re: List iterator not incrementable for published event
« on: September 09, 2014, 09:36:32 pm »
You were removing the iterator from inside of the iteration and then trying to increment it. At that point the iterator was no longer valid, thus you were getting the error.

Out of curiosity, is there any compelling reason on why you're using std::list as opposed to std::vector?

That's just it though, there should have been no iteration at all. It should have seen that subscribers was empty, and not attempted to iterate. Admittedly I'm coming from the Java world so I may be missing a nuance of how the iterator here works -- that's the same excuse as to why I use a list -- it's a pretty standard fallback collection, but if there are better reasons to using a vector then I am happy to switch.

3
General / Re: List iterator not incrementable for published event
« on: September 09, 2014, 09:32:07 pm »
Suggestion:

for (auto& subscriber : subscribers)
  subscriber.deliver(game, e);

That was similar to my original implementation. I changed it to use an iterator in an attempt to fix the error I was seeing originally.

4
General / Re: List iterator not incrementable for published event
« on: September 09, 2014, 07:24:45 pm »
I made the following change:

void EventBus::publish(Game* game, sf::Event e)
{
    std::list<EventSubscriber*>::iterator iterator;
    for (iterator = subscribers.begin(); iterator != subscribers.end();) {
        (*iterator++)->deliver(game, e);
    }
}

I'm still not understanding why this is necessary though. The subscriber should no longer be in the list by the time EventBus::publish is called again. However, that change did fix it so I'm okay to move along.

5
General / List iterator not incrementable for published event
« on: September 08, 2014, 02:12:59 pm »
I have an event bus that I'm using to publish events to subscribers. However, I'm finding that after I unsubscribe a subscriber successfully the publish method is already mid-delivery for the same event that just got delivered. It was my understanding that event polling was all on the same thread as the window, but I'm probably just not understanding it correctly. Here is some of my code:

EventBus.cpp

#include "engine/events/EventBus.hpp"

void EventBus::unsubscribe(EventSubscriber* eventSubscriber)
{
        std::list<EventSubscriber*>::iterator iterator;
        for (iterator = subscribers.begin(); iterator != subscribers.end(); ++iterator)
        {
                if (*iterator == eventSubscriber)
                {
                        subscribers.erase(iterator);
                        break;
                }
        }
}

void EventBus::subscribe(EventSubscriber* eventSubscriber)
{
        subscribers.push_back(eventSubscriber);
}

void EventBus::publish(Game* game, sf::Event e)
{
        std::list<EventSubscriber*>::iterator iterator;
        for (iterator = subscribers.begin(); iterator != subscribers.end(); iterator++) {
                (*iterator)->deliver(game, e);
        }
}

std::list<EventSubscriber*> EventBus::subscribers = {};

EventBus.cpp has all static members.

Button.cpp (partial)

void Button::deliver(Game* game, sf::Event e)
{
        switch (e.type)
        {
        case sf::Event::MouseButtonReleased:
       
                if (e.mouseButton.button == sf::Mouse::Left && m_text.getGlobalBounds().contains(sf::Mouse::getPosition(m_window).x, sf::Mouse::getPosition(m_window).y))
                {
                        EventBus::unsubscribe(this);
                        game->newGame();
                }
               
                break;
       
        case sf::Event::MouseMoved:
               
                if (m_text.getGlobalBounds().contains(sf::Mouse::getPosition(m_window).x, sf::Mouse::getPosition(m_window).y))
                {
                        if (handCursor)
                        {
                                SetCursor(handCursor);
                        }
                }
                else
                {
                        if (arrowCursor)
                        {
                                SetCursor(arrowCursor);
                        }
                }
               
                break;
        }
}

Before the new game is created I unsubscribe the "New Game" button, and then create the game. It does remove the button from the list of subscribers, but if I step out after that point it immediately jumps to this line in the publish method of EventBus.cpp:

(*iterator)->deliver(game, e);

The event it is delivering is the MouseButtonReleased, which was already delivered to the button, or else I wouldn't have been able to call unsubscribe. This throws the runtime exception "List iterator not incrementable". My event handling is pretty standard.

Game.cpp (partial)

void Game::handleEvents()
{
        sf::Event event;
        while (m_window.pollEvent(event))
        {
                EventBus::publish(this, event);

                switch (event.type)
                {
                case sf::Event::Closed:

                        exit();
                        break;

                case sf::Event::Resized:

                        // TODO: Resize view.
                        break;

                case sf::Event::LostFocus:

                        pause();
                        break;

                case sf::Event::GainedFocus:

                        pause();
                        break;

                case sf::Event::KeyPressed:

                        if (event.key.code == sf::Keyboard::Escape)
                        {
                                exit();
                        }
                        else if (event.key.code == sf::Keyboard::P)
                        {
                                pause();
                        }

                        break;

                default:
                        break;
                }
        }
}

That method is called in my main loop.

I'm drawing a blank on why this control flow would be incorrect, or what is wrong with my C++ code. Any help is appreciated. Thanks!

Brian

6
Graphics / Re: Questions about the scene graph example
« on: August 15, 2014, 03:02:18 pm »
Thanks zsbzsb and Laurent for the additional clarification. This was really helpful.

One of my implementations from a "Node" type class does inherit from Transformable, but now that you mentioned it I think it makes more sense to me to move that up to the abstract class.

Thanks again!

7
Graphics / Questions about the scene graph example
« on: August 15, 2014, 01:14:37 am »
http://sfml-dev.org/tutorials/2.1/graphics-transform.php#object-hierarchies-scene-graph

I'm probably missing something here, but the Node in the example is an abstract class, but owns a private m_transform member variable with no transformations. This Transform is passed into the onRender function of the derived class, but the SpriteNode does nothing with it. So the only way I can see the m_transform member variable as useful is if the Node had a protected method that returned it so SpriteNode could use it for its own transformation, or if SpriteNode had its own Transform member variable and it was combined with the m_transform passed through. What am I not understanding from the example?

My high level understanding is that this pattern is meant for a scene graph where one transformation can be "relative" to a parent transformation. Is that right?

Also, I've seen this in C++ a few different times, but I can't find documentation on combining pointers:

sf::Transform combinedTransform = parentTransform * m_transform;

states.transform *= getTransform();

Are these overloaded operators that know internally how to resolve a "combination", or is there another term for this where I can read more?

Thanks!

Brian

8
Thanks for the quick reply and confirmation Laurent. I didn't want to start going down that route only to find out I had misinterpreted the answer on the gamedev stackexchange.

9
Graphics / Should every TileMap layer be composed of tile vertices?
« on: July 14, 2014, 10:39:07 pm »
I was using this example for some time in getting my feet wet, and it worked well with a single map layer:

http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php#example-tile-map

However, it got me wondering why I wouldn't just have a small subset of large textures or backgrounds that I layer as graphics for a level. This presents the general concept:

http://gamedev.stackexchange.com/questions/62391/whats-the-purpose-of-layers-in-map-editors

Does this have to do with memory and graphic's card constraints? If I had multiple layers, should each layer still be composed of a collection of tile vertices? Why not redraw each layer as a large graphic?

10
Graphics / Re: ERROR: identifier "getTransform" is undefined
« on: December 28, 2013, 05:05:12 pm »
Thanks Nexus and G., that worked!

I'm definitely going through the motions to learn C++. Accelerated C++: Practical Programming by Example is in the mail, and I've been walking through learncpp.com. I had a game I was making using Java 2D, and the API just wasn't intended for that purpose. So I was going to try out JSFML, but decided this was a good opportunity to finally pick up C++.

Thanks again for the help.

11
Graphics / ERROR: identifier "getTransform" is undefined
« on: December 28, 2013, 05:54:25 am »
I apologize as I'm coming from the Java world and trying to learn C++ while using SFML as my motivation, so this will probably come across as very novice:

LevelOne.hpp

#ifndef LEVEL_ONE_H
#define LEVEL_ONE_H

#include <SFML/Graphics.hpp>

class LevelOne : public sf::Drawable, public sf::Transformable
{
public:
        LevelOne();
        ~LevelOne();
};

#endif
 

LevelOne.cpp

#include "LevelOne.hpp"

LevelOne::LevelOne()
{
}

LevelOne::~LevelOne()
{
}

void draw(sf::RenderTarget& target, sf::RenderStates states)
{
        sf::VertexArray vertices;
        sf::Texture texture;

        states.transform *= getTransform();
        states.texture = &texture;
        target.draw(vertices, states);
}
 

For getTransform() I get ERROR: identifier "getTransform" is undefined. If I don't use a header file, and just simply have a class declaration then it finds the method.

Thanks for the help.

Pages: [1]
anything