optionally exclude delay time from checking if animation is playing

This commit is contained in:
Frank DeMarco 2020-09-05 19:40:59 -04:00
parent 0e6e506c68
commit 35a5acbd53
8 changed files with 47 additions and 49 deletions

View File

@ -44,9 +44,9 @@ void Animation::reset()
timer.reset();
}
bool Animation::is_playing()
bool Animation::is_playing(bool include_delay)
{
return playing && !paused;
return playing && !paused && (include_delay || delay <= 0);
}
void Animation::update()

View File

@ -35,7 +35,7 @@ struct Animation
void pause();
void unpause();
void reset();
bool is_playing();
bool is_playing(bool = true);
void update();
};

View File

@ -1,3 +1,5 @@
#include "extension.hpp"
#include "Segment.hpp"
#include "Box.hpp"
Box::Box(const glm::vec2& nw, const glm::vec2& size)

View File

@ -4,7 +4,7 @@
#include <iostream>
#include <algorithm>
#include <SDL.h>
#include "SDL.h"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/common.hpp"
@ -78,7 +78,4 @@ struct Box : SDL_FRect
std::ostream& operator<<(std::ostream&, const Box&);
#include "extension.hpp"
#include "Segment.hpp"
#endif

View File

@ -26,7 +26,6 @@ void Sprite::reset()
Node::reset();
activate();
wipe_animation.reset();
reset_wipe_index();
unhide();
}
@ -108,7 +107,6 @@ void Sprite::add_frames(SDL_Texture* frame)
frameset.set_size();
}
update_size(preserve_center);
reset_wipe_index();
}
void Sprite::add_frames(const std::vector<SDL_Texture*>& frames)
@ -166,7 +164,14 @@ void Sprite::set_frame_length(float length)
SDL_Texture* Sprite::get_current_frame() const
{
return frames[get_current_frameset().get_current_frame_index()];
if (frames.size() == 0)
{
return nullptr;
}
else
{
return frames[get_current_frameset().get_current_frame_index()];
}
}
const Box& Sprite::get_box(int index) const
@ -198,7 +203,6 @@ void Sprite::update_size(bool preserve_center)
boxes[ii].scale(scale, preserve_center);
}
}
wipe_blinds = sfw::get_blinds_boxes(get_size());
}
void Sprite::set_scale(float s)
@ -738,12 +742,21 @@ bool Sprite::collide(const Sprite& sprite, Box& overlap, bool precise, bool all,
return collide(sprite, precise, &overlap, all, all_other);
}
void Sprite::wipe()
void Sprite::wipe(float delay)
{
wipe_animation.play();
wipe_blinds = sfw::get_blinds_boxes(get_size());
if (wipe_increment < 0)
{
wipe_index = static_cast<int>(wipe_blinds.size() - 1);
}
else
{
wipe_index = 0;
}
wipe_animation.play(delay);
for (Child& child : children)
{
child.sprite.wipe();
child.sprite.wipe(delay);
}
}
@ -773,19 +786,6 @@ const std::vector<Box>& Sprite::get_current_wipe_blinds()
void Sprite::reverse_wipe_direction()
{
wipe_increment *= -1;
reset_wipe_index();
}
void Sprite::reset_wipe_index()
{
if (wipe_increment < 0)
{
wipe_index = static_cast<int>(wipe_blinds.size() - 1);
}
else
{
wipe_index = 0;
}
}
Sprite& Sprite::insert_child(std::string name, std::list<Child>::iterator position)
@ -858,9 +858,9 @@ void Sprite::update()
blink_animation.update();
wipe_animation.update();
toggle_hidden_animation.update();
SDL_Texture* texture = get_current_frame();
if (is_loaded() && !is_hidden() && get_current_frameset().get_frame_count())
{
SDL_Texture* texture = get_current_frame();
SDL_Renderer* renderer = get_root()->renderer;
SDL_SetTextureAlphaMod(texture, alpha_mod);
SDL_SetTextureColorMod(texture, color_mod.r, color_mod.g, color_mod.b);
@ -872,7 +872,7 @@ void Sprite::update()
}
for (std::size_t box_ii = 0; box_ii < boxes.size(); box_ii++)
{
if (!wipe_animation.is_playing())
if (!wipe_animation.is_playing(false))
{
SDL_RenderCopyF(renderer, texture, nullptr, &boxes[box_ii]);
}
@ -901,23 +901,23 @@ void Sprite::update()
{
SDL_RenderSetClipRect(renderer, nullptr);
}
for (Child& child : children)
}
for (Child& child : children)
{
if (draw_children_on_frame)
{
if (draw_children_on_frame)
{
child.sprite.set_canvas(texture);
}
else
{
child.sprite.set_canvas(get_canvas());
child_relative_position = child.sprite.get_nw();
child.sprite.move(get_nw());
}
child.sprite.update();
if (!draw_children_on_frame)
{
child.sprite.set_nw(child_relative_position);
}
child.sprite.set_canvas(texture);
}
else
{
child.sprite.set_canvas(get_canvas());
child_relative_position = child.sprite.get_nw();
child.sprite.move(get_nw());
}
child.sprite.update();
if (!draw_children_on_frame)
{
child.sprite.set_nw(child_relative_position);
}
}
}

View File

@ -126,11 +126,10 @@ struct Sprite : Node
bool collide(const Box&, Box&, bool = false, bool = false) const;
bool collide(const Sprite&, bool = false, Box* = NULL, bool = false, bool = false) const;
bool collide(const Sprite&, Box&, bool = false, bool = false, bool = false) const;
void wipe();
void wipe(float = 0.0f);
void advance_wipe_frame();
const std::vector<Box>& get_current_wipe_blinds();
void reverse_wipe_direction();
void reset_wipe_index();
Sprite& insert_child(std::string, std::list<Child>::iterator);
Sprite& insert_child(std::string, std::string);
Sprite& insert_child(std::string, int);

View File

@ -15,7 +15,7 @@ Box sfw::get_texture_box(SDL_Texture* texture)
return Box(glm::vec2(0, 0), glm::vec2(width, height));
}
glm::vec2 sfw::fit_and_preserve_aspect(glm::vec2 inner, glm::vec2 outer)
glm::vec2 sfw::fit_and_preserve_aspect(const glm::vec2& inner, const glm::vec2& outer)
{
glm::vec2 delta = inner - outer;
float aspect = inner.x / inner.y;

View File

@ -35,7 +35,7 @@ namespace sfw
void set_magnitude(glm::vec2&, float);
Box get_texture_box(SDL_Texture*);
glm::vec2 fit_and_preserve_aspect(glm::vec2, glm::vec2);
glm::vec2 fit_and_preserve_aspect(const glm::vec2&, const glm::vec2&);
std::vector<std::vector<Box>> get_blinds_boxes(glm::vec2, float = 0.05f, int = 4);
void populate_pixel_2d_array(SDL_Renderer*, SDL_Texture*, std::vector<std::vector<SDL_Color>>&);
void populate_pixel_2d_array(SDL_Renderer*, SDL_Texture*, std::vector<std::vector<SDL_Color>>&, const Box&);