gremlin/src/common/Application.cpp

90 lines
1.5 KiB
C++

#include "Application.h"
#include "Time.h"
#include "Network.h"
#include <iostream>
namespace gln {
Application::Application() :
running(false), master(false), time(0.0), network(&game) {
}
Application::~Application() {
}
bool Application::isRunning() {
return running;
}
void Application::start() {
std::cout << "[Application] start" << std::endl;
running = true;
}
void Application::stop() {
std::cout << "[Application] stop" << std::endl;
running = false;
}
void Application::initialize(Config &config) {
std::cout << "[Application] initialize" << std::endl;
// setup time
PerformanceTimer::set(0.0);
time = PerformanceTimer::get();
// setup game
game.setup();
game.reset();
// setup schedules
gameUpdateSchudule.setExact(true);
gameUpdateSchudule.setInterval(0.05);
std::string host;
config.get("host", host);
network.initialize(host);
// setup network
if (host.empty()) {
setMaster(true);
} else {
setMaster(false);
}
// start
start();
}
void Application::shutdown() {
std::cout << "[Application] shutdown" << std::endl;
network.shutdown();
}
void Application::update() {
// update time values
time = PerformanceTimer::get();
dt = time - lastTime;
lastTime = time;
// network stuff
network.service(master ? 1 : 0);
game.update(time, dt);
if (master && gameUpdateSchudule.next(time)) {
network.sendGameUpdates();
}
}
void Application::setMaster(bool master) {
std::cout << "[Application] setMaster = " << master << std::endl;
this->master = master;
game.set_master(master ? 1 : 0);
}
} // namespace grln