Porytiles
Loading...
Searching...
No Matches
anim_frame_loader.hpp
Go to the documentation of this file.
1#pragma once
2
23
24#include <filesystem>
25#include <string>
26
34
35namespace porytiles {
36
45template <SupportsTransparency PixelType>
48 std::size_t width_tiles;
49 std::size_t height_tiles;
50};
51
75template <SupportsTransparency PixelType, typename LoaderType>
77 const std::filesystem::path &png_path, const std::string &frame_name, const LoaderType &loader)
78{
79 // Step 1: Load the PNG file
80 auto image_result = loader.load_from_file(png_path);
81 if (!image_result.has_value()) {
83 FormattableError{"'{}': Failed to load animation frame PNG.", FormatParam{png_path.string(), Style::bold}},
84 image_result};
85 }
86
87 const Image<PixelType> &img = *image_result.value();
88
89 // Step 2: Calculate dimensions in tiles
90 const std::size_t width_tiles = img.width() / tile::side_length_pix;
91 const std::size_t height_tiles = img.height() / tile::side_length_pix;
92
93 // Step 3: Extract tiles from the image
94 std::vector<PixelTile<PixelType>> tiles = extract_tiles_from_image(img);
95
96 // Step 4: Create the AnimFrame
97 AnimFrame<PixelType> frame{frame_name, std::move(tiles)};
98
99 // Step 5: Set palette if the image has one
100 if (img.palette().has_value()) {
101 frame.palette(Palette<Rgba32>{img.palette().value()});
102 }
103
104 return FrameLoadResult<PixelType>{std::move(frame), width_tiles, height_tiles};
105}
106
107} // namespace porytiles
Represents a single frame of an animation, containing tiles and a frame name.
const Palette< Rgba32 > & palette() const
A result type that maintains a chainable sequence of errors for debugging and error reporting.
A text parameter with associated styling for formatted output.
General-purpose error implementation with formatted message support.
Definition error.hpp:57
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:21
std::size_t width() const
Definition image.hpp:102
const std::optional< std::vector< Rgba32 > > & palette() const
Definition image.hpp:136
std::size_t height() const
Definition image.hpp:107
A generic palette container for colors that support transparency checking.
Definition palette.hpp:45
static const Style bold
Bold text formatting.
constexpr std::size_t side_length_pix
std::vector< PixelTile< PixelType > > extract_tiles_from_image(const Image< PixelType > &img, std::size_t tile_offset, std::size_t tile_count, std::size_t tiles_per_row=16)
Extracts a subset of 8x8 tiles from a tileset image at a specific offset.
ChainableResult< FrameLoadResult< PixelType > > load_animation_frame_from_png(const std::filesystem::path &png_path, const std::string &frame_name, const LoaderType &loader)
Loads an animation frame from a PNG file.
Result of loading an animation frame from a PNG file.
AnimFrame< PixelType > frame