OpenRaider  0.1.4-dev
Open Source Tomb Raider Game Engine implementation
main.cpp
Go to the documentation of this file.
1 
8 #include <iostream>
9 #include <memory>
10 
11 #include "global.h"
12 #include "Camera.h"
13 #include "Log.h"
14 #include "Menu.h"
15 #include "Render.h"
16 #include "RunTime.h"
17 #include "SoundManager.h"
18 #include "TextureManager.h"
19 #include "UI.h"
20 #include "World.h"
21 #include "commander/commander.h"
22 #include "commands/Command.h"
23 #include "system/Font.h"
24 #include "system/Shader.h"
25 #include "system/Sound.h"
26 #include "system/Window.h"
27 #include "utils/time.h"
28 
29 static std::string configFileToUse;
30 static std::shared_ptr<World> gWorld;
31 
33  return *gWorld;
34 }
35 
36 int main(int argc, char* argv[]) {
37  command_t cmd;
38  command_init(&cmd, argv[0], VERSION);
39  command_option(&cmd, "-c", "--config <file>", "select config file to use",
40  [](command_t* self) {
41  configFileToUse = self->arg;
42  });
43  command_parse(&cmd, argc, argv);
44  command_free(&cmd);
45 
46  // RunTime is required by other constructors
48 
49  gWorld.reset(new World());
50 
52 
53  Log::get(LOG_INFO) << "Initializing " << VERSION << Log::endl;
54 
55  // Initialize Windowing
56  int error = Window::initialize();
57  if (error != 0) {
58  std::cout << "Could not initialize Window (" << error << ")!" << std::endl;
59  return -1;
60  }
61 
62  error = Shader::initialize();
63  if (error != 0) {
64  std::cout << "Could not initialize OpenGL (" << error << ")!" << std::endl;
65  return -2;
66  }
67 
68  // Initialize Texture Manager
70  if (error != 0) {
71  std::cout << "Could not initialize TextureManager (" << error << ")!" << std::endl;
72  return -3;
73  }
74 
75  if (configFileToUse == "") {
76  if (Command::executeFile(DEFAULT_CONFIG_FILE) != 0) {
77  if (Command::executeFile(std::string(DEFAULT_CONFIG_PATH) + "/" + DEFAULT_CONFIG_FILE) != 0) {
78  std::string p = INSTALL_PREFIX;
79  if (p != "/")
80  p += "/";
81  p += "share/OpenRaider/";
82  Command::executeFile(p + DEFAULT_CONFIG_FILE);
83  }
84  }
85  } else {
87  }
88 
90  if (error != 0) {
91  std::cout << "Coult not load Splash Texture (" << error << ")!" << std::endl;
92  return -4;
93  }
94 
95  // Initialize Sound
96  error = Sound::initialize();
97  if (error != 0) {
98  std::cout << "Could not initialize Sound (" << error << ")!" << std::endl;
99  return -5;
100  }
101 
102  // Initialize Debug UI
103  error = UI::initialize();
104  if (error != 0) {
105  std::cout << "Could not initialize Debug UI (" << error << ")!" << std::endl;
106  return -6;
107  }
108 
109  // Initialize Menu
110  error = Menu::initialize();
111  if (error != 0) {
112  std::cout << "Could not initialize Menu (" << error << ")!" << std::endl;
113  return -7;
114  }
115 
116  Log::get(LOG_INFO) << "Starting " << VERSION << Log::endl;
118  Menu::setVisible(true);
120  RunTime::setRunning(true);
122 
123  while (RunTime::isRunning()) {
125  renderFrame();
126  }
127 
128  Menu::shutdown();
129  UI::shutdown();
130  Font::shutdown();
131  Sound::shutdown();
134 
135 #ifdef DEBUG
136  std::cout << std::endl;
137  std::cout << "Thanks for testing " << VERSION << std::endl;
138  std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
139  std::cout << "Build host: " << BUILD_HOST << std::endl;
140  std::cout << "Web site : http://github.com/xythobuz/OpenRaider" << std::endl;
141  std::cout << "Contact : xythobuz@xythobuz.de" << std::endl;
142 #endif
143 
144  return 0;
145 }
146 
147 void renderFrame() {
148  Render::display();
149  UI::display();
152 }
153 
154 #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
155 #ifndef NODEBUG
156 
157 #include <exception>
158 #include <execinfo.h>
159 
160 [[noreturn]] static void terminateHandler();
161 static std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
162 
163 [[noreturn]] static void terminateHandler() {
164  const unsigned int maxSize = 128;
165  void* callstack[maxSize];
166  int frames = backtrace(callstack, maxSize);
167  char** strs = backtrace_symbols(callstack, frames);
168 
169  std::cout << std::endl;
170  for (int i = frames; i > 0; i++)
171  std::cout << strs[i - 1] << std::endl;
172 
173  delete [] strs;
174 
175  oldTerminateHandler();
176  abort();
177 }
178 
179 #endif // NODEBUG
180 #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS
181 
static int initialize()
Definition: Sound.cpp:15
static int initialize()
Definition: UI.cpp:44
static int initializeSplash()
static void setMode(RenderMode m)
Definition: Render.h:40
World & getWorld()
Definition: main.cpp:32
Time handling utilities.
OpenGL Shader Implementation.
The game world (model)
Definition: World.h:25
static std::shared_ptr< World > gWorld
Definition: main.cpp:30
World Model.
Commands.
Main Menu.
static void shutdown()
Definition: Shader.cpp:262
Sound Source Manager.
Included everywhere.
static void shutdown()
Definition: Sound.cpp:23
static LogLevel & get(int level)
Definition: Log.cpp:14
static bool isRunning()
Definition: RunTime.h:35
static void display()
Definition: UI.cpp:220
static void fillCommandList()
Definition: Command.cpp:28
static void display()
Definition: Render.cpp:32
static int initialize()
Definition: Shader.cpp:218
void command_free(command_t *self)
Definition: commander.c:82
Global Logging Utility.
Windowing Interface.
static std::string configFileToUse
Definition: main.cpp:29
static void error(char *msg)
Definition: commander.c:19
World Renderer.
static void setRunning(bool run)
Definition: RunTime.h:36
#define LOG_INFO
Definition: Log.h:21
static void shutdown()
Definition: Font.cpp:23
static void initialize()
Definition: RunTime.cpp:33
static void eventHandling()
Definition: Window.cpp:35
Font Interface.
static const char endl
Definition: Log.h:35
Runtime Configuration Storage.
void command_init(command_t *self, const char *name, const char *version)
Definition: commander.c:66
static int executeFile(std::string file)
Definition: Command.cpp:99
void systemTimerReset()
Reset the system timer.
Definition: time.cpp:23
static void shutdown()
Definition: Window.cpp:51
void command_option(command_t *self, const char *small, const char *large, const char *desc, command_callback_t cb)
Definition: commander.c:173
void command_parse(command_t *self, int argc, char **argv)
Definition: commander.c:266
static void swapBuffers()
Definition: Window.cpp:43
UI/Event Manager.
static int initialize()
Definition: Menu.cpp:32
void renderFrame()
Definition: main.cpp:147
Texture Registry.
Camera, View Frustum.
static void shutdown()
Definition: UI.cpp:351
static void shutdown()
Definition: Menu.cpp:73
int main(int argc, char *argv[])
Definition: main.cpp:36
static void setVisible(bool v)
Definition: Menu.h:23
static int initialize()
Definition: Window.cpp:21
Sound Interface.
static int initialize()
static void setSize(glm::i32vec2 s)
Definition: Camera.cpp:80
static glm::i32vec2 getSize()
Definition: Window.cpp:71
static void updateFPS()
Definition: RunTime.cpp:66