reverse wipe; more precision converting floats in box to sdl_rect

This commit is contained in:
Frank DeMarco 2020-08-31 16:36:56 -04:00
parent 54a8c219ed
commit 9058f93ad9
6 changed files with 63 additions and 21 deletions

View File

@ -4,6 +4,7 @@ SDL2_gfxPrimitives.c: graphics primitives for SDL2 renderers
Copyright (C) 2012-2014 Andreas Schiffler
Modifications and additions for BBC BASIC (C) 2016-2019 Richard Russell
SLIGHT FURTHER MODIFICATIONS MARKED "NOTE:" 2020 by 420@shampoo.ooo
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
@ -1983,7 +1984,10 @@ int filledEllipseRGBA(SDL_Renderer * renderer, Sint16 x, Sint16 y, Sint16 rx, Si
* Set color
*/
result = 0;
result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND);
// NOTE: removing so the user can set their own blend mode before calling
/* result |= SDL_SetRenderDrawBlendMode(renderer, (a == 255) ? SDL_BLENDMODE_NONE : SDL_BLENDMODE_BLEND); */
result |= SDL_SetRenderDrawColor(renderer, r, g, b, a);
/*

View File

@ -246,9 +246,15 @@ void Box::set_center(const glm::vec2& center)
Box::operator SDL_Rect() const
{
float rounded_x = std::round(get_x());
float dx = rounded_x - get_x();
float adjusted_width = get_w() + dx;
float rounded_y = std::round(get_y());
float dy = rounded_y - get_y();
float adjusted_height = get_h() + dy;
return {
static_cast<int>(get_x()), static_cast<int>(get_y()),
static_cast<int>(get_w()), static_cast<int>(get_h())
static_cast<int>(rounded_x), static_cast<int>(rounded_y),
static_cast<int>(std::round(adjusted_width)), static_cast<int>(std::round(adjusted_height))
};
}

View File

@ -23,7 +23,7 @@ void Sprite::reset()
Node::reset();
activate();
wipe_animation.reset();
wipe_index = static_cast<int>(wipe_blinds.size() - 1);
reset_wipe_index();
}
void Sprite::associate(std::string path)
@ -101,7 +101,7 @@ void Sprite::add_frames(SDL_Texture* frame)
}
update_size(preserve_center);
wipe_blinds = sfw::get_blinds_boxes(get_size());
wipe_index = static_cast<int>(wipe_blinds.size() - 1);
reset_wipe_index();
}
void Sprite::add_frames(const std::vector<SDL_Texture*>& frames)
@ -678,11 +678,19 @@ bool Sprite::collide(const Sprite& sprite, Box& overlap, bool precise, bool all,
void Sprite::wipe()
{
if (--wipe_index < 0)
wipe_index += wipe_increment;
if (wipe_index < 0 || wipe_index >= static_cast<int>(wipe_blinds.size()))
{
wipe_index = static_cast<int>(wipe_blinds.size() - 1);
if (wipe_index < 0)
{
wipe_index = static_cast<int>(wipe_blinds.size() - 1);
hide();
}
else
{
wipe_index = 0;
}
wipe_animation.reset();
hide();
}
}
@ -691,6 +699,24 @@ const std::vector<Box>& Sprite::get_current_wipe_blinds()
return wipe_blinds[wipe_index];
}
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;
}
}
void Sprite::update()
{
if (active)
@ -724,10 +750,13 @@ void Sprite::update()
{
for (const Box& blind : get_current_wipe_blinds())
{
float bottom_save = std::round(blind.get_bottom());
subsection = blind;
subsection_destination = blind;
subsection_destination.move(get_nw(box_ii));
SDL_RenderCopyF(renderer, texture, &subsection, &subsection_destination);
subsection.y += bottom_save - (subsection.y + subsection.h);
subsection_destination = subsection;
subsection_destination.x += get_left();
subsection_destination.y += get_top();
SDL_RenderCopy(renderer, texture, &subsection, &subsection_destination);
}
}
}

View File

@ -38,11 +38,11 @@ struct Sprite : Node
std::string current_frameset_name;
glm::bvec2 wrap = {false, false};
int texture_access = SDL_TEXTUREACCESS_TARGET;
int wipe_index = 0;
Box wrap_frame, subsection_destination;
int wipe_index = 0, wipe_increment = -1;
Box wrap_frame;
bool leave_memory_allocated = false;
std::vector<std::vector<Box>> wipe_blinds;
SDL_Rect subsection;
SDL_Rect subsection, subsection_destination;
Sprite();
Sprite(Node*);
@ -125,6 +125,8 @@ struct Sprite : Node
bool collide(const Sprite&, Box&, bool = false, bool = false, bool = false) const;
void wipe();
const std::vector<Box>& get_current_wipe_blinds();
void reverse_wipe_direction();
void reset_wipe_index();
virtual void update();
void set_to_leave_memory_allocated();
void set_to_deallocate_memory();

View File

@ -122,22 +122,22 @@ void sfw::populate_pixel_2d_array(
}
std::vector<SDL_Texture*> sfw::get_halo_frames(
Node& node, float radius, int segment_count, const std::vector<SDL_Color>& colors, float min_radius, bool fade)
SDL_Renderer* renderer, float radius, int segment_count, const std::vector<Color>& colors, float min_radius, bool fade)
{
std::vector<SDL_Texture*> frames;
frames.reserve(segment_count);
SDL_Renderer* renderer = node.get_renderer();
SDL_Texture* frame;
float alpha = 255, alpha_step = 255.0f / segment_count, segment_radius;
int color_count = colors.size();
SDL_Color color;
Color color;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
for (int color_offset = 0; color_offset < color_count; color_offset++)
{
if (fade)
{
alpha = alpha_step;
}
frame = sfw::get_filled_texture(renderer, {2 * radius, 2 * radius}, {255, 255, 255, 0});
frame = sfw::get_filled_texture(renderer, {2 * radius, 2 * radius}, {0, 0, 0, 0});
SDL_SetTextureBlendMode(frame, SDL_BLENDMODE_BLEND);
SDL_SetRenderTarget(renderer, frame);
for (int segment_ii = 0; segment_ii < segment_count; segment_ii++)
@ -145,8 +145,8 @@ std::vector<SDL_Texture*> sfw::get_halo_frames(
color = colors[(color_offset + segment_ii) % color_count];
color.a = std::round(alpha);
segment_radius = min_radius + (segment_count - 1.0f - segment_ii) / (segment_count - 1.0f) * (radius - min_radius);
aaFilledEllipseRGBA(
renderer, radius, radius, segment_radius, segment_radius, color.r, color.g, color.b, color.a);
filledCircleRGBA(renderer, radius, radius, static_cast<int>(std::round(segment_radius)),
color.r, color.g, color.b, color.a);
if (fade)
{
alpha += alpha_step;

View File

@ -43,7 +43,8 @@ namespace sfw
void apply_array_to_texture(SDL_Renderer*, SDL_Texture*, std::vector<std::vector<SDL_Color>>&);
void apply_array_to_texture(SDL_Renderer*, SDL_Texture*, std::vector<std::vector<SDL_Color>>&, const Box&);
std::vector<SDL_Texture*> get_halo_frames(
Node&, float, int, const std::vector<SDL_Color>& = {{0, 0, 0, 255}, {255, 255, 255, 255}}, float = 4.0f, bool = true);
SDL_Renderer*, float, int, const std::vector<Color>& = {Color(0, 0, 0), Color(255, 255, 255)},
float = 4.0f, bool = true);
std::vector<SDL_Texture*> get_portal_frames(SDL_Renderer*, glm::vec2, float = 60, float = 30, int = 4, int = 6);
void fill_texture(SDL_Renderer*, SDL_Texture*, const SDL_Color&, const Box&);
void fill_texture(SDL_Renderer*, SDL_Texture*, const SDL_Color&);