diff --git a/private/sdl_gpu_test/SModule b/private/sdl_gpu_test/SModule index e69aa64..bc851d5 100644 --- a/private/sdl_gpu_test/SModule +++ b/private/sdl_gpu_test/SModule @@ -23,6 +23,7 @@ prog_app = env.UnityProgram( target = env['BIN_DIR'] + '/sdl_gpu_test', source = src_files, dependencies = { + 'argparse': {}, 'glm': {}, 'mijin': {}, 'SDL': { diff --git a/private/sdl_gpu_test/main.cpp b/private/sdl_gpu_test/main.cpp index 7222380..9b441be 100644 --- a/private/sdl_gpu_test/main.cpp +++ b/private/sdl_gpu_test/main.cpp @@ -1,35 +1,89 @@ +#include +#include + #include "./0_clear_swapchain/app.hpp" #include "./1_green_triangle/app.hpp" #include "./2_triangle_with_texcoords/app.hpp" #include "./3_textured_quad/app.hpp" #include "./4_textured_cube/app.hpp" +#include "./util/spdlog_wrapper.hpp" -#include -#include -#include +namespace +{ +inline constexpr int APP_ERROR = 1; +inline constexpr int USER_ERROR = 2; + +struct AppListEntry +{ + std::string name; + std::function()> factoryFn; +}; + +template +AppListEntry makeAppHelper(std::string name) noexcept +{ + return { + .name = std::move(name), + .factoryFn = []() { return std::make_unique(); } + }; +} + +const std::array APPS = { + makeAppHelper("clear_swapchain"), + makeAppHelper("green_triangle"), + makeAppHelper("triangle_with_texcoords"), + makeAppHelper("textured_quad"), + makeAppHelper("textured_cube") +}; +} int main(int argc, char* argv[]) { if (argc < 1) { - return 1; + return APP_ERROR; } if (SDL_Init(0) != SDL_TRUE) { spdlog::error("Error initializing SDL."); - return 1; + return APP_ERROR; + } + + std::string appString = APPS.back().name; + argparse::ArgumentParser parser("sdl_gpu_test"); + argparse::Argument& appArg = parser.add_argument("--app") + .help("Application to run") + .store_into(appString); + for (const AppListEntry& appHelper : APPS) + { + appArg.add_choice(appHelper.name); + } + + // now parse + try + { + parser.parse_args(argc, argv); + } + catch (std::runtime_error& error) + { + spdlog::error("{}.", error.what()); + return USER_ERROR; } try { - // make sure app is destructed before shutting down SDL - // std::unique_ptr app = std::make_unique(); - // std::unique_ptr app = std::make_unique(); - // std::unique_ptr app = std::make_unique(); - // std::unique_ptr app = std::make_unique(); - std::unique_ptr app = std::make_unique(); + std::unique_ptr app; + for (const AppListEntry& appHelper : APPS) + { + if (appHelper.name == appString) + { + app = appHelper.factoryFn(); + break; + } + } + MIJIN_ASSERT_FATAL(app != nullptr, "Error creating app."); app->run(std::span(const_cast(argv), argc)); } catch (std::exception& exception) diff --git a/private/sdl_gpu_test/util/spdlog_wrapper.hpp b/private/sdl_gpu_test/util/spdlog_wrapper.hpp new file mode 100644 index 0000000..35caaad --- /dev/null +++ b/private/sdl_gpu_test/util/spdlog_wrapper.hpp @@ -0,0 +1,11 @@ + +#pragma once + +#if !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_UTIL_SPDLOG_WRAPPER_HPP_INCLUDED) +#define SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_UTIL_SPDLOG_WRAPPER_HPP_INCLUDED 1 + +// spdlog includes windows.h ... +#include +#include + +#endif // !defined(SDL_GPU_TEST_PRIVATE_SDL_GPU_TEST_UTIL_SPDLOG_WRAPPER_HPP_INCLUDED)