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 - Naofu

Pages: [1]
1
General / dealing with massive frame time
« on: September 21, 2019, 01:14:07 pm »
When I move/resize window without pausing game, game messes up like Goomba instakills Mario beyond obstacles, because Clock is still progressing while window is completely frozen.
First I fixed it by this:
if(frameTime > 1 / 60.f) frameTime = 1 / 60.f;
But this looks unsmart. This may cause lags when fps drops below 60.
Is there any good way? thx.

2
Graphics / Drawing one time is slower than 100 times.
« on: August 05, 2019, 04:41:03 pm »
Hello, I'm new to SFML and also to C++.
This question is similar to previous one but I also stuck with this problem.

#include <SFML\Graphics.hpp>
#include <iostream>

class Block
{
public:
        Block();
        ~Block();
        void Allocate(sf::Vector2f position, sf::Vector2f size);
        sf::VertexArray GetShape() { return shape; }
        bool CheckAlive() { return alive; }
private:
        sf::VertexArray shape;
        bool alive;
};

Block::Block()
{
}

Block::~Block()
{
}

void Block::Allocate(sf::Vector2f position, sf::Vector2f size)
{
        shape = sf::VertexArray(sf::Quads, 4);
        shape[0].position = position;
        shape[1].position = { position.x + size.x, position.y };
        shape[2].position = { position.x + size.x, position.y + size.y };
        shape[3].position = { position.x, position.y  + size.y};
        shape[0].color = sf::Color::Red;
        shape[1].color = sf::Color::Blue;
        shape[2].color = sf::Color::Green;
        shape[3].color = sf::Color::Yellow;
        alive = true;
}

int main()
{
        sf::RenderWindow window(sf::VideoMode(400, 400), "TEST");
        Block block[100];
        for (int i = 0; i < 10; i++)
                for (int j = 0; j < 10; j++)
                        block[j + 10 * i].Allocate(sf::Vector2f(40.f * j, 40.f * i), sf::Vector2f(40.f, 40.f));
        sf::VertexArray bunchOfShapes(sf::Quads, 0);
        sf::Clock clock;
        float frameTime = 0.f;
        float timer = 0.f;
        int fps = 0;
        while (window.isOpen())
        {
                frameTime = clock.restart().asSeconds();
                sf::Event e;
                while (window.pollEvent(e))
                {
                        if (e.type == sf::Event::Closed)
                                window.close();
                }
                timer += frameTime;
                fps++;
                if (timer >= 1.f)
                {
                        std::cout << fps << std::endl;
                        fps = 0;
                        timer = 0.f;
                }

                window.clear();
                // draw every shapes 100 times.
                for (int i = 0; i < 100; i++)
                        if (block[i].CheckAlive())
                                window.draw(block[i].GetShape());
                // draw one big shape.
                /*for (int i = 0; i < 100; i++)                                                                                
                        if (block[i].CheckAlive())
                                for (int j = 0; j < block[i].GetShape().getVertexCount(); j++)
                                        bunchOfShapes.append(block[i].GetShape()[j]);
                window.draw(bunchOfShapes);*/


                window.display();
        }
}

 

I expected drawing one time is faster but turned to be much worse. Each of those blocks has a state and it is to be changed frequently in game, so this one big shape has to be updated in every frame. This code is definitely wrong but I can't figure out.

Pages: [1]