aboutsummaryrefslogtreecommitdiff
path: root/src/RoundedRectangle.cpp
blob: 6b956e89d6c1bbce34d7f1b8c88b42f8d8dc61e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "../include/RoundedRectangle.hpp"
#include <SFML/Graphics/Shader.hpp>
#include <SFML/Graphics/RenderWindow.hpp>
#include <assert.h>

namespace QuickMedia {
    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) {
        assert(rounded_rectangle_shader);
        vertices[0].color = color;
        vertices[1].color = color;
        vertices[2].color = color;
        vertices[3].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);
    }

    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);
    }

    void RoundedRectangle::set_size(sf::Vector2f size) {
        this->size = size;
        set_position(pos);
    }

    sf::Vector2f RoundedRectangle::get_position() const {
        return pos;
    }

    sf::Vector2f RoundedRectangle::get_size() const {
        return size;
    }

    void RoundedRectangle::draw(sf::RenderWindow &window) {
        rounded_rectangle_shader->setUniform("resolution", size);
        rounded_rectangle_shader->setUniform("radius", radius);
        window.draw(vertices, 4, sf::Quads, rounded_rectangle_shader);
    }
}