Porytiles
Loading...
Searching...
No Matches
anim_frame_loader.hpp
Go to the documentation of this file.
1#pragma once
2
26#include <filesystem>
27#include <string>
28
36
37namespace porytiles {
38
49template <SupportsTransparency PixelType>
52 std::size_t width_tiles;
53 std::size_t height_tiles;
54};
55
81template <SupportsTransparency PixelType, typename LoaderType>
83 const std::filesystem::path &png_path, const std::string &frame_name, const LoaderType &loader)
84{
85 // Step 1: Load the PNG file
86 auto image_result = loader.load_from_file(png_path);
87 if (!image_result.has_value()) {
89 FormattableError{"'{}': Failed to load animation frame PNG.", FormatParam{png_path.string(), Style::bold}},
90 image_result};
91 }
92
93 const Image<PixelType> &img = *image_result.value();
94
95 // Step 2: Calculate dimensions in tiles
96 const std::size_t width_tiles = img.width() / tile::side_length_pix;
97 const std::size_t height_tiles = img.height() / tile::side_length_pix;
98
99 // Step 3: Extract tiles from the image
100 std::vector<PixelTile<PixelType>> tiles = extract_tiles_from_image(img);
101
102 // Step 4: Create the AnimFrame
103 AnimFrame<PixelType> frame{frame_name, std::move(tiles)};
104
105 // Step 5: Set palette if the image has one
106 if (img.palette().has_value()) {
107 frame.palette(Palette<Rgba32>{img.palette().value()});
108 }
109
110 return FrameLoadResult<PixelType>{std::move(frame), width_tiles, height_tiles};
111}
112
113} // 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:63
A template for two-dimensional images with arbitrarily typed pixel values.
Definition image.hpp:23
std::size_t width() const
Definition image.hpp:114
const std::optional< std::vector< Rgba32 > > & palette() const
Definition image.hpp:150
std::size_t height() const
Definition image.hpp:119
A generic palette container for colors that support transparency checking.
Definition palette.hpp:47
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