Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Chart Graph  (Read 10432 times)

0 Members and 1 Guest are viewing this topic.

milen

  • Newbie
  • *
  • Posts: 8
    • View Profile
Chart Graph
« on: August 10, 2015, 06:22:23 pm »
I want to draw moving Chart Graph - like wave on oscilloscope. I refresh the points coordinates on every cycle BUT nothing changes on the screen - only one fully static sinusoide (blue on yellow), which do not moves :(
I also try to update positions with sf::Vectror2f(x, y), but the result is same unsuccessful. My code is:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <cmath>

using namespace std;

constexpr double pi() { return atan(1)*4; }


int main()
{
    const unsigned int s = 500u;
    const unsigned int N = 1000u;

    sf::RenderWindow window(sf::VideoMode(s, s),
                            "Osc",
                            sf::Style::Close);
    window.setVerticalSyncEnabled(true);
    window.setFramerateLimit(60);

    sf::VertexArray chart(sf::LinesStrip, N);
    for (unsigned int i=0; i<N-1; i++){
        unsigned int x = i*s/N;
        unsigned int y = s/2 - (s/2-100)*sin(i*2*pi()/N);
        chart.append(sf::Vertex(sf::Vector2f(x, y), sf::Color::Blue));
    }

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed) window.close();

        // HERE?!:
        unsigned int tmpy = chart[0].position.y;
        for (unsigned int i=0; i<N-2; i++)
            chart[i].position.y = chart[i+1].position.y;
        chart[N-1].position.y = tmpy;

        window.clear(sf::Color::Yellow);
        window.draw(chart);
        window.display();
    }
    return 0;
}
 

I just start to play with C++ and SFML.
I use Code::Blocks with  TDM-GCC 4.9.2, 64 bit and SFML-2.3.1-windows-gcc-4.8.1-tdm-64-bit.zip.
My system is: Windows 7 64 bit, NVIDIA GeForce GT610

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Chart Graph
« Reply #1 on: August 10, 2015, 07:30:27 pm »
sf::VertexArray chart(sf::LinesStrip, N);
It creates a vertex array with N default (positionned at 0, 0) vertices.

chart.append(sf::Vertex(sf::Vector2f(x, y), sf::Color::Blue));
Then, you append N vertices to your vertex array. Now you actually have 2 * N vertices.

        unsigned int tmpy = chart[0].position.y;
        for (unsigned int i=0; i<N-2; i++)
            chart[i].position.y = chart[i+1].position.y;
        chart[N-1].position.y = tmpy;
You modify the y value of your N first vertices. Since all these vertices are at x = 0 and y = 0 you don't see any change.

Create an empty sf::VertexArray and append N vertices to it.
Or create a sf::VertexArray with N vertices, then set the coordinates (and color) of these vertices to what you want.

milen

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Chart Graph
« Reply #2 on: August 11, 2015, 05:25:02 pm »
Thank you!
I slowly learn the basics of the language and library. I correct the array and this works - now the sinusoid moves (slowly, but moves)!

 

anything