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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - milen

Pages: [1]
1
Window / Failed to activate OpenGL context
« on: October 23, 2018, 08:32:32 am »
Failed to activate OpenGL context: The handle is invalid.

Hello,
I try to make my simple Chart class for C++ with SFML. The idea is to make simple windows for charts, each in different thread so to not affect the main program.

The problem is that the opened windows must be closed in reverse order - from the last opened to the first. Else the upper error message displays in the console. Seems that the program works, but this message worry me.
I am not C++ developer, so preliminary thank you for any help!

My source is:

MChart.hpp
#pragma once

#include <SFML/Graphics.hpp>
#include <string>


class MChart
{

public:
  MChart(const unsigned int width = 500, const unsigned int height = 500,
         const std::string xlabel = "", const std::string ylabel = "", const std::string title = "");

  ~MChart();

  void set_data_len(unsigned int N);
  void append_data(float x, float y);
  void update_data(unsigned int i, float x, float y);

private:
  static unsigned int fig_num;

  const unsigned int border{10};
  unsigned int width, height;
  const std::string xlabel, ylabel, title;
  unsigned int fig_num_c;

  sf::Thread th;
  bool MustToStop{false};
  bool Stopped{false};

  sf::VertexArray graph{sf::LinesStrip};
  sf::VertexArray box_grid{sf::Lines};

  void run_dis();
};
 

MChart.cpp
#include <string>
#include <iostream>
#include <SFML/Graphics.hpp>
#include "MChart.hpp"


unsigned int MChart::fig_num{1};


MChart::MChart(const unsigned int width, const unsigned int height,
               const std::string xlabel, const std::string ylabel, const std::string title) :
               width{width}, height{height}, xlabel{xlabel}, ylabel{ylabel}, title{title},
               th{&MChart::run_dis, this}
               // the list of initializers is initialized in declaration order in *.hpp file (see -Wreorder)
{
    this->fig_num_c = MChart::fig_num++;
    this->th.launch();
}

MChart::~MChart()
{
    this->MustToStop = true;
    while (!this->Stopped)
        ;
}

void MChart::run_dis()
{
    // Initialize window
    sf::ContextSettings settings;
    settings.antialiasingLevel = 8;
    sf::RenderWindow window{sf::VideoMode(this->width, this->height),
                            std::to_string(this->fig_num_c), sf::Style::Close, settings};
    window.setVerticalSyncEnabled(true);

    // Draw window
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if ((event.type == sf::Event::Closed) ||
                ((event.type == sf::Event::KeyPressed) &&
                 (event.key.code == sf::Keyboard::Escape)))
                window.close();
        }

        if (this->MustToStop)
            window.close();

        window.clear(sf::Color(0, 5, 55));

        window.draw(this->graph);
        window.draw(this->box_grid);

        window.display();
    }

    this->Stopped = true;
}

void MChart::set_data_len(unsigned int N)
{
    this->graph.resize(N);
}

void MChart::append_data(float x, float y)
{
    this->graph.append(sf::Vertex(sf::Vector2f(x, y), sf::Color::Yellow));
}

void MChart::update_data(unsigned int i, float x, float y)
{
    if (i < this->graph.getVertexCount())
        graph[i].position = sf::Vector2f(x, y);
}
 

main.cpp
#include "MChart.hpp"

int main()
{
        MChart ch1, ch2;

        for (int i = 0; i < 300; i++)
        {
                ch1.append_data(i, i);
                ch2.append_data(i, i / 2);
        }
        system("pause");
        return 0;
}
 

2
Graphics / 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

3
Graphics / fastPixelRendering
« on: August 10, 2015, 04:26:04 pm »
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

I try to make window with effect of snowflakes as on the old televisions. Make two variants. The PROBLEMS are the same:
- flickering
- when I set texture (window) size - power of 2 (as I read that this is faster for the graphic card), on the screen are almost static image of dots, more or less grouped in rows or columns.
- at different size of the screen - the effects are different - flickering (the best case, shown in the next examples), or "moving" random pixels...

The VerticalSync or FrameRate do not change anything special.

Is proper this my code or I make some error logic?

First variant:
----------------
(with Texture from Image)

#include <sstream>
#include <time.h>
#include <stdlib.h>

using namespace std;

string num2str(int i){
    stringstream s;
    s << i;
    return s.str();
}

int main()
{
    const unsigned int s = 500;
    unsigned char c;


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

    sf::Image img;
    img.create(s, s, sf::Color(0, 0, 0));

    sf::Texture texture;
    sf::Sprite sprite;
    //window.setTitle(num2str(sf::Texture::getMaximumSize())); // for my card this is 16384
    texture.loadFromImage(img);
    sprite.setTexture(texture, true);

    srand(time(0));

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

        for (unsigned int i=0; i<s; i++)
            for (unsigned int j=0; j<s; j++){
                c = (rand()%2 == 0) ? 0 : 255;
                img.setPixel(i, j, sf::Color(c, c, c));
            }

        texture.update(img);

        window.clear();
        window.draw(sprite);
        window.display();
    }
    return 0;
}
 


Second variant:
--------------------
(with Texture from Array)

#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <time.h>
#include <stdlib.h>

using namespace std;

string num2str(int i){
    stringstream s;
    s << i;
    return s.str();
}

int main()
{
    const unsigned int s = 500;
    const unsigned int sz = s*s*4;
    sf::Uint8 px[sz];

    for (unsigned int i=3; i<sz; i+=4) px[i] = 255; // set alfa once


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

    sf::Texture texture;
    texture.create(s, s);

    sf::Sprite sprite;
    //window.setTitle(num2str(sf::Texture::getMaximumSize()));
    //window.setTitle(num2str(RAND_MAX));
    sprite.setTexture(texture);

    srand(time(0));

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed) window.close();
/* [b]I try this to optimize the random fast pixel choice, but the screen become black with only little white random dots[/b]
        for (unsigned int i=0; i<sz-2*16*4; i+=16*4){
                c = rand();
                for (unsigned int j=0; j<16; j++){
                    px[i+j] = (((c >> j) & 1u) == 0) ? 0 : 255;
                    px[i+j+1] = px[i+j]; px[i+j+2] = px[i+j];
                    px[i+j+3] = 255;
                }
        }
*/

        for (unsigned int i=0; i<sz-4; i+=4){
            px[i] = (rand()%2 == 0) ? 0 : 255;
            px[i+1] = px[i]; px[i+2] = px[i];
        }

        texture.update(px);

        window.clear();
        window.draw(sprite);
        window.display();
    }
    return 0;
}
 

Pages: [1]
anything