MCPcopy Create free account
hub / github.com/Hopson97/HopsonCraft / runMainGameLoop

Method runMainGameLoop

Source/Application.cpp:19–62  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17}
18
19void Application::runMainGameLoop()
20{
21 sf::Clock gameTimer;
22
23 auto MS_PER_UPDATE = 0.05; //20 Ticks/ updates per second
24
25 float lastTime = gameTimer.getElapsedTime().asSeconds();
26 float lag = 0.0f;
27 while (Display::isOpen())
28 {
29 float current = gameTimer.getElapsedTime().asSeconds();
30 float elapsed = current - lastTime;
31 lastTime = current;
32 lag += elapsed;
33
34 sf::Event e;
35 while (Display::get().pollEvent(e))
36 {
37 handleEvents(e);
38 }
39 if (!Display::isOpen() || m_states.empty())
40 {
41 break;
42 }
43
44 m_states.back()->input (m_camera);
45
46 //Fixed time-step of "MS_PER_UPDATE"
47 while (lag >= MS_PER_UPDATE)
48 {
49 update(elapsed);
50 lag -= MS_PER_UPDATE;
51 }
52 ///@TODO Are the the two update function calls able to be put in the fixed
53 ///time step thing above?
54 m_camera.update();
55 m_states.back()->update(m_camera, elapsed);
56
57 m_states.back()->draw(m_renderer);
58
59 m_renderer.clear();
60 m_renderer.update(m_camera);
61 }
62}
63
64void Application::update(float elapsed)
65{

Callers 1

runGameFunction · 0.80

Calls 6

isOpenFunction · 0.85
updateFunction · 0.85
inputMethod · 0.45
updateMethod · 0.45
drawMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected