Пример #1
0
int WINAPI WinMain(HINSTANCE,HINSTANCE,LPSTR,int){

	SetWindowText("Title");
	SetGraphMode(WINDOW_WIDTH , WINDOW_HEIGHT,32 );
	ChangeWindowMode(TRUE), DxLib_Init(), SetDrawScreen( DX_SCREEN_BACK );

	int LoadImage = LoadGraph("Natsuiro/BLOCK/load.png");
	DrawExtendGraph(0,0,WINDOW_WIDTH,WINDOW_HEIGHT, LoadImage ,false);
	ScreenFlip();
	
	SetTransColor(255,0,255);
	Awake();

	long long TIME = GetNowHiPerformanceCount();
#	if	BENCHMARK == TRUE
	long long int count = GetNowCount();
#	endif

	while( ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 && !CheckHitKey(KEY_INPUT_ESCAPE) ){
		GameLoop();
		Sleep( (unsigned long)max( 16 - (int)( GetNowHiPerformanceCount() - TIME ) / 1000 , 0 ) );
		TIME = GetNowHiPerformanceCount();

#		if BENCHMARK == TRUE
		DrawFormatString(WINDOW_WIDTH-200,0,BLACK,"FPS %d (%dms)", (int)( 1000/( GetNowCount() - count ) ) , GetNowCount() - count );
		count = GetNowCount();
#		endif

	}
        
	DxLib_End();
	return 0;
} 
//画面の更新(ここで今は何のシーンかを判断・切り替える)
void FpsController::update()
{
	//初期値として前フレームにおける時間を疑似的に与える 【?】
	if (beforeTime == -1){
		beforeTime = GetNowHiPerformanceCount() - (100000 / standardFps);
	}
	//【GetNowHiPerformanceCount】 GetNowCountの高精度ver. それまでのミリ秒からマイクロ秒単位に。
	//戻り値は【long long】。 longの更にlongってどういう事なの・・・

	//フレームかうんとが規定値を超えていた場合初期化する
	if (countFrames >= updateFrames){
		countFrames = 0;
	}

	//1フレーム目(初期化直後)開始時の時間を記録しておく(上のif文と一緒くたではだめなのか?)
	if (countFrames == 0){
		startTime = GetNowHiPerformanceCount();
	}

	//1フレームの処理時間の計測
	long long nowTime = GetNowHiPerformanceCount();

	//カウンタが1週した場合の例外処理
	//numeric_limitsに関する記述は下記URL参照のこと
	// http://d.hatena.ne.jp/eldesh/20110120/1295497346
	if (nowTime < beforeTime){
		totalTime += (std::numeric_limits<long long>::max)() - beforeTime + nowTime;
	}
	else{
		totalTime += nowTime - beforeTime;
	}

	//今の時間を以前の時間として記憶
	beforeTime = nowTime;

	//フレームのカウントアップ
	++countFrames;

	//フレームカウントが規定値を超えていた場合、平均FPS(フレームレート)を算出する。
	if (countFrames >= updateFrames){
		//【static_cast】 本来ならばエラーの出る処理を「わざとやっている」と知らせるために付けておくもの。
		double aveTime = totalTime / static_cast<double>(countFrames);
		if (aveTime > 0){
			//1Mμsec (= 1sec)
			averageFps = 1000000 / aveTime;
		}
		else{
			averageFps = 0.0;
		}
		totalTime = 0;
	}

}
//待機処理・fpsカウント
void FpsController::wait(){
	//経過秒数を求め、規定FPSで動作しているように処理動作を調整する
	long long nowTime = GetNowHiPerformanceCount();
	long long tookTime;

	//カウンタが
	if (nowTime < beforeTime){
		totalTime += (std::numeric_limits<long long>::max)() - beforeTime + nowTime;
	}
	else{
		totalTime += nowTime - beforeTime;
	}
}