Low FPS with a lot of sprite display

Hello,

I’m student and i try to do a game in 2D for my project in first year. I’m too beginner in C ++.
I’m trying to do this game with openframework and i have FPS problem for the map display.

   // 32 pixel * 32 pixel = image size.
nbrCaseX = ofGetScreenWidth() / 32;
nbrCaseY = ofGetScreenHeight() / 32;

for (int x = 0; x < nbrCaseX; x++) {
	for (int y = 0; y < nbrCaseY; y++) {
	sprite.drawSubsection(x*32, y*32, 32, 32, 608, 0, 32, 32);
	}
}

But with this draw, FPS is now 10-15 fps maximum.

How can I optimize the code for a lot of sprite display ?

Thanks

Hi,

Asuming the sprites are not going to change dynamically, instead of drawing multiple times every frame the small sprites, you could pregenerate the large sprite by combining the small sprites, and just draw that one once every frame.

Hi,

Ok. But how can i generate this sprite ? Which function can do that ?
I have found on the forum that some people work with pixel. Must i work with pixel or can I work with small image ?

You could use an ofFbo.

Declare in the .h

ofFbo fbo;
bool bFillFbo;

In the .cpp:

void setup()
{
    fbo.allocate(ofGetScreenWidth(), ofGetScreenHeight());
    bFillFbo = true;
}

void draw()
{
    if(bFillFbo)
    {
        bFillFbo = false;
        fbo.begin();
            nbrCaseX = ofGetScreenWidth() / 32;
            nbrCaseY = ofGetScreenHeight() / 32;
            for (int x = 0; x < nbrCaseX; x++) {
	            for (int y = 0; y < nbrCaseY; y++) {
	            sprite.drawSubsection(x*32, y*32, 32, 32, 608, 0, 32, 32);
	        }
        }
        fbo.end();
    }
    else
    {
        fbo.draw();
    }
}

Ok thanks i see this in the documentation too.
I’ll make test this evening.

Thanks

You could also use ofxSpriteSheetRenderer to batch render a lot of sprites. It may be a little bit more complex if you are new to c++, but well worth it if you want to have a lot of particles etc.

I test on my monitor ( 1920 *1080 ). Thus i have 60 image in width and 40 image in height, I must display 2400 small image on my screen. And this each frame. It’s not good and useless because this background map is always the same. Build an image is a good idea. And i can refresh it each time i need.

I can test, but i think that the solve of Hennio is better.

I post the resultat after my test.

Ok i have make my test and the low FPS come from the draw() ( 2400 / Frame is really too much ^^ )

Wiklund : ofxSpriteSheetRenderer is really good for easier animation. Thanks :slightly_smiling:

Hennio : I have the same low FPS with the fbo class.

I have make a test with a large image ( 5000 / 2000 ) and i can move the camera without lag ( 59 minimum FPS / 60 Fps ).

Now i must learn how to build and generate a multilayer map :slight_smile:

Thanks a lot for your help !