aboutsummaryrefslogtreecommitdiff
path: root/src/RoundedRectangle.cpp
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2021-11-17 09:47:45 +0100
committerdec05eba <dec05eba@protonmail.com>2021-11-17 09:59:29 +0100
commit453eac7f1f5ef70390ec51087fc1f190811a7507 (patch)
tree21a32ef6de9a3d7c29562484104b56c12518a6f0 /src/RoundedRectangle.cpp
parentfc49d40c0d2f6edbbe9dde1f1b53d6a17e9d9f7d (diff)
Replace sfml with mgl
Diffstat (limited to 'src/RoundedRectangle.cpp')
-rw-r--r--src/RoundedRectangle.cpp61
1 files changed, 32 insertions, 29 deletions
diff --git a/src/RoundedRectangle.cpp b/src/RoundedRectangle.cpp
index fc56823..587560c 100644
--- a/src/RoundedRectangle.cpp
+++ b/src/RoundedRectangle.cpp
@@ -1,68 +1,71 @@
#include "../include/RoundedRectangle.hpp"
-#include <SFML/Graphics/Shader.hpp>
-#include <SFML/Graphics/RenderTarget.hpp>
-#include <atomic>
+#include <mglpp/graphics/Shader.hpp>
+#include <mglpp/window/Window.hpp>
#include <assert.h>
namespace QuickMedia {
static const float shadow_radius = 20.0f; // Has to match the shadow offset in rounded_rectangle.glsl
- static std::atomic<float> cached_radius = 0.0f;
- static std::atomic<float> cached_resolution_x = 0.0f;
- static std::atomic<float> cached_resolution_y = 0.0f;
+ static float cached_radius = 0.0f;
+ static float cached_resolution_x = 0.0f;
+ static float cached_resolution_y = 0.0f;
- RoundedRectangle::RoundedRectangle(sf::Vector2f size, float radius, sf::Color color, sf::Shader *rounded_rectangle_shader) :
+ static float abs(float value) {
+ return value >= 0.0f ? value : -value;
+ }
+
+ RoundedRectangle::RoundedRectangle(mgl::vec2f size, float radius, mgl::Color color, mgl::Shader *rounded_rectangle_shader) :
radius(radius), pos(0.0f, 0.0f), size(size), rounded_rectangle_shader(rounded_rectangle_shader)
{
assert(rounded_rectangle_shader);
set_color(color);
- vertices[0].texCoords = sf::Vector2f(0.0f, 0.0f);
- 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);
+ vertices[0].texcoords = mgl::vec2f(0.0f, 0.0f);
+ vertices[1].texcoords = mgl::vec2f(1.0f, 0.0f);
+ vertices[2].texcoords = mgl::vec2f(1.0f, 1.0f);
+ vertices[3].texcoords = mgl::vec2f(0.0f, 1.0f);
}
- void RoundedRectangle::set_position(sf::Vector2f pos) {
+ void RoundedRectangle::set_position(mgl::vec2f pos) {
this->pos = pos;
- const float shadow_radius_ins = rounded_rectangle_shader->getNativeHandle() == 0 ? 0.0f : shadow_radius;
- vertices[0].position = pos + sf::Vector2f(-shadow_radius_ins, -shadow_radius_ins);
- vertices[1].position = pos + sf::Vector2f(shadow_radius_ins, -shadow_radius_ins) + sf::Vector2f(size.x, 0.0f);
- vertices[2].position = pos + sf::Vector2f(shadow_radius_ins, shadow_radius_ins) + sf::Vector2f(size.x, size.y);
- vertices[3].position = pos + sf::Vector2f(-shadow_radius_ins, shadow_radius_ins) + sf::Vector2f(0.0f, size.y);
+ const float shadow_radius_ins = rounded_rectangle_shader->is_valid() ? shadow_radius : 0.0f;
+ vertices[0].position = pos + mgl::vec2f(-shadow_radius_ins, -shadow_radius_ins);
+ vertices[1].position = pos + mgl::vec2f(shadow_radius_ins, -shadow_radius_ins) + mgl::vec2f(size.x, 0.0f);
+ vertices[2].position = pos + mgl::vec2f(shadow_radius_ins, shadow_radius_ins) + mgl::vec2f(size.x, size.y);
+ vertices[3].position = pos + mgl::vec2f(-shadow_radius_ins, shadow_radius_ins) + mgl::vec2f(0.0f, size.y);
}
- void RoundedRectangle::set_size(sf::Vector2f size) {
+ void RoundedRectangle::set_size(mgl::vec2f size) {
this->size = size;
set_position(pos);
}
- void RoundedRectangle::set_color(sf::Color color) {
+ void RoundedRectangle::set_color(mgl::Color color) {
for(size_t i = 0; i < 4; ++i)
vertices[i].color = color;
}
- sf::Vector2f RoundedRectangle::get_position() const {
+ mgl::vec2f RoundedRectangle::get_position() const {
return pos;
}
- sf::Vector2f RoundedRectangle::get_size() const {
+ mgl::vec2f RoundedRectangle::get_size() const {
return size;
}
- void RoundedRectangle::draw(sf::RenderTarget &target) {
- const sf::Vector2f resolution = size + sf::Vector2f(shadow_radius*2.0f, shadow_radius*2.0f);
+ void RoundedRectangle::draw(mgl::Window &target) {
+ const mgl::vec2f resolution = size + mgl::vec2f(shadow_radius*2.0f, shadow_radius*2.0f);
- // TODO: Remove these for optimizations. Also do the same in other places where setUniform is called
- if(std::abs(cached_radius - radius) > 0.01f) {
- rounded_rectangle_shader->setUniform("radius", radius);
+ if(abs(cached_radius - radius) > 0.01f) {
+ // TODO: Remove these for optimizations. Also do the same in other places where set_uniform is called
+ rounded_rectangle_shader->set_uniform("radius", radius);
cached_radius = radius;
}
- if(std::abs(cached_resolution_x - resolution.x) > 0.01f || std::abs(cached_resolution_y - resolution.y) > 0.01f) {
- rounded_rectangle_shader->setUniform("resolution", size + sf::Vector2f(shadow_radius*2.0f, shadow_radius*2.0f));
+ if(abs(cached_resolution_x - resolution.x) > 0.01f || abs(cached_resolution_y - resolution.y) > 0.01f) {
+ rounded_rectangle_shader->set_uniform("resolution", size + mgl::vec2f(shadow_radius*2.0f, shadow_radius*2.0f));
cached_resolution_x = resolution.x;
cached_resolution_y = resolution.y;
}
- target.draw(vertices, 4, sf::Quads, rounded_rectangle_shader);
+ target.draw(vertices, 4, mgl::PrimitiveType::Quads, rounded_rectangle_shader);
}
} \ No newline at end of file