/**
* Triangles
* Copyright (C) 2016 POSITIVE MENTAL ATTITUDE
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "Triangle.hpp"
#include
#include
Triangle::Triangle(): _texture(nullptr), _size(100.f)
{
_vertices.setPrimitiveType(sf::Triangles);
_vertices.resize(3);
_vertices[0].color = sf::Color(255, 255, 255, 255);
_vertices[1].color = sf::Color(255, 255, 255, 255);
_vertices[2].color = sf::Color(255, 255, 255, 255);
setPosition(500.f, 300.f);
redraw();
}
float Triangle::getSize()
{
return _size;
}
void Triangle::setSize(float size)
{
_size = size;
redraw();
}
void Triangle::vertexFit()
{
if(_texture == nullptr)
return;
_vertices[0].texCoords = sf::Vector2f(_texture->getSize().x / 2, 0);
_vertices[1].texCoords = sf::Vector2f(_texture->getSize().x, _texture->getSize().y);
_vertices[2].texCoords = sf::Vector2f(0, _texture->getSize().y);
}
void Triangle::setTexture(sf::Texture& texture)
{
_texture = &texture;
vertexFit();
}
void Triangle::setColor(sf::Color color)
{
_vertices[0].color = color;
_vertices[1].color = color;
_vertices[2].color = color;
}
void Triangle::setColor(unsigned r, unsigned g, unsigned b, unsigned a)
{
setColor(sf::Color(r, g, b, a));
}
void Triangle::redraw()
{
double degree = 3.14159265358 / 180.;
_vertices[0].position = sf::Vector2f(0, -_size);
_vertices[1].position = sf::Vector2f(std::sin(135.f * degree) * _size, -std::cos(135.f * degree) * _size);
_vertices[2].position = sf::Vector2f(std::sin(225.f * degree) * _size, -std::cos(225.f * degree) * _size);
}
void Triangle::draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
if(_texture != nullptr)
states.texture = (_texture);
target.draw(_vertices, states);
}