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

Author Topic: Optimizing my main loop.  (Read 2177 times)

0 Members and 1 Guest are viewing this topic.

freesoul

  • Newbie
  • *
  • Posts: 19
    • View Profile
Optimizing my main loop.
« on: September 24, 2012, 02:52:51 pm »
Hello all, a week ago I started working on my first game project, and a friend recommended me this library, which I found really good :)

The thing is, as I'm new to game programming, I don't know how to increase FPS. Actually I post this because I've noticed I my sprites stuck a bit while moving. My main code is below, I'm looking just for some advices, thanks :)

NOTE: this was written using SFML 1.6, I will pass to 2.0 soon.

I'd appreciate some advise to make my main loop more readable, because I'm tired of scrolling up an down to search my game loop case :P I know, maybe put code into functions, but I avoid calling functions the more I can, I think that ralentizes a bit, doesn't it?

« Last Edit: September 24, 2012, 06:46:52 pm by freesoul »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Optimizing my main loop.
« Reply #1 on: September 24, 2012, 03:29:55 pm »
This is far too much code, and it is distorted in this forum because of the big indentation levels.

Come up with a minimal and complete example, remove everything unrelated and not relevant for your FPS issue.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

freesoul

  • Newbie
  • *
  • Posts: 19
    • View Profile
Re: Optimizing my main loop.
« Reply #2 on: September 24, 2012, 06:50:46 pm »
Ok sorry it was really long. Well, this is the part of code I think can ralentize more.

I've to say that my game is slow intermittently, 1 sec goes good, 1 sec goes bad

I check for each missile (there can be 1 to 150 at a time) if it's being hit by any of the current bullets casted (1 to 15 aprox)

void Missile_mgr::UpdateAndDrawMissiles(sf::RenderWindow& _window, float elapsedTime) {

        bool bHitByBullet;

         //////// MISSILE LOOP START ///////
        // This could be done with missileList.RemoveAllThatComply_nodeArg() + a condition function... but simplier this way.
        for(std::list<Missile>::iterator mIt = missileList.begin(); mIt!=missileList.end(); )
        {
                mIt->Update(elapsedTime);
                bHitByBullet = false;


                ////// BULLET LOOP START ////////
                for(std::list<Bullet>::iterator bIt=pBullet_mgr->bulletList.begin(); bIt!=pBullet_mgr->bulletList.end(); )
                {

                        if(Collision::BoundingBoxTest(mIt->spMissile, bIt->spBullet)) //// BULLET-MISSILE COLLISION.
                        {
                                mIt->hp -= pPlayer->damage;
                                if(mIt->hp <= 0)
                                {
                                        bHitByBullet=true; // should be bDeadMonster

                                        switch(mIt->type)
                                        {
                                        case Missile::Apple: // DEAD FRUIT -> NO REWARD.
                                                break;
                                        case Missile::Normal:
                                        default:
                                                        pLevel_mgr->xpReward+=10;
                                                        pLevel_mgr->coinReward+=10;
                                        }
                       
                                }

                                pBullet_mgr->bulletList.erase(bIt++);// laser weapon may ignore this line -> go through missiles.
                                                                                                                                                  // we can also break, so only 1 bullet is deleted, no 2. Plus we directly delete missile and no need bool bHitByBullet
                        } else ++bIt;
                       
                }
                ////// BULLET LOOP END ////////

                if(bHitByBullet) {
                        // score already increased in bullet loop. (may hit more than 1 missile)
                        missileList.erase(mIt++);
                } else {
                        if(Collision::BoundingBoxTest(mIt->spMissile, pPlayer->spPlayer)) //// MISSILE HIT PLAYER
                        {
                                switch(mIt->type) {
                                case Missile::Apple:
                                        pPlayer->Heal(1); // to be updated
                                        break;
                                case Missile::Normal:
                                default:
                                        pPlayer->DoDmg(2.5); // to be updated
                                        break;
                                }
                                missileList.erase(mIt++);
                        } else {
                                if(mIt->spMissile.getPosition().y > GROUND_ALTITUDE) { //// MISSILE HIT GROUND
                                        missileList.erase(mIt++);
                                } else { // Nothing happens
                                        _window.draw(mIt->spMissile);
                                        ++mIt;
                                }
                        }

                }

        } //////// MISSILE LOOP END ///////

}
« Last Edit: September 24, 2012, 06:54:17 pm by freesoul »

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Optimizing my main loop.
« Reply #3 on: September 26, 2012, 10:52:50 pm »
I did  a quick look at it, and I must tell you that it is still too long, try to make it as short as possible.

Well, from what I saw you should get an FPS increase if you use a std::vector instead of a list. You can also look at the topic, "ways to increase FPS", I got help in the same matter and most suggestions could apply to your program as well. Maybe using collision in another thread. There's also the fact that you are using 1.6. And a little explanation of what the program does currently wouldn't be bad either.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Optimizing my main loop.
« Reply #4 on: September 26, 2012, 11:21:49 pm »
Quote
Maybe using collision in another thread.
Maybe separating logic and drawing for easier profiling, abstracting away update functions into entity classes and using something that doesn't involve O(n^2) quadratic stupidity complexity(ie Box2d).
Back to C++ gamedev with SFML in May 2023