-
Notifications
You must be signed in to change notification settings - Fork 7
/
Engine.cpp
72 lines (63 loc) · 1.65 KB
/
Engine.cpp
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
/* Copyright (c) 2020 by Stan Fortoński */
#include "Engine.hpp"
namespace Engine
{
void Engine::initGLFW() const
{
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, Config::get().getMajorVersion());
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, Config::get().getMinorVersion());
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
}
void Engine::initGLEW() const
{
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK)
throw std::runtime_error("Can\'t init GLEW");
}
void Engine::initDefaultOptionsGL() const
{
if (Config::get().getSamples() > 0)
glEnable(GL_MULTISAMPLE);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glEnable(GL_CULL_FACE);
glEnable(GL_BLEND);
glEnable(GL_CLIP_DISTANCE0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void Engine::initDeltaTime()
{
float currentTime = glfwGetTime();
deltaTime = currentTime - lastTime;
lastTime = currentTime;
}
void Engine::calcFPS()
{
float currentTime = glfwGetTime();
if ((currentTime - lastTimeFromShow) >= 1)
{
actualFPS = std::to_string(frames)+" FPS";
frames = 0;
lastTimeFromShow = currentTime;
}
else ++frames;
}
void Engine::limitFPS(unsigned limit)
{
static double lastTime = 0;
while (glfwGetTime() < lastTime + 1.0/limit){;}
lastTime += 1.0/limit;
}
#if DEBUG_ENGINE == 1
void Engine::showErrors()
{
GLenum err;
while ((err = glGetError()) != GL_NO_ERROR)
std::cout<<"GL error code: "<<err<<std::endl;
}
#endif
}