|
1 | | -//////////////////////////////////////////////////////////////////////////////////////// |
| 1 | +//////////////////////////////////////////////////////////////////////////////////////// |
2 | 2 | // Copyright(C) 2019 - 2025 Dusan Jocic <dusanjocic@msn.com> |
3 | 3 | // |
4 | 4 | // This file is part of OpenWolf. |
|
19 | 19 | // |
20 | 20 | // ------------------------------------------------------------------------------------- |
21 | 21 | // File name: systemLauncher.cpp |
22 | | -// Version: v1.01 |
| 22 | +// Version: v1.02 |
23 | 23 | // Created: 11/06/2019 |
24 | 24 | // Compilers: Microsoft (R) C/C++ Optimizing Compiler Version 19.26.28806 for x64, |
25 | 25 | // gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0, |
|
38 | 38 |
|
39 | 39 | #include <iostream> |
40 | 40 | #include <string> |
41 | | -#include <stdexcept> |
| 41 | +#include <memory> |
42 | 42 |
|
43 | | -int main(int argc, char **argv) { |
44 | | - std::string dynName; |
45 | | -#if defined (_WIN32) |
46 | | - dynName = "engine.AMD64.dll"; |
47 | | -#elif defined (__LINUX__) |
48 | | - dynName = "./engine.x86_64.so"; |
| 43 | +#ifdef _WIN32 |
| 44 | +constexpr const char* ENGINE_LIB = "engine.AMD64.dll"; |
| 45 | +#elif defined(__LINUX__) |
| 46 | +constexpr const char* ENGINE_LIB = "./engine.x86_64.so"; |
49 | 47 | #else |
50 | | - dynName = "./engine.x86_64.dylib"; |
| 48 | +constexpr const char* ENGINE_LIB = "./engine.x86_64.dylib"; |
51 | 49 | #endif |
52 | | - const std::string engineExportName = "engineMain"; |
53 | 50 |
|
54 | | - SDL_Init(0); |
55 | | - const auto libraryHandle = SDL_LoadObject(dynName.c_str()); |
| 51 | +constexpr const char* ENGINE_EXPORT_FUNC = "engineMain"; |
| 52 | + |
| 53 | +typedef int (*EngineMainFunc)(int, char**); |
| 54 | + |
| 55 | +/* |
| 56 | +================= |
| 57 | +LoadEngine |
| 58 | +================= |
| 59 | +*/ |
| 60 | +bool LoadEngine(std::unique_ptr<void, decltype(&SDL_UnloadObject)>& libraryHandle, EngineMainFunc& engineMain) { |
| 61 | + libraryHandle.reset(SDL_LoadObject(ENGINE_LIB)); |
56 | 62 |
|
57 | | - if(libraryHandle == nullptr) { |
58 | | - throw std::runtime_error("Failed to load library: " + std::string( |
59 | | - SDL_GetError())); |
| 63 | + if (!libraryHandle) { |
| 64 | + std::cerr << "[ERROR] Failed to load library: " << SDL_GetError() << std::endl; |
| 65 | + return false; |
60 | 66 | } |
61 | 67 |
|
62 | | - const auto engineMain = reinterpret_cast<int(*)(int, char **)> |
63 | | - (SDL_LoadFunction(libraryHandle, engineExportName.c_str())); |
| 68 | + engineMain = reinterpret_cast<EngineMainFunc>(SDL_LoadFunction(libraryHandle.get(), ENGINE_EXPORT_FUNC)); |
64 | 69 |
|
65 | | - if(engineMain == nullptr) { |
66 | | - throw std::runtime_error("Failed to find function: " + std::string( |
67 | | - SDL_GetError())); |
| 70 | + if (!engineMain) { |
| 71 | + std::cerr << "[ERROR] Failed to find function: " << SDL_GetError() << std::endl; |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + return true; |
| 76 | +} |
| 77 | + |
| 78 | +/* |
| 79 | +================= |
| 80 | +main |
| 81 | +================= |
| 82 | +*/ |
| 83 | +int main(int argc, char** argv) { |
| 84 | + if (SDL_Init(0) != 0) { |
| 85 | + std::cerr << "[ERROR] SDL initialization failed: " << SDL_GetError() << std::endl; |
| 86 | + return EXIT_FAILURE; |
| 87 | + } |
| 88 | + |
| 89 | + std::unique_ptr<void, decltype(&SDL_UnloadObject)> libraryHandle(nullptr, SDL_UnloadObject); |
| 90 | + EngineMainFunc engineMain = nullptr; |
| 91 | + |
| 92 | + if (!LoadEngine(libraryHandle, engineMain)) { |
| 93 | + std::cerr << "[ERROR] Engine failed to load. Exiting..." << std::endl; |
| 94 | + SDL_Quit(); |
| 95 | + return EXIT_FAILURE; |
68 | 96 | } |
69 | 97 |
|
70 | 98 | engineMain(argc, argv); |
71 | 99 |
|
72 | | - SDL_UnloadObject(libraryHandle); |
73 | 100 | SDL_Quit(); |
74 | | - |
75 | | - return 0; |
| 101 | + return EXIT_SUCCESS; |
76 | 102 | } |
77 | | - |
|
0 commit comments