123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403 |
- /*!
- * \file src/main.cpp
- * \brief Where main() is
- *
- * \author xythobuz
- */
-
- #include <iostream>
- #include <cstdlib>
- #include <cstring>
-
- #include "global.h"
- #include "Camera.h"
- #include "Console.h"
- #include "Exception.h"
- #include "FontManager.h"
- #include "Game.h"
- #include "Menu.h"
- #include "OpenRaider.h"
- #include "Render.h"
- #include "TextureManager.h"
- #include "World.h"
- #include "commander/commander.h"
- #include "utils/strings.h"
- #include "utils/time.h"
-
- #ifndef UNIT_TEST
-
- #ifdef USING_AL
- #include "SoundAL.h"
- #else
- #include "SoundNull.h"
- #endif
-
- #ifdef USING_SDL
- #include "WindowSDL.h"
- #else
- #error No Windowing Library selected!
- #endif
-
- Camera &getCamera() {
- static Camera gCamera;
- return gCamera;
- }
-
- Console &getConsole() {
- static Console gConsole;
- return gConsole;
- }
-
- Font &getFont() {
- static FontManager gFont;
- return gFont;
- }
-
- Game &getGame() {
- static Game gGame;
- return gGame;
- }
-
- Menu &getMenu() {
- static Menu gMenu;
- return gMenu;
- }
-
- OpenRaider &getOpenRaider() {
- static OpenRaider gOpenRaider;
- return gOpenRaider;
- }
-
- Render &getRender() {
- static Render gRender;
- return gRender;
- }
-
- Sound &getSound() {
- #ifdef USING_AL
- static SoundAL gSound;
- #else
- static SoundNull gSound;
- #endif
- return gSound;
- }
-
- TextureManager &getTextureManager() {
- static TextureManager gTextureManager;
- return gTextureManager;
- }
-
- Window &getWindow() {
- #ifdef USING_SDL
- static WindowSDL gWindow;
- #endif
- return gWindow;
- }
-
- World &getWorld() {
- static World gWorld;
- return gWorld;
- }
-
- namespace {
- bool configFileWasSpecified = false;
-
- void configFileCallback(command_t *self) {
- getOpenRaider().loadConfig(self->arg);
- configFileWasSpecified = true;
- }
-
- void cleanupHandler(void) {
- #ifdef DEBUG
- std::cout << std::endl;
- std::cout << "Thanks for testing " << VERSION << std::endl;
- std::cout << "Build date: " << __DATE__ << " @ " << __TIME__ << std::endl;
- std::cout << "Build host: " << BUILD_HOST << std::endl;
- std::cout << "Web site : http://github.com/xythobuz/OpenRaider" << std::endl;
- std::cout << "Contact : xythobuz@xythobuz.de" << std::endl;
- #endif
- }
- }
-
- int main(int argc, char *argv[]) {
- command_t cmd;
- command_init(&cmd, argv[0], VERSION);
- //command_option(&cmd, "-v", "--verbose", "enable verbose output", functionPointer);
- command_option(&cmd, "-c", "--config <file>", "select config file to use", configFileCallback);
- command_parse(&cmd, argc, argv);
-
- if (!configFileWasSpecified) {
- if (getOpenRaider().loadConfig(DEFAULT_CONFIG_FILE) != 0) {
- getOpenRaider().loadConfig(DEFAULT_CONFIG_PATH "/" DEFAULT_CONFIG_FILE);
- }
- }
-
- #ifdef DEBUG
- getConsole().print("Initializing %s", VERSION);
- #endif
-
- atexit(cleanupHandler);
-
- int error = getOpenRaider().initialize();
- if (error != 0) {
- std::cout << "Could not initialize OpenRaider (" << error << ")!" << std::endl;
- return 2;
- }
-
- command_free(&cmd);
-
- getConsole().print("Starting %s", VERSION);
- getOpenRaider().run();
-
- return 0;
- }
-
- ActionEvents stringToActionEvent(const char *action) {
- if (strcmp(action, "menu") == 0) {
- return menuAction;
- } else if (strcmp(action, "console") == 0) {
- return consoleAction;
- } else if (strcmp(action, "forward") == 0) {
- return forwardAction;
- } else if (strcmp(action, "backward") == 0) {
- return backwardAction;
- } else if (strcmp(action, "left") == 0) {
- return leftAction;
- } else if (strcmp(action, "right") == 0) {
- return rightAction;
- } else if (strcmp(action, "jump") == 0) {
- return jumpAction;
- } else if (strcmp(action, "crouch") == 0) {
- return crouchAction;
- } else if (strcmp(action, "use") == 0) {
- return useAction;
- } else if (strcmp(action, "holster") == 0) {
- return holsterAction;
- } else if (strcmp(action, "walk") == 0) {
- return walkAction;
- } else {
- getConsole().print("Unknown action: \"%s\"", action);
- return ActionEventCount;
- }
- }
-
- KeyboardButton stringToKeyboardButton(const char *key) {
- size_t length = strlen(key);
- if ((key[0] == '\'') && (key[length - 1] == '\'') && (length == 3)) {
- // Literal character like w, a, s, d, 0, 1...
- char c = key[1];
- if (((c >= '0') && (c <= '9'))
- || ((c >= 'a') && (c <= 'z')))
- return (KeyboardButton)c;
- } else if ((key[0] == '\"') && (key[length - 1] == '\"')) {
- // Special characters like tilde, esc, quote...
- char *tmp = stringRemoveQuotes(key);
- if (strcmp(tmp, "quote") == 0) {
- delete [] tmp;
- return quoteKey;
- } else if (strcmp(tmp, "backslash") == 0) {
- delete [] tmp;
- return backslashKey;
- } else if (strcmp(tmp, "backspace") == 0) {
- delete [] tmp;
- return backspaceKey;
- } else if (strcmp(tmp, "capslock") == 0) {
- delete [] tmp;
- return capslockKey;
- } else if (strcmp(tmp, "comma") == 0) {
- delete [] tmp;
- return commaKey;
- } else if (strcmp(tmp, "del") == 0) {
- delete [] tmp;
- return delKey;
- } else if (strcmp(tmp, "up") == 0) {
- delete [] tmp;
- return upKey;
- } else if (strcmp(tmp, "down") == 0) {
- delete [] tmp;
- return downKey;
- } else if (strcmp(tmp, "left") == 0) {
- delete [] tmp;
- return leftKey;
- } else if (strcmp(tmp, "right") == 0) {
- delete [] tmp;
- return rightKey;
- } else if (strcmp(tmp, "end") == 0) {
- delete [] tmp;
- return endKey;
- } else if (strcmp(tmp, "equals") == 0) {
- delete [] tmp;
- return equalsKey;
- } else if (strcmp(tmp, "escape") == 0) {
- delete [] tmp;
- return escapeKey;
- } else if (strcmp(tmp, "f1") == 0) {
- delete [] tmp;
- return f1Key;
- } else if (strcmp(tmp, "f2") == 0) {
- delete [] tmp;
- return f2Key;
- } else if (strcmp(tmp, "f3") == 0) {
- delete [] tmp;
- return f3Key;
- } else if (strcmp(tmp, "f4") == 0) {
- delete [] tmp;
- return f4Key;
- } else if (strcmp(tmp, "f5") == 0) {
- delete [] tmp;
- return f5Key;
- } else if (strcmp(tmp, "f6") == 0) {
- delete [] tmp;
- return f6Key;
- } else if (strcmp(tmp, "f7") == 0) {
- delete [] tmp;
- return f7Key;
- } else if (strcmp(tmp, "f8") == 0) {
- delete [] tmp;
- return f8Key;
- } else if (strcmp(tmp, "f9") == 0) {
- delete [] tmp;
- return f9Key;
- } else if (strcmp(tmp, "f10") == 0) {
- delete [] tmp;
- return f10Key;
- } else if (strcmp(tmp, "f11") == 0) {
- delete [] tmp;
- return f11Key;
- } else if (strcmp(tmp, "f12") == 0) {
- delete [] tmp;
- return f12Key;
- } else if (strcmp(tmp, "backquote") == 0) {
- delete [] tmp;
- return backquoteKey;
- } else if (strcmp(tmp, "home") == 0) {
- delete [] tmp;
- return homeKey;
- } else if (strcmp(tmp, "insert") == 0) {
- delete [] tmp;
- return insertKey;
- } else if (strcmp(tmp, "leftalt") == 0) {
- delete [] tmp;
- return leftaltKey;
- } else if (strcmp(tmp, "leftctrl") == 0) {
- delete [] tmp;
- return leftctrlKey;
- } else if (strcmp(tmp, "leftbracket") == 0) {
- delete [] tmp;
- return leftbracketKey;
- } else if (strcmp(tmp, "leftgui") == 0) {
- delete [] tmp;
- return leftguiKey;
- } else if (strcmp(tmp, "leftshift") == 0) {
- delete [] tmp;
- return leftshiftKey;
- } else if (strcmp(tmp, "minus") == 0) {
- delete [] tmp;
- return minusKey;
- } else if (strcmp(tmp, "numlock") == 0) {
- delete [] tmp;
- return numlockKey;
- } else if (strcmp(tmp, "pagedown") == 0) {
- delete [] tmp;
- return pagedownKey;
- } else if (strcmp(tmp, "pageup") == 0) {
- delete [] tmp;
- return pageupKey;
- } else if (strcmp(tmp, "pause") == 0) {
- delete [] tmp;
- return pauseKey;
- } else if (strcmp(tmp, "dot") == 0) {
- delete [] tmp;
- return dotKey;
- } else if (strcmp(tmp, "rightalt") == 0) {
- delete [] tmp;
- return rightaltKey;
- } else if (strcmp(tmp, "rightctrl") == 0) {
- delete [] tmp;
- return rightctrlKey;
- } else if (strcmp(tmp, "enter") == 0) {
- delete [] tmp;
- return enterKey;
- } else if (strcmp(tmp, "rightgui") == 0) {
- delete [] tmp;
- return rightguiKey;
- } else if (strcmp(tmp, "rightbracket") == 0) {
- delete [] tmp;
- return rightbracketKey;
- } else if (strcmp(tmp, "rightshift") == 0) {
- delete [] tmp;
- return rightshiftKey;
- } else if (strcmp(tmp, "scrolllock") == 0) {
- delete [] tmp;
- return scrolllockKey;
- } else if (strcmp(tmp, "semicolon") == 0) {
- delete [] tmp;
- return semicolonKey;
- } else if (strcmp(tmp, "slash") == 0) {
- delete [] tmp;
- return slashKey;
- } else if (strcmp(tmp, "space") == 0) {
- delete [] tmp;
- return spaceKey;
- } else if (strcmp(tmp, "tab") == 0) {
- delete [] tmp;
- return tabKey;
- } else if (strcmp(tmp, "leftmouse") == 0) {
- delete [] tmp;
- return leftmouseKey;
- } else if (strcmp(tmp, "middlemouse") == 0) {
- delete [] tmp;
- return middlemouseKey;
- } else if (strcmp(tmp, "rightmouse") == 0) {
- delete [] tmp;
- return rightmouseKey;
- } else if (strcmp(tmp, "fourthmouse") == 0) {
- delete [] tmp;
- return fourthmouseKey;
- } else if (strcmp(tmp, "fifthmouse") == 0) {
- delete [] tmp;
- return fifthmouseKey;
- }
- delete [] tmp;
- }
-
- getConsole().print("Unknown key: %s", key);
- return unknownKey;
- }
-
- #endif // UNIT_TEST
-
- #if defined(HAVE_EXECINFO_H) && defined(HAVE_BACKTRACE) && defined(HAVE_BACKTRACE_SYMBOLS)
- #ifndef NDEBUG
-
- #include <exception>
- #include <execinfo.h>
-
- namespace {
- extern std::terminate_handler oldTerminateHandler;
-
- [[noreturn]] void terminateHandler() {
- const unsigned int maxSize = 128;
- void *callstack[maxSize];
- int frames = backtrace(callstack, maxSize);
- char **strs = backtrace_symbols(callstack, frames);
-
- std::cout << std::endl;
- for (int i = 0; i < frames; i++)
- std::cout << strs[i] << std::endl;
-
- delete [] strs;
-
- std::cout << std::endl << "Last custom Exception:" << std::endl;
- std::cout << " " << Exception::getLastException() << std::endl << std::endl;
-
- oldTerminateHandler();
- abort();
- }
-
- std::terminate_handler oldTerminateHandler = std::set_terminate(terminateHandler);
- }
-
- #endif // NDEBUG
- #endif // HAVE_EXECINFO_H && HAVE_BACKTRACE && HAVE_BACKTRACE_SYMBOLS
|