Animations can now be applied to sf::View to do animated camera effects.

This commit is contained in:
Zed A. Shaw 2025-11-01 12:29:22 -04:00
parent d60e1af6df
commit f1f4cbc80f
4 changed files with 27 additions and 22 deletions

View file

@ -119,6 +119,22 @@ namespace components {
return playing; return playing;
} }
bool Animation::apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size) {
sf::Vector2f scale{min_x, min_y};
sf::IntRect ignored{{0,0}, {int(size.x), int(size.y)}};
step(scale, pos, ignored);
view_out.setCenter(pos);
if(scaled) {
view_out.setSize({size.x * scale.x, size.y * scale.y});
} else {
view_out.setSize(size);
}
return playing;
}
} }
namespace animation { namespace animation {

View file

@ -292,16 +292,16 @@
"test_zoom": { "test_zoom": {
"_type": "Animation", "_type": "Animation",
"easing": 6, "easing": 6,
"motion": 1, "motion": 6,
"ease_rate": 0.5, "ease_rate": 0.5,
"min_x": -10.0, "min_x": 0.8,
"min_y": -10.0, "min_y": 0.9,
"max_x": 10.0, "max_x": 1.1,
"max_y": 10.0, "max_y": 1.1,
"simple": true, "simple": true,
"frames": 1, "frames": 1,
"speed": 0.01, "speed": 0.01,
"scaled": false, "scaled": true,
"stationary": true, "stationary": true,
"toggled": false, "toggled": false,
"flipped": false, "flipped": false,

View file

@ -81,23 +81,9 @@ namespace boss {
$view_texture.setView(zoom); $view_texture.setView(zoom);
} else if($zoom_anim.playing) { } else if($zoom_anim.playing) {
auto& cell = $arena.$ui.cell_for(cell_name); auto& cell = $arena.$ui.cell_for(cell_name);
sf::Vector2f scale{$zoom_anim.min_x, $zoom_anim.min_y};
sf::Vector2f pos{float(cell.x), float(cell.y)}; sf::Vector2f pos{float(cell.x), float(cell.y)};
sf::IntRect rect{{0,0}, {cell.w, cell.h}}; sf::View zoom;
$zoom_anim.apply(zoom, pos, {BOSS_VIEW_WIDTH/2, BOSS_VIEW_HEIGHT/2});
$zoom_anim.step(scale, pos, rect);
fmt::println("SCALE: {},{}; pos: {},{}; rect: {},{};{},{}",
scale.x, scale.y, pos.x, pos.y,
rect.position.x, rect.position.y,
rect.size.x, rect.size.y);
sf::View zoom{pos,
{BOSS_VIEW_WIDTH/2, BOSS_VIEW_HEIGHT/2}};
if($zoom_anim.scaled) {
zoom.zoom(scale.x);
}
$view_texture.setView(zoom); $view_texture.setView(zoom);
} }

View file

@ -6,6 +6,7 @@
#include <SFML/Graphics/Rect.hpp> #include <SFML/Graphics/Rect.hpp>
#include <SFML/Graphics/Shader.hpp> #include <SFML/Graphics/Shader.hpp>
#include <SFML/Graphics/Sprite.hpp> #include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/View.hpp>
#include <SFML/System/Vector2.hpp> #include <SFML/System/Vector2.hpp>
#include <functional> #include <functional>
#include <optional> #include <optional>
@ -140,6 +141,8 @@ namespace components {
int frame_height = -1; int frame_height = -1;
bool apply(sf::Sprite& target, sf::Vector2f pos); bool apply(sf::Sprite& target, sf::Vector2f pos);
bool apply(sf::View& view_out, sf::Vector2f pos, sf::Vector2f size);
void play(); void play();
float twitching(); float twitching();
void step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out); void step(sf::Vector2f& scale_out, sf::Vector2f& pos_out, sf::IntRect& rect_out);