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

Pages: [1]
1
General / State Machine Question
« on: June 29, 2014, 10:48:40 pm »
So reading through the SFML book, I came across some code regarding the Finite State Machine that I do not comprehend. I understand that what I am about to ask may be a C++ question and not necessarily an SFML question but I couldn't find any helpful resources online anywhere that would better my understanding.

My question is in regards to these bits of code:


// StateStack.hpp

typedef std::unique_ptr< State > StatePtr;

class StateStack
{
     //.....

     std::map< States::ID, std::function< State::StatePtr() > > m_factories;
};

template< typename T >
void StateStack::registerState( States::ID )
{
     m_factories[ id ] = [ this ] () { return State::StatePtr( new T(*this, m_context ) ); };
}

 


//StateStack.cpp

State::StatePtr StateStack::createState( States::ID id )
{
     auto found = m_factories.find( id );
     
     assert( found != m_factories.end() );

     return found -> second();
}

 

I guess you could say I'm wondering why std::function is used inside of the m_factories map instead of just say, an std::unique_ptr< State > ? Would it not be possible to use that method and store a new smart pointer in the registerState() method, then just return the smart pointer stored in there by reference in the createState() method? Or is there a specific reason as to why using std::function is more effective? Also I thought that std::function was to be used with functors or functions? Does this mean that a unique_ptr can be used as one of those?

Again sorry if this is more of a C++ question but seeing as it's part of an SFML project I thought I could get some help here. Thanks

2
SFML projects / Galactic Defender
« on: June 27, 2014, 02:33:55 am »
Hey guys, just thought I'd share with you my first completed game I made with a friend of mine. Took us about a week to make it. It's pretty crappy but we're both proud of it and really hope you guys will enjoy playing it :) There's a lot of stuff that could be different or added but we decided to make this the finished product. A lot of thanks to Laurent and the rest of the SFML community for their support and help. And also a big thanks to all of those who helped write the SFML book, which was used as a resource in learning some of the stuff needed to make this game. There are some screenshots below to check out what it looks like :)

All assets used in this game are freeware and not owned by me or my friend.

Here's a link to download it:

http://www.mediafire.com/download/i2cbp38bkea66bz/GalacticDefender.rar

Also, if it doesn't work for some reason on your computer please let me know. I wasn't sure if I added the right libs and files in the release so sorry in advance.

3
General / Question About Fixed Time Step
« on: June 18, 2014, 10:14:40 pm »
I have a question about some of the code involved using a fixed time step. Here is some example code:



const float TimePerFrame = 1 / 60.0f;

sf::Time timeSinceUpdate = sf::Time::Zero;

while ( window.isOpen() )
{
        sf::Time elapsed = clock.restart();
        timeSinceUpdate += elapsed;
       
        while ( timeSinceUpdate > TimePerFrame )
        {
                timeSinceUpdate -= TimePerFrame; // This is the part I don't understand. Why do this?
               
                handleEvents();
               
                update( TimePerFrame );
        }
       
        render();
}

 

I don't understand why you do the line that I commented? Also, is this code suppose to run the game at 60 fps? Because when I run it, the frame rate is over 9000. Sorry if it's a very simple question, but maybe I'm over thinking it and don't see something obvious. Thanks.

4
Graphics / sf::Text not drawing to window at all
« on: September 16, 2013, 08:24:20 pm »
It's been a while since I've programmed, and so I though I'd get back into it. I noticed that SFML 2.1 came out so I downloaded it and decided to mess around a bit by doing some basic stuff like rendering text. However, for some reason the text does not draw at all in my window. Maybe someone can tell me what I'm doing wrong by taking a look at my code? Thanks



#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window( sf::VideoMode( 800, 600 ), "Text Test" );
       
        sf::Event ev;

        sf::Text text;

        text.setString( "Test String" );
        text.setCharacterSize( 30 );
        text.setPosition( 400, 300 );
        text.setColor( sf::Color::Red );

        while ( window.isOpen() )
        {
                while ( window.pollEvent( ev ) )
                {
                        if ( ev.type == sf::Event::Closed )
                        {
                                window.close();
                        }

                        if ( sf::Keyboard::isKeyPressed( sf::Keyboard::Escape ) )
                        {
                                window.close();
                        }
                }

                window.clear();
                window.draw( text );
                window.display();
        }

        return 0;
}

 

Pages: [1]