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

Author Topic: unefficient drawing  (Read 1615 times)

0 Members and 1 Guest are viewing this topic.

nameless_987

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
unefficient drawing
« on: February 05, 2022, 05:46:52 am »
Hello,
I am working on a random project where i need to have a code as efficient as possible so i tried to check if the way I draw is efficient enough but it isn't.
I saw people how draw millions of points whie still having a decent amount of FPS but I can't draw more than around 200000 points.
I tried with circles, recangles, convexs, rectangles with the size of a pixel but it never gets better.
If you have a solution I would be happy to here it.
Here is the code :

    while(window.isOpen()){
        window.clear();
        while(window.pollEvent(event)){
            if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Enter) && (!fullscreen)) {
                                fullscreen = true;
                                window.create(VideoMode(1920, 1080), nameProject, (fullscreen ? Style::Fullscreen : Style::Resize|Style::Close));
                        }
            else if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Escape) && (fullscreen)) {
                                fullscreen = false;
                                window.create(VideoMode(1920, 1080), nameProject, (fullscreen ? Style::Fullscreen : Style::Resize|Style::Close));
                        }
            if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Space) && (fullscreen)) {
                pause = !pause;
                        }
            if(event.type == Event::Closed){
                window.close();
            }
        }

        float dt = gameClock.getElapsedTime().asSeconds();
        gameClock.restart();

        for(int i=0; i<player.size(); i++){
            if(fullscreen && !pause){
                player[i].x+=player[i].vx*dt;
                player[i].y+=player[i].vy*dt;

                if(player[i].x < player[i].r){
                    player[i].x += 2*(player[i].r-player[i].x);
                    player[i].vx *= -1;
                }
                else if(player[i].x > window.getSize().x-player[i].r){
                    player[i].x += 2*(window.getSize().x-player[i].r-player[i].x);
                    player[i].vx *= -1;
                }
                if(player[i].y < player[i].r){
                    player[i].y += 2*(player[i].r-player[i].y);
                    player[i].vy *= -1;
                }
                else if(player[i].y > window.getSize().y-player[i].r){
                    player[i].y += 2*(window.getSize().y-player[i].r-player[i].y);
                    player[i].vy *= -1;
                }
            }
            if(fullscreen){
                player[i].body.setPosition(player[i].x-player[i].r, player[i].y-player[i].r);
                window.draw(player[i].body);
            }
        }

        window.display();
        Sleep(1);
    }
« Last Edit: February 24, 2022, 07:25:38 pm by eXpl0it3r »

fallahn

  • Sr. Member
  • ****
  • Posts: 464
  • Buns.
    • View Profile
    • Trederia
Re: unefficient drawing
« Reply #1 on: February 05, 2022, 12:38:26 pm »
You can probably alleviate the drawing a bit by rendering each point as a vertex in a vertex array, with the primitive type set to points.


    sf::VertexArray vertexArray(sf::PrimitiveType::Points);

    while(window.isOpen()){
        window.clear();
        while(window.pollEvent(event)){
            if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Enter) && (!fullscreen)) {
            fullscreen = true;
            window.create(VideoMode(1920, 1080), nameProject, (fullscreen ? Style::Fullscreen : Style::Resize|Style::Close));
         }
            else if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Escape) && (fullscreen)) {
            fullscreen = false;
            window.create(VideoMode(1920, 1080), nameProject, (fullscreen ? Style::Fullscreen : Style::Resize|Style::Close));
         }
            if((event.type == Event::KeyPressed) && (event.key.code == Keyboard::Space) && (fullscreen)) {
                pause = !pause;
         }
            if(event.type == Event::Closed){
                window.close();
            }
        }

        float dt = gameClock.getElapsedTime().asSeconds();
        gameClock.restart();

        vertexArray.clear();

        for(int i=0; i<player.size(); i++){
            if(fullscreen && !pause){
                player.x+=player.vx*dt;
                player.y+=player.vy*dt;

                if(player.x < player.r){
                    player.x += 2*(player.r-player.x);
                    player.vx *= -1;
                }
                else if(player.x > window.getSize().x-player.r){
                    player.x += 2*(window.getSize().x-player.r-player.x);
                    player.vx *= -1;
                }
                if(player.y < player.r){
                    player.y += 2*(player.r-player.y);
                    player.vy *= -1;
                }
                else if(player.y > window.getSize().y-player.r){
                    player.y += 2*(window.getSize().y-player.r-player.y);
                    player.vy *= -1;
                }
            }
            if(fullscreen){
                player.body.setPosition(player.x-player.r, player.y-player.r);
                vertexArray.append(sf::Vertex(sf::Vector2f(player.x-player.r, player.y-player.r)));
            }
        }

        window.draw(vertexArray);
       
        window.display();
        Sleep(1);
    }
 

This will batch all of your points into a single draw call.

Stauricus

  • Sr. Member
  • ****
  • Posts: 335
    • View Profile
    • Email
Re: unefficient drawing
« Reply #2 on: February 05, 2022, 03:50:50 pm »
also, what is "Sleep(1)"? for how long is it sleeping?
will may have problems when doing it for more than 10 milliseconds.
« Last Edit: February 05, 2022, 03:52:55 pm by Stauricus »

nameless_987

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: unefficient drawing
« Reply #3 on: February 08, 2022, 01:08:44 am »
The Sleep(1); is sleeping for 1 millisecond it's because i didn' set a limit to the framerate (I think) when the pc goes too fast throught the loop : dt = 0 so the Sleep(1); gets the value of dt to at least 1.
And thank you to fallahn it works perfectly.

 

anything