gremlin/src/common/Application.cpp

90 lines
1.5 KiB
C++
Raw Normal View History

2011-01-14 14:59:18 +01:00
#include "Application.h"
#include "Time.h"
2011-01-16 23:02:40 +01:00
#include "Network.h"
#include <iostream>
2011-01-14 14:59:18 +01:00
2018-11-26 13:15:41 +01:00
namespace gln {
2011-01-14 14:59:18 +01:00
Application::Application() :
2018-11-26 13:15:41 +01:00
running(false), master(false), time(0.0), network(&game) {
2011-01-14 14:59:18 +01:00
}
Application::~Application() {
}
bool Application::isRunning() {
return running;
}
void Application::start() {
2011-01-16 23:02:40 +01:00
std::cout << "[Application] start" << std::endl;
2011-01-14 14:59:18 +01:00
running = true;
}
void Application::stop() {
2011-01-16 23:02:40 +01:00
std::cout << "[Application] stop" << std::endl;
2011-01-14 14:59:18 +01:00
running = false;
}
2018-11-26 13:15:41 +01:00
void Application::initialize(Config &config) {
2011-01-16 23:02:40 +01:00
std::cout << "[Application] initialize" << std::endl;
// setup time
PerformanceTimer::set(0.0);
2011-01-14 22:39:27 +01:00
time = PerformanceTimer::get();
2011-01-16 23:02:40 +01:00
// setup game
2011-01-18 21:52:05 +01:00
game.setup();
game.reset();
2011-01-16 23:02:40 +01:00
// setup schedules
2011-01-14 14:59:18 +01:00
gameUpdateSchudule.setExact(true);
gameUpdateSchudule.setInterval(0.05);
2011-01-16 23:02:40 +01:00
2018-11-26 13:15:41 +01:00
std::string host;
config.get("host", host);
2011-01-16 23:02:40 +01:00
network.initialize(host);
// setup network
if (host.empty()) {
setMaster(true);
} else {
setMaster(false);
2011-01-16 23:02:40 +01:00
}
// start
2011-01-14 14:59:18 +01:00
start();
}
void Application::shutdown() {
2011-01-16 23:02:40 +01:00
std::cout << "[Application] shutdown" << std::endl;
network.shutdown();
2011-01-14 14:59:18 +01:00
}
void Application::update() {
2011-01-16 23:02:40 +01:00
// update time values
2011-01-14 14:59:18 +01:00
time = PerformanceTimer::get();
dt = time - lastTime;
lastTime = time;
2011-01-16 23:02:40 +01:00
// network stuff
network.service(master ? 1 : 0);
game.update(time, dt);
2011-01-14 14:59:18 +01:00
if (master && gameUpdateSchudule.next(time)) {
2011-01-16 23:02:40 +01:00
network.sendGameUpdates();
2011-01-14 14:59:18 +01:00
}
}
void Application::setMaster(bool master) {
2011-01-16 23:02:40 +01:00
std::cout << "[Application] setMaster = " << master << std::endl;
2011-01-14 14:59:18 +01:00
this->master = master;
2011-01-18 21:52:05 +01:00
game.set_master(master ? 1 : 0);
2011-01-14 14:59:18 +01:00
}
2018-11-26 13:15:41 +01:00
} // namespace grln