2016-07-02 20:49:31 +02:00
|
|
|
#include "BenchmarkState.hpp"
|
2016-04-19 02:20:54 +02:00
|
|
|
#include <engine/GameState.hpp>
|
2016-08-05 18:00:40 +02:00
|
|
|
#include <fstream>
|
2016-09-09 22:13:20 +02:00
|
|
|
#include "RWGame.hpp"
|
2016-04-19 02:20:54 +02:00
|
|
|
|
|
|
|
BenchmarkState::BenchmarkState(RWGame* game, const std::string& benchfile)
|
2016-09-09 22:13:20 +02:00
|
|
|
: State(game), benchfile(benchfile), benchmarkTime(0.f), frameCounter(0) {
|
2016-04-19 02:20:54 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void BenchmarkState::enter() {
|
|
|
|
getWindow().hideCursor();
|
2016-06-22 12:29:39 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
std::ifstream benchstream(benchfile);
|
2016-04-19 02:20:54 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
unsigned int clockHour;
|
|
|
|
unsigned int clockMinute;
|
|
|
|
benchstream >> clockHour;
|
|
|
|
benchstream.seekg(1, std::ios_base::cur);
|
|
|
|
benchstream >> clockMinute;
|
2016-04-19 02:20:54 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
game->getWorld()->state->basic.gameHour = clockHour;
|
|
|
|
game->getWorld()->state->basic.gameMinute = clockMinute;
|
2016-04-19 02:20:54 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
float time = 0.f;
|
|
|
|
glm::vec3 tmpPos;
|
|
|
|
while (benchstream) {
|
|
|
|
TrackPoint point;
|
|
|
|
benchstream >> point.time;
|
|
|
|
if (!benchstream) break;
|
|
|
|
benchstream >> point.position.x;
|
|
|
|
if (!benchstream) break;
|
|
|
|
benchstream >> point.position.y;
|
|
|
|
if (!benchstream) break;
|
|
|
|
benchstream >> point.position.z;
|
|
|
|
if (!benchstream) break;
|
|
|
|
benchstream >> point.angle.x;
|
|
|
|
if (!benchstream) break;
|
|
|
|
benchstream >> point.angle.y;
|
|
|
|
if (!benchstream) break;
|
|
|
|
benchstream >> point.angle.z;
|
|
|
|
if (!benchstream) break;
|
|
|
|
benchstream >> point.angle.w;
|
|
|
|
if (!benchstream) break;
|
|
|
|
if (track.size() == 0) {
|
|
|
|
tmpPos = point.position;
|
|
|
|
}
|
|
|
|
float pointDist = glm::distance(tmpPos, point.position);
|
|
|
|
tmpPos = point.position;
|
|
|
|
point.time = time + pointDist / 50.f;
|
|
|
|
time = point.time;
|
|
|
|
duration = std::max(duration, point.time);
|
|
|
|
track.push_back(point);
|
|
|
|
}
|
2016-04-19 02:20:54 +02:00
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
std::cout << "Loaded " << track.size() << " points" << std::endl;
|
2016-04-19 02:20:54 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void BenchmarkState::exit() {
|
|
|
|
std::cout << "Results =============\n"
|
|
|
|
<< "Benchmark: " << benchfile << "\n"
|
|
|
|
<< "Frames: " << frameCounter << "\n"
|
|
|
|
<< "Duration: " << duration << " seconds\n"
|
|
|
|
<< "Avg frametime: " << std::setprecision(3)
|
|
|
|
<< (duration / frameCounter) << " (" << (frameCounter / duration)
|
|
|
|
<< " fps)" << std::endl;
|
2016-04-19 02:20:54 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void BenchmarkState::tick(float dt) {
|
|
|
|
if (track.size() > 0) {
|
|
|
|
TrackPoint& a = track.front();
|
|
|
|
TrackPoint& b = track.back();
|
|
|
|
for (TrackPoint& p : track) {
|
|
|
|
if (benchmarkTime < p.time) {
|
|
|
|
b = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
a = p;
|
|
|
|
}
|
|
|
|
if (benchmarkTime > duration) {
|
|
|
|
StateManager::get().exit();
|
|
|
|
}
|
|
|
|
if (b.time != a.time) {
|
|
|
|
float alpha = (benchmarkTime - a.time) / (b.time - a.time);
|
|
|
|
trackCam.position = glm::mix(a.position, b.position, alpha);
|
|
|
|
trackCam.rotation = glm::slerp(a.angle, b.angle, alpha);
|
|
|
|
}
|
|
|
|
benchmarkTime += dt;
|
|
|
|
}
|
2016-04-19 02:20:54 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void BenchmarkState::draw(GameRenderer* r) {
|
|
|
|
frameCounter++;
|
|
|
|
State::draw(r);
|
2016-04-19 02:20:54 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
void BenchmarkState::handleEvent(const SDL_Event& e) {
|
|
|
|
State::handleEvent(e);
|
2016-04-19 02:20:54 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 22:13:20 +02:00
|
|
|
const ViewCamera& BenchmarkState::getCamera() {
|
|
|
|
return trackCam;
|
2016-04-19 02:20:54 +02:00
|
|
|
}
|