Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I'm writing a ORPG. The code below is client side. I have main thread looping in Game::Run:

    Game::~Game()
    {
        PopAllStates();
    }

    void Game::Run()
    {
        sf::Event Event;

        while(Window->isOpen())
        {
            if(LoadQueueMutex.try_lock())
            {
                while(!LoadQueue.empty())
                {
                    LoadQueue.front().first->Load(LoadQueue.front().second);
                    LoadQueue.pop();
                }
                LoadQueueMutex.unlock();
            }
            while(Window->pollEvent(Event))
            {
                StateStack.top()->HandleEvent(Event);
            }
            Window->clear();
            StateStack.top()->Draw();
            Window->display();
            StateStack.top()->Update();
        }
    }

    void Game::AddToLoadQueue(Loadable* pLoadable, WorldPacket Argv)
    {
        boost::mutex::scoped_lock lock(LoadQueueMutex);
        LoadQueue.push(std::make_pair(pLoadable, Argv));
    }

    void Game::PushState(GameState* pState)
    {
        StateStack.push(pState);
    }

    void Game::PopState()
    {
        if(!StateStack.empty())
        {
            delete StateStack.top();
            StateStack.pop();
        }
    }

    void Game::PopAllStates()
    {
        while(!StateStack.empty())
        {
            PopState();
        }
    }

And another thread waiting for packet:

void WorldSession::Start()
{
    Packet = WorldPacket((uint16)MSG_NULL);

    boost::asio::async_read(Socket,
        boost::asio::buffer(Packet.GetDataWithHeader(), WorldPacket::HEADER_SIZE),
        boost::bind(&WorldSession::HandleHeader, this, boost::asio::placeholders::error));
}

When packet "arrives", usually this is done:

void WorldSession::HandleSomeCoolOpcode()
{
    //...
    sGame->AddToLoadQueue(pSomethingCool, Packet);
}

Because OpenGL breaks if loaded from another thread.

When main thread Loads the Loadable*, it will usually mess with state stack:

void World::Load(WorldPacket Argv)
{
    //...
    sGame->PopState();
    sGame->PushState(this);
}

Any way to improve this?

share|improve this question
what's the point of the pushState/popState mechanism? Is is possible to temporarily load/start a game, play it, and close it to return to the previously opened one? – didierc Nov 14 '12 at 15:20

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.