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

Pages: [1]
1
SFML projects / Flappy Bird fine clone
« on: June 26, 2022, 09:12:34 am »
Hello everyone  8)
 ;D Today I want to share my development of a clone of the prestigious Flappy Bird.
It is nothing new and complex but this is my first finished and polished project.
So here is some information about the developer's side:
  • I used Entity-Componen-System model for game structure. I don't know if it is an overkill, but ECS is definitely a strong tool.
  • I also use sf::VertexArray instead of primitive shapes for drawing entities because I think it is more efficient  ;)
  • Then it took me a bit to realise I messed thing up because there is no call to rotate() for array. So what I did is using rotation matrix for rotating our bird (sounds overkill too  :()
  • Last but not least, the thing I enjoy most is shader and sf::RenderTexture usage. I used pixelate shader provided in SFML example package to create super cool transition inside menu.
Please go check the game out, too!
Thanks to SFML and C++ simplicity the game is really light.



Here is the link to download site: https://labg.itch.io/flappy-bird

2
General / A (possible) solution to rendering performance optimisation
« on: July 11, 2021, 08:28:31 am »
Hi guys. I have been using SFML for 2 months. As I learned the command queue system, I made a small test that you can move a tank. I decided to add accelerate each time unit, rather than keep velocity frame-independent.
The code looks like this:

void World::adaptEntityVelocity(Entity& entity, sf::Time dt)
{
    static constexpr float a = 500.0f;//acceleration
    static constexpr float msp = 400.0f;//max speed
    float velo = entity.getVelocity();
    //clamp velo to [-msp,msp]
    velo = std::min(msp, std::max(- msp, velo));
    if(velo > 0)
        entity.setVelocity(std::max(0.0f, velo - a * dt.asSeconds()));
    if(velo < 0)
        entity.setVelocity(std::min(0.0f, velo + a * dt.asSeconds()));
}
//...
adaptEntityVelocity(mPlayer, dt);
//...
mPlayer.move(mPlayer.getVelocity() * dt.asSeconds();

It worked just find until I use sf::RenderWindow::setFrameLimit(). It somehow makes the player moves slightly faster in 60 fps (compared to ~450 fps without setting limit) and crazily fast in 10fps. I think the problem is the time Game::update() and Game::processInput() system is also affected by setFrameLimit(). So I decided not to use setFrameLimit(), but I don’t want to let the game run as fast as possible because that hurts GPU. But if I only limit render frequency the CPU will run massively fast and consume lots of energy (same when you run an endless while() loop). Then I come up with an idea: limit Game::render() to 60 fps and game loop to 500fps. This is the code:

void Game::run()
{
    sf::Clock dtClock;
    sf::Time timeSinceLastFrame = sf::Time::Zero;
    while(mWindow.isOpen())
    {
        sf::Time elapsed = dtClock.restart();
        //limit loops per second;
        if (elapsed < sf::seconds(0.002f))
        {
            sf::sleep(sf::seconds(0.002f) - elapsed);
            elapsed += dtClock.restart();
        }
        timeSinceLastFrame += elapsed;
        while(timeSinceLastFrame >= TIMEPERFRAME)
        {
            timeSinceLastFrame -= TIMEPERFRAME;
            processInput();
            update(TIMEPERFRAME);
        }
        updateStat(elapsed);
        render(elapsed); // pass a time variable to count and set limit
    }
}

Render function:

void Game::render(sf::Time dt)
{
    mRenderTime += dt;
    if(mRenderTime >= sf::seconds(1.0f / 60.0f))
    {
        mRenderCount ++;
        mRenderTime -= sf::seconds(1.0f / 60.0f);

        mWindow.clear(sf::Color(0x00000000ff));

        mWorld.draw();

        mWindow.setView(mWindow.getDefaultView());
        mWindow.draw(mStatistics);
        mWindow.display();
    }
}
 

So what I want to know is would this work on any OP with various speed? Or is it just in my own laptop?

P/s: this is what I got after all: a decent render rate and a limited loops per frame


3
Hello everyone. I am currently using Code::Block 20.03 and C++ 17.I have been using SFML for nearly 3 months. I found Laurent's book "SFML Game Development" one week ago. So in page 63 there is code lines look like this:
"
     Aircraft::Aircraft(Type type, const TextureHolder& textures):
     mType(type), mSprite(textures.get(toTextureID(type)))
     {
     }
"
I made console project, linked to SFML library.
I use .hpp files for headers and .cpp files for either main or class function definitions, I do not use .inl files.
But as soon as I compile it said
"
     D:\2.Tin\Project\Plane_shooter\Aicraft.cpp|20|undefined reference to `ResourceHolder<sf::Texture,
     Textures::ID>::get(Textures::ID) const'|
     ||error: ld returned 1 exit status|
"
This is in the file "Aircraft.cpp". Following are #include of the files:
Aircraft.cpp : #include "Aircraft.hpp"
Aircraft.hpp : #include "ResourceHolder.hpp" (template class for storing resources such as fonts, textures, ... It has a public member function "template <typename Resource, typename Identifier> const Resource& get(Identifier id) const"
                    #include "ResourceIdentifiers.hpp" (which consists a namespace Textures {enum class ID{...} };
Can you help me? I do not think i can figure out the problem myself.
Im sorry i uploaded this post in general but i think its just something to do with graphics module since I have not dealt with others.
Any help and comment would be appreciated, thank you.

Pages: [1]