summaryrefslogtreecommitdiffstats
path: root/src/Utility.cpp
blob: 9fecc084a487cf8018139cf270c96a839faadab9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <thread>
#include "Utility.hpp"

GLenum glCheckError_(const char *file, int line) {
	GLenum errorCode;
	while ((errorCode = glGetError()) != GL_NO_ERROR) {
		std::string error;
		switch (errorCode) {
			case GL_INVALID_ENUM:
				error = "INVALID_ENUM";
				break;
			case GL_INVALID_VALUE:
				error = "INVALID_VALUE";
				break;
			case GL_INVALID_OPERATION:
				error = "INVALID_OPERATION";
				break;
			case GL_STACK_OVERFLOW:
				error = "STACK_OVERFLOW";
				break;
			case GL_STACK_UNDERFLOW:
				error = "STACK_UNDERFLOW";
				break;
			case GL_OUT_OF_MEMORY:
				error = "OUT_OF_MEMORY";
				break;
			case GL_INVALID_FRAMEBUFFER_OPERATION:
				error = "INVALID_FRAMEBUFFER_OPERATION";
				break;
		}
		static int t = 0;
		t++;
		if (t > 10)
			LOG(FATAL);
		LOG(ERROR) << "OpenGL error: " << error << " at " << file << ":" << line;
	}
	return errorCode;
}

LoopExecutionTimeController::LoopExecutionTimeController(duration delayLength) : delayLength(delayLength) {
	previousUpdate = clock::now();
}

LoopExecutionTimeController::~LoopExecutionTimeController() {

}

void LoopExecutionTimeController::SetDelayLength(duration length) {
	delayLength = length;
}

unsigned long long LoopExecutionTimeController::GetIterations() {
	return iterations;
}

void LoopExecutionTimeController::Update() {
	iterations++;
	auto timeToSleep = delayLength - GetDelta();
	if (timeToSleep.count()>0)
		std::this_thread::sleep_for(timeToSleep);
	previousUpdate = clock::now();
}

double LoopExecutionTimeController::GetDeltaMs() {
	auto now = clock::now();
	return duration(now-previousUpdate).count();
}

LoopExecutionTimeController::duration LoopExecutionTimeController::GetDelta() {
	auto now = clock::now();
	//std::cerr<<duration(now-previousUpdate).count()<<std::endl;
	return duration(now-previousUpdate);
}