anm2ed-cli and render tests: look at animations without the editor
- anm2ed-cli info|frame|render: a text summary, one frame as a PNG, or the whole animation as a contact sheet, PNG sequence, GIF or video, drawn by the editor's OpenGL renderer in a hidden window (render::HeadlessGL). - render::animation_frame_render / animation_record draw an animation's layers over its bounds (Canvas::layers_render, also used by About) and record them through the existing Recorder. - The editor's code builds as anm2ed_app, shared by the editor, the CLI and anm2ed_render_tests, which checks a fixture's pixels and golden PNGs (with tolerance; skipped without OpenGL) and leaves its renders in build/renders. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
356d74ebb6
commit
2dbbfa5e86
13 files changed
+643
-27
No files matched your search
+30
-13
@@ -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})
|
||||
list(REMOVE_ITEM PROJECT_SRC ${CORE_SRC} ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/cli.cpp)
|
||||
|
||||
set(CORE_INCLUDE_DIRS
|
||||
include
|
||||
@@ -136,7 +136,10 @@ if(NOT MSVC)
|
||||
target_compile_options(anm2ed_core PRIVATE -Wall -Wextra -Wpedantic)
|
||||
endif()
|
||||
|
||||
add_executable(${PROJECT_NAME} ${GLAD_SRC} ${IMGUI_SRC} ${PROJECT_SRC})
|
||||
# The editor's code as a library, shared by the editor, the CLI 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"
|
||||
@@ -146,9 +149,11 @@ 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(
|
||||
${PROJECT_NAME}
|
||||
PRIVATE include
|
||||
anm2ed_app
|
||||
PUBLIC include
|
||||
include/glad
|
||||
external
|
||||
external/imgui
|
||||
@@ -168,7 +173,7 @@ if(NOT CMAKE_BUILD_TYPE)
|
||||
endif()
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE DEBUG)
|
||||
target_compile_definitions(anm2ed_app PUBLIC DEBUG)
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
@@ -182,12 +187,12 @@ if(WIN32)
|
||||
endif()
|
||||
else()
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -O0 -pg)
|
||||
target_compile_options(anm2ed_app PUBLIC -Wall -Wextra -Wpedantic -O0 -pg)
|
||||
else()
|
||||
set(CMAKE_BUILD_TYPE "Release")
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -Wextra -Wpedantic -O3)
|
||||
target_compile_options(anm2ed_app PUBLIC -Wall -Wextra -Wpedantic -O3)
|
||||
endif()
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE m)
|
||||
target_link_libraries(anm2ed_app PUBLIC m)
|
||||
endif()
|
||||
|
||||
if(WIN32)
|
||||
@@ -196,12 +201,12 @@ if(WIN32)
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE "$<$<COMPILE_LANGUAGE:CXX>:/std:c++latest>")
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE /EHsc)
|
||||
target_compile_options(anm2ed_app PUBLIC "$<$<COMPILE_LANGUAGE:CXX>:/std:c++latest>")
|
||||
target_compile_options(anm2ed_app PUBLIC /EHsc)
|
||||
target_link_options(${PROJECT_NAME} PRIVATE /STACK:0xffffff)
|
||||
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
target_compile_options(${PROJECT_NAME} PRIVATE /Zi)
|
||||
target_compile_options(anm2ed_app PUBLIC /Zi)
|
||||
target_link_options(${PROJECT_NAME} PRIVATE /DEBUG)
|
||||
endif()
|
||||
endif()
|
||||
@@ -232,8 +237,10 @@ else()
|
||||
set(SDL_MAIN_TARGET)
|
||||
endif()
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE anm2ed_core ${OPENGL_LIB} SDL3-static ${SDL_MAIN_TARGET}
|
||||
SDL3_mixer::SDL3_mixer lunasvg ${PLATFORM_LIBS})
|
||||
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)
|
||||
@@ -244,6 +251,16 @@ if(ANM2ED_TESTS)
|
||||
target_include_directories(anm2ed_tests PRIVATE external/doctest)
|
||||
target_compile_definitions(anm2ed_tests PRIVATE ANM2ED_TEST_DIR="${CMAKE_CURRENT_SOURCE_DIR}/tests")
|
||||
add_test(NAME anm2ed_tests COMMAND anm2ed_tests WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/tests)
|
||||
|
||||
# Drawing animations needs OpenGL (skipped without a display); renders land in build/renders for a look.
|
||||
file(GLOB RENDER_TEST_SRC CONFIGURE_DEPENDS tests/render/*.cpp)
|
||||
add_executable(anm2ed_render_tests tests/main.cpp ${RENDER_TEST_SRC})
|
||||
target_link_libraries(anm2ed_render_tests PRIVATE anm2ed_app)
|
||||
target_include_directories(anm2ed_render_tests PRIVATE external/doctest 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)
|
||||
endif()
|
||||
|
||||
message(STATUS "System: ${CMAKE_SYSTEM_NAME}")
|
||||
|
||||
@@ -54,3 +54,13 @@ cmake --build build -j
|
||||
```sh
|
||||
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:
|
||||
```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)
|
||||
```
|
||||
Common options: `--animation NAME|INDEX`, `--scale`, `--fps`, `--start`/`--end`, `--background RRGGBB`. Run it without arguments for the full list.
|
||||
@@ -6,6 +6,7 @@
|
||||
#include <glm/gtc/type_ptr.hpp>
|
||||
|
||||
#include "math.hpp"
|
||||
#include "texture.hpp"
|
||||
|
||||
using namespace glm;
|
||||
using namespace anm2ed::resource;
|
||||
@@ -270,6 +271,28 @@ namespace anm2ed
|
||||
rect_end(rectVAO, mode, count);
|
||||
}
|
||||
|
||||
// An animation's layers alone (no markers or helpers); spritesheet images and custom shaders are looked up by id.
|
||||
void Canvas::layers_render(Shader& shader, const model::Model& model, const model::Animation& animation,
|
||||
const model::DrawOptions& options, const mat4& transform,
|
||||
const std::function<const Image*(int)>& image_get,
|
||||
const std::function<Shader*(int)>& shader_get) const
|
||||
{
|
||||
for (const auto& draw : model::animation_draws_get(model, animation, options))
|
||||
{
|
||||
if (draw.type != model::DrawType::LAYER) continue;
|
||||
auto& frame = draw.frame;
|
||||
auto layer = model::item_get(model.content.layers, draw.id);
|
||||
auto image = layer ? image_get(layer->spritesheetId) : nullptr;
|
||||
if (!image || !image->is_valid() || frame.size == vec2()) continue;
|
||||
auto imageSize = vec2(image->size);
|
||||
auto uvVertices = math::uv_vertices_get(frame.crop / imageSize, (frame.crop + frame.size) / imageSize);
|
||||
auto customShader = shader_get ? shader_get(frame.shaderId) : nullptr;
|
||||
texture_render(customShader ? *customShader : shader, texture::id_get(*image),
|
||||
transform * draw.parent * model::draw_quad_model_get(draw), frame.tint, frame.colorOffset,
|
||||
uvVertices.data(), imageSize, draw.time);
|
||||
}
|
||||
}
|
||||
|
||||
void Canvas::rect_render(Shader& shader, const mat4& transform, const mat4& model, vec4 color, float dashLength,
|
||||
float dashGap, float dashOffset) const
|
||||
{
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include <array>
|
||||
#include <functional>
|
||||
|
||||
#include <glad/glad.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
#include "framebuffer.hpp"
|
||||
#include "image.hpp"
|
||||
#include "model/draw.hpp"
|
||||
#include "shader.hpp"
|
||||
|
||||
#include "types.hpp"
|
||||
@@ -65,6 +68,9 @@ namespace anm2ed
|
||||
void texture_render(resource::Shader&, GLuint, glm::mat4 = {1.0f}, glm::vec4 = glm::vec4(1.0f), glm::vec3 = {},
|
||||
float* = (float*)TEXTURE_VERTICES, glm::vec2 = {}, float = 0.0f) const;
|
||||
void rect_fill_render(resource::Shader&, const glm::mat4&, const glm::mat4&, glm::vec4 = glm::vec4(1.0f)) const;
|
||||
void layers_render(resource::Shader&, const model::Model&, const model::Animation&, const model::DrawOptions&,
|
||||
const glm::mat4&, const std::function<const resource::Image*(int)>&,
|
||||
const std::function<resource::Shader*(int)>& = {}) const;
|
||||
void dashed_render(resource::Shader&, const glm::mat4&, const glm::mat4&, glm::vec4, float, float, float, GLenum,
|
||||
int) const;
|
||||
void rect_render(resource::Shader&, const glm::mat4&, const glm::mat4&, glm::vec4 = glm::vec4(1.0f),
|
||||
|
||||
+284
@@ -0,0 +1,284 @@
|
||||
// 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 <charconv>
|
||||
#include <cstdio>
|
||||
#include <format>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <print>
|
||||
#include <string>
|
||||
|
||||
#include "canvas.hpp"
|
||||
#include "document.hpp"
|
||||
#include "image.hpp"
|
||||
#include "model/xml.hpp"
|
||||
#include "path.hpp"
|
||||
#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 <command> <file.anm2> [options]
|
||||
|
||||
commands:
|
||||
info animations, items, spritesheets and Isaac compatibility, as text
|
||||
frame one frame as a PNG (--out, --time)
|
||||
render the animation as --type:
|
||||
sheet all frames in a grid PNG (default; --rows, --columns)
|
||||
pngs one PNG per frame in the --out folder (--format, default "{}.png")
|
||||
gif, webm, mp4, ogv through FFmpeg (--ffmpeg, default "ffmpeg")
|
||||
|
||||
options:
|
||||
--animation NAME|INDEX which animation (default: the default animation)
|
||||
--out PATH where to write
|
||||
--time T frame time for "frame" (default 0)
|
||||
--start N, --end N frame range for "render" (default: the whole animation)
|
||||
--fps N frames per second to sample (default: the animation's)
|
||||
--scale S output scale (default 1)
|
||||
--background RRGGBB background color, or "transparent" (default)
|
||||
--no-root-transform draw without the root's transform
|
||||
)";
|
||||
|
||||
constexpr float SCALE_DEFAULT = 1.0f;
|
||||
constexpr int HEX_BASE = 16;
|
||||
constexpr int HEX_RGB_LENGTH = 6;
|
||||
constexpr float CHANNEL_MAX = 255.0f;
|
||||
|
||||
struct Arguments
|
||||
{
|
||||
std::string command{};
|
||||
std::filesystem::path file{};
|
||||
std::map<std::string, std::string> options{};
|
||||
|
||||
std::optional<std::string> get(const std::string& name) const
|
||||
{
|
||||
auto it = options.find(name);
|
||||
return it == options.end() ? std::nullopt : std::optional(it->second);
|
||||
}
|
||||
|
||||
template <class T> T number_get(const std::string& name, T value) const
|
||||
{
|
||||
if (auto text = get(name)) std::from_chars(text->data(), text->data() + text->size(), value);
|
||||
return value;
|
||||
}
|
||||
|
||||
bool is(const std::string& name) const { return options.contains(name); }
|
||||
};
|
||||
|
||||
// PNG sequences go into a folder; everything else is a file.
|
||||
bool type_is_folder(const Arguments& arguments)
|
||||
{
|
||||
return arguments.command == "render" && arguments.get("type").value_or("") == "pngs";
|
||||
}
|
||||
|
||||
std::optional<Arguments> arguments_get(int argc, char** argv)
|
||||
{
|
||||
if (argc < 3) return {};
|
||||
Arguments arguments{.command = argv[1], .file = path::from_utf8(argv[2])};
|
||||
for (int i = 3; i < argc; ++i)
|
||||
{
|
||||
std::string name = argv[i];
|
||||
if (!name.starts_with("--")) return {};
|
||||
name.erase(0, 2);
|
||||
auto isFlag = i + 1 >= argc || std::string_view(argv[i + 1]).starts_with("--");
|
||||
arguments.options[name] = isFlag ? std::string{} : std::string(argv[++i]);
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
|
||||
// RRGGBB (opaque) or "transparent".
|
||||
glm::vec4 background_get(const Arguments& arguments)
|
||||
{
|
||||
auto text = arguments.get("background").value_or("transparent");
|
||||
if (text.starts_with('#')) text.erase(0, 1);
|
||||
unsigned value{};
|
||||
if (text.size() != HEX_RGB_LENGTH ||
|
||||
std::from_chars(text.data(), text.data() + text.size(), value, HEX_BASE).ec != std::errc{})
|
||||
return {};
|
||||
return {(float)((value >> 16) & 0xFF) / CHANNEL_MAX, (float)((value >> 8) & 0xFF) / CHANNEL_MAX,
|
||||
(float)(value & 0xFF) / CHANNEL_MAX, 1.0f};
|
||||
}
|
||||
|
||||
// By index or name; the default animation otherwise.
|
||||
std::pair<int, const model::Animation*> animation_get(const model::Model& model, const Arguments& arguments)
|
||||
{
|
||||
auto animations = model.animations_get();
|
||||
auto name = arguments.get("animation").value_or(model.animations.defaultAnimation);
|
||||
int index{-1};
|
||||
if (std::from_chars(name.data(), name.data() + name.size(), index).ec == std::errc{} && index >= 0 &&
|
||||
index < (int)animations.size())
|
||||
return animations[index];
|
||||
for (auto [i, animation] : animations)
|
||||
if (animation->name == name) return {i, animation};
|
||||
if (!arguments.is("animation") && !animations.empty()) return animations.front();
|
||||
return {-1, nullptr};
|
||||
}
|
||||
|
||||
int info_print(const Arguments& arguments)
|
||||
{
|
||||
model::Model model{};
|
||||
std::string error{};
|
||||
if (!model::model_load(model, arguments.file, &error))
|
||||
{
|
||||
std::println(stderr, "could not load {}: {}", path::to_utf8(arguments.file), error);
|
||||
return 1;
|
||||
}
|
||||
auto& content = model.content;
|
||||
std::println("file: {}", path::to_utf8(arguments.file));
|
||||
std::println("fps: {}, created by: {}", model.info.fps, model.info.createdBy);
|
||||
|
||||
std::println("animations (default: {}):", model.animations.defaultAnimation);
|
||||
for (auto [index, animation] : model.animations_get())
|
||||
{
|
||||
int layers{}, nulls{};
|
||||
model::tracks_each(animation->layers, [&](const model::Track&, auto*) { ++layers; });
|
||||
model::tracks_each(animation->nulls, [&](const model::Track&, auto*) { ++nulls; });
|
||||
std::println(" [{}] {}: {} frames{}, {} layer tracks, {} null tracks, {} triggers", index, animation->name,
|
||||
animation->frameNum, animation->isLoop ? ", loops" : "", layers, nulls,
|
||||
animation->triggers.frames.size());
|
||||
}
|
||||
|
||||
WorkingDirectory workingDirectory(arguments.file.parent_path());
|
||||
std::println("spritesheets:");
|
||||
for (const auto& spritesheet : content.spritesheets)
|
||||
{
|
||||
auto image = resource::Image(path::case_insensitive_find(spritesheet.path));
|
||||
auto status = image.is_valid() ? std::format("{}x{}", image.size.x, image.size.y) : std::string("missing");
|
||||
std::println(" #{} {} ({}), {} regions", spritesheet.id, path::to_utf8(spritesheet.path), status,
|
||||
spritesheet.regions.size());
|
||||
}
|
||||
for (const auto& layer : content.layers)
|
||||
std::println("layer #{} {} (spritesheet #{})", layer.id, layer.name, layer.spritesheetId);
|
||||
for (const auto& null : content.nulls)
|
||||
std::println("null #{} {}", null.id, null.name);
|
||||
for (const auto& event : content.events)
|
||||
std::println("event #{} {}", event.id, event.name);
|
||||
for (const auto& sound : content.sounds)
|
||||
std::println("sound #{} {}", sound.id, path::to_utf8(sound.path));
|
||||
|
||||
auto issues = model::isaac_issues_get(model);
|
||||
std::println("isaac issues: {} eased frames, {} group transforms, {} sheared frames, {} shader frames, {} trigger "
|
||||
"sounds",
|
||||
issues.easedFrames, issues.groupTransforms, issues.shearFrames, issues.shaderFrames,
|
||||
issues.triggerSounds);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int draw_run(const Arguments& arguments)
|
||||
{
|
||||
auto out = arguments.get("out");
|
||||
if (!out)
|
||||
{
|
||||
std::println(stderr, "--out is required");
|
||||
return 2;
|
||||
}
|
||||
|
||||
render::HeadlessGL gl{};
|
||||
if (!gl.is_valid())
|
||||
{
|
||||
std::println(stderr, "could not create an OpenGL 3.3 context");
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string error{};
|
||||
Document document(arguments.file, false, &error);
|
||||
if (!document.is_valid())
|
||||
{
|
||||
std::println(stderr, "could not load {}: {}", path::to_utf8(arguments.file), error);
|
||||
return 1;
|
||||
}
|
||||
auto [index, animation] = animation_get(document.model, arguments);
|
||||
if (!animation)
|
||||
{
|
||||
std::println(stderr, "no such animation: {}", arguments.get("animation").value_or(""));
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto& textureInfo = resource::shader::SHADERS[resource::shader::TEXTURE];
|
||||
resource::Shader shader(textureInfo.vertex, textureInfo.fragment);
|
||||
Canvas canvas(glm::vec2(1.0f));
|
||||
auto isRootTransform = !arguments.is("no-root-transform");
|
||||
auto bounds = render::animation_bounds_get(document, *animation, isRootTransform);
|
||||
auto scale = arguments.number_get("scale", SCALE_DEFAULT);
|
||||
auto background = background_get(arguments);
|
||||
auto frame_render = [&](float time)
|
||||
{
|
||||
return render::animation_frame_render(canvas, shader, document, *animation, time, bounds, scale, background,
|
||||
isRootTransform);
|
||||
};
|
||||
auto outPath = path::from_utf8(*out);
|
||||
std::error_code ec{};
|
||||
std::filesystem::create_directories(type_is_folder(arguments) ? outPath : outPath.parent_path(), ec);
|
||||
|
||||
if (arguments.command == "frame")
|
||||
{
|
||||
auto pixels = frame_render(arguments.number_get("time", 0.0f));
|
||||
render::pixels_unpremultiply(pixels);
|
||||
if (!resource::Image::write_pixels_png(outPath, glm::ivec2(canvas.size), pixels.data()))
|
||||
{
|
||||
std::println(stderr, "could not write {}", *out);
|
||||
return 1;
|
||||
}
|
||||
std::println("{}: \"{}\" at time {}, {}x{}", *out, animation->name, arguments.number_get("time", 0.0f),
|
||||
(int)canvas.size.x, (int)canvas.size.y);
|
||||
return 0;
|
||||
}
|
||||
|
||||
auto typeName = arguments.get("type").value_or("sheet");
|
||||
auto type = typeName == "sheet" ? render::SPRITESHEET : render::PNGS;
|
||||
for (int i = 0; i < render::COUNT; ++i)
|
||||
if (render::EXTENSIONS[i] == "." + typeName) type = (render::Type)i;
|
||||
if (typeName != "sheet" && typeName != "pngs" && type == render::PNGS)
|
||||
{
|
||||
std::println(stderr, "unknown --type: {}", typeName);
|
||||
return 2;
|
||||
}
|
||||
|
||||
auto fps = std::max(document.model.info.fps, 1);
|
||||
render::RecorderOptions options{.type = type,
|
||||
.path = outPath,
|
||||
.format = path::from_utf8(arguments.get("format").value_or("{}.png")),
|
||||
.ffmpegPath = path::from_utf8(arguments.get("ffmpeg").value_or("ffmpeg")),
|
||||
.start = arguments.number_get("start", 0),
|
||||
.end = arguments.number_get("end", animation->frameNum - 1),
|
||||
.animationFps = fps,
|
||||
.fps = arguments.number_get("fps", fps),
|
||||
.rows = arguments.number_get("rows", 0),
|
||||
.columns = arguments.number_get("columns", 0)};
|
||||
auto count = render::frame_count_get(options.start, options.end, options.animationFps, options.fps);
|
||||
if (!render::animation_record(options, canvas, shader, document, *animation, bounds, scale, background,
|
||||
isRootTransform))
|
||||
{
|
||||
std::println(stderr, "could not write {} (FFmpeg: {})", *out, path::to_utf8(options.ffmpegPath));
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::print("{}: \"{}\", {} frames ({} to {} at {} fps), each {}x{}", *out, animation->name, count, options.start,
|
||||
options.end, options.fps, (int)canvas.size.x, (int)canvas.size.y);
|
||||
if (type == render::SPRITESHEET)
|
||||
{
|
||||
auto layout = render::spritesheet_layout_get(options.rows, options.columns, count);
|
||||
std::print(", {} rows x {} columns, left to right", layout.rows, layout.columns);
|
||||
}
|
||||
std::println("");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
auto arguments = cli::arguments_get(argc, argv);
|
||||
if (!arguments)
|
||||
{
|
||||
std::print(stderr, "{}", cli::USAGE);
|
||||
return 2;
|
||||
}
|
||||
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;
|
||||
}
|
||||
@@ -251,20 +251,13 @@ namespace anm2ed::imgui::wizard
|
||||
vec2 pan{};
|
||||
state.canvas->set_to_rect(zoom, pan, rect);
|
||||
|
||||
auto transform = state.canvas->transform_get(zoom, pan);
|
||||
for (const auto& draw :
|
||||
model::animation_draws_get(state.model, *animation, {.time = state.time, .isRootTransform = true}))
|
||||
{
|
||||
auto& frame = draw.frame;
|
||||
auto layer = draw.type == model::DrawType::LAYER ? model::item_get(state.model.content.layers, draw.id) : nullptr;
|
||||
auto texture = layer ? state.textures.find(layer->spritesheetId) : state.textures.end();
|
||||
if (texture == state.textures.end() || !texture->second.is_valid() || frame.size == vec2()) continue;
|
||||
auto textureSize = vec2(texture->second.size);
|
||||
auto uvVertices = math::uv_vertices_get(frame.crop / textureSize, (frame.crop + frame.size) / textureSize);
|
||||
state.canvas->texture_render(resources.shaders[shader::TEXTURE], resource::texture::id_get(texture->second),
|
||||
transform * draw.parent * model::draw_quad_model_get(draw), frame.tint,
|
||||
frame.colorOffset, uvVertices.data());
|
||||
}
|
||||
state.canvas->layers_render(resources.shaders[shader::TEXTURE], state.model, *animation,
|
||||
{.time = state.time, .isRootTransform = true}, state.canvas->transform_get(zoom, pan),
|
||||
[&](int id)
|
||||
{
|
||||
auto texture = state.textures.find(id);
|
||||
return texture == state.textures.end() ? nullptr : &texture->second;
|
||||
});
|
||||
|
||||
state.canvas->unbind();
|
||||
}
|
||||
|
||||
@@ -10,11 +10,15 @@
|
||||
#include <string_view>
|
||||
|
||||
#include <SDL3/SDL_timer.h>
|
||||
#include <SDL3/SDL_video.h>
|
||||
#include <glad/glad.h>
|
||||
|
||||
#include "audio.hpp"
|
||||
#include "canvas.hpp"
|
||||
#include "document.hpp"
|
||||
#include "file.hpp"
|
||||
#include "log.hpp"
|
||||
#include "model/frames.hpp"
|
||||
#include "path.hpp"
|
||||
#include "process.hpp"
|
||||
#include "toast.hpp"
|
||||
@@ -178,6 +182,7 @@ namespace anm2ed
|
||||
namespace anm2ed::render
|
||||
{
|
||||
constexpr int TEMP_DIRECTORY_ATTEMPTS = 1000;
|
||||
constexpr glm::vec4 BOUNDS_DEFAULT = {-32.0f, -32.0f, 64.0f, 64.0f};
|
||||
constexpr int ALPHA_OPAQUE = 255;
|
||||
constexpr float CHANNEL_MAX = 255.0f;
|
||||
|
||||
@@ -275,6 +280,59 @@ namespace anm2ed::render
|
||||
return isGenerated;
|
||||
}
|
||||
|
||||
HeadlessGL::HeadlessGL()
|
||||
{
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) return;
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
||||
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
|
||||
window = SDL_CreateWindow("anm2ed", 1, 1, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN);
|
||||
if (window) context = SDL_GL_CreateContext(window);
|
||||
if (!context || !gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress))
|
||||
{
|
||||
if (context) SDL_GL_DestroyContext(context);
|
||||
context = nullptr;
|
||||
return;
|
||||
}
|
||||
glEnable(GL_BLEND);
|
||||
glDisable(GL_DEPTH_TEST);
|
||||
}
|
||||
|
||||
HeadlessGL::~HeadlessGL()
|
||||
{
|
||||
if (context) SDL_GL_DestroyContext(context);
|
||||
if (window) SDL_DestroyWindow(window);
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
bool HeadlessGL::is_valid() const { return context != nullptr; }
|
||||
|
||||
glm::vec4 animation_bounds_get(const Document& document, const model::Animation& animation, bool isRootTransform)
|
||||
{
|
||||
auto bounds = model::animation_rect(document.model, animation, isRootTransform);
|
||||
return bounds != glm::vec4(-1.0f) ? bounds : BOUNDS_DEFAULT;
|
||||
}
|
||||
|
||||
std::vector<uint8_t> animation_frame_render(Canvas& canvas, Shader& shader, Document& document,
|
||||
const model::Animation& animation, float time, glm::vec4 bounds,
|
||||
float scale, glm::vec4 background, bool isRootTransform)
|
||||
{
|
||||
canvas.size_set(glm::vec2(glm::max(glm::ivec2(glm::ceil(glm::vec2(bounds.z, bounds.w) * scale)), glm::ivec2(1))));
|
||||
float zoom{};
|
||||
glm::vec2 pan{};
|
||||
canvas.set_to_rect(zoom, pan, bounds);
|
||||
canvas.bind();
|
||||
canvas.viewport_set();
|
||||
canvas.clear(glm::vec4(glm::vec3(background) * background.a, background.a));
|
||||
canvas.layers_render(
|
||||
shader, document.model, animation, {.time = time, .isRootTransform = isRootTransform},
|
||||
canvas.transform_get(zoom, pan), [&](int id) { return document.texture_get(id); },
|
||||
[&](int id) { return document.shader_get(id); });
|
||||
auto pixels = canvas.pixels_get();
|
||||
canvas.unbind();
|
||||
return pixels;
|
||||
}
|
||||
|
||||
bool Recorder::start(const RecorderOptions& recorderOptions)
|
||||
{
|
||||
options = recorderOptions;
|
||||
@@ -360,4 +418,25 @@ namespace anm2ed::render
|
||||
soundIds.clear();
|
||||
index = count = 0;
|
||||
}
|
||||
|
||||
bool animation_record(const RecorderOptions& recorderOptions, Canvas& canvas, Shader& shader, Document& document,
|
||||
const model::Animation& animation, glm::vec4 bounds, float scale, glm::vec4 background,
|
||||
bool isRootTransform)
|
||||
{
|
||||
auto options = recorderOptions;
|
||||
options.isIsolated = options.isBounded = true;
|
||||
Recorder recorder{};
|
||||
if (!recorder.start(options)) return false;
|
||||
while (!recorder.is_done())
|
||||
{
|
||||
auto pixels = animation_frame_render(canvas, shader, document, animation, recorder.time_get(), bounds, scale,
|
||||
background, isRootTransform);
|
||||
if (recorder.frame_capture(std::move(pixels), glm::ivec2(canvas.size), -1)) continue;
|
||||
recorder.cancel();
|
||||
return false;
|
||||
}
|
||||
AudioStream audioStream(nullptr);
|
||||
recorder.finish(document, audioStream);
|
||||
return path::is_exist(options.path);
|
||||
}
|
||||
}
|
||||
@@ -8,11 +8,25 @@
|
||||
#include "audio_stream.hpp"
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
struct SDL_Window;
|
||||
struct SDL_GLContextState;
|
||||
|
||||
namespace anm2ed
|
||||
{
|
||||
class Canvas;
|
||||
class Document;
|
||||
}
|
||||
|
||||
namespace anm2ed::model
|
||||
{
|
||||
struct Animation;
|
||||
}
|
||||
|
||||
namespace anm2ed::resource
|
||||
{
|
||||
class Shader;
|
||||
}
|
||||
|
||||
namespace anm2ed::render
|
||||
{
|
||||
#define RENDER_LIST \
|
||||
@@ -111,6 +125,25 @@ namespace anm2ed::render
|
||||
return {rows, columns};
|
||||
}
|
||||
|
||||
// An OpenGL context with no visible window, for drawing animations outside the editor (the CLI, render tests).
|
||||
class HeadlessGL
|
||||
{
|
||||
SDL_Window* window{};
|
||||
SDL_GLContextState* context{};
|
||||
|
||||
public:
|
||||
HeadlessGL();
|
||||
~HeadlessGL();
|
||||
bool is_valid() const;
|
||||
};
|
||||
|
||||
// The area an animation covers over all its frames (or a default one when it draws nothing).
|
||||
glm::vec4 animation_bounds_get(const Document&, const model::Animation&, bool);
|
||||
// One frame drawn over `bounds` at `scale`, as premultiplied RGBA rows from the top.
|
||||
std::vector<uint8_t> animation_frame_render(Canvas&, resource::Shader&, Document&, const model::Animation&, float,
|
||||
glm::vec4, float, glm::vec4, bool);
|
||||
void pixels_unpremultiply(std::vector<uint8_t>&);
|
||||
|
||||
struct RecorderOptions
|
||||
{
|
||||
Type type{};
|
||||
@@ -151,6 +184,11 @@ namespace anm2ed::render
|
||||
std::vector<int> soundIds{};
|
||||
int soundTime{-1};
|
||||
};
|
||||
|
||||
// Records a whole animation with these options (bounded and isolated), each frame drawn as animation_frame_render
|
||||
// draws it; false when a frame or the output can't be written.
|
||||
bool animation_record(const RecorderOptions&, Canvas&, resource::Shader&, Document&, const model::Animation&,
|
||||
glm::vec4, float, glm::vec4, bool);
|
||||
}
|
||||
|
||||
namespace anm2ed
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 765 B |
Binary file not shown.
|
After Width: | Height: | Size: 6.6 KiB |
@@ -0,0 +1,134 @@
|
||||
// Animations drawn by the editor's OpenGL renderer, compared with golden PNGs (ANM2ED_UPDATE_GOLDEN=1 rewrites them).
|
||||
// Every render is also written to ANM2ED_RENDER_DIR (build/renders) so it can be looked at. Skipped without OpenGL.
|
||||
|
||||
#include <doctest.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <format>
|
||||
|
||||
#include "canvas.hpp"
|
||||
#include "document.hpp"
|
||||
#include "image.hpp"
|
||||
#include "render.hpp"
|
||||
|
||||
using namespace anm2ed;
|
||||
|
||||
namespace anm2ed::test
|
||||
{
|
||||
const std::filesystem::path RENDER_TEST_DIR = std::filesystem::path(ANM2ED_TEST_DIR) / "render";
|
||||
const std::filesystem::path GOLDEN_DIR = RENDER_TEST_DIR / "golden";
|
||||
const std::filesystem::path OUTPUT_DIR = ANM2ED_RENDER_DIR;
|
||||
constexpr int CHANNEL_TOLERANCE = 8;
|
||||
constexpr double MISMATCH_RATIO_MAX = 0.01;
|
||||
constexpr float SCALE = 3.0f;
|
||||
constexpr glm::vec4 BACKGROUND = {0.2f, 0.2f, 0.2f, 1.0f};
|
||||
}
|
||||
|
||||
using namespace anm2ed::test;
|
||||
|
||||
// Different OpenGL implementations round a little differently: a few pixels may differ, each only slightly.
|
||||
void golden_image_check(const std::string& name)
|
||||
{
|
||||
auto actualPath = OUTPUT_DIR / name;
|
||||
auto goldenPath = GOLDEN_DIR / name;
|
||||
INFO("render: ", actualPath.string());
|
||||
if (std::getenv("ANM2ED_UPDATE_GOLDEN"))
|
||||
{
|
||||
std::filesystem::copy_file(actualPath, goldenPath, std::filesystem::copy_options::overwrite_existing);
|
||||
return;
|
||||
}
|
||||
resource::Image actual(actualPath);
|
||||
resource::Image golden(goldenPath);
|
||||
REQUIRE(golden.is_valid());
|
||||
REQUIRE(actual.size == golden.size);
|
||||
int mismatches{};
|
||||
for (std::size_t i = 0; i < actual.pixels.size(); ++i)
|
||||
mismatches += std::abs((int)actual.pixels[i] - (int)golden.pixels[i]) > CHANNEL_TOLERANCE;
|
||||
CHECK((double)mismatches / (double)actual.pixels.size() <= MISMATCH_RATIO_MAX);
|
||||
}
|
||||
|
||||
struct RenderFixture
|
||||
{
|
||||
render::HeadlessGL gl{};
|
||||
std::optional<Document> document{};
|
||||
std::optional<resource::Shader> shader{};
|
||||
std::optional<Canvas> canvas{};
|
||||
const model::Animation* animation{};
|
||||
glm::vec4 bounds{};
|
||||
|
||||
explicit RenderFixture(const char* file)
|
||||
{
|
||||
if (!gl.is_valid()) return;
|
||||
std::filesystem::create_directories(OUTPUT_DIR);
|
||||
document.emplace(RENDER_TEST_DIR / file, false);
|
||||
auto& info = resource::shader::SHADERS[resource::shader::TEXTURE];
|
||||
shader.emplace(info.vertex, info.fragment);
|
||||
canvas.emplace(glm::vec2(1.0f));
|
||||
animation = document->model.animation_get(0);
|
||||
bounds = render::animation_bounds_get(*document, *animation, true);
|
||||
}
|
||||
|
||||
std::vector<uint8_t> frame_get(float time)
|
||||
{
|
||||
auto pixels =
|
||||
render::animation_frame_render(*canvas, *shader, *document, *animation, time, bounds, SCALE, BACKGROUND, true);
|
||||
render::pixels_unpremultiply(pixels);
|
||||
return pixels;
|
||||
}
|
||||
|
||||
glm::u8vec4 pixel_get(const std::vector<uint8_t>& pixels, glm::vec2 point)
|
||||
{
|
||||
auto position = glm::ivec2((point - glm::vec2(bounds.x, bounds.y)) * SCALE);
|
||||
auto index = ((std::size_t)position.y * (std::size_t)canvas->size.x + (std::size_t)position.x) * 4;
|
||||
return {pixels[index], pixels[index + 1], pixels[index + 2], pixels[index + 3]};
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("frames draw where the animation puts them")
|
||||
{
|
||||
RenderFixture fixture("spin.anm2");
|
||||
if (!fixture.gl.is_valid())
|
||||
{
|
||||
MESSAGE("skipped: no OpenGL context");
|
||||
return;
|
||||
}
|
||||
REQUIRE(fixture.document->is_valid());
|
||||
|
||||
// At time 0 the red square is centered at (-16, 0) with its white dot up and left; the half-transparent blue
|
||||
// square (32 x 32, at the origin) covers the middle.
|
||||
auto pixels = fixture.frame_get(0.0f);
|
||||
auto red = fixture.pixel_get(pixels, {-20.0f, 4.0f});
|
||||
CHECK(red.r > 200);
|
||||
CHECK(red.g < 80);
|
||||
auto dot = fixture.pixel_get(pixels, {-18.5f, -2.5f});
|
||||
CHECK(dot.g > 200);
|
||||
auto blue = fixture.pixel_get(pixels, {12.0f, 12.0f});
|
||||
CHECK(blue.b > blue.r + 40);
|
||||
|
||||
auto framePath = OUTPUT_DIR / "spin_frame_0.png";
|
||||
REQUIRE(resource::Image::write_pixels_png(framePath, glm::ivec2(fixture.canvas->size), pixels.data()));
|
||||
golden_image_check("spin_frame_0.png");
|
||||
}
|
||||
|
||||
TEST_CASE("a recorded spritesheet holds every frame")
|
||||
{
|
||||
RenderFixture fixture("spin.anm2");
|
||||
if (!fixture.gl.is_valid())
|
||||
{
|
||||
MESSAGE("skipped: no OpenGL context");
|
||||
return;
|
||||
}
|
||||
|
||||
render::RecorderOptions options{.type = render::SPRITESHEET,
|
||||
.path = OUTPUT_DIR / "spin_sheet.png",
|
||||
.start = 0,
|
||||
.end = fixture.animation->frameNum - 1,
|
||||
.animationFps = fixture.document->model.info.fps,
|
||||
.fps = fixture.document->model.info.fps};
|
||||
REQUIRE(render::animation_record(options, *fixture.canvas, *fixture.shader, *fixture.document, *fixture.animation,
|
||||
fixture.bounds, SCALE, BACKGROUND, true));
|
||||
resource::Image sheet(options.path);
|
||||
auto layout = render::spritesheet_layout_get(0, 0, fixture.animation->frameNum);
|
||||
CHECK(sheet.size == glm::ivec2(fixture.canvas->size) * glm::ivec2(layout.columns, layout.rows));
|
||||
golden_image_check("spin_sheet.png");
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 148 B |
@@ -0,0 +1,32 @@
|
||||
<AnimatedActor>
|
||||
<Info CreatedBy="fixture" CreatedOn="01/01/2026 12:00:00 PM" Fps="30" Version="2"/>
|
||||
<Content>
|
||||
<Spritesheets>
|
||||
<Spritesheet Id="0" Path="sheet.png"/>
|
||||
</Spritesheets>
|
||||
<Layers>
|
||||
<Layer Id="0" Name="red" SpritesheetId="0"/>
|
||||
<Layer Id="1" Name="blue" SpritesheetId="0"/>
|
||||
</Layers>
|
||||
<Nulls/>
|
||||
<Events/>
|
||||
</Content>
|
||||
<Animations DefaultAnimation="Spin">
|
||||
<Animation Name="Spin" FrameNum="8" Loop="true">
|
||||
<RootAnimation>
|
||||
<Frame XPosition="0" YPosition="0" Delay="8" Visible="true" XScale="100" YScale="100" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="false"/>
|
||||
</RootAnimation>
|
||||
<LayerAnimations>
|
||||
<LayerAnimation LayerId="1" Visible="true">
|
||||
<Frame XPivot="8" YPivot="8" XCrop="16" YCrop="0" Width="16" Height="16" XPosition="0" YPosition="0" Delay="8" Visible="true" XScale="200" YScale="200" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="128" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="false"/>
|
||||
</LayerAnimation>
|
||||
<LayerAnimation LayerId="0" Visible="true">
|
||||
<Frame XPivot="8" YPivot="8" XCrop="0" YCrop="0" Width="16" Height="16" XPosition="-16" YPosition="0" Delay="4" Visible="true" XScale="100" YScale="100" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="0" Interpolated="true"/>
|
||||
<Frame XPivot="8" YPivot="8" XCrop="0" YCrop="0" Width="16" Height="16" XPosition="16" YPosition="0" Delay="4" Visible="true" XScale="100" YScale="100" RedTint="255" GreenTint="255" BlueTint="255" AlphaTint="255" RedOffset="0" GreenOffset="0" BlueOffset="0" Rotation="90" Interpolated="false"/>
|
||||
</LayerAnimation>
|
||||
</LayerAnimations>
|
||||
<NullAnimations/>
|
||||
<Triggers/>
|
||||
</Animation>
|
||||
</Animations>
|
||||
</AnimatedActor>
|
||||
Reference in new issue
Block a user