今回はゲーム内の処理時間がどのぐらいかと言うのを計測してグラフを描画してやろうというお話です。
Profilerクラスを新たに作り、開始と終了時にメンバ関数を呼ぶと任意フレーム間の平均値を取って
平均フレーム時間を棒グラフで描画してくれるというものです。Fpsクラスを参考にしました。いずれFpsクラスの機能をProfilerに内包したい...
相変わらず美しいとはいい難いソースですが...
Profiler.h
#pragma once
#include
#include
class Profiler {
public:
Profiler() : _begin(0), _end(0), _enableFlag(false) {}
void update();
bool draw() const;
void begin() { _begin = GetNowHiPerformanceCount(); }
void end() { _end = GetNowHiPerformanceCount(); }
void setMode(bool enableFlag) { _enableFlag = enableFlag; };
private:
std::list _list;
std::list _ave;
LONGLONG _begin, _end;
int _counter;
bool _enableFlag;
const int SAMPLE_LEN = 240;
const int AVE_LEN = 10;
};
#include "Profiler.h"
#include
void Profiler::update() {
double differ = (double)(_end-_begin)/1000;
int listLen = _list.size();
int aveLen = _ave.size();
_counter++;
_ave.push_back(differ);
if (aveLen > AVE_LEN) { //サンプル数を超えたらポップ
_ave.pop_front();
}
//平均の長さフレーム毎に平均をとる
if (_counter%AVE_LEN == 0) {
double sum = 0;
for (auto itr = _ave.begin(); itr != _ave.end(); itr++) {
sum += *itr;
}
sum = sum / AVE_LEN;
_list.push_back(sum);
if (listLen > SAMPLE_LEN) { //サンプル数を超えたらポップ
_list.pop_front();
}
}
}
bool Profiler::draw() const {
if (_enableFlag != true) {
return false;
}
int i = 0;
int red = GetColor(255, 64, 64);
//グラフの表示位置の設定
const int START_X = 64, START_Y = 64;
const int SIZE_X = SAMPLE_LEN, SIZE_Y = 128;
//枠を描画
DrawBox(START_X,START_Y,START_X + SIZE_X,START_Y + SIZE_Y, red, false);
//棒グラフを描画
for (auto itr = _list.begin(); itr != _list.end(); itr++,i++) {
double current = *itr * 20;
double x = START_X + SIZE_X - i;
double y_1 = START_Y + SIZE_Y - current;
double y_2 = START_Y + SIZE_Y;
DrawLine(x, y_1, x, y_2, red);
}
DrawFormatString(START_X, START_Y, red, "%.2f ms", _list.back());
return true;
}