I wanted to create a circle with RGB color effect (Just like RBG color pallet) in SFML. but could not find any method to make it except using an image.
First, I Tried making a RGB rectangle (Just like RBG color pallet), I used several small Rectangles to make the effect work (let know if there's an efficient method to do it), but now I want to make Circle, am pasting the code of Rectangle here:
#include <SFML/Graphics.hpp>
//Global Variable and define's
#define windowSize 450
#define rectSize 1.0f
int colorRange = 255;
int main()
{
//Creating new Window
sf::RenderWindow window(sf::VideoMode(windowSize, windowSize), "Colors", sf::Style::Close | sf::Style::Resize);
sf::View mainView = window.getDefaultView();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
//Close Window Code
window.close();
else if(event.type == sf::Event::Resized)
{
//View Resize Code
mainView.setSize
(
static_cast<float> (event.size.width),
static_cast<float> (event.size.height)
);
mainView.setCenter
(
static_cast<float> (event.size.width) / 2,
static_cast<float> (event.size.height)/ 2
);
}
window.setView(mainView);
}
for(int i = 0; i < colorRange; i++)
{
for(int j = 0; j < colorRange; j++)
{
sf::RectangleShape shape(sf::Vector2f(rectSize, rectSize));
shape.setPosition(sf::Vector2f(i * rectSize, j * rectSize));
shape.setFillColor(sf::Color(i, j, 127, 255));
window.draw(shape);
}
}
window.display();
window.clear(sf::Color::White);
}
return 0;
}
Consider using sf::VertexArray
of type sf::PrimitiveType::TriangleFan
which may appear like a circle if you choose appropriate (larger is better) number of elements.
See SFML : designing your own entities with vertex arrays for more details about vertex drawing.
For an array with 10 elements
#include <cmath>
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(1000, 500), "window");
const size_t N{10};
const sf::Vector2f Circle_center{100.f, 100.f};
const float Radius{50.f};
const double sqrt2 = std::sqrt(2);
sf::VertexArray wheel(sf::PrimitiveType::TriangleFan, N);
wheel[0].position = Circle_center;
wheel[1].position = Circle_center + Radius * sf::Vector2f(1.0f, 0.0f);
wheel[2].position = Circle_center + Radius * sf::Vector2f(sqrt2 / 2.0f, sqrt2 / 2.0f);
wheel[3].position = Circle_center + Radius * sf::Vector2f(0.0f, 1.0f);
wheel[4].position = Circle_center + Radius * sf::Vector2f(-sqrt2 / 2.0f, sqrt2 / 2.0f);
wheel[5].position = Circle_center + Radius * sf::Vector2f(-1.0f, 0.0f);
wheel[6].position = Circle_center + Radius * sf::Vector2f(-sqrt2 / 2.0f, -sqrt2 / 2.0f);
wheel[7].position = Circle_center + Radius * sf::Vector2f(0.0f, -1.0f);
wheel[8].position = Circle_center + Radius * sf::Vector2f(sqrt2 / 2.0f, -sqrt2 / 2.0f);
wheel[9].position = Circle_center + Radius * sf::Vector2f(1.0f, 0.0f);
wheel[0].color = sf::Color::White;
wheel[1].color = sf::Color(255, 0, 0);
wheel[2].color = sf::Color(233, 63, 31);
wheel[3].color = sf::Color(191, 127, 63);
wheel[4].color = sf::Color(159, 191, 95);
wheel[5].color = sf::Color(127, 255, 127);
wheel[6].color = sf::Color(95, 191, 159);
wheel[7].color = sf::Color(63, 127, 191);
wheel[8].color = sf::Color(31, 63, 233);
wheel[9].color = sf::Color(0, 0, 255);
while(window.isOpen())
{
window.clear();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
window.draw(wheel);
window.display();
}
return 0;
}
you'll get such picture
It'll be surely convenient to use parametrization for vertices positions (with sin
& cos
functions for calculating horizontal and vertical positions respectively) and RGB's components with larger number of TriangleFan
s.
As a side note: you don't have to create shapes or vertices each frame (iteration of loop inside while(window.isOpen()
). This repeating is unnecessary. Instead, create it, set positions and colours once before loop and just call window.draw(...)
inside it.