Loading...
Searching...
No Matches
ClipView.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4#ifndef MALENA_CLIPVIEW_H
5#define MALENA_CLIPVIEW_H
6
7#pragma once
8
12
13namespace ml
14{
36 template<typename F>
37 void withClipView(sf::RenderTarget& target, sf::FloatRect rect, F&& body)
38 {
39 if (rect.size.x <= 0.f || rect.size.y <= 0.f)
40 {
41 body();
42 return;
43 }
44
45 const auto sz = target.getSize();
46 const float tw = static_cast<float>(sz.x);
47 const float th = static_cast<float>(sz.y);
48 const sf::View saved = target.getView();
49
50 const sf::Vector2i tl = target.mapCoordsToPixel(rect.position, saved);
51 const sf::Vector2i br = target.mapCoordsToPixel(
52 {rect.position.x + rect.size.x, rect.position.y + rect.size.y}, saved);
53
54 sf::View clip;
55 clip.setCenter({rect.position.x + rect.size.x / 2.f,
56 rect.position.y + rect.size.y / 2.f});
57 clip.setSize(rect.size);
59 {tl.x / tw, tl.y / th},
60 {(br.x - tl.x) / tw, (br.y - tl.y) / th}
61 });
62
63 target.setView(clip);
64 body();
65 target.setView(saved);
66 }
67
68} // namespace ml
69
70#endif // MALENA_CLIPVIEW_H
Vector2< T > size
Vector2< T > position
void setView(const View &view)
virtual Vector2u getSize() const=0
Vector2i mapCoordsToPixel(Vector2f point) const
const View & getView() const
void setCenter(Vector2f center)
void setSize(Vector2f size)
void setViewport(const FloatRect &viewport)
void withClipView(sf::RenderTarget &target, sf::FloatRect rect, F &&body)
Render body with an sf::View clipped to rect (world coords).
Definition ClipView.h:37
Definition Component.h:22
Rect< float > FloatRect
Vector2< int > Vector2i