diff --git a/CMakeLists.txt b/CMakeLists.txt index 91a33b3..4843994 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -111,7 +111,7 @@ foreach(CORE_FILE log resource/image resource/audio_data resource/asset_store ut util/pack util/imgui/multiselect snapshots playback selection) list(APPEND CORE_SRC ${CORE_DIR}/${CORE_FILE}.cpp) endforeach() -list(REMOVE_ITEM PROJECT_SRC ${CORE_SRC} ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/cli.cpp) +list(REMOVE_ITEM PROJECT_SRC ${CORE_SRC} ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp) set(CORE_INCLUDE_DIRS include @@ -136,10 +136,9 @@ if(NOT MSVC) target_compile_options(anm2ed_core PRIVATE -Wall -Wextra -Wpedantic) endif() -# The editor's code as a library, shared by the editor, the CLI and the render tests. +# The editor's code as a library, shared by the editor and the render tests. add_library(anm2ed_app STATIC ${GLAD_SRC} ${IMGUI_SRC} ${PROJECT_SRC}) add_executable(${PROJECT_NAME} src/main.cpp) -add_executable(anm2ed-cli src/cli.cpp) set_target_properties( ${PROJECT_NAME} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin" @@ -149,8 +148,6 @@ set_target_properties( RUNTIME_OUTPUT_DIRECTORY_MINSIZEREL "${CMAKE_BINARY_DIR}/bin") -set_target_properties(anm2ed-cli PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin") - target_include_directories( anm2ed_app PUBLIC include @@ -240,7 +237,6 @@ endif() target_link_libraries(anm2ed_app PUBLIC anm2ed_core ${OPENGL_LIB} SDL3-static SDL3_mixer::SDL3_mixer lunasvg ${PLATFORM_LIBS}) target_link_libraries(${PROJECT_NAME} PRIVATE anm2ed_app ${SDL_MAIN_TARGET}) -target_link_libraries(anm2ed-cli PRIVATE anm2ed_app) option(ANM2ED_TESTS "Build the anm2ed test suite" ON) if(ANM2ED_TESTS) @@ -260,7 +256,7 @@ if(ANM2ED_TESTS) target_compile_definitions(anm2ed_render_tests PRIVATE ANM2ED_TEST_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests" ANM2ED_RENDER_DIR="${CMAKE_BINARY_DIR}/renders") add_test(NAME anm2ed_render_tests COMMAND anm2ed_render_tests WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests) - add_test(NAME anm2ed_cli_info COMMAND anm2ed-cli info ${CMAKE_CURRENT_SOURCE_DIR}/tests/render/spin.anm2) + add_test(NAME anm2ed_cli_info COMMAND ${PROJECT_NAME} info ${CMAKE_CURRENT_SOURCE_DIR}/tests/render/spin.anm2) endif() message(STATUS "System: ${CMAKE_SYSTEM_NAME}") diff --git a/README.md b/README.md index 43cae4e..b9ea196 100644 --- a/README.md +++ b/README.md @@ -57,10 +57,10 @@ ctest --test-dir build Render tests draw with OpenGL (skipped without a display) and leave their images in `build/renders`. ## Command Line -`anm2ed-cli` looks at a file without the editor, drawing with the editor's renderer: +`anm2ed ` looks at a file without opening the editor, drawing with the editor's renderer: ```sh -anm2ed-cli info file.anm2 # animations, items, spritesheets, Isaac issues -anm2ed-cli frame file.anm2 --out frame.png --time 4 # one frame -anm2ed-cli render file.anm2 --out sheet.png --scale 3 # every frame in a grid (--type pngs|gif|webm|mp4|ogv) +anm2ed info file.anm2 # animations, items, spritesheets, Isaac issues +anm2ed frame file.anm2 --out frame.png --time 4 # one frame +anm2ed render file.anm2 --out sheet.png --scale 3 # every frame in a grid (--type pngs|gif|webm|mp4|ogv) ``` -Common options: `--animation NAME|INDEX`, `--scale`, `--fps`, `--start`/`--end`, `--background RRGGBB`. Run it without arguments for the full list. +Common options: `--animation NAME|INDEX`, `--scale`, `--fps`, `--start`/`--end`, `--background RRGGBB`. `anm2ed render` alone lists them all. diff --git a/src/cli.cpp b/src/cli.cpp index f1f222e..ea8ffbb 100644 --- a/src/cli.cpp +++ b/src/cli.cpp @@ -1,6 +1,6 @@ -// anm2ed-cli: look at .anm2 files without the editor - a text summary, or animations drawn (with the editor's own -// OpenGL renderer, in a hidden window) to a PNG frame, a contact sheet, a PNG sequence, a GIF or a video. +#include "cli.hpp" +#include #include #include #include @@ -17,12 +17,11 @@ #include "render.hpp" #include "working_directory.hpp" -using namespace anm2ed; using namespace anm2ed::util; namespace anm2ed::cli { - constexpr const char* USAGE = R"(usage: anm2ed-cli [options] + constexpr const char* USAGE = R"(usage: anm2ed [options] commands: info animations, items, spritesheets and Isaac compatibility, as text @@ -75,7 +74,7 @@ options: return arguments.command == "render" && arguments.get("type").value_or("") == "pngs"; } - std::optional arguments_get(int argc, char** argv) + std::optional arguments_get(int argc, const char** argv) { if (argc < 3) return {}; Arguments arguments{.command = argv[1], .file = path::from_utf8(argv[2])}; @@ -267,18 +266,23 @@ options: std::println(""); return 0; } -} -int main(int argc, char** argv) -{ - auto arguments = cli::arguments_get(argc, argv); - if (!arguments) + constexpr std::string_view COMMANDS[] = {"info", "frame", "render"}; + + bool is_cli(int argc, const char** argv) { - std::print(stderr, "{}", cli::USAGE); - return 2; + return argc >= 2 && std::ranges::contains(COMMANDS, std::string_view(argv[1])) && !path::is_exist(argv[1]); + } + + int run(int argc, const char** argv) + { + auto arguments = arguments_get(argc, argv); + if (!arguments) + { + std::print(stderr, "{}", USAGE); + return 2; + } + if (arguments->command == "info") return info_print(*arguments); + return draw_run(*arguments); } - if (arguments->command == "info") return cli::info_print(*arguments); - if (arguments->command == "frame" || arguments->command == "render") return cli::draw_run(*arguments); - std::print(stderr, "{}", cli::USAGE); - return 2; } diff --git a/src/cli.hpp b/src/cli.hpp new file mode 100644 index 0000000..1d5d07f --- /dev/null +++ b/src/cli.hpp @@ -0,0 +1,10 @@ +#pragma once + +// The command-line mode: "anm2ed [options]" looks at a file without the editor, as +// text or drawn (by the editor's OpenGL renderer, in a hidden window) to a PNG frame, a contact sheet, PNGs, a GIF or +// a video. +namespace anm2ed::cli +{ + bool is_cli(int, const char**); + int run(int, const char**); +} diff --git a/src/main.cpp b/src/main.cpp index a5139a7..7195095 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,3 +1,4 @@ +#include "cli.hpp" #include "loader.hpp" #include "log.hpp" #include "sdl.hpp" @@ -6,11 +7,11 @@ #ifdef _WIN32 #define SDL_MAIN_HANDLED #include + #include #include #include #include #include - #include #endif #ifdef _WIN32 @@ -63,20 +64,34 @@ namespace anm2ed { int application_run(int argc, const char** argv) { - logger.session_open(util::sdl::preferences_directory_get()); - return log_exceptions([&]() + if (cli::is_cli(argc, argv)) { - Loader loader(argc, argv); +#ifdef _WIN32 + // A GUI-subsystem build has no console of its own; print to the one it was started from. + if (AttachConsole(ATTACH_PARENT_PROCESS)) + { + freopen("CONOUT$", "w", stdout); + freopen("CONOUT$", "w", stderr); + } +#endif + return cli::run(argc, argv); + } - if (loader.isError) return EXIT_FAILURE; + logger.session_open(util::sdl::preferences_directory_get()); + return log_exceptions( + [&]() + { + Loader loader(argc, argv); - State state(loader.window, loader.settings, loader.arguments); + if (loader.isError) return EXIT_FAILURE; - while (!state.isQuit) - state.loop(loader.window, loader.settings); + State state(loader.window, loader.settings, loader.arguments); - return EXIT_SUCCESS; - }); + while (!state.isQuit) + state.loop(loader.window, loader.settings); + + return EXIT_SUCCESS; + }); } }