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

Pages: [1]
1
Graphics / Re: Transforming Individual Quad/Sprite in Vertex Array
« on: June 26, 2022, 03:11:20 pm »
You can use rotation matrix, something like:
    float alpha = 3.14f/2, sine = std::sin(alpha), cosine = std::cos(alpha);
    sf::Vector2f offset{(v[0].position + v[4].position)/2.f};
    for(int i = 0; i < 4; ++ i)
    {
        sf::Vector2f pos = v[i].position - offset;
        v[i].position = sf::Vector2f{pos.x*cosine - pos.y*sine, pos.x*sine + pos.y*cosine} + offset;
    }
This code rotates a quad including 4 vertex 90 degrees clockwise.

2
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

3
SFML projects / Re: Screenshot Thread
« on: March 09, 2022, 09:22:41 am »
Here is my fighting game project. I implemented hitboxes, animation and such:



4
Hello :> I'm new guy to game dev, ECS. Sfml and a bunch of things. I just want to ask that it has been 2 or 3 years, have you released the game? Can I find it somewhere? And your project is so cool and awesome, you really spent lots of time for it, which maybe I will never be able to do. Thats all I want to say, amazing work you did, so wonderful!

5
Graphics / Re: How to optimize multiple draws
« on: July 19, 2021, 10:40:57 am »
@Stauricus: Uhhh i dont really get it... So it's better to handle multiple smaller sprites than a big one? Sorry if my question sounds silly but i'm still quite new
@firet: :'> n^2 and n^2/2 doesnt make a big difference when you having large n I think

6
Graphics / Re: How to optimize multiple draws
« on: July 18, 2021, 11:04:29 am »
Uhmm so what about when there are many white cells... Isn't it the same

7
Graphics / Re: May sprites vs 1 sprite ?
« on: July 18, 2021, 06:07:20 am »
Hmm... then I think we have to trade CPU resources right. I think unless you're having a serious and heavy project then GPU performance doesn neccessarily need to be optimised, since most devices are capable of drawing multiple sprites and calculate stuffs at an suficent speed. But would setFrameLimit or V-sync be good choices?

8
Graphics / Re: How to optimize multiple draws
« on: July 11, 2021, 02:36:33 pm »
It has been 1 month... have you found a way? I intend to make Minesweeper but the same problem happens. The only thing i can do is to limit fps to about 10 or so.

9
Graphics / Re: May sprites vs 1 sprite ?
« on: July 11, 2021, 02:27:52 pm »
It might be the time you call sf::RenderTarget::draw() right? It is the only thing that matters. I would prefer using only 1 sprite to save GPU resource.  Just wonder why you want to separate it like that? To optimise performance and only draw whats on the view right? Hmm anyway try using Task Manager if you re using Windows and see which way benefits you the most.

10
Try adding "#pragma once" too all header's beginning. It works like the block guards like "#inclide<Book/Source/World.hpp>".

P/s: As long as it compiles it may not run the right way. You should pay attention to World::World() initialisation. Well I got white screen once too. Be careful with mWorldView and mWorldBounds. They don't immediately work after initialisation for some reason. So in World(sf::RenderWindow& window) all you need to do is pass every arguments by window, not my World's members itself. One thing that works to me is to print out everything in the console. It will help you find out what is the problem.

11
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


12
I solved this. There is an old post about the exact error. Turns out I have to define member functions inside header files when I use template, or I can use .inl file, but not .cpp file.
" http://www.cplusplus.com/forum/general/122601/ "
This is the post.

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