SuperTuxKart
main_loop.hpp
1 //
2 // SuperTuxKart - a fun racing game with go-kart
3 // Copyright (C) 2004-2015 Ingo Ruhnke <grumbel@gmx.de>
4 // Copyright (C) 2006-2015 SuperTuxKart-Team
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 3
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 
20 #ifndef HEADER_MAIN_LOOP_HPP
21 #define HEADER_MAIN_LOOP_HPP
22 
23 #include "utils/synchronised.hpp"
24 #include "utils/types.hpp"
25 #include <atomic>
26 #include <chrono>
27 
30 class MainLoop
31 {
32 private:
33  using TimePoint = std::chrono::steady_clock::time_point;
35  std::atomic_bool m_abort;
36 
37  std::atomic_bool m_request_abort;
38 
39  std::atomic_bool m_paused;
40 
43 
46 
47  bool m_frame_before_loading_world;
48 
49  bool m_download_assets;
50 
51  Synchronised<int> m_ticks_adjustment;
52 
53  TimePoint m_curr_time;
54  TimePoint m_prev_time;
55  unsigned m_parent_pid;
56  double getLimitedDt();
57  void updateRace(int ticks, bool fast_forward);
58  double convertToTime(const TimePoint& cur, const TimePoint& prev) const
59  {
60  auto duration = cur - prev;
61  auto value =
62  std::chrono::duration_cast<std::chrono::nanoseconds>(duration);
63  return value.count() / (1000.0 * 1000.0);
64  }
65 public:
66  MainLoop(unsigned parent_pid, bool download_assets = false);
67  ~MainLoop();
68  void run();
70  void abort() { m_abort = true; }
71  void requestAbort() { m_request_abort = true; }
72  void setThrottleFPS(bool throttle) { m_throttle_fps = throttle; }
73  void setAllowLargeDt(bool enable) { m_allow_large_dt = enable; }
74  void renderGUI(int phase, int loop_index=-1, int loop_size=-1);
75  // ------------------------------------------------------------------------
77  bool isAborted() const { return m_abort; }
78  // ------------------------------------------------------------------------
79  void setFrameBeforeLoadingWorld() { m_frame_before_loading_world = true; }
80  // ------------------------------------------------------------------------
81  void setTicksAdjustment(int ticks)
82  {
83  m_ticks_adjustment.lock();
84  m_ticks_adjustment.getData() += ticks;
85  m_ticks_adjustment.unlock();
86  }
87  // ------------------------------------------------------------------------
88  void setPaused(bool val) { m_paused.store(val); }
89  // ------------------------------------------------------------------------
90  bool isPaused() const { return m_paused.load(); }
91 }; // MainLoop
92 
93 extern MainLoop* main_loop;
94 
95 #endif
96 
97 /* EOF */
Management class for the whole gameflow, this is where the main-loop is.
Definition: main_loop.hpp:31
void run()
Run the actual main loop.
Definition: main_loop.cpp:400
void updateRace(int ticks, bool fast_forward)
Updates all race related objects.
Definition: main_loop.cpp:320
double getLimitedDt()
Returns the current dt, which guarantees a limited frame rate.
Definition: main_loop.cpp:196
std::atomic_bool m_abort
True if the main loop should exit.
Definition: main_loop.hpp:35
bool m_throttle_fps
True if the frame rate should be throttled.
Definition: main_loop.hpp:42
void abort()
Set the abort flag, causing the mainloop to be left.
Definition: main_loop.hpp:70
bool m_allow_large_dt
True if dt is not decreased for low fps.
Definition: main_loop.hpp:45
void renderGUI(int phase, int loop_index=-1, int loop_size=-1)
Renders the GUI.
Definition: main_loop.cpp:759
bool isAborted() const
Returns true if STK is to be stoppe.
Definition: main_loop.hpp:77
void lock() const
Locks the mutex.
Definition: synchronised.hpp:95
void unlock() const
Unlocks the mutex.
Definition: synchronised.hpp:99
TYPE & getData()
Returns a reference to the original data file.
Definition: synchronised.hpp:82
Declares the general types that are used by the network.