ST_engine  0.3-ALPHA
main.cpp
1 
10 #include <main/main.hpp>
11 #include "metrics.hpp"
12 
13 #ifdef TESTING
14 message_bus gMessage_bus;
15 #endif
16 
21 #ifndef TESTING
22 
23 int main(int argc, char *argv[]) {
24 #elif defined(TESTING)
25  int ST_engine_main(int argc, char *argv[]) {
26 #endif
27  //we get rid of the two warnings for unused parameters.
28  //int main() will work fine on linux, but not on Windows
29  //where SDL does some weird stuff with the main function,
30  //so we need to specify main like this to avoid conflict with SDL
31  (void) argc;
32  (void) argv;
33 
34  //Order of subsystem initialization is crucial
35 #ifndef TESTING
36  message_bus gMessage_bus;
37 #endif
38  fps gFps;
39  console gConsole(gMessage_bus);
40  gConsole.set_log_level(ST::log_type::INFO | ST::log_type::SUCCESS | ST::log_type::ERROR);
41 
42  //TODO: The initial resolution does not get passed to the game_manager due to the initialization order
43  task_manager gTask_manager;
44  audio_manager gAudio_manager(gTask_manager, gMessage_bus);
45  input_manager gInput_manager(gTask_manager, gMessage_bus);
46  window_manager gDisplay_manager(gMessage_bus, gTask_manager, "ST");
47  drawing_manager gDrawing_manager(gDisplay_manager.get_window(), gMessage_bus);
48 
49  assets_manager gAssets_manager(gMessage_bus, gTask_manager);
50  physics_manager gPhysics_manager(gMessage_bus);
51  game_manager gGame_manager(gMessage_bus);
52  timer gTimer;
53 
54  gGame_manager.update();
55  gConsole.post_init();
56 
57  //time keeping variables
58  const double LOGIC_UPDATE_RATE = 16.66666;
59  double total_time = 0;
60  double current_time = gTimer.time_since_start();
61  double frame_time;
62  double new_time;
63 
64  ST::metrics gMetrics{};
65  assets_manager::update_task(&gAssets_manager);
66  gDisplay_manager.update();
67 
68  //main loop
69  while (gGame_manager.game_is_running()) {
70  new_time = gTimer.time_since_start();
71  frame_time = new_time - current_time;
72  current_time = new_time;
73  total_time += frame_time;
74 
75  gMetrics.reset_accumulators();
76 
77  if (total_time >= LOGIC_UPDATE_RATE) {
78  do {
79  gInput_manager.update();
80 
81  double game_logic_begin = gTimer.time_since_start();
82 
83  gGame_manager.update();
84  double game_logic_end = gTimer.time_since_start();
85  gMetrics.game_logic_time += game_logic_end - game_logic_begin;
86 
87  double physics_begin = gTimer.time_since_start();
88  gPhysics_manager.update(*gGame_manager.get_level());
89  double physics_end = gTimer.time_since_start();
90  gMetrics.physics_time += physics_end - physics_begin;
91 
92  total_time -= LOGIC_UPDATE_RATE;
93  } while (total_time >= LOGIC_UPDATE_RATE);
94  //All three start their own update tasks which run in the background
95  gAssets_manager.update();
96  gDisplay_manager.update();
97  gAudio_manager.update();
98  }
99  gConsole.update();
100  gFps.update(current_time, frame_time);
101 
102  gMetrics.frame_time = frame_time;
103 
104  double render_begin = gTimer.time_since_start();
105  gDrawing_manager.update(*gGame_manager.get_level(), gFps.get_value(), gConsole, gMetrics);
106  double render_end = gTimer.time_since_start();
107  gMetrics.render_time = render_end - render_begin;
108  }
109  return 0;
110 }
This object is responsible for loading/unloading assets.
static void update_task(void *arg)
This object is responsible for playing sounds and music.
This object represents the console window.
Definition: console.hpp:17
This object is responsible for issuing drawing commands and drawing the current level.
An fps counter.
Definition: fps.hpp:21
void update(double time, double frame_time)
Definition: fps.cpp:17
float get_value() const
Definition: fps.cpp:41
This class is responsible for managing all levels and the lua backend, it is the heart of the engine.
This object is responsible for taking input.
The central messaging system of the engine. All subsystem make extensive use of it.
Definition: message_bus.hpp:29
This class handles all physics related actions in the engine.
The Task Manager handles all things multi-threaded in the engine.
A timer used for keeping track of time in the main loop.
Definition: timer.hpp:19
double time_since_start() const
Definition: timer.cpp:22
This object is responsible for managing the window.