hi, I have a sfml program where it is comprised of pixel...it is a battle of pixel and it moved according to a set speed...but the way it moved when i run it is instantaneously moved...
main.cpp
#include <TGUI/TGUI.hpp>
#include "pixels/pixels.hpp"
#include <iostream>
int main()
{
const float speed = 0.01;
sf::RenderWindow win{sf::VideoMode{450, 400}, "pixel vs pixel"};
Pixels p1{sf::Color::Red, sf::Vector2f{200, 400}, sf::Vector2f{0, 0}, (200 * 400) / 2, speed};
Pixels p2{sf::Color::Blue, sf::Vector2f{200, 400}, sf::Vector2f{250, 0}, (200 * 400) / 2, speed};
while (win.isOpen())
{
sf::Event event;
while (win.pollEvent(event))
{
if (event.type == sf::Event::Closed) win.close();
}
p1.update(p2);
p2.update(p1);
win.clear();
win.draw(p1);
win.draw(p2);
win.display();
}
}
Pixels.hpp
class Pixels : public sf::Drawable, public sf::Transformable
{
private :
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
states.transform *= getTransform();
states.texture = 0;
target.draw(m_vertices, states);
}
sf::VertexArray m_vertices;
const float speed = 0;
public :
Pixels(sf::Color color, sf::Vector2f start, sf::Vector2f end, int numbers, float pspeed);
void update(Pixels& pixels);
sf::Vector2f& getPoint(size_t index);
size_t getPointCount();
};
pixels.cpp
#include <cstdlib>
#include <ctime>
#include "pixels.hpp"
Pixels::Pixels(sf::Color color, sf::Vector2f start, sf::Vector2f end, int numbers, float pspeed) :
speed(pspeed)
{
srand(time(0));
m_vertices.setPrimitiveType(sf::PrimitiveType::Points);
m_vertices.resize((size_t) numbers);
for (size_t i = 0; i < m_vertices.getVertexCount(); ++i)
{
m_vertices[i].color = color;
float temp_x = (rand() % (int) start.x) + (int) end.x;
rand();
float temp_y = (rand() % (int) start.y) + (int) end.y;
m_vertices[i].position = sf::Vector2f{temp_x, temp_y};
}
}
void Pixels::update(Pixels& pixels)
{
for (int i = 0; i < m_vertices.getVertexCount(); ++i)
{
if (i >= pixels.getPointCount())
{
if (getPoint(i).x < pixels.getPoint(pixels.getPointCount() - 1).x)
{
m_vertices[i].position = sf::Vector2f{m_vertices[i].position.x + speed, m_vertices[i].position.x};
}
else if (getPoint(i).x > pixels.getPoint(pixels.getPointCount() - 1).x)
{
m_vertices[i].position = sf::Vector2f{m_vertices[i].position.x - speed, m_vertices[i].position.x};
}
if (getPoint(i).y < pixels.getPoint(pixels.getPointCount() - 1).y)
{
m_vertices[i].position = sf::Vector2f{m_vertices[i].position.y, m_vertices[i].position.y + speed};
}
else if (getPoint(i).y > pixels.getPoint(pixels.getPointCount() - 1).y)
{
m_vertices[i].position = sf::Vector2f{m_vertices[i].position.y, m_vertices[i].position.y - speed};
}
continue;
}
if (getPoint(i).x < pixels.getPoint(i).x)
{
m_vertices[i].position = sf::Vector2f{m_vertices[i].position.x + speed, m_vertices[i].position.x};
}
else if (getPoint(i).x > pixels.getPoint(i).x)
{
m_vertices[i].position = sf::Vector2f{m_vertices[i].position.x - speed, m_vertices[i].position.x};
}
if (getPoint(i).y < pixels.getPoint(i).y)
{
m_vertices[i].position = sf::Vector2f{m_vertices[i].position.y, m_vertices[i].position.y + speed};
}
else if (getPoint(i).y > pixels.getPoint(i).y)
{
m_vertices[i].position = sf::Vector2f{m_vertices[i].position.y, m_vertices[i].position.y - speed};
}
}
}
sf::Vector2f& Pixels::getPoint(size_t index)
{
return m_vertices[index].position;
}
size_t Pixels::getPointCount()
{
return m_vertices.getVertexCount();
}