Sunday, July 31, 2016

Only 12 Enemies, And My Fps Drops To 30, Why Is That?

Only 12 Enemies, And My Fps Drops To 30, Why Is That?

Guys. What I did so far:

 

1.  I wanted to cap the frame rate of my game, but SDL_GetTicks() returns time since app start in Uint32 milliseconds, so I'm getting slightly wrong fps due to small rounding errors and the second reason why frame cap is bad in my case is that SDL_Delay() is not precise, as is said below.

 

fastcall22 says:

 


I would guess that relying on SDL_Delay to regulate framerate is not a good idea, since suspending a thread is not guaranteed to return exactly at the requested duration. From SDL documentation: This function [SDL_Delay] waits a specified number of milliseconds before returning. It waits at least the specified time, but possible longer due to OS scheduling. The delay granularity is at least 10 ms. Some platforms have shorter clock ticks but this is the most common. 

 

 

 

2. That's why I decided to make a frame-independent movement, so I used a variable called deltaTime which is calculated based on the current fps. Sounds cool. But the problem is that I have 7 enemies and in my 'Enemy' class I have various different functions that include movement in some direction, so I somehow need to put the deltaTime variable in all the Enemy objects every frame.

 

The braindead solution, which is what I did, is:

for( int i = 0; i < enemies.size(); i ++ )
{
    enemies[ i ].setDeltaTime( myrpg.deltaTime );
}

But I have to create 'for' loops for every single thing. For example:

        for( int i = 0; i < enemies.size() ; i ++ )
        {
            //If left mouse button is clicked and the bullet ray collides with an enemy AABB, kill the enemy.
            myrpg.kill( playerCam, enemies[ i ] );
        }

        // Clear the screen
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        for( int i = 0; i < enemies.size(); i ++ )
        {
            //If the enemy is not dead, make him run and rotate towards me.
            if( enemies[ i ].getCurrentAnim() == ANIM_RUN )
            {
                enemies[ i ].moveTowardsPlayer( playerCam.getPosition() );
            }
        }

        //Render all enemies
        for( int i = 0; i < enemies.size(); i ++ )
        {
            myrpg.renderAnimatedModel( enemies[ i ], Timer, shaderAnim, shaderStatic );
        }

It seems like I'm using C, not C++. Is there some other way I can make stuff more compact?

 

For example, can I add some virtual base class and just set deltaTime value in that class and make all the enemies inherit the variable somehow, is it possible? (it's a really stupid question, but I decided to ask in case there is something I have missed. ) I haven't used inheritance by now and I kind of don't know what are the pros it offers.

 

And the next question is:

I have 7 animated enemies. And my fps is 62 for now( not capped). But when I add 5 more enemies and make them 12, my fps drops to 31. I traced the problem and I finally found it, it's my BoneTransform() function, which fills my vector of TransformMatrices that I use in the vertex shader in order to animate the skeleton. But it rapes my CPU. ( when I comment the BoneTransform() function, framerate goes from 30 to 166!( sometimes jumps between 166 and 200 ). And I kind of stole most of the function from a tutorial on skeletal animation, and I'm sure it's pretty optimized, so there must be some other reason.

 

I used some models from World of Warcraft. And the interesting thing is that I have the game, and when I play it( when I play WoW ), I can have 20 players around me, and my fps is great, but when I add the same models in my own game, my fps drops like crazy and it's 10 times slower than the original game, why? ( bear in mind that I haven't even loaded any map, I just spawn 12 enemies walking on air, and my cpu runs like a fat truckdriver, wtf is that?? ).


No comments:

Post a Comment