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.


Messages - Naofu

Pages: [1]
1
SFML projects / Re: Wallbreaker (Arkanoid clone)
« on: October 31, 2019, 08:48:57 am »
Cool! And the code is really helpful to me. Nice work! :D


By the way, I found a bug: When move the window while playing in stage, the game freezes.

2
General / Re: dealing with massive frame time
« on: September 23, 2019, 01:03:53 pm »
I'm surprised there are such several ways to solve this.

Ok, Thank you Hapax!  :D

3
General / Re: dealing with massive frame time
« on: September 22, 2019, 03:39:27 am »
So...do I have to separate all updatings and drawings like this?

// ......

if(frameTime > 1.f) frameTime = 1.f;
while(frameTime > 1 / 60.f)
{
        gameContent.Update(1 / 60.f);
        frameTime -= 1 / 60.f;
}
gameContent.Update(frameTime);
gameContent.Draw(window);       // or window.draw(gameContent);
window.display();
 

I've not tested it yet. Applying this to my current code needs a lot of changes, because of my badness which I combined some draw calls and updating sprites/vertices together. :'(

4
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.

5
Graphics / Re: Drawing one time is slower than 100 times.
« on: August 06, 2019, 01:41:28 am »
Thanks for your reply. I still don't understand pointers, memories,,,etc. I gotta study hard.
In this case I'm bothered with discarding vertices of vanished entities.
Fairly improved by this way but still be defeated by drawing 100 times.

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

class Block
{
public:
        Block();
        ~Block();
        sf::Vertex* GetShape() { return shape; }
        void Allocate(sf::Vector2f position, sf::Vector2f size);
        bool CheckAlive() { return alive; }
        int GetVertexCount() { return vertexCount; }
private:
        sf::Vertex shape[8];
        bool alive;
        int vertexCount;
};

Block::Block()
{
}

Block::~Block()
{
}

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

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::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 -= 1.f;
                }

                window.clear();
                // draw every shapes 100 times.
                /*for (int i = 0; i < 100; i++)
                        if (block[i].CheckAlive())
                                window.draw(block[i].GetShape(), block[i].GetVertexCount(), sf::Quads);*/

                // draw one big shape.
                std::vector<sf::Vertex> vertices(0);
                int totalVertexCount = 0;
                for (int i = 0; i < 100; i++)
                        if (block[i].CheckAlive())
                                for (int j = 0; j < block[i].GetVertexCount(); j++)
                                {
                                        vertices.emplace_back(block[i].GetShape()[j]);
                                        totalVertexCount++;
                                }
                window.draw(&vertices[0], totalVertexCount, sf::Quads);

                window.display();
        }
}

 

6
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]
anything