# OGL "Big Framerate drop with 2d tile map" (on iOS)

This topic is 2351 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

[SOLVED]!!!!!!
Hello Game Dev Community,

once again I have a question to you and maybe you can help me out with it. The fact is that I try to display a tile map on my iPhone. The problem occurs when i try to display more than 30x30 tiles.. Beyond that benchmark, the FPS is ok but at around 50x50 the character movement is tremendously slow.

Maybe the problems lays in the scrolling? When the player moves up, the whole map moves down (with glTranslatef) and i do glPushMatrix() glPopMatrix() before i draw each tile.. can this be the reason?

My other ideas to handle this where:

-GL_TRIANGLE_STRIP instead of GL_TRIANGLE
-spriteBatch which loads texture and vbo for units who share this information (tried, didn't speed it up)

Here are the codes which may be interesting for you:

Core Drawing routine:

 void Sprite2D::draw_to_scene(GLuint texture_id) { glPushMatrix(); glTranslatef(this->rect.pos.x, this->rect.pos.y, 0.0f); glRotatef(this->angle, 0, 0, 1); glScalef(this->scalar, this->scalar, 0.0f); // i tried to cut out this part into a batchBegin function glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glBindTexture(GL_TEXTURE_2D, this->texture); glBindBuffer(GL_ARRAY_BUFFER, this->vbo); glVertexPointer(3, GL_FLOAT, 0, (char*)NULL); // until here.. didn't gave me a big performance hit glBindBuffer(GL_ARRAY_BUFFER, this->tbo[texture_id]); <- right now i create for each texture on the tex atlas a tbo which i switch glTexCoordPointer(2, GL_FLOAT, 0, (char*)NULL); mabye this slows it down? glDrawArrays(GL_TRIANGLES, 0, this->vertex_count); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glPopMatrix(); } 
Scrolling and Drawing Routine from World Map

 void WorldMap::draw() { for (int i = 0; i < nRows; i++) { for (int j = 0; j < nCols; j++) { this->tiles_layer1[j].sprite->draw(this->tiles_layer1[j].tex_id); } } } void WorldMap::update_world_coordinates(const AnimatedChar &_char) { Point2D temp; if (_char.getMoving()) { for (int i = 0; i < nRows; i++) { for (int j = 0; j < nCols; j++) { temp.x = this->tiles_layer1[j].sprite->getRect().pos.x; temp.y = this->tiles_layer1[j].sprite->getRect().pos.y; if (_char.getViewDirection()==DOWN) temp.y -= _char.getMovingSpeed(); else if(_char.getViewDirection()==LEFT) temp.x += _char.getMovingSpeed(); else if(_char.getViewDirection()==RIGHT) temp.x -= _char.getMovingSpeed(); else if(_char.getViewDirection()==UP) temp.y += _char.getMovingSpeed(); this->tiles_layer1[j].sprite->setX(temp.x); this->tiles_layer1[j].sprite->setY(temp.y); } } } } 

Any help is highly appreciated. I think i have a logic mistake somewhere. I read on stacks overflow that i should scroll with glOrtho for example instead of translating the whole world all the time..

Thanks very much!!

Bye

Markus

##### Share on other sites
Your biggest optimization opportunity is the number of times you're calling glDrawArrays. In a 30x30 map, that's 900 individual draw calls, and the 50x50 is 2,500 draw calls. Each time you call glDrawArrays, there is overhead involved, and on the iPhone it's pretty extreme. Load those tiles as a single mesh ( and yes, using [color="#1C2837"]GL_TRIANGLE_STRIP would be an additional improvement) instead of thousands of individual units, and you should see a huge speedup.
[color="#1C2837"]
[color="#1C2837"]as an added bonus: when the tile map is a single mesh, you'll be able to remove that double loop in update_world_coordinates too.

##### Share on other sites
He's also binding the texture on every call. He should not rebind if he's already bound the same texture.

Drawing it all in one triangle strip would be great, but he'll have to make sure that all the vertices in the strip are using the same texture.

##### Share on other sites

i appreciate that a lot.. i reworked my engine the whole day and now i am able to show 5000x5000 (seriously ^^) tiles without problems..

if there is a demand for that solution than i will post what i did..
otherwise this thread can be closed!!