aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-06-12 18:39:32 +0200
committerdec05eba <dec05eba@protonmail.com>2021-06-12 18:39:32 +0200
commitc7e10b0910473c99dbc139f514220b134093c9f0 (patch)
tree9172c2969af6d7eea2a100a79ba19348fe1f1479 /src
parent6c442930ca3bef5c3e9e6a75d8aa1d93fe4eb3e1 (diff)
Add show in shader instead of sf::Vertex
Diffstat (limited to 'src')
-rw-r--r--src/RoundedRectangle.cpp87
1 files changed, 8 insertions, 79 deletions
diff --git a/src/RoundedRectangle.cpp b/src/RoundedRectangle.cpp
index f9b79c4..20abe14 100644
--- a/src/RoundedRectangle.cpp
+++ b/src/RoundedRectangle.cpp
@@ -4,6 +4,8 @@
#include <assert.h>
namespace QuickMedia {
+ static const float shadow_radius = 20.0f; // Has to match the shadow offset in rounded_rectangle.glsl
+
RoundedRectangle::RoundedRectangle(sf::Vector2f size, float radius, sf::Color color, sf::Shader *rounded_rectangle_shader) :
radius(radius), pos(0.0f, 0.0f), size(size), rounded_rectangle_shader(rounded_rectangle_shader), band_color(sf::Color::Transparent)
{
@@ -13,84 +15,14 @@ namespace QuickMedia {
vertices[1].texCoords = sf::Vector2f(1.0f, 0.0f);
vertices[2].texCoords = sf::Vector2f(1.0f, 1.0f);
vertices[3].texCoords = sf::Vector2f(0.0f, 1.0f);
-
- const sf::Color shadow_color(0, 0, 0, 50);
-
- // Sides
- for(size_t i = 0; i < 16; i += 4) {
- drop_shadow_vertices[i + 0].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 1].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 2].color = shadow_color;
- drop_shadow_vertices[i + 3].color = shadow_color;
- }
-
- // Corners
- for(size_t i = 16; i < 32; i += 4) {
- drop_shadow_vertices[i + 0].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 1].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 2].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 0);
- drop_shadow_vertices[i + 3].color = sf::Color(shadow_color.r, shadow_color.g, shadow_color.b, 100);
- }
}
void RoundedRectangle::set_position(sf::Vector2f pos) {
this->pos = pos;
- vertices[0].position = pos;
- vertices[1].position = pos + sf::Vector2f(size.x, 0.0f);
- vertices[2].position = pos + sf::Vector2f(size.x, size.y);
- vertices[3].position = pos + sf::Vector2f(0.0f, size.y);
-
- const float shadow_radius = 5.0f;
-
- // Top
- drop_shadow_vertices[0].position = pos + sf::Vector2f(radius*0.5f, 0.0f) - sf::Vector2f(0.0f, shadow_radius);
- drop_shadow_vertices[1].position = pos + sf::Vector2f(size.x - radius*0.5f, 0.0f) - sf::Vector2f(0.0f, shadow_radius);
- drop_shadow_vertices[2].position = pos + sf::Vector2f(size.x - radius*0.5f, 0.0f);
- drop_shadow_vertices[3].position = pos + sf::Vector2f(radius*0.5f, 0.0f);
-
- // Bottom
- drop_shadow_vertices[4].position = pos + sf::Vector2f(size.x - radius*0.5f, size.y + shadow_radius);
- drop_shadow_vertices[5].position = pos + sf::Vector2f(radius*0.5f, size.y + shadow_radius);
- drop_shadow_vertices[6].position = pos + sf::Vector2f(radius*0.5f, size.y);
- drop_shadow_vertices[7].position = pos + sf::Vector2f(size.x - radius*0.5f, size.y);
-
- // Left
- drop_shadow_vertices[8].position = pos + sf::Vector2f(-shadow_radius, size.y - radius*0.5f);
- drop_shadow_vertices[9].position = pos + sf::Vector2f(-shadow_radius, radius*0.5f);
- drop_shadow_vertices[10].position = pos + sf::Vector2f(0.0f, radius*0.5f);
- drop_shadow_vertices[11].position = pos + sf::Vector2f(0.0f, size.y - radius*0.5f);
-
- // Right
- drop_shadow_vertices[12].position = pos + sf::Vector2f(size.x + shadow_radius, radius*0.5f);
- drop_shadow_vertices[13].position = pos + sf::Vector2f(size.x + shadow_radius, size.y - radius*0.5f);
- drop_shadow_vertices[14].position = pos + sf::Vector2f(size.x, size.y - radius*0.5f);
- drop_shadow_vertices[15].position = pos + sf::Vector2f(size.x, radius*0.5f);
-
- const float overshoot = 1.7f;
-
- // Top left
- drop_shadow_vertices[16].position = pos + sf::Vector2f(-shadow_radius, radius*0.5f);
- drop_shadow_vertices[17].position = pos + sf::Vector2f(-shadow_radius*overshoot, -shadow_radius*overshoot);
- drop_shadow_vertices[18].position = pos + sf::Vector2f(radius*0.5f, -shadow_radius);
- drop_shadow_vertices[19].position = pos + sf::Vector2f(radius*0.5f, radius*0.5f);
-
- // Top right
- drop_shadow_vertices[20].position = pos + sf::Vector2f(size.x - radius*0.5f, -shadow_radius);
- drop_shadow_vertices[21].position = pos + sf::Vector2f(size.x + shadow_radius*overshoot, -shadow_radius*overshoot);
- drop_shadow_vertices[22].position = pos + sf::Vector2f(size.x + shadow_radius, radius*0.5f);
- drop_shadow_vertices[23].position = pos + sf::Vector2f(size.x - radius*0.5f, radius*0.5f);
-
- // Bottom right
- drop_shadow_vertices[24].position = pos + sf::Vector2f(size.x + shadow_radius, size.y - radius*0.5f);
- drop_shadow_vertices[25].position = pos + sf::Vector2f(size.x + shadow_radius*overshoot, size.y + shadow_radius*overshoot);
- drop_shadow_vertices[26].position = pos + sf::Vector2f(size.x - radius*0.5f, size.y + shadow_radius);
- drop_shadow_vertices[27].position = pos + sf::Vector2f(size.x - radius*0.5f, size.y - radius*0.5f);
-
- // Bottom left
- drop_shadow_vertices[28].position = pos + sf::Vector2f(radius*0.5f, size.y + shadow_radius);
- drop_shadow_vertices[29].position = pos + sf::Vector2f(-shadow_radius*overshoot, size.y + shadow_radius*overshoot);
- drop_shadow_vertices[30].position = pos + sf::Vector2f(-shadow_radius, size.y - radius*0.5f);
- drop_shadow_vertices[31].position = pos + sf::Vector2f(radius*0.5f, size.y - radius*0.5f);
+ vertices[0].position = pos + sf::Vector2f(-shadow_radius, -shadow_radius);
+ vertices[1].position = pos + sf::Vector2f(shadow_radius, -shadow_radius) + sf::Vector2f(size.x, 0.0f);
+ vertices[2].position = pos + sf::Vector2f(shadow_radius, shadow_radius) + sf::Vector2f(size.x, size.y);
+ vertices[3].position = pos + sf::Vector2f(-shadow_radius, shadow_radius) + sf::Vector2f(0.0f, size.y);
}
void RoundedRectangle::set_size(sf::Vector2f size) {
@@ -121,15 +53,12 @@ namespace QuickMedia {
}
void RoundedRectangle::draw(sf::RenderTarget &target) {
- if(drop_shadow_enabled)
- target.draw(drop_shadow_vertices, 32, sf::Quads);
-
// TODO: Remove these for optimizations. Also do the same in other places where setUniform is called
rounded_rectangle_shader->setUniform("radius", radius);
- rounded_rectangle_shader->setUniform("band_pos", band_pos);
+ rounded_rectangle_shader->setUniform("band_pos", band_pos + sf::Vector2f(shadow_radius, shadow_radius));
rounded_rectangle_shader->setUniform("band_size", band_size);
rounded_rectangle_shader->setUniform("band_color", sf::Glsl::Vec4(band_color.r/255.0f, band_color.g/255.0f, band_color.b/255.0f, band_color.a/255.0f));
- rounded_rectangle_shader->setUniform("resolution", size);
+ rounded_rectangle_shader->setUniform("resolution", size + sf::Vector2f(shadow_radius*2.0f, shadow_radius*2.0f));
target.draw(vertices, 4, sf::Quads, rounded_rectangle_shader);
}
} \ No newline at end of file