Source

pyGAP / game / src / game / state / state_game.cpp

Full commit
#include "game/state/state_game.h"
#include "game/state/state_manager.h"
#include "game/input/command_sample.h"

#include "input/command_manager.h"
#include "input/command.h"
#include "renderer/renderer.h"

#include "common/log.h"


namespace game {


static bool registered = register_state< StateGame >("game");


StateGame::StateGame() : state::State(), common::Singleton< StateGame >() {
#if defined(DEBUG)
    log_ = common::Log::get(__FUNCTION__);
#endif
}


void StateGame::enter()
{
    LOG("Entering state game\n");

    color_ = 0.0;
    pause_ = false;
}


int StateGame::translate(input::CommandManager* commands)
{
    input::Command* quit = &(*commands)[CommandSample::QUIT];
    input::Command* sw = &(*commands)[CommandSample::SWITCH];
    input::Command* pause = &(*commands)[CommandSample::PAUSE];

    if (!quit->is_triggered() && quit->has_changed())
        return 1;

    if (!sw->is_triggered() && sw->has_changed())
        StateManager::instance()->switch_to_state("intro");

    if (pause->is_triggered() && pause->has_changed()) // is_triggered and has_changed means "has just been triggered"
        pause_ = !pause_;

    return 0;
}


void StateGame::update(unsigned int dt)
{
    if(pause_)
        return;

    float dtf = (float(dt) / 1000000.0);

    color_ > 1.0 ? color_ = 0.0 : color_ += 1.0 * dtf;
}


void StateGame::render(renderer::Renderer* renderer) const
{
    renderer->clear(color_, 0.0, color_);
}


} // namespace game