修复图片大小不正确

This commit is contained in:
Nanako 2025-03-26 22:33:41 +08:00
parent dc896d51f3
commit de33e765d4
8 changed files with 34 additions and 6 deletions

View File

@ -31,8 +31,15 @@ int main(int argc, char* argv[]) {
auto image = std::make_shared<mimage>();
image->set_image(i);
auto hbox = std::make_shared<mh_box>();
hbox->add_child()
.auto_size()
[
image
];
const auto& window = mwindow::create({ 800, 600 }, L"Hello, World!");
window->set_content(image);
window->set_content(hbox);
mirage_app::get().run();
return 0;

View File

@ -23,7 +23,9 @@ public:
[[nodiscard]] const auto& get_image() const { return image_; }
[[nodiscard]] const auto& get_size() const { return size_; }
[[nodiscard]] const auto& get_pixel_format() const { return pixel_format_; }
private:
sg_image image_{};
sg_pixel_format pixel_format_{};
Eigen::Vector2i size_{};
};

View File

@ -80,7 +80,7 @@ public:
* @param anisotropy (1-16)
* @return
*/
static texture_sampler_builder create_anisotropic(uint32_t anisotropy = 4);
static texture_sampler_builder create_anisotropic(uint32_t anisotropy = 16);
/**
* @brief

View File

@ -7,6 +7,8 @@
void texture2d::create(void* in_data, const Eigen::Vector2i& in_size, sg_pixel_format in_pixel_format) {
auto format_info = sg_query_pixelformat(in_pixel_format);
pixel_format_ = in_pixel_format;
size_ = in_size;
sg_image_desc sg_desc{};
sg_desc.type = SG_IMAGETYPE_2D;

View File

@ -12,11 +12,28 @@ void mimage::on_paint(mirage_paint_context& in_context) {
in_context.geo().get_local_size(),
in_context.geo(),
image_->get_image(),
sampler_type::default_,
sampler_type::anisotropic,
{ color_ }
);
}
void mimage::set_image(const std::shared_ptr<texture2d>& in_image) {
image_ = in_image;
auto image_format = image_->get_pixel_format();
// 根据像素格式选择合适的过滤器
if (image_format == sg_pixel_format::SG_PIXELFORMAT_BC1_RGBA ||
image_format == sg_pixel_format::SG_PIXELFORMAT_BC2_RGBA ||
image_format == sg_pixel_format::SG_PIXELFORMAT_BC3_RGBA ||
image_format == sg_pixel_format::SG_PIXELFORMAT_BC4_R ||
image_format == sg_pixel_format::SG_PIXELFORMAT_BC5_RG ||
image_format == sg_pixel_format::SG_PIXELFORMAT_BC6H_RGBF ||
image_format == sg_pixel_format::SG_PIXELFORMAT_BC7_RGBA) {
filter_ = sg_filter::SG_FILTER_NEAREST;
} else {
filter_ = sg_filter::SG_FILTER_LINEAR;
}
}
Eigen::Vector2f mimage::compute_desired_size(float in_layout_scale_multiplier) const {
if (image_) {
return image_->get_size().cast<float>();

View File

@ -10,7 +10,7 @@ public:
Eigen::Vector2f compute_desired_size(float in_layout_scale_multiplier) const override;
const auto& get_image() const { return image_; }
void set_image(const std::shared_ptr<texture2d>& in_image) { image_ = in_image; }
void set_image(const std::shared_ptr<texture2d>& in_image);
const auto& get_color() const { return color_; }
void set_color(const linear_color& in_color) { color_ = in_color; }
@ -18,4 +18,5 @@ private:
std::shared_ptr<texture2d> image_;
std::shared_ptr<texture_sampler> sampler_;
linear_color color_ = { 1, 1, 1, 1 };
sg_filter filter_{};
};

View File

@ -72,14 +72,13 @@ public:
*
*
*/
template<typename T>
auto add_slot() -> SlotType& {
auto& slot = slots_.emplace_back();
slot.slot_owner = shared_from_this();
invalidate(invalidate_reason::all);
return slot;
}
template<typename T, typename... Args>
template<typename... Args>
auto add_child(Args&&... in_args) -> SlotType& {
auto& slot = slots_.emplace_back(std::forward<Args>(in_args)...);
slot.slot_owner = shared_from_this();

Binary file not shown.